@sveltejs/vite-plugin-svelte 1.0.0-next.28 → 1.0.0-next.31
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 +1 -0
- package/dist/index.cjs +163 -23
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +6 -0
- package/dist/index.js +161 -37
- package/dist/index.js.map +3 -3
- package/package.json +10 -10
- package/src/index.ts +7 -1
- package/src/utils/compile.ts +7 -0
- package/src/utils/error.ts +92 -0
- package/src/utils/esbuild.ts +99 -0
- package/src/utils/log.ts +1 -1
- package/src/utils/options.ts +22 -2
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -44,11 +44,11 @@ __export(exports, {
|
|
|
44
44
|
svelte: () => svelte
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
-
// ../../node_modules/.pnpm/tsup@5.
|
|
48
|
-
var importMetaUrlShim = typeof document === "undefined" ? new
|
|
47
|
+
// ../../node_modules/.pnpm/tsup@5.10.1_typescript@4.5.2/node_modules/tsup/assets/cjs_shims.js
|
|
48
|
+
var importMetaUrlShim = typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
|
|
49
49
|
|
|
50
50
|
// src/index.ts
|
|
51
|
-
var
|
|
51
|
+
var import_fs6 = __toModule(require("fs"));
|
|
52
52
|
|
|
53
53
|
// src/utils/log.ts
|
|
54
54
|
var import_colors = __toModule(require("kleur/colors"));
|
|
@@ -192,7 +192,7 @@ function buildExtendedLogMessage(w) {
|
|
|
192
192
|
}
|
|
193
193
|
|
|
194
194
|
// src/handle-hot-update.ts
|
|
195
|
-
async function handleHotUpdate(
|
|
195
|
+
async function handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options) {
|
|
196
196
|
const { read, server } = ctx;
|
|
197
197
|
const cachedJS = cache.getJS(svelteRequest);
|
|
198
198
|
if (!cachedJS) {
|
|
@@ -201,7 +201,7 @@ async function handleHotUpdate(compileSvelte, ctx, svelteRequest, cache, options
|
|
|
201
201
|
}
|
|
202
202
|
const cachedCss = cache.getCSS(svelteRequest);
|
|
203
203
|
const content = await read();
|
|
204
|
-
const compileData = await
|
|
204
|
+
const compileData = await compileSvelte2(svelteRequest, content, options);
|
|
205
205
|
cache.update(compileData);
|
|
206
206
|
const affectedModules = new Set();
|
|
207
207
|
const cssModule = server.moduleGraph.getModuleById(svelteRequest.cssId);
|
|
@@ -291,7 +291,7 @@ function toSafe(base64) {
|
|
|
291
291
|
}
|
|
292
292
|
|
|
293
293
|
// src/utils/compile.ts
|
|
294
|
-
var _createCompileSvelte = (makeHot) => async function
|
|
294
|
+
var _createCompileSvelte = (makeHot) => async function compileSvelte2(svelteRequest, code, options) {
|
|
295
295
|
var _a, _b;
|
|
296
296
|
const { filename, normalizedFilename, cssId, ssr } = svelteRequest;
|
|
297
297
|
const { emitCss = true } = options;
|
|
@@ -305,6 +305,13 @@ var _createCompileSvelte = (makeHot) => async function compileSvelte(svelteReque
|
|
|
305
305
|
log.debug(`setting cssHash ${hash} for ${normalizedFilename}`);
|
|
306
306
|
compileOptions.cssHash = () => hash;
|
|
307
307
|
}
|
|
308
|
+
if (ssr && compileOptions.enableSourcemap !== false) {
|
|
309
|
+
if (typeof compileOptions.enableSourcemap === "object") {
|
|
310
|
+
compileOptions.enableSourcemap.css = false;
|
|
311
|
+
} else {
|
|
312
|
+
compileOptions.enableSourcemap = { js: true, css: false };
|
|
313
|
+
}
|
|
314
|
+
}
|
|
308
315
|
let preprocessed;
|
|
309
316
|
if (options.preprocess) {
|
|
310
317
|
preprocessed = await (0, import_compiler.preprocess)(code, options.preprocess, { filename });
|
|
@@ -481,7 +488,7 @@ async function loadSvelteConfig(viteConfig, inlineOptions) {
|
|
|
481
488
|
}
|
|
482
489
|
if (!configFile.endsWith(".mjs")) {
|
|
483
490
|
try {
|
|
484
|
-
const _require = importMetaUrlShim ? esmRequire
|
|
491
|
+
const _require = importMetaUrlShim ? esmRequire ?? (esmRequire = (0, import_module.createRequire)(importMetaUrlShim)) : require;
|
|
485
492
|
delete _require.cache[_require.resolve(configFile)];
|
|
486
493
|
const result = _require(configFile);
|
|
487
494
|
if (result != null) {
|
|
@@ -694,6 +701,126 @@ function needsOptimization(dep, localRequire) {
|
|
|
694
701
|
|
|
695
702
|
// src/utils/options.ts
|
|
696
703
|
var import_module3 = __toModule(require("module"));
|
|
704
|
+
|
|
705
|
+
// src/utils/esbuild.ts
|
|
706
|
+
var import_fs3 = __toModule(require("fs"));
|
|
707
|
+
var import_compiler2 = __toModule(require("svelte/compiler"));
|
|
708
|
+
|
|
709
|
+
// src/utils/error.ts
|
|
710
|
+
function toRollupError(error) {
|
|
711
|
+
const { filename, frame, start, code, name } = error;
|
|
712
|
+
const rollupError = {
|
|
713
|
+
name,
|
|
714
|
+
id: filename,
|
|
715
|
+
message: buildExtendedLogMessage(error),
|
|
716
|
+
frame: formatFrameForVite(frame),
|
|
717
|
+
code,
|
|
718
|
+
stack: ""
|
|
719
|
+
};
|
|
720
|
+
if (start) {
|
|
721
|
+
rollupError.loc = {
|
|
722
|
+
line: start.line,
|
|
723
|
+
column: start.column,
|
|
724
|
+
file: filename
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
return rollupError;
|
|
728
|
+
}
|
|
729
|
+
function toESBuildError(error) {
|
|
730
|
+
const { filename, frame, start } = error;
|
|
731
|
+
const partialMessage = {
|
|
732
|
+
text: buildExtendedLogMessage(error)
|
|
733
|
+
};
|
|
734
|
+
if (start) {
|
|
735
|
+
partialMessage.location = {
|
|
736
|
+
line: start.line,
|
|
737
|
+
column: start.column,
|
|
738
|
+
file: filename,
|
|
739
|
+
lineText: lineFromFrame(start.line, frame)
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
return partialMessage;
|
|
743
|
+
}
|
|
744
|
+
function lineFromFrame(lineNo, frame) {
|
|
745
|
+
if (!frame) {
|
|
746
|
+
return "";
|
|
747
|
+
}
|
|
748
|
+
const lines = frame.split("\n");
|
|
749
|
+
const errorLine = lines.find((line) => line.trimStart().startsWith(`${lineNo}: `));
|
|
750
|
+
return errorLine ? errorLine.substring(errorLine.indexOf(": ") + 3) : "";
|
|
751
|
+
}
|
|
752
|
+
function formatFrameForVite(frame) {
|
|
753
|
+
if (!frame) {
|
|
754
|
+
return "";
|
|
755
|
+
}
|
|
756
|
+
return frame.split("\n").map((line) => line.match(/^\s+\^/) ? " " + line : " " + line.replace(":", " | ")).join("\n");
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
// src/utils/esbuild.ts
|
|
760
|
+
function esbuildSveltePlugin(options) {
|
|
761
|
+
return {
|
|
762
|
+
name: "vite-plugin-svelte:optimize-svelte",
|
|
763
|
+
setup(build) {
|
|
764
|
+
disableVitePrebundleSvelte(build);
|
|
765
|
+
const svelteExtensions = (options.extensions ?? [".svelte"]).map((ext) => ext.slice(1));
|
|
766
|
+
const svelteFilter = new RegExp(`\\.(` + svelteExtensions.join("|") + `)(\\?.*)?$`);
|
|
767
|
+
build.onLoad({ filter: svelteFilter }, async ({ path: filename }) => {
|
|
768
|
+
const code = await import_fs3.promises.readFile(filename, "utf8");
|
|
769
|
+
try {
|
|
770
|
+
const contents = await compileSvelte(options, { filename, code });
|
|
771
|
+
return { contents };
|
|
772
|
+
} catch (e) {
|
|
773
|
+
return { errors: [toESBuildError(e)] };
|
|
774
|
+
}
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
function disableVitePrebundleSvelte(build) {
|
|
780
|
+
var _a;
|
|
781
|
+
const viteDepPrebundlePlugin = (_a = build.initialOptions.plugins) == null ? void 0 : _a.find((v) => v.name === "vite:dep-pre-bundle");
|
|
782
|
+
if (!viteDepPrebundlePlugin)
|
|
783
|
+
return;
|
|
784
|
+
const _setup = viteDepPrebundlePlugin.setup.bind(viteDepPrebundlePlugin);
|
|
785
|
+
viteDepPrebundlePlugin.setup = function(build2) {
|
|
786
|
+
const _onResolve = build2.onResolve.bind(build2);
|
|
787
|
+
build2.onResolve = function(options, callback) {
|
|
788
|
+
if (options.filter.source.includes("svelte")) {
|
|
789
|
+
options.filter = new RegExp(options.filter.source.replace("|svelte", ""), options.filter.flags);
|
|
790
|
+
}
|
|
791
|
+
return _onResolve(options, callback);
|
|
792
|
+
};
|
|
793
|
+
return _setup(build2);
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
async function compileSvelte(options, { filename, code }) {
|
|
797
|
+
var _a, _b;
|
|
798
|
+
const compileOptions = __spreadProps(__spreadValues({}, options.compilerOptions), {
|
|
799
|
+
css: true,
|
|
800
|
+
filename,
|
|
801
|
+
generate: "dom"
|
|
802
|
+
});
|
|
803
|
+
let preprocessed;
|
|
804
|
+
if (options.preprocess) {
|
|
805
|
+
preprocessed = await (0, import_compiler2.preprocess)(code, options.preprocess, { filename });
|
|
806
|
+
if (preprocessed.map)
|
|
807
|
+
compileOptions.sourcemap = preprocessed.map;
|
|
808
|
+
}
|
|
809
|
+
const finalCode = preprocessed ? preprocessed.code : code;
|
|
810
|
+
const dynamicCompileOptions = await ((_b = (_a = options.experimental) == null ? void 0 : _a.dynamicCompileOptions) == null ? void 0 : _b.call(_a, {
|
|
811
|
+
filename,
|
|
812
|
+
code: finalCode,
|
|
813
|
+
compileOptions
|
|
814
|
+
}));
|
|
815
|
+
if (dynamicCompileOptions && log.debug.enabled) {
|
|
816
|
+
log.debug(`dynamic compile options for ${filename}: ${JSON.stringify(dynamicCompileOptions)}`);
|
|
817
|
+
}
|
|
818
|
+
const finalCompileOptions = dynamicCompileOptions ? __spreadValues(__spreadValues({}, compileOptions), dynamicCompileOptions) : compileOptions;
|
|
819
|
+
const compiled = (0, import_compiler2.compile)(finalCode, finalCompileOptions);
|
|
820
|
+
return compiled.js.code + "//# sourceMappingURL=" + compiled.js.map.toUrl();
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
// src/utils/options.ts
|
|
697
824
|
var knownOptions = new Set([
|
|
698
825
|
"configFile",
|
|
699
826
|
"include",
|
|
@@ -789,12 +916,11 @@ function mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfig, v
|
|
|
789
916
|
return merged;
|
|
790
917
|
}
|
|
791
918
|
async function resolveOptions(inlineOptions = {}, viteConfig, viteEnv) {
|
|
792
|
-
var _a;
|
|
793
919
|
const viteConfigWithResolvedRoot = __spreadProps(__spreadValues({}, viteConfig), {
|
|
794
920
|
root: resolveViteRoot(viteConfig)
|
|
795
921
|
});
|
|
796
922
|
const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions) || {};
|
|
797
|
-
const defaultOptions = buildDefaultOptions(viteEnv.mode === "production",
|
|
923
|
+
const defaultOptions = buildDefaultOptions(viteEnv.mode === "production", inlineOptions.emitCss ?? svelteConfig.emitCss);
|
|
798
924
|
const resolvedOptions = mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfigWithResolvedRoot, viteEnv);
|
|
799
925
|
enforceOptionsForProduction(resolvedOptions);
|
|
800
926
|
enforceOptionsForHmr(resolvedOptions);
|
|
@@ -818,7 +944,6 @@ function buildExtraViteConfig(options, config, configEnv) {
|
|
|
818
944
|
return extraViteConfig;
|
|
819
945
|
}
|
|
820
946
|
function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) {
|
|
821
|
-
svelteDeps = svelteDeps.filter((dep) => dep.type === "component-library");
|
|
822
947
|
const include = [];
|
|
823
948
|
const exclude = ["svelte-hmr"];
|
|
824
949
|
const isIncluded = (dep) => {
|
|
@@ -836,6 +961,16 @@ function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) {
|
|
|
836
961
|
} else {
|
|
837
962
|
log.debug('"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.');
|
|
838
963
|
}
|
|
964
|
+
if (options.experimental.prebundleSvelteLibraries) {
|
|
965
|
+
return {
|
|
966
|
+
include,
|
|
967
|
+
exclude,
|
|
968
|
+
esbuildOptions: {
|
|
969
|
+
plugins: [esbuildSveltePlugin(options)]
|
|
970
|
+
}
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
svelteDeps = svelteDeps.filter((dep) => dep.type === "component-library");
|
|
839
974
|
const svelteDepsToExclude = Array.from(new Set(svelteDeps.map((dep) => dep.name))).filter((dep) => !isIncluded(dep));
|
|
840
975
|
log.debug(`automatically excluding found svelte dependencies: ${svelteDepsToExclude.join(", ")}`);
|
|
841
976
|
exclude.push(...svelteDepsToExclude.filter((x) => !isExcluded(x)));
|
|
@@ -947,7 +1082,7 @@ var VitePluginSvelteCache = class {
|
|
|
947
1082
|
};
|
|
948
1083
|
|
|
949
1084
|
// src/utils/watch.ts
|
|
950
|
-
var
|
|
1085
|
+
var import_fs4 = __toModule(require("fs"));
|
|
951
1086
|
var import_path4 = __toModule(require("path"));
|
|
952
1087
|
function setupWatchers(options, cache, requestParser) {
|
|
953
1088
|
const { server, configFile: svelteConfigFile } = options;
|
|
@@ -959,7 +1094,7 @@ function setupWatchers(options, cache, requestParser) {
|
|
|
959
1094
|
const emitChangeEventOnDependants = (filename) => {
|
|
960
1095
|
const dependants = cache.getDependants(filename);
|
|
961
1096
|
dependants.forEach((dependant) => {
|
|
962
|
-
if (
|
|
1097
|
+
if (import_fs4.default.existsSync(dependant)) {
|
|
963
1098
|
log.debug(`emitting virtual change event for "${dependant}" because depdendency "${filename}" changed`);
|
|
964
1099
|
watcher.emit("change", dependant);
|
|
965
1100
|
}
|
|
@@ -1016,19 +1151,19 @@ function setupWatchers(options, cache, requestParser) {
|
|
|
1016
1151
|
});
|
|
1017
1152
|
}
|
|
1018
1153
|
function ensureWatchedFile(watcher, file, root) {
|
|
1019
|
-
if (file && !file.startsWith(root + "/") && !file.includes("\0") &&
|
|
1154
|
+
if (file && !file.startsWith(root + "/") && !file.includes("\0") && import_fs4.default.existsSync(file)) {
|
|
1020
1155
|
watcher.add(import_path4.default.resolve(file));
|
|
1021
1156
|
}
|
|
1022
1157
|
}
|
|
1023
1158
|
|
|
1024
1159
|
// src/utils/resolve.ts
|
|
1025
1160
|
var import_path5 = __toModule(require("path"));
|
|
1026
|
-
var
|
|
1161
|
+
var import_fs5 = __toModule(require("fs"));
|
|
1027
1162
|
var import_require_relative = __toModule(require("require-relative"));
|
|
1028
1163
|
function resolveViaPackageJsonSvelte(importee, importer) {
|
|
1029
1164
|
if (importer && isBareImport(importee)) {
|
|
1030
1165
|
const importeePkgFile = import_require_relative.default.resolve(`${importee}/package.json`, import_path5.default.dirname(importer));
|
|
1031
|
-
const importeePkg = JSON.parse(
|
|
1166
|
+
const importeePkg = JSON.parse(import_fs5.default.readFileSync(importeePkgFile, { encoding: "utf-8" }));
|
|
1032
1167
|
if (importeePkg.svelte) {
|
|
1033
1168
|
return import_path5.default.resolve(import_path5.default.dirname(importeePkgFile), importeePkg.svelte);
|
|
1034
1169
|
}
|
|
@@ -1052,14 +1187,14 @@ function isBareImport(importee) {
|
|
|
1052
1187
|
// src/utils/preprocess.ts
|
|
1053
1188
|
var import_vite3 = __toModule(require("vite"));
|
|
1054
1189
|
var import_magic_string2 = __toModule(require("magic-string"));
|
|
1055
|
-
var
|
|
1190
|
+
var import_compiler3 = __toModule(require("svelte/compiler"));
|
|
1056
1191
|
|
|
1057
1192
|
// src/utils/sourcemap.ts
|
|
1058
1193
|
var import_magic_string = __toModule(require("magic-string"));
|
|
1059
1194
|
async function buildMagicString(from, to, options) {
|
|
1060
1195
|
let diff_match_patch, DIFF_DELETE, DIFF_INSERT;
|
|
1061
1196
|
try {
|
|
1062
|
-
const dmpPkg = await
|
|
1197
|
+
const dmpPkg = await import("diff-match-patch");
|
|
1063
1198
|
diff_match_patch = dmpPkg.diff_match_patch;
|
|
1064
1199
|
DIFF_INSERT = dmpPkg.DIFF_INSERT;
|
|
1065
1200
|
DIFF_DELETE = dmpPkg.DIFF_DELETE;
|
|
@@ -1152,7 +1287,7 @@ function createViteStylePreprocessor(config) {
|
|
|
1152
1287
|
function createVitePreprocessorGroup(config) {
|
|
1153
1288
|
return {
|
|
1154
1289
|
markup({ content, filename }) {
|
|
1155
|
-
return (0,
|
|
1290
|
+
return (0, import_compiler3.preprocess)(content, {
|
|
1156
1291
|
script: createViteScriptPreprocessor(),
|
|
1157
1292
|
style: createViteStylePreprocessor(config)
|
|
1158
1293
|
}, { filename });
|
|
@@ -1287,7 +1422,7 @@ function svelte(inlineOptions) {
|
|
|
1287
1422
|
let requestParser;
|
|
1288
1423
|
let options;
|
|
1289
1424
|
let viteConfig;
|
|
1290
|
-
let
|
|
1425
|
+
let compileSvelte2;
|
|
1291
1426
|
let resolvedSvelteSSR;
|
|
1292
1427
|
return {
|
|
1293
1428
|
name: "vite-plugin-svelte",
|
|
@@ -1306,7 +1441,7 @@ function svelte(inlineOptions) {
|
|
|
1306
1441
|
async configResolved(config) {
|
|
1307
1442
|
addExtraPreprocessors(options, config);
|
|
1308
1443
|
requestParser = buildIdParser(options);
|
|
1309
|
-
|
|
1444
|
+
compileSvelte2 = createCompileSvelte(options);
|
|
1310
1445
|
viteConfig = config;
|
|
1311
1446
|
log.debug("resolved options", options);
|
|
1312
1447
|
},
|
|
@@ -1328,7 +1463,7 @@ function svelte(inlineOptions) {
|
|
|
1328
1463
|
}
|
|
1329
1464
|
if (viteConfig.assetsInclude(filename)) {
|
|
1330
1465
|
log.debug(`load returns raw content for ${filename}`);
|
|
1331
|
-
return
|
|
1466
|
+
return import_fs6.default.readFileSync(filename, "utf-8");
|
|
1332
1467
|
}
|
|
1333
1468
|
}
|
|
1334
1469
|
},
|
|
@@ -1392,7 +1527,12 @@ function svelte(inlineOptions) {
|
|
|
1392
1527
|
log.error("failed to transform tagged svelte request", svelteRequest);
|
|
1393
1528
|
throw new Error(`failed to transform tagged svelte request for id ${id}`);
|
|
1394
1529
|
}
|
|
1395
|
-
|
|
1530
|
+
let compileData;
|
|
1531
|
+
try {
|
|
1532
|
+
compileData = await compileSvelte2(svelteRequest, code, options);
|
|
1533
|
+
} catch (e) {
|
|
1534
|
+
throw toRollupError(e);
|
|
1535
|
+
}
|
|
1396
1536
|
logCompilerWarnings(compileData.compiled.warnings, options);
|
|
1397
1537
|
cache.update(compileData);
|
|
1398
1538
|
if (((_a = compileData.dependencies) == null ? void 0 : _a.length) && options.server) {
|
|
@@ -1409,7 +1549,7 @@ function svelte(inlineOptions) {
|
|
|
1409
1549
|
}
|
|
1410
1550
|
const svelteRequest = requestParser(ctx.file, false, ctx.timestamp);
|
|
1411
1551
|
if (svelteRequest) {
|
|
1412
|
-
return handleHotUpdate(
|
|
1552
|
+
return handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options);
|
|
1413
1553
|
}
|
|
1414
1554
|
},
|
|
1415
1555
|
buildEnd() {
|