@wyw-in-js/vite 1.1.0 → 2.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/esm/index.mjs +636 -656
- package/esm/index.mjs.map +1 -1
- package/package.json +12 -13
- package/types/index.d.ts +1 -1
- package/types/index.js +112 -127
- package/lib/index.js +0 -695
- package/lib/index.js.map +0 -1
package/esm/index.mjs
CHANGED
|
@@ -1,681 +1,661 @@
|
|
|
1
1
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
10
|
-
import
|
|
11
|
-
|
|
12
|
-
const {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
stringifyTransformManifest,
|
|
17
|
-
transform,
|
|
18
|
-
TransformCacheCollection
|
|
19
|
-
} = transformPkg;
|
|
20
|
-
const createMetadataManifest = (metadata, context) => typeof createTransformManifest === 'function' ? createTransformManifest(metadata, context) : {
|
|
21
|
-
...metadata,
|
|
22
|
-
...context,
|
|
23
|
-
version: 1
|
|
2
|
+
* This file contains a Vite loader for wyw-in-js.
|
|
3
|
+
* It uses the transform.ts function to generate class names from source code,
|
|
4
|
+
* returns transformed code without template literals and attaches generated source maps
|
|
5
|
+
*/
|
|
6
|
+
import { existsSync } from "fs";
|
|
7
|
+
import path from "path";
|
|
8
|
+
import { createFilter, loadEnv } from "vite";
|
|
9
|
+
import { asyncResolverFactory, logger, mergeOxcResolverAlias, syncResolve, toNativeResolverAlias } from "@wyw-in-js/shared";
|
|
10
|
+
import * as transformPkg from "@wyw-in-js/transform";
|
|
11
|
+
const { createTransformManifest, createFileReporter, disposeEvalBroker, getFileIdx, stringifyTransformManifest, transform, TransformCacheCollection } = transformPkg;
|
|
12
|
+
const createMetadataManifest = (metadata, context) => typeof createTransformManifest === "function" ? createTransformManifest(metadata, context) : {
|
|
13
|
+
...metadata,
|
|
14
|
+
...context,
|
|
15
|
+
version: 1
|
|
24
16
|
};
|
|
25
|
-
const stringifyMetadataManifest = manifest => typeof stringifyTransformManifest ===
|
|
26
|
-
const isWindowsAbsolutePath = value => /^[a-zA-Z]:[\\/]/.test(value);
|
|
27
|
-
const normalizeToPosix = value => value.replace(/\\/g, path.posix.sep);
|
|
17
|
+
const stringifyMetadataManifest = (manifest) => typeof stringifyTransformManifest === "function" ? stringifyTransformManifest(manifest) : `${JSON.stringify(manifest, null, 2)}\n`;
|
|
18
|
+
const isWindowsAbsolutePath = (value) => /^[a-zA-Z]:[\\/]/.test(value);
|
|
19
|
+
const normalizeToPosix = (value) => value.replace(/\\/g, path.posix.sep);
|
|
28
20
|
const isInside = (childPath, parentPath) => {
|
|
29
|
-
|
|
30
|
-
|
|
21
|
+
const rel = path.relative(parentPath, childPath);
|
|
22
|
+
return rel === "" || !rel.startsWith("..") && !path.isAbsolute(rel);
|
|
31
23
|
};
|
|
32
|
-
const isWywCssAssetName = value => value.endsWith(
|
|
33
|
-
const normalizeAssetRelativePath = value => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
24
|
+
const isWywCssAssetName = (value) => value.endsWith(".wyw-in-js.css");
|
|
25
|
+
const normalizeAssetRelativePath = (value) => {
|
|
26
|
+
const normalized = path.posix.normalize(normalizeToPosix(value).replace(/^\/+/, ""));
|
|
27
|
+
if (normalized.startsWith("..") || path.posix.isAbsolute(normalized)) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
return normalized;
|
|
39
31
|
};
|
|
40
|
-
const stripExtension = value => {
|
|
41
|
-
|
|
42
|
-
|
|
32
|
+
const stripExtension = (value) => {
|
|
33
|
+
const ext = path.posix.extname(value);
|
|
34
|
+
return ext ? value.slice(0, -ext.length) : value;
|
|
43
35
|
};
|
|
44
36
|
const getComparableAssetPaths = (value, rootDir) => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
37
|
+
const variants = new Set();
|
|
38
|
+
const normalized = normalizeToPosix(value);
|
|
39
|
+
variants.add(normalized);
|
|
40
|
+
if (path.isAbsolute(value) || isWindowsAbsolutePath(normalized)) {
|
|
41
|
+
if (isInside(value, rootDir)) {
|
|
42
|
+
const relativeToRoot = normalizeAssetRelativePath(path.relative(rootDir, value));
|
|
43
|
+
if (relativeToRoot) {
|
|
44
|
+
variants.add(relativeToRoot);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return variants;
|
|
48
|
+
}
|
|
49
|
+
const relativePath = normalizeAssetRelativePath(value);
|
|
50
|
+
if (relativePath) {
|
|
51
|
+
variants.add(relativePath);
|
|
52
|
+
}
|
|
53
|
+
return variants;
|
|
62
54
|
};
|
|
63
|
-
const getStringValues = value => {
|
|
64
|
-
|
|
65
|
-
|
|
55
|
+
const getStringValues = (value) => {
|
|
56
|
+
if (!Array.isArray(value)) return [];
|
|
57
|
+
return value.filter((item) => typeof item === "string");
|
|
66
58
|
};
|
|
67
|
-
const getOutputAssetNames =
|
|
68
|
-
|
|
69
|
-
|
|
59
|
+
const getOutputAssetNames = (asset) => [
|
|
60
|
+
...typeof asset.name === "string" ? [asset.name] : [],
|
|
61
|
+
...getStringValues(asset.names),
|
|
62
|
+
...typeof asset.originalFileName === "string" ? [asset.originalFileName] : [],
|
|
63
|
+
...getStringValues(asset.originalFileNames)
|
|
64
|
+
];
|
|
65
|
+
const isOutputAssetLike = (value) => !!value && typeof value === "object" && value.type === "asset" && typeof value.fileName === "string";
|
|
66
|
+
const isOutputChunkLike = (value) => !!value && typeof value === "object" && value.type === "chunk" && typeof value.fileName === "string" && typeof value.code === "string";
|
|
70
67
|
const getTrackedModuleIdForChunk = (chunk, cssFilesByModuleId) => {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
if (typeof chunk.facadeModuleId === "string" && cssFilesByModuleId.has(chunk.facadeModuleId)) {
|
|
69
|
+
return chunk.facadeModuleId;
|
|
70
|
+
}
|
|
71
|
+
if (!Array.isArray(chunk.moduleIds)) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
const moduleId = chunk.moduleIds.find((id) => typeof id === "string" && cssFilesByModuleId.has(id));
|
|
75
|
+
return moduleId ?? null;
|
|
79
76
|
};
|
|
80
77
|
const findWywCssAssetFileName = (bundle, cssFilename, rootDir) => {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
78
|
+
const expectedNames = getComparableAssetPaths(cssFilename, rootDir);
|
|
79
|
+
for (const item of Object.values(bundle)) {
|
|
80
|
+
if (isOutputAssetLike(item) && item.fileName.endsWith(".css")) {
|
|
81
|
+
const isMatch = getOutputAssetNames(item).some((assetName) => {
|
|
82
|
+
const variants = getComparableAssetPaths(assetName, rootDir);
|
|
83
|
+
return Array.from(variants).some((variant) => expectedNames.has(variant));
|
|
84
|
+
});
|
|
85
|
+
if (isMatch) {
|
|
86
|
+
return normalizeToPosix(item.fileName);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
94
91
|
};
|
|
95
92
|
const getRelativeImportPath = (fromFileName, toFileName) => {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
93
|
+
const fromDir = path.posix.dirname(normalizeToPosix(fromFileName));
|
|
94
|
+
const relativePath = path.posix.relative(fromDir, normalizeToPosix(toFileName));
|
|
95
|
+
return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
|
|
99
96
|
};
|
|
100
|
-
const escapeForRegExp = value => value.replace(/[.*+?^${}()|[\]\\]/g,
|
|
101
|
-
const hasStaticImport = (code, specifier) => new RegExp(`(^|\\n)\\s*import\\s*(?:["']${escapeForRegExp(specifier)}["']|[^\\n;]+\\s+from\\s+["']${escapeForRegExp(specifier)}["'])`,
|
|
102
|
-
const hasRequireCall = (code, specifier) => new RegExp(`(^|[;\\n])\\s*require\\(\\s*["']${escapeForRegExp(specifier)}["']\\s*\\)`,
|
|
103
|
-
const getCssLoadStatement = (format, specifier) => format ===
|
|
104
|
-
const hasCssLoadStatement = (code, specifier, format) => format ===
|
|
97
|
+
const escapeForRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
98
|
+
const hasStaticImport = (code, specifier) => new RegExp(`(^|\\n)\\s*import\\s*(?:["']${escapeForRegExp(specifier)}["']|[^\\n;]+\\s+from\\s+["']${escapeForRegExp(specifier)}["'])`, "m").test(code);
|
|
99
|
+
const hasRequireCall = (code, specifier) => new RegExp(`(^|[;\\n])\\s*require\\(\\s*["']${escapeForRegExp(specifier)}["']\\s*\\)`, "m").test(code);
|
|
100
|
+
const getCssLoadStatement = (format, specifier) => format === "cjs" ? `require(${JSON.stringify(specifier)});\n` : `import ${JSON.stringify(specifier)};\n`;
|
|
101
|
+
const hasCssLoadStatement = (code, specifier, format) => format === "cjs" ? hasRequireCall(code, specifier) : hasStaticImport(code, specifier);
|
|
105
102
|
const prependCssLoadStatement = (code, specifier, format) => {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
103
|
+
const statement = getCssLoadStatement(format, specifier);
|
|
104
|
+
let insertAt = 0;
|
|
105
|
+
if (code.startsWith("#!")) {
|
|
106
|
+
const lineBreakIndex = code.indexOf("\n");
|
|
107
|
+
if (lineBreakIndex >= 0) {
|
|
108
|
+
insertAt = lineBreakIndex + 1;
|
|
109
|
+
} else {
|
|
110
|
+
return `${code}\n${statement}`;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (format === "cjs") {
|
|
114
|
+
const directiveMatch = /^(?:\s*(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*');)+/.exec(code.slice(insertAt));
|
|
115
|
+
if (directiveMatch) {
|
|
116
|
+
insertAt += directiveMatch[0].length;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return `${code.slice(0, insertAt)}${statement}${code.slice(insertAt)}`;
|
|
123
120
|
};
|
|
124
|
-
const VITE_FS_PREFIX =
|
|
125
|
-
const safeDecodeURIComponent = value => {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
121
|
+
const VITE_FS_PREFIX = "/@fs/";
|
|
122
|
+
const safeDecodeURIComponent = (value) => {
|
|
123
|
+
try {
|
|
124
|
+
return decodeURIComponent(value);
|
|
125
|
+
} catch {
|
|
126
|
+
return value;
|
|
127
|
+
}
|
|
131
128
|
};
|
|
132
|
-
const normalizeViteFsPath = value => {
|
|
133
|
-
|
|
134
|
-
|
|
129
|
+
const normalizeViteFsPath = (value) => {
|
|
130
|
+
const fsPath = value.slice(VITE_FS_PREFIX.length);
|
|
131
|
+
return path.normalize(safeDecodeURIComponent(fsPath));
|
|
135
132
|
};
|
|
136
|
-
const isCssReloadTarget = value => {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
133
|
+
const isCssReloadTarget = (value) => {
|
|
134
|
+
if (!value || typeof value !== "object") return false;
|
|
135
|
+
const reloadTarget = value;
|
|
136
|
+
return !!reloadTarget.moduleGraph && typeof reloadTarget.moduleGraph.getModuleById === "function" && typeof reloadTarget.reloadModule === "function";
|
|
140
137
|
};
|
|
141
138
|
const getCssReloadTarget = (environment, server) => {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
139
|
+
if (isCssReloadTarget(environment)) {
|
|
140
|
+
return environment;
|
|
141
|
+
}
|
|
142
|
+
if (server?.moduleGraph) {
|
|
143
|
+
return {
|
|
144
|
+
moduleGraph: server.moduleGraph,
|
|
145
|
+
reloadModule: (module) => server.reloadModule(module)
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
152
149
|
};
|
|
153
150
|
const getWywCssAssetFileNames = (resolvedConfig, output, originalAssetFileNames) => {
|
|
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
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
151
|
+
if (!output.preserveModules) return null;
|
|
152
|
+
const rootDir = resolvedConfig.root;
|
|
153
|
+
const preserveModulesRootValue = output.preserveModulesRoot;
|
|
154
|
+
let preserveModulesRootAbs = null;
|
|
155
|
+
if (typeof preserveModulesRootValue === "string") {
|
|
156
|
+
preserveModulesRootAbs = path.isAbsolute(preserveModulesRootValue) ? preserveModulesRootValue : path.resolve(rootDir, preserveModulesRootValue);
|
|
157
|
+
}
|
|
158
|
+
const preserveModulesRootRel = preserveModulesRootAbs && isInside(preserveModulesRootAbs, rootDir) ? normalizeToPosix(path.relative(rootDir, preserveModulesRootAbs)) : null;
|
|
159
|
+
return (assetInfo) => {
|
|
160
|
+
const template = typeof originalAssetFileNames === "function" ? originalAssetFileNames(assetInfo) : originalAssetFileNames;
|
|
161
|
+
const assetName = assetInfo?.name;
|
|
162
|
+
if (typeof assetName !== "string" || !isWywCssAssetName(assetName)) {
|
|
163
|
+
return template;
|
|
164
|
+
}
|
|
165
|
+
if (!template.includes("[")) {
|
|
166
|
+
return template;
|
|
167
|
+
}
|
|
168
|
+
let relativePath = null;
|
|
169
|
+
const assetNameNormalized = normalizeToPosix(assetName);
|
|
170
|
+
if (path.isAbsolute(assetName) || isWindowsAbsolutePath(assetNameNormalized)) {
|
|
171
|
+
const preserveRel = preserveModulesRootAbs && isInside(assetName, preserveModulesRootAbs) ? path.relative(preserveModulesRootAbs, assetName) : null;
|
|
172
|
+
if (preserveRel && !path.isAbsolute(preserveRel) && !preserveRel.startsWith("..")) {
|
|
173
|
+
relativePath = preserveRel;
|
|
174
|
+
} else if (isInside(assetName, rootDir)) {
|
|
175
|
+
relativePath = path.relative(rootDir, assetName);
|
|
176
|
+
}
|
|
177
|
+
} else if (preserveModulesRootRel && assetNameNormalized.startsWith(`${preserveModulesRootRel}/`)) {
|
|
178
|
+
relativePath = assetNameNormalized.slice(preserveModulesRootRel.length + 1);
|
|
179
|
+
} else {
|
|
180
|
+
relativePath = assetNameNormalized;
|
|
181
|
+
}
|
|
182
|
+
const normalized = relativePath ? normalizeAssetRelativePath(relativePath) : null;
|
|
183
|
+
if (!normalized) {
|
|
184
|
+
return template;
|
|
185
|
+
}
|
|
186
|
+
const withoutExt = stripExtension(normalized);
|
|
187
|
+
if (template.includes("[name]")) {
|
|
188
|
+
const dir = path.posix.dirname(withoutExt);
|
|
189
|
+
if (dir === "." || dir === "") {
|
|
190
|
+
return template;
|
|
191
|
+
}
|
|
192
|
+
return template.replace(/\[name\]/g, `${dir}/[name]`);
|
|
193
|
+
}
|
|
194
|
+
const dir = path.posix.dirname(withoutExt);
|
|
195
|
+
if (dir === "." || dir === "") {
|
|
196
|
+
return template;
|
|
197
|
+
}
|
|
198
|
+
const idx = template.indexOf("[");
|
|
199
|
+
if (idx < 0) {
|
|
200
|
+
return template;
|
|
201
|
+
}
|
|
202
|
+
const prefix = template.slice(0, idx);
|
|
203
|
+
if (prefix !== "" && !prefix.endsWith("/")) {
|
|
204
|
+
return template;
|
|
205
|
+
}
|
|
206
|
+
return `${prefix}${dir}/${template.slice(idx)}`;
|
|
207
|
+
};
|
|
211
208
|
};
|
|
212
|
-
export default function wywInJS({
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
});else target.dependencies = dependencies;
|
|
664
|
-
if (didCssChange) {
|
|
665
|
-
const reloadTarget = getCssReloadTarget(this.environment, devServer);
|
|
666
|
-
if (reloadTarget) {
|
|
667
|
-
scheduleCssReload(reloadTarget, cssFilename);
|
|
668
|
-
}
|
|
669
|
-
if (ssrDevCssEnabled && config.command === 'serve') {
|
|
670
|
-
ssrDevCssVersion += 1;
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
/* eslint-disable-next-line consistent-return */
|
|
674
|
-
return {
|
|
675
|
-
code: result.code,
|
|
676
|
-
map: result.sourceMap
|
|
677
|
-
};
|
|
678
|
-
}
|
|
679
|
-
};
|
|
209
|
+
export default function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepComments, prefixer, preprocessor, ssrDevCss, ssrDevCssPath, transformLibraries, ...rest } = {}) {
|
|
210
|
+
const supportedModuleExtensions = new Set([
|
|
211
|
+
".cjs",
|
|
212
|
+
".cts",
|
|
213
|
+
".js",
|
|
214
|
+
".jsx",
|
|
215
|
+
".mjs",
|
|
216
|
+
".mts",
|
|
217
|
+
".ts",
|
|
218
|
+
".tsx"
|
|
219
|
+
]);
|
|
220
|
+
const filter = createFilter(include, exclude);
|
|
221
|
+
const cssLookup = {};
|
|
222
|
+
const cssFileLookup = {};
|
|
223
|
+
const metadataLookup = {};
|
|
224
|
+
const cssFilesByModuleId = new Map();
|
|
225
|
+
const pendingCssReloads = new WeakMap();
|
|
226
|
+
let ssrDevCssVersion = 0;
|
|
227
|
+
let config;
|
|
228
|
+
let devServer;
|
|
229
|
+
let importMetaEnvForEval = null;
|
|
230
|
+
let nativeResolverAlias = {};
|
|
231
|
+
const buildOverrideContext = (getEnv) => (context, filename) => {
|
|
232
|
+
const env = getEnv();
|
|
233
|
+
const withEnv = env ? {
|
|
234
|
+
...context,
|
|
235
|
+
__wyw_import_meta_env: env
|
|
236
|
+
} : context;
|
|
237
|
+
return rest.overrideContext ? rest.overrideContext(withEnv, filename) : withEnv;
|
|
238
|
+
};
|
|
239
|
+
const overrideContextClient = buildOverrideContext(() => importMetaEnvForEval?.client);
|
|
240
|
+
const overrideContextSsr = buildOverrideContext(() => importMetaEnvForEval?.ssr);
|
|
241
|
+
const ssrDevCssEnabled = Boolean(ssrDevCss);
|
|
242
|
+
const [ssrDevCssPathname, ssrDevCssQuery] = (ssrDevCssPath ?? "/_wyw-in-js/ssr.css").split("?", 2);
|
|
243
|
+
const ssrDevCssRoute = ssrDevCssPathname.startsWith("/") ? ssrDevCssPathname : `/${ssrDevCssPathname}`;
|
|
244
|
+
const getSsrDevCssHref = () => {
|
|
245
|
+
const versionParam = `v=${ssrDevCssVersion}`;
|
|
246
|
+
const query = ssrDevCssQuery ? `${ssrDevCssQuery}&${versionParam}` : versionParam;
|
|
247
|
+
return `${ssrDevCssRoute}?${query}`;
|
|
248
|
+
};
|
|
249
|
+
const getSsrDevCssContents = () => {
|
|
250
|
+
const entries = Object.entries(cssLookup);
|
|
251
|
+
if (entries.length === 0) return "";
|
|
252
|
+
const merged = entries.sort(([a], [b]) => a.localeCompare(b)).map(([, cssText]) => cssText).join("\n");
|
|
253
|
+
return `${merged}\n`;
|
|
254
|
+
};
|
|
255
|
+
const { emitter, onDone } = createFileReporter(debug ?? false);
|
|
256
|
+
const isSafeAssetPath = (fileName) => fileName !== "" && fileName !== ".." && !fileName.startsWith(`..${path.posix.sep}`) && !path.posix.isAbsolute(fileName) && !isWindowsAbsolutePath(fileName);
|
|
257
|
+
const replaceModuleExtension = (filename, nextExtension) => {
|
|
258
|
+
const extension = path.extname(filename);
|
|
259
|
+
return supportedModuleExtensions.has(extension) ? `${filename.slice(0, -extension.length)}${nextExtension}` : `${filename}${nextExtension}`;
|
|
260
|
+
};
|
|
261
|
+
const toBundleRelativePath = (filename) => {
|
|
262
|
+
const relativePath = normalizeToPosix(path.relative(config.root, filename));
|
|
263
|
+
if (isSafeAssetPath(relativePath)) {
|
|
264
|
+
return relativePath;
|
|
265
|
+
}
|
|
266
|
+
if (!path.isAbsolute(relativePath) && !isWindowsAbsolutePath(relativePath)) {
|
|
267
|
+
return path.posix.join("_wyw-in-js", "external", ...relativePath.split(path.posix.sep).filter(Boolean).map((segment) => segment === ".." ? "__up__" : segment));
|
|
268
|
+
}
|
|
269
|
+
return path.posix.join("_wyw-in-js", "external", ...normalizeToPosix(path.resolve(filename)).split(path.posix.sep).filter(Boolean).map((segment) => segment.replace(/:$/, "")));
|
|
270
|
+
};
|
|
271
|
+
const scheduleCssReload = (reloadTarget, cssFilename) => {
|
|
272
|
+
let state = pendingCssReloads.get(reloadTarget);
|
|
273
|
+
if (!state) {
|
|
274
|
+
state = { files: new Set() };
|
|
275
|
+
pendingCssReloads.set(reloadTarget, state);
|
|
276
|
+
}
|
|
277
|
+
state.files.add(cssFilename);
|
|
278
|
+
if (state.timer) return;
|
|
279
|
+
state.timer = setTimeout(() => {
|
|
280
|
+
state.timer = undefined;
|
|
281
|
+
const ids = Array.from(state.files);
|
|
282
|
+
state.files.clear();
|
|
283
|
+
const { moduleGraph } = reloadTarget;
|
|
284
|
+
for (const id of ids) {
|
|
285
|
+
const module = moduleGraph.getModuleById(id);
|
|
286
|
+
if (module) reloadTarget.reloadModule(module);
|
|
287
|
+
}
|
|
288
|
+
}, 0);
|
|
289
|
+
};
|
|
290
|
+
// <dependency id, targets>
|
|
291
|
+
const targets = [];
|
|
292
|
+
const clientCache = new TransformCacheCollection();
|
|
293
|
+
const ssrCache = new TransformCacheCollection();
|
|
294
|
+
const caches = new Set([clientCache, ssrCache]);
|
|
295
|
+
let evalBrokersDisposed = false;
|
|
296
|
+
const disposeEvalBrokers = () => {
|
|
297
|
+
if (evalBrokersDisposed) return;
|
|
298
|
+
evalBrokersDisposed = true;
|
|
299
|
+
for (const cache of caches) {
|
|
300
|
+
disposeEvalBroker(cache);
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
const getCache = (isSsr) => isSsr ? ssrCache : clientCache;
|
|
304
|
+
const isInsideCacheDir = (filename) => {
|
|
305
|
+
if (!config.cacheDir) {
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
const relative = path.relative(config.cacheDir, filename);
|
|
309
|
+
return relative !== "" && !relative.startsWith("..") && !path.isAbsolute(relative);
|
|
310
|
+
};
|
|
311
|
+
const getDepsOptimizer = () => {
|
|
312
|
+
if (!devServer) return null;
|
|
313
|
+
const server = devServer;
|
|
314
|
+
return server.environments?.client?.depsOptimizer ?? server.depsOptimizer ?? server._depsOptimizer ?? null;
|
|
315
|
+
};
|
|
316
|
+
const waitForOptimizedDep = async (filename) => {
|
|
317
|
+
const depsOptimizer = getDepsOptimizer();
|
|
318
|
+
if (!depsOptimizer?.isOptimizedDepFile?.(filename)) {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
await depsOptimizer.init?.();
|
|
322
|
+
await depsOptimizer.scanProcessing;
|
|
323
|
+
const info = depsOptimizer.metadata?.depInfoList?.find((item) => item.file === filename);
|
|
324
|
+
if (info?.processing) {
|
|
325
|
+
await info.processing;
|
|
326
|
+
}
|
|
327
|
+
return true;
|
|
328
|
+
};
|
|
329
|
+
let viteResolver = null;
|
|
330
|
+
const resolveClient = (what, importer) => {
|
|
331
|
+
if (!viteResolver) {
|
|
332
|
+
throw new Error("Vite resolver is not initialized yet");
|
|
333
|
+
}
|
|
334
|
+
return viteResolver(what, importer, false, false);
|
|
335
|
+
};
|
|
336
|
+
const resolveSsr = (what, importer) => {
|
|
337
|
+
if (!viteResolver) {
|
|
338
|
+
throw new Error("Vite resolver is not initialized yet");
|
|
339
|
+
}
|
|
340
|
+
return viteResolver(what, importer, false, true);
|
|
341
|
+
};
|
|
342
|
+
const createAsyncResolver = asyncResolverFactory(async (resolved, what, importer, stack) => {
|
|
343
|
+
const log = logger.extend("vite").extend(getFileIdx(importer));
|
|
344
|
+
if (resolved) {
|
|
345
|
+
log("resolve ✅ '%s'@'%s -> %O\n%s", what, importer, resolved);
|
|
346
|
+
// Vite adds param like `?v=667939b3` to cached modules
|
|
347
|
+
let resolvedId = resolved.split("?", 1)[0];
|
|
348
|
+
if (resolvedId.startsWith("\0")) {
|
|
349
|
+
// \0 is a special character in Rollup that tells Rollup to not include this in the bundle
|
|
350
|
+
// https://rollupjs.org/guide/en/#outputexports
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
if (resolvedId.startsWith(VITE_FS_PREFIX)) {
|
|
354
|
+
resolvedId = normalizeViteFsPath(resolvedId);
|
|
355
|
+
}
|
|
356
|
+
if (resolvedId.startsWith("/@")) {
|
|
357
|
+
return null;
|
|
358
|
+
}
|
|
359
|
+
if (!existsSync(resolvedId)) {
|
|
360
|
+
// When Vite resolves to an optimized deps entry (cacheDir) it may not be written yet.
|
|
361
|
+
// Wait for Vite's optimizer instead of calling optimizeDeps() manually (deprecated in Vite 7).
|
|
362
|
+
try {
|
|
363
|
+
await waitForOptimizedDep(resolvedId);
|
|
364
|
+
} catch {}
|
|
365
|
+
// Vite can return an optimized deps entry (from cacheDir) before it's written to disk.
|
|
366
|
+
// Manually calling optimizeDeps is deprecated in Vite 7 and can also get called many times.
|
|
367
|
+
// Instead, fall back to resolving the original module path directly.
|
|
368
|
+
if (!existsSync(resolvedId) && isInsideCacheDir(resolvedId)) {
|
|
369
|
+
try {
|
|
370
|
+
return syncResolve(what, importer, stack);
|
|
371
|
+
} catch {}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (!existsSync(resolvedId) && !path.isAbsolute(resolvedId)) {
|
|
375
|
+
// Vite can resolve an import to a bare specifier when bundling for SSR and marking it as external.
|
|
376
|
+
// In that case we still need a real file path for WyW evaluation.
|
|
377
|
+
return syncResolve(what, importer, stack);
|
|
378
|
+
}
|
|
379
|
+
return resolvedId;
|
|
380
|
+
}
|
|
381
|
+
log("resolve ❌ '%s'@'%s", what, importer);
|
|
382
|
+
// Vite can inject virtual ids like /@react-refresh in dev.
|
|
383
|
+
if (what.startsWith("/@") || what.startsWith("\0")) {
|
|
384
|
+
return null;
|
|
385
|
+
}
|
|
386
|
+
if (!what.startsWith(".") && !what.startsWith("/") && !path.isAbsolute(what)) {
|
|
387
|
+
// Keep compatibility with SSR externalization: fall back to Node resolution for bare specifiers.
|
|
388
|
+
return syncResolve(what, importer, stack);
|
|
389
|
+
}
|
|
390
|
+
throw new Error(`Could not resolve ${what}`);
|
|
391
|
+
}, (what, importer) => [what, importer]);
|
|
392
|
+
const asyncResolveClient = createAsyncResolver(resolveClient);
|
|
393
|
+
const asyncResolveSsr = createAsyncResolver(resolveSsr);
|
|
394
|
+
return {
|
|
395
|
+
name: "wyw-in-js",
|
|
396
|
+
enforce: "post",
|
|
397
|
+
buildStart() {
|
|
398
|
+
Object.keys(metadataLookup).forEach((key) => {
|
|
399
|
+
delete metadataLookup[key];
|
|
400
|
+
});
|
|
401
|
+
},
|
|
402
|
+
buildEnd() {
|
|
403
|
+
onDone(process.cwd());
|
|
404
|
+
if (config.command === "build") {
|
|
405
|
+
disposeEvalBrokers();
|
|
406
|
+
}
|
|
407
|
+
},
|
|
408
|
+
configResolved(resolvedConfig) {
|
|
409
|
+
config = resolvedConfig;
|
|
410
|
+
viteResolver = config.createResolver();
|
|
411
|
+
nativeResolverAlias = toNativeResolverAlias(config.resolve?.alias);
|
|
412
|
+
if (preserveCssPaths && config.command === "build") {
|
|
413
|
+
const outputs = config.build.rollupOptions.output;
|
|
414
|
+
let outputEntries = [];
|
|
415
|
+
if (Array.isArray(outputs)) {
|
|
416
|
+
outputEntries = outputs;
|
|
417
|
+
} else if (outputs) {
|
|
418
|
+
outputEntries = [outputs];
|
|
419
|
+
}
|
|
420
|
+
outputEntries.forEach((entry) => {
|
|
421
|
+
if (!entry || typeof entry !== "object") return;
|
|
422
|
+
const output = entry;
|
|
423
|
+
if (!output.preserveModules) return;
|
|
424
|
+
const template = output.assetFileNames ?? `${config.build.assetsDir ?? "assets"}/[name].[hash].[ext]`;
|
|
425
|
+
const assetFileNames = getWywCssAssetFileNames(config, output, template);
|
|
426
|
+
if (assetFileNames) output.assetFileNames = assetFileNames;
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
const envPrefix = config.envPrefix ?? "VITE_";
|
|
430
|
+
const envDir = "envDir" in config && typeof config.envDir === "string" ? config.envDir : config.root;
|
|
431
|
+
const loaded = loadEnv(config.mode, envDir, envPrefix);
|
|
432
|
+
const base = {
|
|
433
|
+
...loaded,
|
|
434
|
+
BASE_URL: config.base,
|
|
435
|
+
MODE: config.mode,
|
|
436
|
+
DEV: config.command === "serve",
|
|
437
|
+
PROD: config.command === "build"
|
|
438
|
+
};
|
|
439
|
+
importMetaEnvForEval = {
|
|
440
|
+
client: {
|
|
441
|
+
...base,
|
|
442
|
+
SSR: false
|
|
443
|
+
},
|
|
444
|
+
ssr: {
|
|
445
|
+
...base,
|
|
446
|
+
SSR: true
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
},
|
|
450
|
+
configureServer(_server) {
|
|
451
|
+
devServer = _server;
|
|
452
|
+
devServer.httpServer?.once("close", disposeEvalBrokers);
|
|
453
|
+
if (ssrDevCssEnabled && config.command === "serve") {
|
|
454
|
+
devServer.middlewares.use((req, res, next) => {
|
|
455
|
+
const { url } = req;
|
|
456
|
+
if (!url) {
|
|
457
|
+
next();
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
const [pathname] = url.split("?", 1);
|
|
461
|
+
if (pathname !== ssrDevCssRoute) {
|
|
462
|
+
next();
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const etag = `W/"${ssrDevCssVersion}"`;
|
|
466
|
+
const ifNoneMatch = req.headers["if-none-match"];
|
|
467
|
+
if (ifNoneMatch === etag) {
|
|
468
|
+
res.statusCode = 304;
|
|
469
|
+
res.end();
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
res.statusCode = 200;
|
|
473
|
+
res.setHeader("Content-Type", "text/css; charset=utf-8");
|
|
474
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
475
|
+
res.setHeader("ETag", etag);
|
|
476
|
+
res.end(getSsrDevCssContents());
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
},
|
|
480
|
+
transformIndexHtml(html) {
|
|
481
|
+
if (!ssrDevCssEnabled || config.command !== "serve") return undefined;
|
|
482
|
+
return {
|
|
483
|
+
html,
|
|
484
|
+
tags: [{
|
|
485
|
+
tag: "link",
|
|
486
|
+
attrs: {
|
|
487
|
+
rel: "stylesheet",
|
|
488
|
+
href: getSsrDevCssHref()
|
|
489
|
+
},
|
|
490
|
+
injectTo: "head-prepend"
|
|
491
|
+
}]
|
|
492
|
+
};
|
|
493
|
+
},
|
|
494
|
+
load(url) {
|
|
495
|
+
const [id] = url.split("?", 1);
|
|
496
|
+
return cssLookup[id];
|
|
497
|
+
},
|
|
498
|
+
/* eslint-disable-next-line consistent-return */
|
|
499
|
+
resolveId(importeeUrl) {
|
|
500
|
+
const [id] = importeeUrl.split("?", 1);
|
|
501
|
+
if (cssLookup[id]) return id;
|
|
502
|
+
return cssFileLookup[id];
|
|
503
|
+
},
|
|
504
|
+
handleHotUpdate(ctx) {
|
|
505
|
+
// it's module, so just transform it
|
|
506
|
+
if (ctx.modules.length) return ctx.modules;
|
|
507
|
+
// Select affected modules of changed dependency
|
|
508
|
+
const affected = targets.filter((x) => x.dependencies.some((dep) => dep === ctx.file) || x.dependencies.some((dep) => ctx.modules.some((m) => m.file === dep)));
|
|
509
|
+
const deps = affected.flatMap((target) => target.dependencies);
|
|
510
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
511
|
+
for (const depId of deps) {
|
|
512
|
+
for (const cache of caches) {
|
|
513
|
+
cache.invalidateForFile(depId);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return affected.map((target) => devServer.moduleGraph.getModuleById(target.id)).concat(ctx.modules).filter((m) => !!m);
|
|
517
|
+
},
|
|
518
|
+
generateBundle(outputOptions, bundle) {
|
|
519
|
+
Object.entries(metadataLookup).forEach(([fileName, source]) => {
|
|
520
|
+
this.emitFile({
|
|
521
|
+
fileName,
|
|
522
|
+
source,
|
|
523
|
+
type: "asset"
|
|
524
|
+
});
|
|
525
|
+
});
|
|
526
|
+
if (config.command !== "build") return;
|
|
527
|
+
if (!outputOptions.preserveModules) return;
|
|
528
|
+
if (config.build.cssCodeSplit === false) return;
|
|
529
|
+
Object.values(bundle).forEach((item) => {
|
|
530
|
+
if (!isOutputChunkLike(item)) {
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
const chunk = item;
|
|
534
|
+
const moduleId = getTrackedModuleIdForChunk(chunk, cssFilesByModuleId);
|
|
535
|
+
if (!moduleId) {
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
const cssFilename = cssFilesByModuleId.get(moduleId);
|
|
539
|
+
if (!cssFilename) {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
const emittedCssFileName = findWywCssAssetFileName(bundle, cssFilename, config.root);
|
|
543
|
+
if (!emittedCssFileName) {
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
const relativeCssImport = getRelativeImportPath(chunk.fileName, emittedCssFileName);
|
|
547
|
+
if (hasCssLoadStatement(chunk.code, relativeCssImport, outputOptions.format)) {
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
chunk.code = prependCssLoadStatement(chunk.code, relativeCssImport, outputOptions.format);
|
|
551
|
+
});
|
|
552
|
+
},
|
|
553
|
+
async transform(code, url, transformOptions) {
|
|
554
|
+
const [id] = url.split("?", 1);
|
|
555
|
+
// Do not transform ignored and generated files
|
|
556
|
+
if (!transformLibraries && url.includes("node_modules") || !filter(url) || id in cssLookup) return;
|
|
557
|
+
const log = logger.extend("vite").extend(getFileIdx(id));
|
|
558
|
+
log("transform %s", id);
|
|
559
|
+
const isSsr = typeof transformOptions === "boolean" ? transformOptions : Boolean(transformOptions?.ssr);
|
|
560
|
+
const overrideContext = isSsr ? overrideContextSsr : overrideContextClient;
|
|
561
|
+
const transformServices = {
|
|
562
|
+
options: {
|
|
563
|
+
filename: id,
|
|
564
|
+
root: process.cwd(),
|
|
565
|
+
prefixer,
|
|
566
|
+
keepComments,
|
|
567
|
+
preprocessor,
|
|
568
|
+
pluginOptions: {
|
|
569
|
+
...rest,
|
|
570
|
+
oxcOptions: mergeOxcResolverAlias(rest.oxcOptions, nativeResolverAlias),
|
|
571
|
+
overrideContext
|
|
572
|
+
}
|
|
573
|
+
},
|
|
574
|
+
cache: getCache(isSsr),
|
|
575
|
+
emitWarning: (message) => this.warn(message),
|
|
576
|
+
eventEmitter: emitter
|
|
577
|
+
};
|
|
578
|
+
const asyncResolve = isSsr ? asyncResolveSsr : asyncResolveClient;
|
|
579
|
+
const result = await transform(transformServices, code, asyncResolve);
|
|
580
|
+
result.diagnostics?.forEach((diagnostic) => {
|
|
581
|
+
this.warn({
|
|
582
|
+
id: diagnostic.filename,
|
|
583
|
+
loc: diagnostic.start ? {
|
|
584
|
+
column: diagnostic.start.column,
|
|
585
|
+
file: diagnostic.filename,
|
|
586
|
+
line: diagnostic.start.line
|
|
587
|
+
} : undefined,
|
|
588
|
+
message: `[wyw-in-js] ${diagnostic.severity} [${diagnostic.category}] ${diagnostic.message}`,
|
|
589
|
+
pluginCode: diagnostic.category
|
|
590
|
+
});
|
|
591
|
+
});
|
|
592
|
+
const relativeId = normalizeToPosix(path.relative(config.root, id));
|
|
593
|
+
const metadataFilename = replaceModuleExtension(id, ".wyw-in-js.json");
|
|
594
|
+
const metadataRelativePath = toBundleRelativePath(metadataFilename);
|
|
595
|
+
delete metadataLookup[metadataRelativePath];
|
|
596
|
+
if (result.metadata) {
|
|
597
|
+
const cssFile = typeof result.cssText === "string" && result.cssText !== "" ? replaceModuleExtension(relativeId, ".wyw-in-js.css") : undefined;
|
|
598
|
+
metadataLookup[metadataRelativePath] = stringifyMetadataManifest(createMetadataManifest(result.metadata, {
|
|
599
|
+
cssFile,
|
|
600
|
+
source: relativeId
|
|
601
|
+
}));
|
|
602
|
+
}
|
|
603
|
+
let { cssText, dependencies } = result;
|
|
604
|
+
// Heads up, there are three cases:
|
|
605
|
+
// 1. cssText is undefined, it means that file was not transformed
|
|
606
|
+
// 2. cssText is empty, it means that file was transformed, but it does not contain any styles
|
|
607
|
+
// 3. cssText is not empty, it means that file was transformed and it contains styles
|
|
608
|
+
if (typeof cssText === "undefined") {
|
|
609
|
+
cssFilesByModuleId.delete(id);
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
if (cssText === "") {
|
|
613
|
+
cssFilesByModuleId.delete(id);
|
|
614
|
+
/* eslint-disable-next-line consistent-return */
|
|
615
|
+
return {
|
|
616
|
+
code: result.code,
|
|
617
|
+
map: result.sourceMap
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
dependencies ??= [];
|
|
621
|
+
const cssFilename = normalizeToPosix(replaceModuleExtension(id, ".wyw-in-js.css"));
|
|
622
|
+
cssFilesByModuleId.set(id, cssFilename);
|
|
623
|
+
const cssRelativePath = normalizeToPosix(path.relative(config.root, cssFilename));
|
|
624
|
+
const cssId = `/${cssRelativePath}`;
|
|
625
|
+
if (sourceMap && result.cssSourceMapText) {
|
|
626
|
+
const map = Buffer.from(result.cssSourceMapText).toString("base64");
|
|
627
|
+
cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
|
|
628
|
+
}
|
|
629
|
+
const didCssChange = cssLookup[cssFilename] !== cssText;
|
|
630
|
+
cssLookup[cssFilename] = cssText;
|
|
631
|
+
cssFileLookup[cssId] = cssFilename;
|
|
632
|
+
result.code += `\nimport ${JSON.stringify(cssFilename)};\n`;
|
|
633
|
+
for (let i = 0, end = dependencies.length; i < end; i++) {
|
|
634
|
+
// eslint-disable-next-line no-await-in-loop
|
|
635
|
+
const depModule = await this.resolve(dependencies[i], url, { isEntry: false });
|
|
636
|
+
if (depModule) dependencies[i] = depModule.id;
|
|
637
|
+
}
|
|
638
|
+
const target = targets.find((t) => t.id === id);
|
|
639
|
+
if (!target) targets.push({
|
|
640
|
+
id,
|
|
641
|
+
dependencies
|
|
642
|
+
});
|
|
643
|
+
else target.dependencies = dependencies;
|
|
644
|
+
if (didCssChange) {
|
|
645
|
+
const reloadTarget = getCssReloadTarget(this.environment, devServer);
|
|
646
|
+
if (reloadTarget) {
|
|
647
|
+
scheduleCssReload(reloadTarget, cssFilename);
|
|
648
|
+
}
|
|
649
|
+
if (ssrDevCssEnabled && config.command === "serve") {
|
|
650
|
+
ssrDevCssVersion += 1;
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
/* eslint-disable-next-line consistent-return */
|
|
654
|
+
return {
|
|
655
|
+
code: result.code,
|
|
656
|
+
map: result.sourceMap
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
};
|
|
680
660
|
}
|
|
681
|
-
//# sourceMappingURL=index.mjs.map
|
|
661
|
+
//# sourceMappingURL=index.mjs.map
|