forked from stenciljs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-plugin.ts
More file actions
67 lines (60 loc) · 1.83 KB
/
Copy pathserver-plugin.ts
File metadata and controls
67 lines (60 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { isOutputTargetHydrate, isString, normalizeFsPath } from '@utils';
import { isAbsolute } from 'path';
import type { Plugin } from 'rollup';
import type * as d from '../../declarations';
import type { BundlePlatform } from './bundle-interface';
export const serverPlugin = (config: d.ValidatedConfig, platform: BundlePlatform): Plugin => {
const isHydrateBundle = platform === 'hydrate';
const serverVarid = `@removed-server-code`;
const isServerOnlyModule = (id: string) => {
if (isString(id)) {
id = normalizeFsPath(id);
return id.includes('.server/') || id.endsWith('.server');
}
return false;
};
const externals = isHydrateBundle
? config.outputTargets.filter(isOutputTargetHydrate).flatMap((o) => o.external)
: [];
return {
name: 'serverPlugin',
resolveId(id, importer) {
if (id === serverVarid) {
return id;
}
if (isHydrateBundle) {
if (externals.includes(id)) {
// don't attempt to bundle node builtins for the hydrate bundle
return {
id,
external: true,
};
}
if (isServerOnlyModule(importer) && !id.startsWith('.') && !isAbsolute(id)) {
// do not bundle if the importer is a server-only module
// and the module it is importing is a node module
return {
id,
external: true,
};
}
} else {
if (isServerOnlyModule(id)) {
// any path that has .server in it shouldn't actually
// be bundled in the web build, only the hydrate build
return serverVarid;
}
}
return null;
},
load(id) {
if (id === serverVarid) {
return {
code: 'export default {};',
syntheticNamedExports: true,
};
}
return null;
},
};
};