@sveltejs/vite-plugin-svelte 1.3.1 → 2.0.0-beta.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/dist/index.d.ts +12 -14
- package/dist/index.js +448 -339
- package/dist/index.js.map +1 -1
- package/package.json +8 -21
- package/src/handle-hot-update.ts +24 -24
- package/src/index.ts +23 -24
- package/src/preprocess.ts +114 -0
- package/src/utils/compile.ts +71 -23
- package/src/utils/id.ts +55 -3
- package/src/utils/load-raw.ts +132 -0
- package/src/utils/options.ts +16 -9
- package/src/utils/preprocess.ts +9 -150
- package/dist/index.cjs +0 -2226
- package/dist/index.cjs.map +0 -1
- package/src/utils/__tests__/sourcemap.spec.ts +0 -25
- package/src/utils/sourcemap.ts +0 -58
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
7
7
|
});
|
|
8
8
|
|
|
9
9
|
// src/index.ts
|
|
10
|
-
import
|
|
10
|
+
import fs8 from "fs";
|
|
11
11
|
import { isDepExcluded as isDepExcluded2 } from "vitefu";
|
|
12
12
|
|
|
13
13
|
// src/utils/log.ts
|
|
@@ -184,7 +184,7 @@ async function handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, option
|
|
|
184
184
|
log.debug(`handleHotUpdate called before initial transform for ${svelteRequest.id}`);
|
|
185
185
|
return;
|
|
186
186
|
}
|
|
187
|
-
const { read, server } = ctx;
|
|
187
|
+
const { read, server, modules } = ctx;
|
|
188
188
|
const cachedJS = cache.getJS(svelteRequest);
|
|
189
189
|
const cachedCss = cache.getCSS(svelteRequest);
|
|
190
190
|
const content = await read();
|
|
@@ -196,34 +196,35 @@ async function handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, option
|
|
|
196
196
|
cache.setError(svelteRequest, e);
|
|
197
197
|
throw e;
|
|
198
198
|
}
|
|
199
|
-
const affectedModules =
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
const jsUpdated = mainModule && jsChanged(cachedJS, compileData.compiled.js, svelteRequest.filename);
|
|
208
|
-
if (jsUpdated) {
|
|
209
|
-
log.debug(`handleHotUpdate js changed for ${svelteRequest.id}`);
|
|
210
|
-
affectedModules.add(mainModule);
|
|
199
|
+
const affectedModules = [...modules];
|
|
200
|
+
const cssIdx = modules.findIndex((m) => m.id === svelteRequest.cssId);
|
|
201
|
+
if (cssIdx > -1) {
|
|
202
|
+
const cssUpdated = cssChanged(cachedCss, compileData.compiled.css);
|
|
203
|
+
if (!cssUpdated) {
|
|
204
|
+
log.debug(`skipping unchanged css for ${svelteRequest.cssId}`);
|
|
205
|
+
affectedModules.splice(cssIdx, 1);
|
|
206
|
+
}
|
|
211
207
|
}
|
|
212
|
-
|
|
213
|
-
|
|
208
|
+
const jsIdx = modules.findIndex((m) => m.id === svelteRequest.id);
|
|
209
|
+
if (jsIdx > -1) {
|
|
210
|
+
const jsUpdated = jsChanged(cachedJS, compileData.compiled.js, svelteRequest.filename);
|
|
211
|
+
if (!jsUpdated) {
|
|
212
|
+
log.debug(`skipping unchanged js for ${svelteRequest.id}`);
|
|
213
|
+
affectedModules.splice(jsIdx, 1);
|
|
214
|
+
logCompilerWarnings(svelteRequest, compileData.compiled.warnings, options);
|
|
215
|
+
}
|
|
214
216
|
}
|
|
215
|
-
const
|
|
216
|
-
const ssrModulesToInvalidate = result.filter((m) => !!m.ssrTransformResult);
|
|
217
|
+
const ssrModulesToInvalidate = affectedModules.filter((m) => !!m.ssrTransformResult);
|
|
217
218
|
if (ssrModulesToInvalidate.length > 0) {
|
|
218
219
|
log.debug(`invalidating modules ${ssrModulesToInvalidate.map((m) => m.id).join(", ")}`);
|
|
219
220
|
ssrModulesToInvalidate.forEach((moduleNode) => server.moduleGraph.invalidateModule(moduleNode));
|
|
220
221
|
}
|
|
221
|
-
if (
|
|
222
|
+
if (affectedModules.length > 0) {
|
|
222
223
|
log.debug(
|
|
223
|
-
`handleHotUpdate for ${svelteRequest.id} result: ${
|
|
224
|
+
`handleHotUpdate for ${svelteRequest.id} result: ${affectedModules.map((m) => m.id).join(", ")}`
|
|
224
225
|
);
|
|
225
226
|
}
|
|
226
|
-
return
|
|
227
|
+
return affectedModules;
|
|
227
228
|
}
|
|
228
229
|
function cssChanged(prev, next) {
|
|
229
230
|
return !isCodeEqual(prev?.code, next?.code);
|
|
@@ -260,7 +261,7 @@ function normalizeJsCode(code) {
|
|
|
260
261
|
}
|
|
261
262
|
|
|
262
263
|
// src/utils/compile.ts
|
|
263
|
-
import { compile, preprocess, walk } from "svelte/compiler";
|
|
264
|
+
import { compile, preprocess as preprocess2, walk } from "svelte/compiler";
|
|
264
265
|
import { createMakeHot } from "svelte-hmr";
|
|
265
266
|
|
|
266
267
|
// src/utils/hash.ts
|
|
@@ -287,12 +288,210 @@ function toSafe(base64) {
|
|
|
287
288
|
return base64.replace(replaceRE, (x) => replacements[x]);
|
|
288
289
|
}
|
|
289
290
|
|
|
291
|
+
// src/utils/preprocess.ts
|
|
292
|
+
import MagicString from "magic-string";
|
|
293
|
+
import { preprocess } from "svelte/compiler";
|
|
294
|
+
import path2 from "path";
|
|
295
|
+
|
|
296
|
+
// src/preprocess.ts
|
|
297
|
+
import path from "path";
|
|
298
|
+
import * as vite from "vite";
|
|
299
|
+
var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss", "sss"];
|
|
300
|
+
var supportedScriptLangs = ["ts"];
|
|
301
|
+
function vitePreprocess(opts) {
|
|
302
|
+
const preprocessor = {};
|
|
303
|
+
if (opts?.script !== false) {
|
|
304
|
+
preprocessor.script = viteScript().script;
|
|
305
|
+
}
|
|
306
|
+
if (opts?.style !== false) {
|
|
307
|
+
const styleOpts = typeof opts?.style == "object" ? opts?.style : void 0;
|
|
308
|
+
preprocessor.style = viteStyle(styleOpts).style;
|
|
309
|
+
}
|
|
310
|
+
return preprocessor;
|
|
311
|
+
}
|
|
312
|
+
function viteScript() {
|
|
313
|
+
return {
|
|
314
|
+
async script({ attributes, content, filename = "" }) {
|
|
315
|
+
const lang = attributes.lang;
|
|
316
|
+
if (!supportedScriptLangs.includes(lang))
|
|
317
|
+
return;
|
|
318
|
+
const transformResult = await vite.transformWithEsbuild(content, filename, {
|
|
319
|
+
loader: lang,
|
|
320
|
+
target: "esnext",
|
|
321
|
+
tsconfigRaw: {
|
|
322
|
+
compilerOptions: {
|
|
323
|
+
importsNotUsedAsValues: "preserve",
|
|
324
|
+
preserveValueImports: true
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
return {
|
|
329
|
+
code: transformResult.code,
|
|
330
|
+
map: transformResult.map
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
function viteStyle(config = {}) {
|
|
336
|
+
let transform;
|
|
337
|
+
const style = async ({ attributes, content, filename = "" }) => {
|
|
338
|
+
const lang = attributes.lang;
|
|
339
|
+
if (!supportedStyleLangs.includes(lang))
|
|
340
|
+
return;
|
|
341
|
+
if (!transform) {
|
|
342
|
+
let resolvedConfig;
|
|
343
|
+
if (style.__resolvedConfig) {
|
|
344
|
+
resolvedConfig = style.__resolvedConfig;
|
|
345
|
+
} else if (isResolvedConfig(config)) {
|
|
346
|
+
resolvedConfig = config;
|
|
347
|
+
} else {
|
|
348
|
+
resolvedConfig = await vite.resolveConfig(
|
|
349
|
+
config,
|
|
350
|
+
process.env.NODE_ENV === "production" ? "build" : "serve"
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
transform = getCssTransformFn(resolvedConfig);
|
|
354
|
+
}
|
|
355
|
+
const moduleId = `${filename}.${lang}`;
|
|
356
|
+
const result = await transform(content, moduleId);
|
|
357
|
+
if (result.map?.sources?.[0] === moduleId) {
|
|
358
|
+
result.map.sources[0] = path.basename(filename);
|
|
359
|
+
}
|
|
360
|
+
return {
|
|
361
|
+
code: result.code,
|
|
362
|
+
map: result.map ?? void 0
|
|
363
|
+
};
|
|
364
|
+
};
|
|
365
|
+
style.__resolvedConfig = null;
|
|
366
|
+
return { style };
|
|
367
|
+
}
|
|
368
|
+
function getCssTransformFn(config) {
|
|
369
|
+
if (vite.preprocessCSS) {
|
|
370
|
+
return async (code, filename) => {
|
|
371
|
+
return vite.preprocessCSS(code, filename, config);
|
|
372
|
+
};
|
|
373
|
+
} else {
|
|
374
|
+
const pluginName = "vite:css";
|
|
375
|
+
const plugin = config.plugins.find((p) => p.name === pluginName);
|
|
376
|
+
if (!plugin) {
|
|
377
|
+
throw new Error(`failed to find plugin ${pluginName}`);
|
|
378
|
+
}
|
|
379
|
+
if (!plugin.transform) {
|
|
380
|
+
throw new Error(`plugin ${pluginName} has no transform`);
|
|
381
|
+
}
|
|
382
|
+
return plugin.transform.bind(null);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
function isResolvedConfig(config) {
|
|
386
|
+
return !!config.inlineConfig;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// src/utils/preprocess.ts
|
|
390
|
+
function createVitePreprocessorGroup(config) {
|
|
391
|
+
return {
|
|
392
|
+
markup({ content, filename }) {
|
|
393
|
+
return preprocess(content, vitePreprocess({ style: config }), { filename });
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
function createInjectScopeEverythingRulePreprocessorGroup() {
|
|
398
|
+
return {
|
|
399
|
+
style({ content, filename }) {
|
|
400
|
+
const s = new MagicString(content);
|
|
401
|
+
s.append(" *{}");
|
|
402
|
+
return {
|
|
403
|
+
code: s.toString(),
|
|
404
|
+
map: s.generateDecodedMap({
|
|
405
|
+
source: filename ? path2.basename(filename) : void 0,
|
|
406
|
+
hires: true
|
|
407
|
+
})
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
function buildExtraPreprocessors(options, config) {
|
|
413
|
+
const prependPreprocessors = [];
|
|
414
|
+
const appendPreprocessors = [];
|
|
415
|
+
if (options.experimental?.useVitePreprocess) {
|
|
416
|
+
log.warn(
|
|
417
|
+
"`experimental.useVitePreprocess` is deprecated. Use the `vitePreprocess()` preprocessor instead. See https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/preprocess.md for more information."
|
|
418
|
+
);
|
|
419
|
+
prependPreprocessors.push(createVitePreprocessorGroup(config));
|
|
420
|
+
}
|
|
421
|
+
const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p?.sveltePreprocess);
|
|
422
|
+
if (pluginsWithPreprocessorsDeprecated.length > 0) {
|
|
423
|
+
log.warn(
|
|
424
|
+
`The following plugins use the deprecated 'plugin.sveltePreprocess' field. Please contact their maintainers and ask them to move it to 'plugin.api.sveltePreprocess': ${pluginsWithPreprocessorsDeprecated.map((p) => p.name).join(", ")}`
|
|
425
|
+
);
|
|
426
|
+
pluginsWithPreprocessorsDeprecated.forEach((p) => {
|
|
427
|
+
if (!p.api) {
|
|
428
|
+
p.api = {};
|
|
429
|
+
}
|
|
430
|
+
if (p.api.sveltePreprocess === void 0) {
|
|
431
|
+
p.api.sveltePreprocess = p.sveltePreprocess;
|
|
432
|
+
} else {
|
|
433
|
+
log.error(
|
|
434
|
+
`ignoring plugin.sveltePreprocess of ${p.name} because it already defined plugin.api.sveltePreprocess.`
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
const pluginsWithPreprocessors = config.plugins.filter((p) => p?.api?.sveltePreprocess);
|
|
440
|
+
const ignored = [], included = [];
|
|
441
|
+
for (const p of pluginsWithPreprocessors) {
|
|
442
|
+
if (options.ignorePluginPreprocessors === true || Array.isArray(options.ignorePluginPreprocessors) && options.ignorePluginPreprocessors?.includes(p.name)) {
|
|
443
|
+
ignored.push(p);
|
|
444
|
+
} else {
|
|
445
|
+
included.push(p);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
if (ignored.length > 0) {
|
|
449
|
+
log.debug(
|
|
450
|
+
`Ignoring svelte preprocessors defined by these vite plugins: ${ignored.map((p) => p.name).join(", ")}`
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
if (included.length > 0) {
|
|
454
|
+
log.debug(
|
|
455
|
+
`Adding svelte preprocessors defined by these vite plugins: ${included.map((p) => p.name).join(", ")}`
|
|
456
|
+
);
|
|
457
|
+
appendPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess));
|
|
458
|
+
}
|
|
459
|
+
return { prependPreprocessors, appendPreprocessors };
|
|
460
|
+
}
|
|
461
|
+
function addExtraPreprocessors(options, config) {
|
|
462
|
+
const { prependPreprocessors, appendPreprocessors } = buildExtraPreprocessors(options, config);
|
|
463
|
+
if (prependPreprocessors.length > 0 || appendPreprocessors.length > 0) {
|
|
464
|
+
if (!options.preprocess) {
|
|
465
|
+
options.preprocess = [...prependPreprocessors, ...appendPreprocessors];
|
|
466
|
+
} else if (Array.isArray(options.preprocess)) {
|
|
467
|
+
options.preprocess.unshift(...prependPreprocessors);
|
|
468
|
+
options.preprocess.push(...appendPreprocessors);
|
|
469
|
+
} else {
|
|
470
|
+
options.preprocess = [...prependPreprocessors, options.preprocess, ...appendPreprocessors];
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
290
475
|
// src/utils/compile.ts
|
|
476
|
+
import path3 from "path";
|
|
291
477
|
var scriptLangRE = /<script [^>]*lang=["']?([^"' >]+)["']?[^>]*>/;
|
|
478
|
+
function mapSourcesToRelative(map, filename) {
|
|
479
|
+
if (map?.sources) {
|
|
480
|
+
map.sources = map.sources.map((s) => {
|
|
481
|
+
if (path3.isAbsolute(s)) {
|
|
482
|
+
const relative = path3.relative(filename, s);
|
|
483
|
+
return relative === "" ? path3.basename(filename) : relative;
|
|
484
|
+
} else {
|
|
485
|
+
return s;
|
|
486
|
+
}
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
}
|
|
292
490
|
var _createCompileSvelte = (makeHot) => {
|
|
293
491
|
let stats;
|
|
492
|
+
const devStylePreprocessor = createInjectScopeEverythingRulePreprocessorGroup();
|
|
294
493
|
return async function compileSvelte2(svelteRequest, code, options) {
|
|
295
|
-
const { filename, normalizedFilename, cssId, ssr } = svelteRequest;
|
|
494
|
+
const { filename, normalizedFilename, cssId, ssr, raw } = svelteRequest;
|
|
296
495
|
const { emitCss = true } = options;
|
|
297
496
|
const dependencies = [];
|
|
298
497
|
if (options.stats) {
|
|
@@ -314,7 +513,7 @@ var _createCompileSvelte = (makeHot) => {
|
|
|
314
513
|
}
|
|
315
514
|
const compileOptions = {
|
|
316
515
|
...options.compilerOptions,
|
|
317
|
-
filename,
|
|
516
|
+
filename: normalizedFilename,
|
|
318
517
|
generate: ssr ? "ssr" : "dom",
|
|
319
518
|
format: "esm"
|
|
320
519
|
};
|
|
@@ -331,9 +530,17 @@ var _createCompileSvelte = (makeHot) => {
|
|
|
331
530
|
}
|
|
332
531
|
}
|
|
333
532
|
let preprocessed;
|
|
334
|
-
|
|
533
|
+
let preprocessors = options.preprocess;
|
|
534
|
+
if (!options.isBuild && options.emitCss && options.hot) {
|
|
535
|
+
if (!Array.isArray(preprocessors)) {
|
|
536
|
+
preprocessors = preprocessors ? [preprocessors, devStylePreprocessor] : [devStylePreprocessor];
|
|
537
|
+
} else {
|
|
538
|
+
preprocessors = preprocessors.concat(devStylePreprocessor);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
if (preprocessors) {
|
|
335
542
|
try {
|
|
336
|
-
preprocessed = await
|
|
543
|
+
preprocessed = await preprocess2(code, preprocessors, { filename });
|
|
337
544
|
} catch (e) {
|
|
338
545
|
e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ""}`;
|
|
339
546
|
throw e;
|
|
@@ -343,6 +550,12 @@ var _createCompileSvelte = (makeHot) => {
|
|
|
343
550
|
if (preprocessed.map)
|
|
344
551
|
compileOptions.sourcemap = preprocessed.map;
|
|
345
552
|
}
|
|
553
|
+
if (typeof preprocessed?.map === "object") {
|
|
554
|
+
mapSourcesToRelative(preprocessed?.map, filename);
|
|
555
|
+
}
|
|
556
|
+
if (raw && svelteRequest.query.type === "preprocessed") {
|
|
557
|
+
return { preprocessed: preprocessed ?? { code } };
|
|
558
|
+
}
|
|
346
559
|
const finalCode = preprocessed ? preprocessed.code : code;
|
|
347
560
|
const dynamicCompileOptions = await options.experimental?.dynamicCompileOptions?.({
|
|
348
561
|
filename,
|
|
@@ -363,21 +576,25 @@ var _createCompileSvelte = (makeHot) => {
|
|
|
363
576
|
if (endStat) {
|
|
364
577
|
endStat();
|
|
365
578
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
579
|
+
mapSourcesToRelative(compiled.js?.map, filename);
|
|
580
|
+
mapSourcesToRelative(compiled.css?.map, filename);
|
|
581
|
+
if (!raw) {
|
|
582
|
+
const hasCss = compiled.css?.code?.trim().length > 0;
|
|
583
|
+
if (emitCss && hasCss) {
|
|
584
|
+
compiled.js.code += `
|
|
369
585
|
import ${JSON.stringify(cssId)};
|
|
370
586
|
`;
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
587
|
+
}
|
|
588
|
+
if (!ssr && makeHot) {
|
|
589
|
+
compiled.js.code = makeHot({
|
|
590
|
+
id: filename,
|
|
591
|
+
compiledCode: compiled.js.code,
|
|
592
|
+
hotOptions: { ...options.hot, injectCss: options.hot?.injectCss === true && hasCss },
|
|
593
|
+
compiled,
|
|
594
|
+
originalCode: code,
|
|
595
|
+
compileOptions: finalCompileOptions
|
|
596
|
+
});
|
|
597
|
+
}
|
|
381
598
|
}
|
|
382
599
|
compiled.js.dependencies = dependencies;
|
|
383
600
|
return {
|
|
@@ -386,7 +603,8 @@ import ${JSON.stringify(cssId)};
|
|
|
386
603
|
lang: code.match(scriptLangRE)?.[1] || "js",
|
|
387
604
|
compiled,
|
|
388
605
|
ssr,
|
|
389
|
-
dependencies
|
|
606
|
+
dependencies,
|
|
607
|
+
preprocessed: preprocessed ?? { code }
|
|
390
608
|
};
|
|
391
609
|
};
|
|
392
610
|
};
|
|
@@ -414,6 +632,16 @@ import { normalizePath } from "vite";
|
|
|
414
632
|
import * as fs from "fs";
|
|
415
633
|
var VITE_FS_PREFIX = "/@fs/";
|
|
416
634
|
var IS_WINDOWS = process.platform === "win32";
|
|
635
|
+
var SUPPORTED_COMPILER_OPTIONS = [
|
|
636
|
+
"generate",
|
|
637
|
+
"dev",
|
|
638
|
+
"css",
|
|
639
|
+
"hydratable",
|
|
640
|
+
"customElement",
|
|
641
|
+
"immutable",
|
|
642
|
+
"enableSourcemap"
|
|
643
|
+
];
|
|
644
|
+
var TYPES_WITH_COMPILER_OPTIONS = ["style", "script", "all"];
|
|
417
645
|
function splitId(id) {
|
|
418
646
|
const parts = id.split(`?`, 2);
|
|
419
647
|
const filename = parts[0];
|
|
@@ -422,9 +650,11 @@ function splitId(id) {
|
|
|
422
650
|
}
|
|
423
651
|
function parseToSvelteRequest(id, filename, rawQuery, root, timestamp, ssr) {
|
|
424
652
|
const query = parseRequestQuery(rawQuery);
|
|
425
|
-
|
|
653
|
+
const rawOrDirect = !!(query.raw || query.direct);
|
|
654
|
+
if (query.url || !query.svelte && rawOrDirect) {
|
|
426
655
|
return;
|
|
427
656
|
}
|
|
657
|
+
const raw = rawOrDirect;
|
|
428
658
|
const normalizedFilename = normalize(filename, root);
|
|
429
659
|
const cssId = createVirtualImportId(filename, root, "style");
|
|
430
660
|
return {
|
|
@@ -434,7 +664,8 @@ function parseToSvelteRequest(id, filename, rawQuery, root, timestamp, ssr) {
|
|
|
434
664
|
cssId,
|
|
435
665
|
query,
|
|
436
666
|
timestamp,
|
|
437
|
-
ssr
|
|
667
|
+
ssr,
|
|
668
|
+
raw
|
|
438
669
|
};
|
|
439
670
|
}
|
|
440
671
|
function createVirtualImportId(filename, root, type) {
|
|
@@ -456,6 +687,33 @@ function parseRequestQuery(rawQuery) {
|
|
|
456
687
|
query[key] = true;
|
|
457
688
|
}
|
|
458
689
|
}
|
|
690
|
+
const compilerOptions = query.compilerOptions;
|
|
691
|
+
if (compilerOptions) {
|
|
692
|
+
if (!((query.raw || query.direct) && TYPES_WITH_COMPILER_OPTIONS.includes(query.type))) {
|
|
693
|
+
throw new Error(
|
|
694
|
+
`Invalid compilerOptions in query ${rawQuery}. CompilerOptions are only supported for raw or direct queries with type in "${TYPES_WITH_COMPILER_OPTIONS.join(
|
|
695
|
+
", "
|
|
696
|
+
)}" e.g. '?svelte&raw&type=script&compilerOptions={"generate":"ssr","dev":false}`
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
try {
|
|
700
|
+
const parsed = JSON.parse(compilerOptions);
|
|
701
|
+
const invalid = Object.keys(parsed).filter(
|
|
702
|
+
(key) => !SUPPORTED_COMPILER_OPTIONS.includes(key)
|
|
703
|
+
);
|
|
704
|
+
if (invalid.length) {
|
|
705
|
+
throw new Error(
|
|
706
|
+
`Invalid compilerOptions in query ${rawQuery}: ${invalid.join(
|
|
707
|
+
", "
|
|
708
|
+
)}. Supported: ${SUPPORTED_COMPILER_OPTIONS.join(", ")}`
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
query.compilerOptions = parsed;
|
|
712
|
+
} catch (e) {
|
|
713
|
+
log.error("failed to parse request query compilerOptions", e);
|
|
714
|
+
throw e;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
459
717
|
return query;
|
|
460
718
|
}
|
|
461
719
|
function normalize(filename, normalizedRoot) {
|
|
@@ -491,7 +749,7 @@ import { normalizePath as normalizePath3 } from "vite";
|
|
|
491
749
|
|
|
492
750
|
// src/utils/load-svelte-config.ts
|
|
493
751
|
import { createRequire } from "module";
|
|
494
|
-
import
|
|
752
|
+
import path4 from "path";
|
|
495
753
|
import fs2 from "fs";
|
|
496
754
|
import { pathToFileURL } from "url";
|
|
497
755
|
var esmRequire;
|
|
@@ -557,13 +815,13 @@ async function loadSvelteConfig(viteConfig, inlineOptions) {
|
|
|
557
815
|
function findConfigToLoad(viteConfig, inlineOptions) {
|
|
558
816
|
const root = viteConfig?.root || process.cwd();
|
|
559
817
|
if (inlineOptions?.configFile) {
|
|
560
|
-
const abolutePath =
|
|
818
|
+
const abolutePath = path4.isAbsolute(inlineOptions.configFile) ? inlineOptions.configFile : path4.resolve(root, inlineOptions.configFile);
|
|
561
819
|
if (!fs2.existsSync(abolutePath)) {
|
|
562
820
|
throw new Error(`failed to find svelte config file ${abolutePath}.`);
|
|
563
821
|
}
|
|
564
822
|
return abolutePath;
|
|
565
823
|
} else {
|
|
566
|
-
const existingKnownConfigFiles = knownSvelteConfigNames.map((candidate) =>
|
|
824
|
+
const existingKnownConfigFiles = knownSvelteConfigNames.map((candidate) => path4.resolve(root, candidate)).filter((file) => fs2.existsSync(file));
|
|
567
825
|
if (existingKnownConfigFiles.length === 0) {
|
|
568
826
|
log.debug(`no svelte config found at ${root}`);
|
|
569
827
|
return;
|
|
@@ -598,11 +856,11 @@ var SVELTE_HMR_IMPORTS = [
|
|
|
598
856
|
var SVELTE_EXPORT_CONDITIONS = ["svelte"];
|
|
599
857
|
|
|
600
858
|
// src/utils/options.ts
|
|
601
|
-
import
|
|
859
|
+
import path6 from "path";
|
|
602
860
|
|
|
603
861
|
// src/utils/esbuild.ts
|
|
604
862
|
import { readFileSync } from "fs";
|
|
605
|
-
import { compile as compile2, preprocess as
|
|
863
|
+
import { compile as compile2, preprocess as preprocess3 } from "svelte/compiler";
|
|
606
864
|
|
|
607
865
|
// src/utils/error.ts
|
|
608
866
|
function toRollupError(error, options) {
|
|
@@ -734,7 +992,7 @@ async function compileSvelte(options, { filename, code }, statsCollection) {
|
|
|
734
992
|
let preprocessed;
|
|
735
993
|
if (options.preprocess) {
|
|
736
994
|
try {
|
|
737
|
-
preprocessed = await
|
|
995
|
+
preprocessed = await preprocess3(code, options.preprocess, { filename });
|
|
738
996
|
} catch (e) {
|
|
739
997
|
e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ""}`;
|
|
740
998
|
throw e;
|
|
@@ -763,262 +1021,6 @@ async function compileSvelte(options, { filename, code }, statsCollection) {
|
|
|
763
1021
|
return compiled.js.code + "//# sourceMappingURL=" + compiled.js.map.toUrl();
|
|
764
1022
|
}
|
|
765
1023
|
|
|
766
|
-
// src/utils/preprocess.ts
|
|
767
|
-
import * as vite from "vite";
|
|
768
|
-
import MagicString2 from "magic-string";
|
|
769
|
-
import { preprocess as preprocess3 } from "svelte/compiler";
|
|
770
|
-
|
|
771
|
-
// src/utils/sourcemap.ts
|
|
772
|
-
import MagicString from "magic-string";
|
|
773
|
-
async function buildMagicString(from, to, options) {
|
|
774
|
-
let diff_match_patch, DIFF_DELETE, DIFF_INSERT;
|
|
775
|
-
try {
|
|
776
|
-
const dmpPkg = await import("diff-match-patch");
|
|
777
|
-
diff_match_patch = dmpPkg.diff_match_patch;
|
|
778
|
-
DIFF_INSERT = dmpPkg.DIFF_INSERT;
|
|
779
|
-
DIFF_DELETE = dmpPkg.DIFF_DELETE;
|
|
780
|
-
} catch (e) {
|
|
781
|
-
log.error.once(
|
|
782
|
-
'Failed to import optional dependency "diff-match-patch". Please install it to enable generated sourcemaps.'
|
|
783
|
-
);
|
|
784
|
-
return null;
|
|
785
|
-
}
|
|
786
|
-
const dmp = new diff_match_patch();
|
|
787
|
-
const diffs = dmp.diff_main(from, to);
|
|
788
|
-
dmp.diff_cleanupSemantic(diffs);
|
|
789
|
-
const m = new MagicString(from, options);
|
|
790
|
-
let pos = 0;
|
|
791
|
-
for (let i = 0; i < diffs.length; i++) {
|
|
792
|
-
const diff = diffs[i];
|
|
793
|
-
const nextDiff = diffs[i + 1];
|
|
794
|
-
if (diff[0] === DIFF_DELETE) {
|
|
795
|
-
if (nextDiff?.[0] === DIFF_INSERT) {
|
|
796
|
-
m.overwrite(pos, pos + diff[1].length, nextDiff[1]);
|
|
797
|
-
i++;
|
|
798
|
-
} else {
|
|
799
|
-
m.remove(pos, pos + diff[1].length);
|
|
800
|
-
}
|
|
801
|
-
pos += diff[1].length;
|
|
802
|
-
} else if (diff[0] === DIFF_INSERT) {
|
|
803
|
-
if (nextDiff) {
|
|
804
|
-
m.appendRight(pos, diff[1]);
|
|
805
|
-
} else {
|
|
806
|
-
m.append(diff[1]);
|
|
807
|
-
}
|
|
808
|
-
} else {
|
|
809
|
-
pos += diff[1].length;
|
|
810
|
-
}
|
|
811
|
-
}
|
|
812
|
-
return m;
|
|
813
|
-
}
|
|
814
|
-
async function buildSourceMap(from, to, filename) {
|
|
815
|
-
const m = await buildMagicString(from, to, { filename });
|
|
816
|
-
return m ? m.generateDecodedMap({ source: filename, hires: true, includeContent: false }) : null;
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
// src/utils/preprocess.ts
|
|
820
|
-
import path2 from "path";
|
|
821
|
-
var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"];
|
|
822
|
-
var supportedScriptLangs = ["ts"];
|
|
823
|
-
function createViteScriptPreprocessor() {
|
|
824
|
-
return async ({ attributes, content, filename = "" }) => {
|
|
825
|
-
const lang = attributes.lang;
|
|
826
|
-
if (!supportedScriptLangs.includes(lang))
|
|
827
|
-
return;
|
|
828
|
-
const transformResult = await vite.transformWithEsbuild(content, filename, {
|
|
829
|
-
loader: lang,
|
|
830
|
-
target: "esnext",
|
|
831
|
-
tsconfigRaw: {
|
|
832
|
-
compilerOptions: {
|
|
833
|
-
importsNotUsedAsValues: "preserve",
|
|
834
|
-
preserveValueImports: true
|
|
835
|
-
}
|
|
836
|
-
}
|
|
837
|
-
});
|
|
838
|
-
return {
|
|
839
|
-
code: transformResult.code,
|
|
840
|
-
map: transformResult.map
|
|
841
|
-
};
|
|
842
|
-
};
|
|
843
|
-
}
|
|
844
|
-
function createViteStylePreprocessor(config) {
|
|
845
|
-
const transform = getCssTransformFn(config);
|
|
846
|
-
return async ({ attributes, content, filename = "" }) => {
|
|
847
|
-
const lang = attributes.lang;
|
|
848
|
-
if (!supportedStyleLangs.includes(lang))
|
|
849
|
-
return;
|
|
850
|
-
const moduleId = `${filename}.${lang}`;
|
|
851
|
-
const result = await transform(content, moduleId);
|
|
852
|
-
if (result.map?.sources?.[0] === moduleId) {
|
|
853
|
-
result.map.sources[0] = path2.basename(filename);
|
|
854
|
-
}
|
|
855
|
-
return {
|
|
856
|
-
code: result.code,
|
|
857
|
-
map: result.map ?? void 0
|
|
858
|
-
};
|
|
859
|
-
};
|
|
860
|
-
}
|
|
861
|
-
function getCssTransformFn(config) {
|
|
862
|
-
if (vite.preprocessCSS) {
|
|
863
|
-
return async (code, filename) => {
|
|
864
|
-
return vite.preprocessCSS(code, filename, config);
|
|
865
|
-
};
|
|
866
|
-
} else {
|
|
867
|
-
const pluginName = "vite:css";
|
|
868
|
-
const plugin = config.plugins.find((p) => p.name === pluginName);
|
|
869
|
-
if (!plugin) {
|
|
870
|
-
throw new Error(`failed to find plugin ${pluginName}`);
|
|
871
|
-
}
|
|
872
|
-
if (!plugin.transform) {
|
|
873
|
-
throw new Error(`plugin ${pluginName} has no transform`);
|
|
874
|
-
}
|
|
875
|
-
return plugin.transform.bind(null);
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
function createVitePreprocessorGroup(config) {
|
|
879
|
-
return {
|
|
880
|
-
markup({ content, filename }) {
|
|
881
|
-
return preprocess3(
|
|
882
|
-
content,
|
|
883
|
-
{
|
|
884
|
-
script: createViteScriptPreprocessor(),
|
|
885
|
-
style: createViteStylePreprocessor(config)
|
|
886
|
-
},
|
|
887
|
-
{ filename }
|
|
888
|
-
);
|
|
889
|
-
}
|
|
890
|
-
};
|
|
891
|
-
}
|
|
892
|
-
function createInjectScopeEverythingRulePreprocessorGroup() {
|
|
893
|
-
return {
|
|
894
|
-
style({ content, filename }) {
|
|
895
|
-
const s = new MagicString2(content);
|
|
896
|
-
s.append(" *{}");
|
|
897
|
-
return {
|
|
898
|
-
code: s.toString(),
|
|
899
|
-
map: s.generateDecodedMap({
|
|
900
|
-
source: filename ? path2.basename(filename) : void 0,
|
|
901
|
-
hires: true
|
|
902
|
-
})
|
|
903
|
-
};
|
|
904
|
-
}
|
|
905
|
-
};
|
|
906
|
-
}
|
|
907
|
-
function buildExtraPreprocessors(options, config) {
|
|
908
|
-
const prependPreprocessors = [];
|
|
909
|
-
const appendPreprocessors = [];
|
|
910
|
-
if (options.experimental?.useVitePreprocess) {
|
|
911
|
-
log.debug("adding vite preprocessor");
|
|
912
|
-
prependPreprocessors.push(createVitePreprocessorGroup(config));
|
|
913
|
-
}
|
|
914
|
-
const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p?.sveltePreprocess);
|
|
915
|
-
if (pluginsWithPreprocessorsDeprecated.length > 0) {
|
|
916
|
-
log.warn(
|
|
917
|
-
`The following plugins use the deprecated 'plugin.sveltePreprocess' field. Please contact their maintainers and ask them to move it to 'plugin.api.sveltePreprocess': ${pluginsWithPreprocessorsDeprecated.map((p) => p.name).join(", ")}`
|
|
918
|
-
);
|
|
919
|
-
pluginsWithPreprocessorsDeprecated.forEach((p) => {
|
|
920
|
-
if (!p.api) {
|
|
921
|
-
p.api = {};
|
|
922
|
-
}
|
|
923
|
-
if (p.api.sveltePreprocess === void 0) {
|
|
924
|
-
p.api.sveltePreprocess = p.sveltePreprocess;
|
|
925
|
-
} else {
|
|
926
|
-
log.error(
|
|
927
|
-
`ignoring plugin.sveltePreprocess of ${p.name} because it already defined plugin.api.sveltePreprocess.`
|
|
928
|
-
);
|
|
929
|
-
}
|
|
930
|
-
});
|
|
931
|
-
}
|
|
932
|
-
const pluginsWithPreprocessors = config.plugins.filter((p) => p?.api?.sveltePreprocess);
|
|
933
|
-
const ignored = [], included = [];
|
|
934
|
-
for (const p of pluginsWithPreprocessors) {
|
|
935
|
-
if (options.ignorePluginPreprocessors === true || Array.isArray(options.ignorePluginPreprocessors) && options.ignorePluginPreprocessors?.includes(p.name)) {
|
|
936
|
-
ignored.push(p);
|
|
937
|
-
} else {
|
|
938
|
-
included.push(p);
|
|
939
|
-
}
|
|
940
|
-
}
|
|
941
|
-
if (ignored.length > 0) {
|
|
942
|
-
log.debug(
|
|
943
|
-
`Ignoring svelte preprocessors defined by these vite plugins: ${ignored.map((p) => p.name).join(", ")}`
|
|
944
|
-
);
|
|
945
|
-
}
|
|
946
|
-
if (included.length > 0) {
|
|
947
|
-
log.debug(
|
|
948
|
-
`Adding svelte preprocessors defined by these vite plugins: ${included.map((p) => p.name).join(", ")}`
|
|
949
|
-
);
|
|
950
|
-
appendPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess));
|
|
951
|
-
}
|
|
952
|
-
if (options.hot && options.emitCss) {
|
|
953
|
-
appendPreprocessors.push(createInjectScopeEverythingRulePreprocessorGroup());
|
|
954
|
-
}
|
|
955
|
-
return { prependPreprocessors, appendPreprocessors };
|
|
956
|
-
}
|
|
957
|
-
function addExtraPreprocessors(options, config) {
|
|
958
|
-
const { prependPreprocessors, appendPreprocessors } = buildExtraPreprocessors(options, config);
|
|
959
|
-
if (prependPreprocessors.length > 0 || appendPreprocessors.length > 0) {
|
|
960
|
-
if (!options.preprocess) {
|
|
961
|
-
options.preprocess = [...prependPreprocessors, ...appendPreprocessors];
|
|
962
|
-
} else if (Array.isArray(options.preprocess)) {
|
|
963
|
-
options.preprocess.unshift(...prependPreprocessors);
|
|
964
|
-
options.preprocess.push(...appendPreprocessors);
|
|
965
|
-
} else {
|
|
966
|
-
options.preprocess = [...prependPreprocessors, options.preprocess, ...appendPreprocessors];
|
|
967
|
-
}
|
|
968
|
-
}
|
|
969
|
-
const generateMissingSourceMaps = !!options.experimental?.generateMissingPreprocessorSourcemaps;
|
|
970
|
-
if (options.preprocess && generateMissingSourceMaps) {
|
|
971
|
-
options.preprocess = Array.isArray(options.preprocess) ? options.preprocess.map((p, i) => validateSourceMapOutputWrapper(p, i)) : validateSourceMapOutputWrapper(options.preprocess, 0);
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
function validateSourceMapOutputWrapper(group, i) {
|
|
975
|
-
const wrapper = {};
|
|
976
|
-
for (const [processorType, processorFn] of Object.entries(group)) {
|
|
977
|
-
wrapper[processorType] = async (options) => {
|
|
978
|
-
const result = await processorFn(options);
|
|
979
|
-
if (result && result.code !== options.content) {
|
|
980
|
-
let invalidMap = false;
|
|
981
|
-
if (!result.map) {
|
|
982
|
-
invalidMap = true;
|
|
983
|
-
log.warn.enabled && log.warn.once(
|
|
984
|
-
`preprocessor at index ${i} did not return a sourcemap for ${processorType} transform`,
|
|
985
|
-
{
|
|
986
|
-
filename: options.filename,
|
|
987
|
-
type: processorType,
|
|
988
|
-
processor: processorFn.toString()
|
|
989
|
-
}
|
|
990
|
-
);
|
|
991
|
-
} else if (result.map?.mappings === "") {
|
|
992
|
-
invalidMap = true;
|
|
993
|
-
log.warn.enabled && log.warn.once(
|
|
994
|
-
`preprocessor at index ${i} returned an invalid empty sourcemap for ${processorType} transform`,
|
|
995
|
-
{
|
|
996
|
-
filename: options.filename,
|
|
997
|
-
type: processorType,
|
|
998
|
-
processor: processorFn.toString()
|
|
999
|
-
}
|
|
1000
|
-
);
|
|
1001
|
-
}
|
|
1002
|
-
if (invalidMap) {
|
|
1003
|
-
try {
|
|
1004
|
-
const map = await buildSourceMap(options.content, result.code, options.filename);
|
|
1005
|
-
if (map) {
|
|
1006
|
-
log.debug.enabled && log.debug(
|
|
1007
|
-
`adding generated sourcemap to preprocesor result for ${options.filename}`
|
|
1008
|
-
);
|
|
1009
|
-
result.map = map;
|
|
1010
|
-
}
|
|
1011
|
-
} catch (e) {
|
|
1012
|
-
log.error(`failed to build sourcemap`, e);
|
|
1013
|
-
}
|
|
1014
|
-
}
|
|
1015
|
-
}
|
|
1016
|
-
return result;
|
|
1017
|
-
};
|
|
1018
|
-
}
|
|
1019
|
-
return wrapper;
|
|
1020
|
-
}
|
|
1021
|
-
|
|
1022
1024
|
// src/utils/options.ts
|
|
1023
1025
|
import deepmerge from "deepmerge";
|
|
1024
1026
|
import {
|
|
@@ -1030,7 +1032,7 @@ import {
|
|
|
1030
1032
|
} from "vitefu";
|
|
1031
1033
|
|
|
1032
1034
|
// src/utils/dependencies.ts
|
|
1033
|
-
import
|
|
1035
|
+
import path5 from "path";
|
|
1034
1036
|
import fs3 from "fs/promises";
|
|
1035
1037
|
import { findDepPkgJsonPath } from "vitefu";
|
|
1036
1038
|
async function resolveDependencyData(dep, parent) {
|
|
@@ -1039,7 +1041,7 @@ async function resolveDependencyData(dep, parent) {
|
|
|
1039
1041
|
return void 0;
|
|
1040
1042
|
try {
|
|
1041
1043
|
return {
|
|
1042
|
-
dir:
|
|
1044
|
+
dir: path5.dirname(depDataPath),
|
|
1043
1045
|
pkg: JSON.parse(await fs3.readFile(depDataPath, "utf-8"))
|
|
1044
1046
|
};
|
|
1045
1047
|
} catch {
|
|
@@ -1136,7 +1138,7 @@ function formatPackageStats(pkgStats) {
|
|
|
1136
1138
|
}
|
|
1137
1139
|
async function getClosestNamedPackage(file) {
|
|
1138
1140
|
let name = "$unknown";
|
|
1139
|
-
let
|
|
1141
|
+
let path11 = await findClosestPkgJsonPath(file, (pkgPath) => {
|
|
1140
1142
|
const pkg = JSON.parse(readFileSync2(pkgPath, "utf-8"));
|
|
1141
1143
|
if (pkg.name != null) {
|
|
1142
1144
|
name = pkg.name;
|
|
@@ -1144,8 +1146,8 @@ async function getClosestNamedPackage(file) {
|
|
|
1144
1146
|
}
|
|
1145
1147
|
return false;
|
|
1146
1148
|
});
|
|
1147
|
-
|
|
1148
|
-
return { name, path:
|
|
1149
|
+
path11 = normalizePath2(dirname(path11 ?? file)) + "/";
|
|
1150
|
+
return { name, path: path11 };
|
|
1149
1151
|
}
|
|
1150
1152
|
var VitePluginSvelteStats = class {
|
|
1151
1153
|
constructor() {
|
|
@@ -1487,9 +1489,12 @@ function handleDeprecatedOptions(options) {
|
|
|
1487
1489
|
"experimental.prebundleSvelteLibraries is no longer experimental and has moved to prebundleSvelteLibraries"
|
|
1488
1490
|
);
|
|
1489
1491
|
}
|
|
1492
|
+
if (options.experimental?.generateMissingPreprocessorSourcemaps) {
|
|
1493
|
+
log.warn("experimental.generateMissingPreprocessorSourcemaps has been removed.");
|
|
1494
|
+
}
|
|
1490
1495
|
}
|
|
1491
1496
|
function resolveViteRoot(viteConfig) {
|
|
1492
|
-
return normalizePath3(viteConfig.root ?
|
|
1497
|
+
return normalizePath3(viteConfig.root ? path6.resolve(viteConfig.root) : process.cwd());
|
|
1493
1498
|
}
|
|
1494
1499
|
async function buildExtraViteConfig(options, config) {
|
|
1495
1500
|
const extraViteConfig = {
|
|
@@ -1646,6 +1651,13 @@ function buildExtraConfigForSvelte(config) {
|
|
|
1646
1651
|
return { optimizeDeps: { include, exclude }, ssr: { noExternal, external } };
|
|
1647
1652
|
}
|
|
1648
1653
|
function patchResolvedViteConfig(viteConfig, options) {
|
|
1654
|
+
if (options.preprocess) {
|
|
1655
|
+
for (const preprocessor of arraify(options.preprocess)) {
|
|
1656
|
+
if (preprocessor.style && "__resolvedConfig" in preprocessor.style) {
|
|
1657
|
+
preprocessor.style.__resolvedConfig = viteConfig;
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1649
1661
|
const facadeEsbuildSveltePlugin = viteConfig.optimizeDeps.esbuildOptions?.plugins?.find(
|
|
1650
1662
|
(plugin) => plugin.name === facadeEsbuildSveltePluginName
|
|
1651
1663
|
);
|
|
@@ -1653,10 +1665,13 @@ function patchResolvedViteConfig(viteConfig, options) {
|
|
|
1653
1665
|
Object.assign(facadeEsbuildSveltePlugin, esbuildSveltePlugin(options));
|
|
1654
1666
|
}
|
|
1655
1667
|
}
|
|
1668
|
+
function arraify(value) {
|
|
1669
|
+
return Array.isArray(value) ? value : [value];
|
|
1670
|
+
}
|
|
1656
1671
|
|
|
1657
1672
|
// src/utils/watch.ts
|
|
1658
1673
|
import fs4 from "fs";
|
|
1659
|
-
import
|
|
1674
|
+
import path7 from "path";
|
|
1660
1675
|
function setupWatchers(options, cache, requestParser) {
|
|
1661
1676
|
const { server, configFile: svelteConfigFile } = options;
|
|
1662
1677
|
if (!server) {
|
|
@@ -1703,7 +1718,7 @@ function setupWatchers(options, cache, requestParser) {
|
|
|
1703
1718
|
unlink: [removeUnlinkedFromCache, emitChangeEventOnDependants]
|
|
1704
1719
|
};
|
|
1705
1720
|
if (svelteConfigFile !== false) {
|
|
1706
|
-
const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) =>
|
|
1721
|
+
const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path7.join(root, cfg));
|
|
1707
1722
|
const restartOnConfigAdd = (filename) => {
|
|
1708
1723
|
if (possibleSvelteConfigs.includes(filename)) {
|
|
1709
1724
|
triggerViteRestart(filename);
|
|
@@ -1729,12 +1744,12 @@ function setupWatchers(options, cache, requestParser) {
|
|
|
1729
1744
|
}
|
|
1730
1745
|
function ensureWatchedFile(watcher, file, root) {
|
|
1731
1746
|
if (file && !file.startsWith(root + "/") && !file.includes("\0") && fs4.existsSync(file)) {
|
|
1732
|
-
watcher.add(
|
|
1747
|
+
watcher.add(path7.resolve(file));
|
|
1733
1748
|
}
|
|
1734
1749
|
}
|
|
1735
1750
|
|
|
1736
1751
|
// src/utils/resolve.ts
|
|
1737
|
-
import
|
|
1752
|
+
import path8 from "path";
|
|
1738
1753
|
import { builtinModules } from "module";
|
|
1739
1754
|
async function resolveViaPackageJsonSvelte(importee, importer, cache) {
|
|
1740
1755
|
if (importer && isBareImport(importee) && !isNodeInternal(importee) && !isCommonDepWithoutSvelteField(importee)) {
|
|
@@ -1746,7 +1761,7 @@ async function resolveViaPackageJsonSvelte(importee, importer, cache) {
|
|
|
1746
1761
|
if (pkgData) {
|
|
1747
1762
|
const { pkg, dir } = pkgData;
|
|
1748
1763
|
if (pkg.svelte) {
|
|
1749
|
-
const result =
|
|
1764
|
+
const result = path8.resolve(dir, pkg.svelte);
|
|
1750
1765
|
cache.setResolvedSvelteField(importee, importer, result);
|
|
1751
1766
|
return result;
|
|
1752
1767
|
}
|
|
@@ -1757,7 +1772,7 @@ function isNodeInternal(importee) {
|
|
|
1757
1772
|
return importee.startsWith("node:") || builtinModules.includes(importee);
|
|
1758
1773
|
}
|
|
1759
1774
|
function isBareImport(importee) {
|
|
1760
|
-
if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") ||
|
|
1775
|
+
if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || path8.isAbsolute(importee)) {
|
|
1761
1776
|
return false;
|
|
1762
1777
|
}
|
|
1763
1778
|
const parts = importee.split("/");
|
|
@@ -1773,7 +1788,7 @@ function isBareImport(importee) {
|
|
|
1773
1788
|
|
|
1774
1789
|
// src/utils/optimizer.ts
|
|
1775
1790
|
import { promises as fs5 } from "fs";
|
|
1776
|
-
import
|
|
1791
|
+
import path9 from "path";
|
|
1777
1792
|
var PREBUNDLE_SENSITIVE_OPTIONS = [
|
|
1778
1793
|
"compilerOptions",
|
|
1779
1794
|
"configFile",
|
|
@@ -1784,7 +1799,7 @@ var PREBUNDLE_SENSITIVE_OPTIONS = [
|
|
|
1784
1799
|
];
|
|
1785
1800
|
async function saveSvelteMetadata(cacheDir, options) {
|
|
1786
1801
|
const svelteMetadata = generateSvelteMetadata(options);
|
|
1787
|
-
const svelteMetadataPath =
|
|
1802
|
+
const svelteMetadataPath = path9.resolve(cacheDir, "_svelte_metadata.json");
|
|
1788
1803
|
const currentSvelteMetadata = JSON.stringify(svelteMetadata, (_, value) => {
|
|
1789
1804
|
return typeof value === "function" ? value.toString() : value;
|
|
1790
1805
|
});
|
|
@@ -1807,7 +1822,7 @@ function generateSvelteMetadata(options) {
|
|
|
1807
1822
|
|
|
1808
1823
|
// src/ui/inspector/plugin.ts
|
|
1809
1824
|
import { normalizePath as normalizePath4 } from "vite";
|
|
1810
|
-
import
|
|
1825
|
+
import path10 from "path";
|
|
1811
1826
|
import { fileURLToPath } from "url";
|
|
1812
1827
|
import fs6 from "fs";
|
|
1813
1828
|
|
|
@@ -1834,7 +1849,7 @@ var defaultInspectorOptions = {
|
|
|
1834
1849
|
customStyles: true
|
|
1835
1850
|
};
|
|
1836
1851
|
function getInspectorPath() {
|
|
1837
|
-
const pluginPath = normalizePath4(
|
|
1852
|
+
const pluginPath = normalizePath4(path10.dirname(fileURLToPath(import.meta.url)));
|
|
1838
1853
|
return pluginPath.replace(/\/vite-plugin-svelte\/dist$/, "/vite-plugin-svelte/src/ui/inspector/");
|
|
1839
1854
|
}
|
|
1840
1855
|
function svelteInspector() {
|
|
@@ -1860,7 +1875,7 @@ function svelteInspector() {
|
|
|
1860
1875
|
disabled = true;
|
|
1861
1876
|
} else {
|
|
1862
1877
|
if (vps.api.options.kit && !inspectorOptions.appendTo) {
|
|
1863
|
-
const out_dir =
|
|
1878
|
+
const out_dir = path10.basename(vps.api.options.kit.outDir || ".svelte-kit");
|
|
1864
1879
|
inspectorOptions.appendTo = `${out_dir}/generated/root.svelte`;
|
|
1865
1880
|
}
|
|
1866
1881
|
appendTo = inspectorOptions.appendTo;
|
|
@@ -2010,8 +2025,8 @@ var VitePluginSvelteCache = class {
|
|
|
2010
2025
|
getError(svelteRequest) {
|
|
2011
2026
|
return this._errors.get(svelteRequest.normalizedFilename);
|
|
2012
2027
|
}
|
|
2013
|
-
getDependants(
|
|
2014
|
-
const dependants = this._dependants.get(
|
|
2028
|
+
getDependants(path11) {
|
|
2029
|
+
const dependants = this._dependants.get(path11);
|
|
2015
2030
|
return dependants ? [...dependants] : [];
|
|
2016
2031
|
}
|
|
2017
2032
|
getResolvedSvelteField(name, importer) {
|
|
@@ -2028,6 +2043,97 @@ var VitePluginSvelteCache = class {
|
|
|
2028
2043
|
}
|
|
2029
2044
|
};
|
|
2030
2045
|
|
|
2046
|
+
// src/utils/load-raw.ts
|
|
2047
|
+
import fs7 from "fs";
|
|
2048
|
+
async function loadRaw(svelteRequest, compileSvelte2, options) {
|
|
2049
|
+
const { id, filename, query } = svelteRequest;
|
|
2050
|
+
let compileData;
|
|
2051
|
+
const source = fs7.readFileSync(filename, "utf-8");
|
|
2052
|
+
try {
|
|
2053
|
+
svelteRequest.ssr = query.compilerOptions?.generate === "ssr";
|
|
2054
|
+
const type = query.type;
|
|
2055
|
+
compileData = await compileSvelte2(svelteRequest, source, {
|
|
2056
|
+
...options,
|
|
2057
|
+
compilerOptions: {
|
|
2058
|
+
dev: false,
|
|
2059
|
+
css: false,
|
|
2060
|
+
hydratable: false,
|
|
2061
|
+
enableSourcemap: query.sourcemap ? {
|
|
2062
|
+
js: type === "script" || type === "all",
|
|
2063
|
+
css: type === "style" || type === "all"
|
|
2064
|
+
} : false,
|
|
2065
|
+
...svelteRequest.query.compilerOptions
|
|
2066
|
+
},
|
|
2067
|
+
hot: false,
|
|
2068
|
+
emitCss: true
|
|
2069
|
+
});
|
|
2070
|
+
} catch (e) {
|
|
2071
|
+
throw toRollupError(e, options);
|
|
2072
|
+
}
|
|
2073
|
+
let result;
|
|
2074
|
+
if (query.type === "style") {
|
|
2075
|
+
result = compileData.compiled.css;
|
|
2076
|
+
} else if (query.type === "script") {
|
|
2077
|
+
result = compileData.compiled.js;
|
|
2078
|
+
} else if (query.type === "preprocessed") {
|
|
2079
|
+
result = compileData.preprocessed;
|
|
2080
|
+
} else if (query.type === "all" && query.raw) {
|
|
2081
|
+
return allToRawExports(compileData, source);
|
|
2082
|
+
} else {
|
|
2083
|
+
throw new Error(
|
|
2084
|
+
`invalid "type=${query.type}" in ${id}. supported are script, style, preprocessed, all`
|
|
2085
|
+
);
|
|
2086
|
+
}
|
|
2087
|
+
if (query.direct) {
|
|
2088
|
+
const supportedDirectTypes = ["script", "style"];
|
|
2089
|
+
if (!supportedDirectTypes.includes(query.type)) {
|
|
2090
|
+
throw new Error(
|
|
2091
|
+
`invalid "type=${query.type}" combined with direct in ${id}. supported are: ${supportedDirectTypes.join(", ")}`
|
|
2092
|
+
);
|
|
2093
|
+
}
|
|
2094
|
+
log.debug(`load returns direct result for ${id}`);
|
|
2095
|
+
let directOutput = result.code;
|
|
2096
|
+
if (query.sourcemap && result.map?.toUrl) {
|
|
2097
|
+
const map = `sourceMappingURL=${result.map.toUrl()}`;
|
|
2098
|
+
if (query.type === "style") {
|
|
2099
|
+
directOutput += `
|
|
2100
|
+
|
|
2101
|
+
/*# ${map} */
|
|
2102
|
+
`;
|
|
2103
|
+
} else if (query.type === "script") {
|
|
2104
|
+
directOutput += `
|
|
2105
|
+
|
|
2106
|
+
//# ${map}
|
|
2107
|
+
`;
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
return directOutput;
|
|
2111
|
+
} else if (query.raw) {
|
|
2112
|
+
log.debug(`load returns raw result for ${id}`);
|
|
2113
|
+
return toRawExports(result);
|
|
2114
|
+
} else {
|
|
2115
|
+
throw new Error(`invalid raw mode in ${id}, supported are raw, direct`);
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
function allToRawExports(compileData, source) {
|
|
2119
|
+
const exports = {
|
|
2120
|
+
...compileData,
|
|
2121
|
+
...compileData.compiled,
|
|
2122
|
+
source
|
|
2123
|
+
};
|
|
2124
|
+
delete exports.compiled;
|
|
2125
|
+
delete exports.filename;
|
|
2126
|
+
return toRawExports(exports);
|
|
2127
|
+
}
|
|
2128
|
+
function toRawExports(object) {
|
|
2129
|
+
let exports = Object.entries(object).filter(([key, value]) => typeof value !== "function").sort(([a], [b]) => a < b ? -1 : a === b ? 0 : 1).map(([key, value]) => `export const ${key}=${JSON.stringify(value)}`).join("\n") + "\n";
|
|
2130
|
+
if (Object.prototype.hasOwnProperty.call(object, "code")) {
|
|
2131
|
+
exports += `export default code
|
|
2132
|
+
`;
|
|
2133
|
+
}
|
|
2134
|
+
return exports;
|
|
2135
|
+
}
|
|
2136
|
+
|
|
2031
2137
|
// src/index.ts
|
|
2032
2138
|
function svelte(inlineOptions) {
|
|
2033
2139
|
if (process.env.DEBUG != null) {
|
|
@@ -2078,21 +2184,25 @@ function svelte(inlineOptions) {
|
|
|
2078
2184
|
options.server = server;
|
|
2079
2185
|
setupWatchers(options, cache, requestParser);
|
|
2080
2186
|
},
|
|
2081
|
-
load(id, opts) {
|
|
2187
|
+
async load(id, opts) {
|
|
2082
2188
|
const ssr = !!opts?.ssr;
|
|
2083
2189
|
const svelteRequest = requestParser(id, !!ssr);
|
|
2084
2190
|
if (svelteRequest) {
|
|
2085
|
-
const { filename, query } = svelteRequest;
|
|
2086
|
-
if (
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2191
|
+
const { filename, query, raw } = svelteRequest;
|
|
2192
|
+
if (raw) {
|
|
2193
|
+
return loadRaw(svelteRequest, compileSvelte2, options);
|
|
2194
|
+
} else {
|
|
2195
|
+
if (query.svelte && query.type === "style") {
|
|
2196
|
+
const css = cache.getCSS(svelteRequest);
|
|
2197
|
+
if (css) {
|
|
2198
|
+
log.debug(`load returns css for ${filename}`);
|
|
2199
|
+
return css;
|
|
2200
|
+
}
|
|
2201
|
+
}
|
|
2202
|
+
if (viteConfig.assetsInclude(filename)) {
|
|
2203
|
+
log.debug(`load returns raw content for ${filename}`);
|
|
2204
|
+
return fs8.readFileSync(filename, "utf-8");
|
|
2091
2205
|
}
|
|
2092
|
-
}
|
|
2093
|
-
if (viteConfig.assetsInclude(filename)) {
|
|
2094
|
-
log.debug(`load returns raw content for ${filename}`);
|
|
2095
|
-
return fs7.readFileSync(filename, "utf-8");
|
|
2096
2206
|
}
|
|
2097
2207
|
}
|
|
2098
2208
|
},
|
|
@@ -2100,12 +2210,10 @@ function svelte(inlineOptions) {
|
|
|
2100
2210
|
const ssr = !!opts?.ssr;
|
|
2101
2211
|
const svelteRequest = requestParser(importee, ssr);
|
|
2102
2212
|
if (svelteRequest?.query.svelte) {
|
|
2103
|
-
if (svelteRequest.query.type === "style") {
|
|
2213
|
+
if (svelteRequest.query.type === "style" && !svelteRequest.raw) {
|
|
2104
2214
|
log.debug(`resolveId resolved virtual css module ${svelteRequest.cssId}`);
|
|
2105
2215
|
return svelteRequest.cssId;
|
|
2106
2216
|
}
|
|
2107
|
-
log.debug(`resolveId resolved ${importee}`);
|
|
2108
|
-
return importee;
|
|
2109
2217
|
}
|
|
2110
2218
|
if (ssr && importee === "svelte") {
|
|
2111
2219
|
if (!resolvedSvelteSSR) {
|
|
@@ -2147,7 +2255,7 @@ function svelte(inlineOptions) {
|
|
|
2147
2255
|
async transform(code, id, opts) {
|
|
2148
2256
|
const ssr = !!opts?.ssr;
|
|
2149
2257
|
const svelteRequest = requestParser(id, ssr);
|
|
2150
|
-
if (!svelteRequest || svelteRequest.query.
|
|
2258
|
+
if (!svelteRequest || svelteRequest.query.type === "style" || svelteRequest.raw) {
|
|
2151
2259
|
return;
|
|
2152
2260
|
}
|
|
2153
2261
|
let compileData;
|
|
@@ -2197,6 +2305,7 @@ function svelte(inlineOptions) {
|
|
|
2197
2305
|
}
|
|
2198
2306
|
export {
|
|
2199
2307
|
loadSvelteConfig,
|
|
2200
|
-
svelte
|
|
2308
|
+
svelte,
|
|
2309
|
+
vitePreprocess
|
|
2201
2310
|
};
|
|
2202
2311
|
//# sourceMappingURL=index.js.map
|