@wyw-in-js/vite 1.0.9 → 2.0.0-alpha.0
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/esm/index.mjs +634 -591
- package/esm/index.mjs.map +1 -1
- package/package.json +12 -13
- package/types/index.d.ts +1 -1
- package/types/index.js +192 -92
- package/lib/index.js +0 -627
- package/lib/index.js.map +0 -1
package/types/index.js
CHANGED
|
@@ -1,44 +1,49 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* This file contains a Vite loader for wyw-in-js.
|
|
4
3
|
* It uses the transform.ts function to generate class names from source code,
|
|
5
4
|
* returns transformed code without template literals and attaches generated source maps
|
|
6
5
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
6
|
+
import { existsSync } from 'fs';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import { createFilter, loadEnv } from 'vite';
|
|
9
|
+
import { asyncResolverFactory, logger, syncResolve } 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'
|
|
13
|
+
? createTransformManifest(metadata, context)
|
|
14
|
+
: {
|
|
15
|
+
...metadata,
|
|
16
|
+
...context,
|
|
17
|
+
version: 1,
|
|
18
|
+
};
|
|
19
|
+
const stringifyMetadataManifest = (manifest) => typeof stringifyTransformManifest === 'function'
|
|
20
|
+
? stringifyTransformManifest(manifest)
|
|
21
|
+
: `${JSON.stringify(manifest, null, 2)}\n`;
|
|
17
22
|
const isWindowsAbsolutePath = (value) => /^[a-zA-Z]:[\\/]/.test(value);
|
|
18
|
-
const normalizeToPosix = (value) => value.replace(/\\/g,
|
|
23
|
+
const normalizeToPosix = (value) => value.replace(/\\/g, path.posix.sep);
|
|
19
24
|
const isInside = (childPath, parentPath) => {
|
|
20
|
-
const rel =
|
|
21
|
-
return rel === '' || (!rel.startsWith('..') && !
|
|
25
|
+
const rel = path.relative(parentPath, childPath);
|
|
26
|
+
return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
|
|
22
27
|
};
|
|
23
28
|
const isWywCssAssetName = (value) => value.endsWith('.wyw-in-js.css');
|
|
24
29
|
const normalizeAssetRelativePath = (value) => {
|
|
25
|
-
const normalized =
|
|
26
|
-
if (normalized.startsWith('..') ||
|
|
30
|
+
const normalized = path.posix.normalize(normalizeToPosix(value).replace(/^\/+/, ''));
|
|
31
|
+
if (normalized.startsWith('..') || path.posix.isAbsolute(normalized)) {
|
|
27
32
|
return null;
|
|
28
33
|
}
|
|
29
34
|
return normalized;
|
|
30
35
|
};
|
|
31
36
|
const stripExtension = (value) => {
|
|
32
|
-
const ext =
|
|
37
|
+
const ext = path.posix.extname(value);
|
|
33
38
|
return ext ? value.slice(0, -ext.length) : value;
|
|
34
39
|
};
|
|
35
40
|
const getComparableAssetPaths = (value, rootDir) => {
|
|
36
41
|
const variants = new Set();
|
|
37
42
|
const normalized = normalizeToPosix(value);
|
|
38
43
|
variants.add(normalized);
|
|
39
|
-
if (
|
|
44
|
+
if (path.isAbsolute(value) || isWindowsAbsolutePath(normalized)) {
|
|
40
45
|
if (isInside(value, rootDir)) {
|
|
41
|
-
const relativeToRoot = normalizeAssetRelativePath(
|
|
46
|
+
const relativeToRoot = normalizeAssetRelativePath(path.relative(rootDir, value));
|
|
42
47
|
if (relativeToRoot) {
|
|
43
48
|
variants.add(relativeToRoot);
|
|
44
49
|
}
|
|
@@ -100,8 +105,8 @@ const findWywCssAssetFileName = (bundle, cssFilename, rootDir) => {
|
|
|
100
105
|
return null;
|
|
101
106
|
};
|
|
102
107
|
const getRelativeImportPath = (fromFileName, toFileName) => {
|
|
103
|
-
const fromDir =
|
|
104
|
-
const relativePath =
|
|
108
|
+
const fromDir = path.posix.dirname(normalizeToPosix(fromFileName));
|
|
109
|
+
const relativePath = path.posix.relative(fromDir, normalizeToPosix(toFileName));
|
|
105
110
|
return relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
|
|
106
111
|
};
|
|
107
112
|
const escapeForRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
@@ -144,7 +149,7 @@ const safeDecodeURIComponent = (value) => {
|
|
|
144
149
|
};
|
|
145
150
|
const normalizeViteFsPath = (value) => {
|
|
146
151
|
const fsPath = value.slice(VITE_FS_PREFIX.length);
|
|
147
|
-
return
|
|
152
|
+
return path.normalize(safeDecodeURIComponent(fsPath));
|
|
148
153
|
};
|
|
149
154
|
const isCssReloadTarget = (value) => {
|
|
150
155
|
if (!value || typeof value !== 'object')
|
|
@@ -173,12 +178,12 @@ const getWywCssAssetFileNames = (resolvedConfig, output, originalAssetFileNames)
|
|
|
173
178
|
const preserveModulesRootValue = output.preserveModulesRoot;
|
|
174
179
|
let preserveModulesRootAbs = null;
|
|
175
180
|
if (typeof preserveModulesRootValue === 'string') {
|
|
176
|
-
preserveModulesRootAbs =
|
|
181
|
+
preserveModulesRootAbs = path.isAbsolute(preserveModulesRootValue)
|
|
177
182
|
? preserveModulesRootValue
|
|
178
|
-
:
|
|
183
|
+
: path.resolve(rootDir, preserveModulesRootValue);
|
|
179
184
|
}
|
|
180
185
|
const preserveModulesRootRel = preserveModulesRootAbs && isInside(preserveModulesRootAbs, rootDir)
|
|
181
|
-
? normalizeToPosix(
|
|
186
|
+
? normalizeToPosix(path.relative(rootDir, preserveModulesRootAbs))
|
|
182
187
|
: null;
|
|
183
188
|
return (assetInfo) => {
|
|
184
189
|
const template = typeof originalAssetFileNames === 'function'
|
|
@@ -193,18 +198,18 @@ const getWywCssAssetFileNames = (resolvedConfig, output, originalAssetFileNames)
|
|
|
193
198
|
}
|
|
194
199
|
let relativePath = null;
|
|
195
200
|
const assetNameNormalized = normalizeToPosix(assetName);
|
|
196
|
-
if (
|
|
201
|
+
if (path.isAbsolute(assetName) ||
|
|
197
202
|
isWindowsAbsolutePath(assetNameNormalized)) {
|
|
198
203
|
const preserveRel = preserveModulesRootAbs && isInside(assetName, preserveModulesRootAbs)
|
|
199
|
-
?
|
|
204
|
+
? path.relative(preserveModulesRootAbs, assetName)
|
|
200
205
|
: null;
|
|
201
206
|
if (preserveRel &&
|
|
202
|
-
!
|
|
207
|
+
!path.isAbsolute(preserveRel) &&
|
|
203
208
|
!preserveRel.startsWith('..')) {
|
|
204
209
|
relativePath = preserveRel;
|
|
205
210
|
}
|
|
206
211
|
else if (isInside(assetName, rootDir)) {
|
|
207
|
-
relativePath =
|
|
212
|
+
relativePath = path.relative(rootDir, assetName);
|
|
208
213
|
}
|
|
209
214
|
}
|
|
210
215
|
else if (preserveModulesRootRel &&
|
|
@@ -222,13 +227,13 @@ const getWywCssAssetFileNames = (resolvedConfig, output, originalAssetFileNames)
|
|
|
222
227
|
}
|
|
223
228
|
const withoutExt = stripExtension(normalized);
|
|
224
229
|
if (template.includes('[name]')) {
|
|
225
|
-
const dir =
|
|
230
|
+
const dir = path.posix.dirname(withoutExt);
|
|
226
231
|
if (dir === '.' || dir === '') {
|
|
227
232
|
return template;
|
|
228
233
|
}
|
|
229
234
|
return template.replace(/\[name\]/g, `${dir}/[name]`);
|
|
230
235
|
}
|
|
231
|
-
const dir =
|
|
236
|
+
const dir = path.posix.dirname(withoutExt);
|
|
232
237
|
if (dir === '.' || dir === '') {
|
|
233
238
|
return template;
|
|
234
239
|
}
|
|
@@ -243,16 +248,38 @@ const getWywCssAssetFileNames = (resolvedConfig, output, originalAssetFileNames)
|
|
|
243
248
|
return `${prefix}${dir}/${template.slice(idx)}`;
|
|
244
249
|
};
|
|
245
250
|
};
|
|
246
|
-
function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepComments, prefixer, preprocessor, ssrDevCss, ssrDevCssPath, transformLibraries, ...rest } = {}) {
|
|
247
|
-
const
|
|
251
|
+
export default function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepComments, prefixer, preprocessor, ssrDevCss, ssrDevCssPath, transformLibraries, ...rest } = {}) {
|
|
252
|
+
const supportedModuleExtensions = new Set([
|
|
253
|
+
'.cjs',
|
|
254
|
+
'.cts',
|
|
255
|
+
'.js',
|
|
256
|
+
'.jsx',
|
|
257
|
+
'.mjs',
|
|
258
|
+
'.mts',
|
|
259
|
+
'.ts',
|
|
260
|
+
'.tsx',
|
|
261
|
+
]);
|
|
262
|
+
const filter = createFilter(include, exclude);
|
|
248
263
|
const cssLookup = {};
|
|
249
264
|
const cssFileLookup = {};
|
|
265
|
+
const metadataLookup = {};
|
|
250
266
|
const cssFilesByModuleId = new Map();
|
|
251
267
|
const pendingCssReloads = new WeakMap();
|
|
252
268
|
let ssrDevCssVersion = 0;
|
|
253
269
|
let config;
|
|
254
270
|
let devServer;
|
|
255
271
|
let importMetaEnvForEval = null;
|
|
272
|
+
const buildOverrideContext = (getEnv) => (context, filename) => {
|
|
273
|
+
const env = getEnv();
|
|
274
|
+
const withEnv = env
|
|
275
|
+
? { ...context, __wyw_import_meta_env: env }
|
|
276
|
+
: context;
|
|
277
|
+
return rest.overrideContext
|
|
278
|
+
? rest.overrideContext(withEnv, filename)
|
|
279
|
+
: withEnv;
|
|
280
|
+
};
|
|
281
|
+
const overrideContextClient = buildOverrideContext(() => importMetaEnvForEval?.client);
|
|
282
|
+
const overrideContextSsr = buildOverrideContext(() => importMetaEnvForEval?.ssr);
|
|
256
283
|
const ssrDevCssEnabled = Boolean(ssrDevCss);
|
|
257
284
|
const [ssrDevCssPathname, ssrDevCssQuery] = (ssrDevCssPath ?? '/_wyw-in-js/ssr.css').split('?', 2);
|
|
258
285
|
const ssrDevCssRoute = ssrDevCssPathname.startsWith('/')
|
|
@@ -275,7 +302,35 @@ function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepCom
|
|
|
275
302
|
.join('\n');
|
|
276
303
|
return `${merged}\n`;
|
|
277
304
|
};
|
|
278
|
-
const { emitter, onDone } =
|
|
305
|
+
const { emitter, onDone } = createFileReporter(debug ?? false);
|
|
306
|
+
const isSafeAssetPath = (fileName) => fileName !== '' &&
|
|
307
|
+
fileName !== '..' &&
|
|
308
|
+
!fileName.startsWith(`..${path.posix.sep}`) &&
|
|
309
|
+
!path.posix.isAbsolute(fileName) &&
|
|
310
|
+
!isWindowsAbsolutePath(fileName);
|
|
311
|
+
const replaceModuleExtension = (filename, nextExtension) => {
|
|
312
|
+
const extension = path.extname(filename);
|
|
313
|
+
return supportedModuleExtensions.has(extension)
|
|
314
|
+
? `${filename.slice(0, -extension.length)}${nextExtension}`
|
|
315
|
+
: `${filename}${nextExtension}`;
|
|
316
|
+
};
|
|
317
|
+
const toBundleRelativePath = (filename) => {
|
|
318
|
+
const relativePath = normalizeToPosix(path.relative(config.root, filename));
|
|
319
|
+
if (isSafeAssetPath(relativePath)) {
|
|
320
|
+
return relativePath;
|
|
321
|
+
}
|
|
322
|
+
if (!path.isAbsolute(relativePath) &&
|
|
323
|
+
!isWindowsAbsolutePath(relativePath)) {
|
|
324
|
+
return path.posix.join('_wyw-in-js', 'external', ...relativePath
|
|
325
|
+
.split(path.posix.sep)
|
|
326
|
+
.filter(Boolean)
|
|
327
|
+
.map((segment) => (segment === '..' ? '__up__' : segment)));
|
|
328
|
+
}
|
|
329
|
+
return path.posix.join('_wyw-in-js', 'external', ...normalizeToPosix(path.resolve(filename))
|
|
330
|
+
.split(path.posix.sep)
|
|
331
|
+
.filter(Boolean)
|
|
332
|
+
.map((segment) => segment.replace(/:$/, '')));
|
|
333
|
+
};
|
|
279
334
|
const scheduleCssReload = (reloadTarget, cssFilename) => {
|
|
280
335
|
let state = pendingCssReloads.get(reloadTarget);
|
|
281
336
|
if (!state) {
|
|
@@ -299,18 +354,27 @@ function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepCom
|
|
|
299
354
|
};
|
|
300
355
|
// <dependency id, targets>
|
|
301
356
|
const targets = [];
|
|
302
|
-
const clientCache = new
|
|
303
|
-
const ssrCache = new
|
|
357
|
+
const clientCache = new TransformCacheCollection();
|
|
358
|
+
const ssrCache = new TransformCacheCollection();
|
|
304
359
|
const caches = new Set([clientCache, ssrCache]);
|
|
360
|
+
let evalBrokersDisposed = false;
|
|
361
|
+
const disposeEvalBrokers = () => {
|
|
362
|
+
if (evalBrokersDisposed)
|
|
363
|
+
return;
|
|
364
|
+
evalBrokersDisposed = true;
|
|
365
|
+
for (const cache of caches) {
|
|
366
|
+
disposeEvalBroker(cache);
|
|
367
|
+
}
|
|
368
|
+
};
|
|
305
369
|
const getCache = (isSsr) => isSsr ? ssrCache : clientCache;
|
|
306
370
|
const isInsideCacheDir = (filename) => {
|
|
307
371
|
if (!config.cacheDir) {
|
|
308
372
|
return false;
|
|
309
373
|
}
|
|
310
|
-
const relative =
|
|
374
|
+
const relative = path.relative(config.cacheDir, filename);
|
|
311
375
|
return (relative !== '' &&
|
|
312
376
|
!relative.startsWith('..') &&
|
|
313
|
-
!
|
|
377
|
+
!path.isAbsolute(relative));
|
|
314
378
|
};
|
|
315
379
|
const getDepsOptimizer = () => {
|
|
316
380
|
if (!devServer)
|
|
@@ -347,8 +411,8 @@ function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepCom
|
|
|
347
411
|
}
|
|
348
412
|
return viteResolver(what, importer, false, true);
|
|
349
413
|
};
|
|
350
|
-
const createAsyncResolver =
|
|
351
|
-
const log =
|
|
414
|
+
const createAsyncResolver = asyncResolverFactory(async (resolved, what, importer, stack) => {
|
|
415
|
+
const log = logger.extend('vite').extend(getFileIdx(importer));
|
|
352
416
|
if (resolved) {
|
|
353
417
|
log("resolve ✅ '%s'@'%s -> %O\n%s", what, importer, resolved);
|
|
354
418
|
// Vite adds param like `?v=667939b3` to cached modules
|
|
@@ -364,7 +428,7 @@ function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepCom
|
|
|
364
428
|
if (resolvedId.startsWith('/@')) {
|
|
365
429
|
return null;
|
|
366
430
|
}
|
|
367
|
-
if (!
|
|
431
|
+
if (!existsSync(resolvedId)) {
|
|
368
432
|
// When Vite resolves to an optimized deps entry (cacheDir) it may not be written yet.
|
|
369
433
|
// Wait for Vite's optimizer instead of calling optimizeDeps() manually (deprecated in Vite 7).
|
|
370
434
|
try {
|
|
@@ -376,19 +440,19 @@ function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepCom
|
|
|
376
440
|
// Vite can return an optimized deps entry (from cacheDir) before it's written to disk.
|
|
377
441
|
// Manually calling optimizeDeps is deprecated in Vite 7 and can also get called many times.
|
|
378
442
|
// Instead, fall back to resolving the original module path directly.
|
|
379
|
-
if (!
|
|
443
|
+
if (!existsSync(resolvedId) && isInsideCacheDir(resolvedId)) {
|
|
380
444
|
try {
|
|
381
|
-
return
|
|
445
|
+
return syncResolve(what, importer, stack);
|
|
382
446
|
}
|
|
383
447
|
catch {
|
|
384
448
|
// Fall through to preserve previous behavior: return resolvedId and let WyW surface the error.
|
|
385
449
|
}
|
|
386
450
|
}
|
|
387
451
|
}
|
|
388
|
-
if (!
|
|
452
|
+
if (!existsSync(resolvedId) && !path.isAbsolute(resolvedId)) {
|
|
389
453
|
// Vite can resolve an import to a bare specifier when bundling for SSR and marking it as external.
|
|
390
454
|
// In that case we still need a real file path for WyW evaluation.
|
|
391
|
-
return
|
|
455
|
+
return syncResolve(what, importer, stack);
|
|
392
456
|
}
|
|
393
457
|
return resolvedId;
|
|
394
458
|
}
|
|
@@ -399,19 +463,30 @@ function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepCom
|
|
|
399
463
|
}
|
|
400
464
|
if (!what.startsWith('.') &&
|
|
401
465
|
!what.startsWith('/') &&
|
|
402
|
-
!
|
|
466
|
+
!path.isAbsolute(what)) {
|
|
403
467
|
// Keep compatibility with SSR externalization: fall back to Node resolution for bare specifiers.
|
|
404
|
-
return
|
|
468
|
+
return syncResolve(what, importer, stack);
|
|
405
469
|
}
|
|
406
470
|
throw new Error(`Could not resolve ${what}`);
|
|
407
|
-
}, (what, importer) => [
|
|
471
|
+
}, (what, importer) => [
|
|
472
|
+
what,
|
|
473
|
+
importer,
|
|
474
|
+
]);
|
|
408
475
|
const asyncResolveClient = createAsyncResolver(resolveClient);
|
|
409
476
|
const asyncResolveSsr = createAsyncResolver(resolveSsr);
|
|
410
477
|
return {
|
|
411
478
|
name: 'wyw-in-js',
|
|
412
479
|
enforce: 'post',
|
|
480
|
+
buildStart() {
|
|
481
|
+
Object.keys(metadataLookup).forEach((key) => {
|
|
482
|
+
delete metadataLookup[key];
|
|
483
|
+
});
|
|
484
|
+
},
|
|
413
485
|
buildEnd() {
|
|
414
486
|
onDone(process.cwd());
|
|
487
|
+
if (config.command === 'build') {
|
|
488
|
+
disposeEvalBrokers();
|
|
489
|
+
}
|
|
415
490
|
},
|
|
416
491
|
configResolved(resolvedConfig) {
|
|
417
492
|
config = resolvedConfig;
|
|
@@ -444,7 +519,7 @@ function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepCom
|
|
|
444
519
|
'envDir' in config && typeof config.envDir === 'string'
|
|
445
520
|
? config.envDir
|
|
446
521
|
: config.root;
|
|
447
|
-
const loaded =
|
|
522
|
+
const loaded = loadEnv(config.mode, envDir, envPrefix);
|
|
448
523
|
const base = {
|
|
449
524
|
...loaded,
|
|
450
525
|
BASE_URL: config.base,
|
|
@@ -459,32 +534,33 @@ function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepCom
|
|
|
459
534
|
},
|
|
460
535
|
configureServer(_server) {
|
|
461
536
|
devServer = _server;
|
|
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
|
-
|
|
537
|
+
devServer.httpServer?.once('close', disposeEvalBrokers);
|
|
538
|
+
if (ssrDevCssEnabled && config.command === 'serve') {
|
|
539
|
+
devServer.middlewares.use((req, res, next) => {
|
|
540
|
+
const { url } = req;
|
|
541
|
+
if (!url) {
|
|
542
|
+
next();
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
const [pathname] = url.split('?', 1);
|
|
546
|
+
if (pathname !== ssrDevCssRoute) {
|
|
547
|
+
next();
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
const etag = `W/"${ssrDevCssVersion}"`;
|
|
551
|
+
const ifNoneMatch = req.headers['if-none-match'];
|
|
552
|
+
if (ifNoneMatch === etag) {
|
|
553
|
+
res.statusCode = 304;
|
|
554
|
+
res.end();
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
res.statusCode = 200;
|
|
558
|
+
res.setHeader('Content-Type', 'text/css; charset=utf-8');
|
|
559
|
+
res.setHeader('Cache-Control', 'no-cache');
|
|
560
|
+
res.setHeader('ETag', etag);
|
|
561
|
+
res.end(getSsrDevCssContents());
|
|
562
|
+
});
|
|
563
|
+
}
|
|
488
564
|
},
|
|
489
565
|
transformIndexHtml(html) {
|
|
490
566
|
if (!ssrDevCssEnabled || config.command !== 'serve')
|
|
@@ -534,6 +610,13 @@ function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepCom
|
|
|
534
610
|
.filter((m) => !!m);
|
|
535
611
|
},
|
|
536
612
|
generateBundle(outputOptions, bundle) {
|
|
613
|
+
Object.entries(metadataLookup).forEach(([fileName, source]) => {
|
|
614
|
+
this.emitFile({
|
|
615
|
+
fileName,
|
|
616
|
+
source,
|
|
617
|
+
type: 'asset',
|
|
618
|
+
});
|
|
619
|
+
});
|
|
537
620
|
if (config.command !== 'build')
|
|
538
621
|
return;
|
|
539
622
|
if (!outputOptions.preserveModules)
|
|
@@ -571,20 +654,14 @@ function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepCom
|
|
|
571
654
|
!filter(url) ||
|
|
572
655
|
id in cssLookup)
|
|
573
656
|
return;
|
|
574
|
-
const log =
|
|
657
|
+
const log = logger.extend('vite').extend(getFileIdx(id));
|
|
575
658
|
log('transform %s', id);
|
|
576
659
|
const isSsr = typeof transformOptions === 'boolean'
|
|
577
660
|
? transformOptions
|
|
578
661
|
: Boolean(transformOptions?.ssr);
|
|
579
|
-
const overrideContext =
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
? { ...context, __wyw_import_meta_env: env }
|
|
583
|
-
: context;
|
|
584
|
-
return rest.overrideContext
|
|
585
|
-
? rest.overrideContext(withEnv, filename)
|
|
586
|
-
: withEnv;
|
|
587
|
-
};
|
|
662
|
+
const overrideContext = isSsr
|
|
663
|
+
? overrideContextSsr
|
|
664
|
+
: overrideContextClient;
|
|
588
665
|
const transformServices = {
|
|
589
666
|
options: {
|
|
590
667
|
filename: id,
|
|
@@ -602,7 +679,34 @@ function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepCom
|
|
|
602
679
|
eventEmitter: emitter,
|
|
603
680
|
};
|
|
604
681
|
const asyncResolve = isSsr ? asyncResolveSsr : asyncResolveClient;
|
|
605
|
-
const result = await
|
|
682
|
+
const result = await transform(transformServices, code, asyncResolve);
|
|
683
|
+
result.diagnostics?.forEach((diagnostic) => {
|
|
684
|
+
this.warn({
|
|
685
|
+
id: diagnostic.filename,
|
|
686
|
+
loc: diagnostic.start
|
|
687
|
+
? {
|
|
688
|
+
column: diagnostic.start.column,
|
|
689
|
+
file: diagnostic.filename,
|
|
690
|
+
line: diagnostic.start.line,
|
|
691
|
+
}
|
|
692
|
+
: undefined,
|
|
693
|
+
message: `[wyw-in-js] ${diagnostic.severity} [${diagnostic.category}] ${diagnostic.message}`,
|
|
694
|
+
pluginCode: diagnostic.category,
|
|
695
|
+
});
|
|
696
|
+
});
|
|
697
|
+
const relativeId = normalizeToPosix(path.relative(config.root, id));
|
|
698
|
+
const metadataFilename = replaceModuleExtension(id, '.wyw-in-js.json');
|
|
699
|
+
const metadataRelativePath = toBundleRelativePath(metadataFilename);
|
|
700
|
+
delete metadataLookup[metadataRelativePath];
|
|
701
|
+
if (result.metadata) {
|
|
702
|
+
const cssFile = typeof result.cssText === 'string' && result.cssText !== ''
|
|
703
|
+
? replaceModuleExtension(relativeId, '.wyw-in-js.css')
|
|
704
|
+
: undefined;
|
|
705
|
+
metadataLookup[metadataRelativePath] = stringifyMetadataManifest(createMetadataManifest(result.metadata, {
|
|
706
|
+
cssFile,
|
|
707
|
+
source: relativeId,
|
|
708
|
+
}));
|
|
709
|
+
}
|
|
606
710
|
let { cssText, dependencies } = result;
|
|
607
711
|
// Heads up, there are three cases:
|
|
608
712
|
// 1. cssText is undefined, it means that file was not transformed
|
|
@@ -621,13 +725,9 @@ function wywInJS({ debug, include, exclude, sourceMap, preserveCssPaths, keepCom
|
|
|
621
725
|
};
|
|
622
726
|
}
|
|
623
727
|
dependencies ??= [];
|
|
624
|
-
const cssFilename =
|
|
625
|
-
.normalize(`${id.replace(/\.[jt]sx?$/, '')}.wyw-in-js.css`)
|
|
626
|
-
.replace(/\\/g, path_1.default.posix.sep);
|
|
728
|
+
const cssFilename = normalizeToPosix(replaceModuleExtension(id, '.wyw-in-js.css'));
|
|
627
729
|
cssFilesByModuleId.set(id, cssFilename);
|
|
628
|
-
const cssRelativePath =
|
|
629
|
-
.relative(config.root, cssFilename)
|
|
630
|
-
.replace(/\\/g, path_1.default.posix.sep);
|
|
730
|
+
const cssRelativePath = normalizeToPosix(path.relative(config.root, cssFilename));
|
|
631
731
|
const cssId = `/${cssRelativePath}`;
|
|
632
732
|
if (sourceMap && result.cssSourceMapText) {
|
|
633
733
|
const map = Buffer.from(result.cssSourceMapText).toString('base64');
|