forked from stenciljs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore-resolve-plugin.ts
More file actions
194 lines (177 loc) · 6.92 KB
/
Copy pathcore-resolve-plugin.ts
File metadata and controls
194 lines (177 loc) · 6.92 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { isRemoteUrl, join, normalizeFsPath, normalizePath } from '@utils';
import { dirname } from 'path';
import type { Plugin } from 'rollup';
import type * as d from '../../declarations';
import type { BundlePlatform } from './bundle-interface';
import { HYDRATED_CSS } from '../../runtime/runtime-constants';
import { fetchModuleAsync } from '../sys/fetch/fetch-module-async';
import { getStencilModuleUrl, packageVersions } from '../sys/fetch/fetch-utils';
import {
APP_DATA_CONDITIONAL,
STENCIL_CORE_ID,
STENCIL_INTERNAL_CLIENT_ID,
STENCIL_INTERNAL_CLIENT_PATCH_BROWSER_ID,
STENCIL_INTERNAL_HYDRATE_ID,
STENCIL_INTERNAL_ID,
STENCIL_JSX_DEV_RUNTIME_ID,
STENCIL_JSX_RUNTIME_ID,
} from './entry-alias-ids';
export const coreResolvePlugin = (
config: d.ValidatedConfig,
compilerCtx: d.CompilerCtx,
platform: BundlePlatform,
externalRuntime: boolean,
lazyLoad: boolean,
): Plugin => {
const compilerExe = config.sys.getCompilerExecutingPath();
const internalClient = getStencilInternalModule(config, compilerExe, 'client/index.js');
const internalClientPatchBrowser = getStencilInternalModule(config, compilerExe, 'client/patch-browser.js');
const internalHydrate = getStencilInternalModule(config, compilerExe, 'hydrate/index.js');
return {
name: 'coreResolvePlugin',
resolveId(id) {
if (id === STENCIL_CORE_ID || id === STENCIL_INTERNAL_ID) {
if (platform === 'client') {
if (externalRuntime) {
return {
id: STENCIL_INTERNAL_CLIENT_ID,
external: true,
};
}
if (lazyLoad) {
// with a lazy / dist build, add `?app-data=conditional` as an identifier to ensure we don't
// use the default app-data, but build a custom one based on component meta
return internalClient + APP_DATA_CONDITIONAL;
}
// for a non-lazy / dist-custom-elements build, use the default, complete core.
// This ensures all features are available for any importer library
return internalClient;
}
if (platform === 'hydrate') {
return internalHydrate;
}
}
if (id === STENCIL_INTERNAL_CLIENT_ID) {
if (externalRuntime) {
// not bundling the client runtime and the user's component together this
// must be the custom elements build, where @stencil/core/internal/client
// is an import, rather than bundling
return {
id: STENCIL_INTERNAL_CLIENT_ID,
external: true,
};
}
// importing @stencil/core/internal/client directly, so it shouldn't get
// the custom app-data conditionals
return internalClient;
}
if (id === STENCIL_INTERNAL_CLIENT_PATCH_BROWSER_ID) {
if (externalRuntime) {
return {
id: STENCIL_INTERNAL_CLIENT_PATCH_BROWSER_ID,
external: true,
};
}
return internalClientPatchBrowser;
}
if (id === STENCIL_INTERNAL_HYDRATE_ID) {
return internalHydrate;
}
// Handle jsx-runtime and jsx-dev-runtime imports
// These must resolve to the same internal client path as @stencil/core
// to prevent Rollup from bundling duplicate runtime code with different
// minified property names, which causes VNode property mismatches during hydration
if (id === STENCIL_JSX_RUNTIME_ID || id === STENCIL_JSX_DEV_RUNTIME_ID) {
if (platform === 'client') {
if (externalRuntime) {
return {
id: STENCIL_INTERNAL_CLIENT_ID,
external: true,
};
}
if (lazyLoad) {
// with a lazy / dist build, add `?app-data=conditional` as an identifier to ensure we don't
// use the default app-data, but build a custom one based on component meta
return internalClient + APP_DATA_CONDITIONAL;
}
// for a non-lazy / dist-custom-elements build, use the default, complete core.
return internalClient;
}
if (platform === 'hydrate') {
return internalHydrate;
}
}
return null;
},
async load(filePath) {
if (filePath && !filePath.startsWith('\0')) {
filePath = normalizeFsPath(filePath);
if (filePath === internalClient || filePath === internalHydrate) {
if (platform === 'worker') {
return `
export const Build = {
isDev: ${config.devMode},
isBrowser: true,
isServer: false,
isTesting: false,
};`;
}
let code = await compilerCtx.fs.readFile(filePath);
if (typeof code !== 'string' && isRemoteUrl(compilerExe)) {
const url = getStencilModuleUrl(compilerExe, filePath);
code = await fetchModuleAsync(config.sys, compilerCtx.fs, packageVersions, url, filePath);
}
if (typeof code === 'string') {
const hydratedFlag = config.hydratedFlag;
if (hydratedFlag) {
const hydratedFlagHead = getHydratedFlagHead(hydratedFlag);
if (HYDRATED_CSS !== hydratedFlagHead) {
code = code.replace(HYDRATED_CSS, hydratedFlagHead);
if (hydratedFlag.name !== 'hydrated') {
code = code.replace(`.classList.add("hydrated")`, `.classList.add("${hydratedFlag.name}")`);
code = code.replace(`.classList.add('hydrated')`, `.classList.add('${hydratedFlag.name}')`);
code = code.replace(`.setAttribute("hydrated",`, `.setAttribute("${hydratedFlag.name}",`);
code = code.replace(`.setAttribute('hydrated',`, `.setAttribute('${hydratedFlag.name}',`);
}
}
} else {
code = code.replace(HYDRATED_CSS, '{}');
}
}
return code;
}
}
return null;
},
};
};
export const getStencilInternalModule = (config: d.ValidatedConfig, compilerExe: string, internalModule: string) => {
if (isRemoteUrl(compilerExe)) {
return normalizePath(
config.sys.getLocalModulePath({
rootDir: config.rootDir,
moduleId: '@stencil/core',
path: 'internal/' + internalModule,
}),
);
}
const compilerExeDir = dirname(compilerExe);
return normalizePath(join(compilerExeDir, '..', 'internal', internalModule));
};
export const getHydratedFlagHead = (h: d.HydratedFlag) => {
// {visibility:hidden}.hydrated{visibility:inherit}
let initial: string;
let hydrated: string;
if (!String(h.initialValue) || h.initialValue === '' || h.initialValue == null) {
initial = '';
} else {
initial = `{${h.property}:${h.initialValue}}`;
}
const selector = h.selector === 'attribute' ? `[${h.name}]` : `.${h.name}`;
if (!String(h.hydratedValue) || h.hydratedValue === '' || h.hydratedValue == null) {
hydrated = '';
} else {
hydrated = `${selector}{${h.property}:${h.hydratedValue}}`;
}
return initial + hydrated;
};