@sveltejs/vite-plugin-svelte 1.0.0-next.33 → 1.0.0-next.37
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.cjs +174 -92
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +137 -65
- package/dist/index.js.map +1 -1
- package/package.json +6 -7
- package/src/index.ts +13 -15
- package/src/utils/compile.ts +7 -1
- package/src/utils/dependencies.ts +5 -2
- package/src/utils/error.ts +9 -6
- package/src/utils/esbuild.ts +7 -2
- package/src/utils/log.ts +4 -1
- package/src/utils/optimizer.ts +43 -0
- package/src/utils/options.ts +4 -1
- package/src/utils/preprocess.ts +2 -1
- package/src/utils/resolve.ts +33 -9
- package/src/utils/vite-plugin-svelte-cache.ts +20 -0
- package/src/utils/watch.ts +1 -1
package/dist/index.cjs
CHANGED
|
@@ -23,37 +23,42 @@ var __spreadValues = (a, b) => {
|
|
|
23
23
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
24
|
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
25
25
|
var __export = (target, all) => {
|
|
26
|
-
__markAsModule(target);
|
|
27
26
|
for (var name in all)
|
|
28
27
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
29
28
|
};
|
|
30
|
-
var __reExport = (target, module2, desc) => {
|
|
29
|
+
var __reExport = (target, module2, copyDefault, desc) => {
|
|
31
30
|
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
|
32
31
|
for (let key of __getOwnPropNames(module2))
|
|
33
|
-
if (!__hasOwnProp.call(target, key) && key !== "default")
|
|
32
|
+
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
|
|
34
33
|
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
|
|
35
34
|
}
|
|
36
35
|
return target;
|
|
37
36
|
};
|
|
38
|
-
var
|
|
39
|
-
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default",
|
|
37
|
+
var __toESM = (module2, isNodeMode) => {
|
|
38
|
+
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", !isNodeMode && module2 && module2.__esModule ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
|
|
40
39
|
};
|
|
40
|
+
var __toCommonJS = /* @__PURE__ */ ((cache) => {
|
|
41
|
+
return (module2, temp) => {
|
|
42
|
+
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
|
|
43
|
+
};
|
|
44
|
+
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
|
|
41
45
|
|
|
42
46
|
// src/index.ts
|
|
43
|
-
|
|
47
|
+
var src_exports = {};
|
|
48
|
+
__export(src_exports, {
|
|
44
49
|
svelte: () => svelte
|
|
45
50
|
});
|
|
46
51
|
|
|
47
|
-
// ../../node_modules/.pnpm/tsup@5.11.
|
|
52
|
+
// ../../node_modules/.pnpm/tsup@5.11.13_typescript@4.5.5/node_modules/tsup/assets/cjs_shims.js
|
|
48
53
|
var getImportMetaUrl = () => typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
|
|
49
54
|
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
50
55
|
|
|
51
56
|
// src/index.ts
|
|
52
|
-
var import_fs6 =
|
|
57
|
+
var import_fs6 = __toESM(require("fs"), 1);
|
|
53
58
|
|
|
54
59
|
// src/utils/log.ts
|
|
55
|
-
var import_colors =
|
|
56
|
-
var import_debug =
|
|
60
|
+
var import_colors = require("kleur/colors");
|
|
61
|
+
var import_debug = __toESM(require("debug"), 1);
|
|
57
62
|
var levels = ["debug", "info", "warn", "error", "silent"];
|
|
58
63
|
var prefix = "vite-plugin-svelte";
|
|
59
64
|
var loggers = {
|
|
@@ -112,7 +117,7 @@ function _log(logger, message, payload) {
|
|
|
112
117
|
function createLogger(level) {
|
|
113
118
|
const logger = loggers[level];
|
|
114
119
|
const logFn = _log.bind(null, logger);
|
|
115
|
-
const logged = new Set();
|
|
120
|
+
const logged = /* @__PURE__ */ new Set();
|
|
116
121
|
const once = function(message, payload) {
|
|
117
122
|
if (logged.has(message)) {
|
|
118
123
|
return;
|
|
@@ -187,7 +192,10 @@ function buildExtendedLogMessage(w) {
|
|
|
187
192
|
parts.push(":", w.start.line, ":", w.start.column);
|
|
188
193
|
}
|
|
189
194
|
if (w.message) {
|
|
190
|
-
parts.
|
|
195
|
+
if (parts.length > 0) {
|
|
196
|
+
parts.push(" ");
|
|
197
|
+
}
|
|
198
|
+
parts.push(w.message);
|
|
191
199
|
}
|
|
192
200
|
return parts.join("");
|
|
193
201
|
}
|
|
@@ -204,7 +212,7 @@ async function handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, option
|
|
|
204
212
|
const content = await read();
|
|
205
213
|
const compileData = await compileSvelte2(svelteRequest, content, options);
|
|
206
214
|
cache.update(compileData);
|
|
207
|
-
const affectedModules = new Set();
|
|
215
|
+
const affectedModules = /* @__PURE__ */ new Set();
|
|
208
216
|
const cssModule = server.moduleGraph.getModuleById(svelteRequest.cssId);
|
|
209
217
|
const mainModule = server.moduleGraph.getModuleById(svelteRequest.id);
|
|
210
218
|
const cssUpdated = cssModule && cssChanged(cachedCss, compileData.compiled.css);
|
|
@@ -264,12 +272,12 @@ function normalizeJsCode(code) {
|
|
|
264
272
|
}
|
|
265
273
|
|
|
266
274
|
// src/utils/compile.ts
|
|
267
|
-
var import_compiler =
|
|
268
|
-
var import_svelte_hmr =
|
|
275
|
+
var import_compiler = require("svelte/compiler");
|
|
276
|
+
var import_svelte_hmr = require("svelte-hmr");
|
|
269
277
|
|
|
270
278
|
// src/utils/hash.ts
|
|
271
|
-
var crypto =
|
|
272
|
-
var hashes = Object.create(null);
|
|
279
|
+
var crypto = __toESM(require("crypto"), 1);
|
|
280
|
+
var hashes = /* @__PURE__ */ Object.create(null);
|
|
273
281
|
var hash_length = 12;
|
|
274
282
|
function safeBase64Hash(input) {
|
|
275
283
|
if (hashes[input]) {
|
|
@@ -315,7 +323,12 @@ var _createCompileSvelte = (makeHot) => async function compileSvelte2(svelteRequ
|
|
|
315
323
|
}
|
|
316
324
|
let preprocessed;
|
|
317
325
|
if (options.preprocess) {
|
|
318
|
-
|
|
326
|
+
try {
|
|
327
|
+
preprocessed = await (0, import_compiler.preprocess)(code, options.preprocess, { filename });
|
|
328
|
+
} catch (e) {
|
|
329
|
+
e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ""}`;
|
|
330
|
+
throw e;
|
|
331
|
+
}
|
|
319
332
|
if (preprocessed.dependencies)
|
|
320
333
|
dependencies.push(...preprocessed.dependencies);
|
|
321
334
|
if (preprocessed.map)
|
|
@@ -376,9 +389,9 @@ function createCompileSvelte(options) {
|
|
|
376
389
|
}
|
|
377
390
|
|
|
378
391
|
// src/utils/id.ts
|
|
379
|
-
var import_pluginutils =
|
|
380
|
-
var import_vite =
|
|
381
|
-
var fs =
|
|
392
|
+
var import_pluginutils = require("@rollup/pluginutils");
|
|
393
|
+
var import_vite = require("vite");
|
|
394
|
+
var fs = __toESM(require("fs"), 1);
|
|
382
395
|
var VITE_FS_PREFIX = "/@fs/";
|
|
383
396
|
var IS_WINDOWS = process.platform === "win32";
|
|
384
397
|
function splitId(id) {
|
|
@@ -454,13 +467,13 @@ function buildIdParser(options) {
|
|
|
454
467
|
}
|
|
455
468
|
|
|
456
469
|
// src/utils/options.ts
|
|
457
|
-
var import_vite3 =
|
|
470
|
+
var import_vite3 = require("vite");
|
|
458
471
|
|
|
459
472
|
// src/utils/load-svelte-config.ts
|
|
460
|
-
var import_module =
|
|
461
|
-
var import_path =
|
|
462
|
-
var import_fs =
|
|
463
|
-
var import_url =
|
|
473
|
+
var import_module = require("module");
|
|
474
|
+
var import_path = __toESM(require("path"), 1);
|
|
475
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
476
|
+
var import_url = require("url");
|
|
464
477
|
var esmRequire;
|
|
465
478
|
var knownSvelteConfigNames = [
|
|
466
479
|
"svelte.config.js",
|
|
@@ -549,12 +562,12 @@ var SVELTE_HMR_IMPORTS = [
|
|
|
549
562
|
];
|
|
550
563
|
|
|
551
564
|
// src/utils/options.ts
|
|
552
|
-
var import_path3 =
|
|
565
|
+
var import_path3 = __toESM(require("path"), 1);
|
|
553
566
|
|
|
554
567
|
// src/utils/dependencies.ts
|
|
555
|
-
var import_path2 =
|
|
556
|
-
var import_fs2 =
|
|
557
|
-
var import_module2 =
|
|
568
|
+
var import_path2 = __toESM(require("path"), 1);
|
|
569
|
+
var import_fs2 = __toESM(require("fs"), 1);
|
|
570
|
+
var import_module2 = require("module");
|
|
558
571
|
function findRootSvelteDependencies(root, cwdFallback = true) {
|
|
559
572
|
log.debug(`findSvelteDependencies: searching svelte dependencies in ${root}`);
|
|
560
573
|
const pkgFile = import_path2.default.join(root, "package.json");
|
|
@@ -579,7 +592,7 @@ function findRootSvelteDependencies(root, cwdFallback = true) {
|
|
|
579
592
|
].filter((dep) => !is_common_without_svelte_field(dep));
|
|
580
593
|
return getSvelteDependencies(deps, root);
|
|
581
594
|
}
|
|
582
|
-
function getSvelteDependencies(deps, pkgDir,
|
|
595
|
+
function getSvelteDependencies(deps, pkgDir, path7 = []) {
|
|
583
596
|
const result = [];
|
|
584
597
|
const localRequire = (0, import_module2.createRequire)(`${pkgDir}/package.json`);
|
|
585
598
|
const resolvedDeps = deps.map((dep) => resolveDependencyData(dep, localRequire)).filter(Boolean);
|
|
@@ -587,18 +600,18 @@ function getSvelteDependencies(deps, pkgDir, path6 = []) {
|
|
|
587
600
|
const type = getSvelteDependencyType(pkg);
|
|
588
601
|
if (!type)
|
|
589
602
|
continue;
|
|
590
|
-
result.push({ name: pkg.name, type, pkg, dir, path:
|
|
603
|
+
result.push({ name: pkg.name, type, pkg, dir, path: path7 });
|
|
591
604
|
if (type === "component-library" && pkg.dependencies) {
|
|
592
605
|
let dependencyNames = Object.keys(pkg.dependencies);
|
|
593
|
-
const circular = dependencyNames.filter((name) =>
|
|
606
|
+
const circular = dependencyNames.filter((name) => path7.includes(name));
|
|
594
607
|
if (circular.length > 0) {
|
|
595
|
-
log.warn.enabled && log.warn(`skipping circular svelte dependencies in automated vite optimizeDeps handling`, circular.map((x) =>
|
|
596
|
-
dependencyNames = dependencyNames.filter((name) => !
|
|
608
|
+
log.warn.enabled && log.warn(`skipping circular svelte dependencies in automated vite optimizeDeps handling`, circular.map((x) => path7.concat(x).join(">")));
|
|
609
|
+
dependencyNames = dependencyNames.filter((name) => !path7.includes(name));
|
|
597
610
|
}
|
|
598
|
-
if (
|
|
599
|
-
log.debug.once(`encountered deep svelte dependency tree: ${
|
|
611
|
+
if (path7.length === 3) {
|
|
612
|
+
log.debug.once(`encountered deep svelte dependency tree: ${path7.join(">")}`);
|
|
600
613
|
}
|
|
601
|
-
result.push(...getSvelteDependencies(dependencyNames, dir,
|
|
614
|
+
result.push(...getSvelteDependencies(dependencyNames, dir, path7.concat(pkg.name)));
|
|
602
615
|
}
|
|
603
616
|
}
|
|
604
617
|
return result;
|
|
@@ -705,22 +718,22 @@ function needsOptimization(dep, localRequire) {
|
|
|
705
718
|
}
|
|
706
719
|
|
|
707
720
|
// src/utils/options.ts
|
|
708
|
-
var import_module3 =
|
|
721
|
+
var import_module3 = require("module");
|
|
709
722
|
|
|
710
723
|
// src/utils/esbuild.ts
|
|
711
|
-
var import_fs3 =
|
|
712
|
-
var import_compiler2 =
|
|
724
|
+
var import_fs3 = require("fs");
|
|
725
|
+
var import_compiler2 = require("svelte/compiler");
|
|
713
726
|
|
|
714
727
|
// src/utils/error.ts
|
|
715
|
-
function toRollupError(error) {
|
|
716
|
-
const { filename, frame, start, code, name } = error;
|
|
728
|
+
function toRollupError(error, options) {
|
|
729
|
+
const { filename, frame, start, code, name, stack } = error;
|
|
717
730
|
const rollupError = {
|
|
718
731
|
name,
|
|
719
732
|
id: filename,
|
|
720
733
|
message: buildExtendedLogMessage(error),
|
|
721
734
|
frame: formatFrameForVite(frame),
|
|
722
735
|
code,
|
|
723
|
-
stack: ""
|
|
736
|
+
stack: options.isBuild || options.isDebug || !frame ? stack : ""
|
|
724
737
|
};
|
|
725
738
|
if (start) {
|
|
726
739
|
rollupError.loc = {
|
|
@@ -731,8 +744,8 @@ function toRollupError(error) {
|
|
|
731
744
|
}
|
|
732
745
|
return rollupError;
|
|
733
746
|
}
|
|
734
|
-
function toESBuildError(error) {
|
|
735
|
-
const { filename, frame, start } = error;
|
|
747
|
+
function toESBuildError(error, options) {
|
|
748
|
+
const { filename, frame, start, stack } = error;
|
|
736
749
|
const partialMessage = {
|
|
737
750
|
text: buildExtendedLogMessage(error)
|
|
738
751
|
};
|
|
@@ -744,6 +757,9 @@ function toESBuildError(error) {
|
|
|
744
757
|
lineText: lineFromFrame(start.line, frame)
|
|
745
758
|
};
|
|
746
759
|
}
|
|
760
|
+
if (options.isBuild || options.isDebug || !frame) {
|
|
761
|
+
partialMessage.detail = stack;
|
|
762
|
+
}
|
|
747
763
|
return partialMessage;
|
|
748
764
|
}
|
|
749
765
|
function lineFromFrame(lineNo, frame) {
|
|
@@ -776,7 +792,7 @@ function esbuildSveltePlugin(options) {
|
|
|
776
792
|
const contents = await compileSvelte(options, { filename, code });
|
|
777
793
|
return { contents };
|
|
778
794
|
} catch (e) {
|
|
779
|
-
return { errors: [toESBuildError(e)] };
|
|
795
|
+
return { errors: [toESBuildError(e, options)] };
|
|
780
796
|
}
|
|
781
797
|
});
|
|
782
798
|
}
|
|
@@ -809,7 +825,12 @@ async function compileSvelte(options, { filename, code }) {
|
|
|
809
825
|
});
|
|
810
826
|
let preprocessed;
|
|
811
827
|
if (options.preprocess) {
|
|
812
|
-
|
|
828
|
+
try {
|
|
829
|
+
preprocessed = await (0, import_compiler2.preprocess)(code, options.preprocess, { filename });
|
|
830
|
+
} catch (e) {
|
|
831
|
+
e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ""}`;
|
|
832
|
+
throw e;
|
|
833
|
+
}
|
|
813
834
|
if (preprocessed.map)
|
|
814
835
|
compileOptions.sourcemap = preprocessed.map;
|
|
815
836
|
}
|
|
@@ -828,12 +849,12 @@ async function compileSvelte(options, { filename, code }) {
|
|
|
828
849
|
}
|
|
829
850
|
|
|
830
851
|
// src/utils/preprocess.ts
|
|
831
|
-
var import_vite2 =
|
|
832
|
-
var import_magic_string2 =
|
|
833
|
-
var import_compiler3 =
|
|
852
|
+
var import_vite2 = require("vite");
|
|
853
|
+
var import_magic_string2 = __toESM(require("magic-string"), 1);
|
|
854
|
+
var import_compiler3 = require("svelte/compiler");
|
|
834
855
|
|
|
835
856
|
// src/utils/sourcemap.ts
|
|
836
|
-
var import_magic_string =
|
|
857
|
+
var import_magic_string = __toESM(require("magic-string"), 1);
|
|
837
858
|
async function buildMagicString(from, to, options) {
|
|
838
859
|
let diff_match_patch, DIFF_DELETE, DIFF_INSERT;
|
|
839
860
|
try {
|
|
@@ -890,7 +911,8 @@ function createViteScriptPreprocessor() {
|
|
|
890
911
|
loader: lang,
|
|
891
912
|
tsconfigRaw: {
|
|
892
913
|
compilerOptions: {
|
|
893
|
-
importsNotUsedAsValues: "preserve"
|
|
914
|
+
importsNotUsedAsValues: "preserve",
|
|
915
|
+
preserveValueImports: true
|
|
894
916
|
}
|
|
895
917
|
}
|
|
896
918
|
});
|
|
@@ -1054,7 +1076,7 @@ function validateSourceMapOutputWrapper(group, i) {
|
|
|
1054
1076
|
}
|
|
1055
1077
|
|
|
1056
1078
|
// src/utils/options.ts
|
|
1057
|
-
var knownOptions = new Set([
|
|
1079
|
+
var knownOptions = /* @__PURE__ */ new Set([
|
|
1058
1080
|
"configFile",
|
|
1059
1081
|
"include",
|
|
1060
1082
|
"exclude",
|
|
@@ -1091,7 +1113,8 @@ async function preResolveOptions(inlineOptions = {}, viteUserConfig, viteEnv) {
|
|
|
1091
1113
|
experimental: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.experimental), svelteConfig == null ? void 0 : svelteConfig.experimental), inlineOptions == null ? void 0 : inlineOptions.experimental),
|
|
1092
1114
|
root: viteConfigWithResolvedRoot.root,
|
|
1093
1115
|
isBuild: viteEnv.command === "build",
|
|
1094
|
-
isServe: viteEnv.command === "serve"
|
|
1116
|
+
isServe: viteEnv.command === "serve",
|
|
1117
|
+
isDebug: process.env.DEBUG != null
|
|
1095
1118
|
});
|
|
1096
1119
|
if (svelteConfig == null ? void 0 : svelteConfig.configFile) {
|
|
1097
1120
|
merged.configFile = svelteConfig.configFile;
|
|
@@ -1108,6 +1131,7 @@ function resolveOptions(preResolveOptions2, viteConfig) {
|
|
|
1108
1131
|
};
|
|
1109
1132
|
const merged = __spreadProps(__spreadValues(__spreadValues({}, defaultOptions), preResolveOptions2), {
|
|
1110
1133
|
compilerOptions: __spreadValues(__spreadValues({}, defaultOptions.compilerOptions), preResolveOptions2.compilerOptions),
|
|
1134
|
+
root: viteConfig.root,
|
|
1111
1135
|
isProduction: viteConfig.isProduction
|
|
1112
1136
|
});
|
|
1113
1137
|
addExtraPreprocessors(merged, viteConfig);
|
|
@@ -1175,16 +1199,16 @@ function buildExtraViteConfig(options, config, configEnv) {
|
|
|
1175
1199
|
extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config);
|
|
1176
1200
|
return extraViteConfig;
|
|
1177
1201
|
}
|
|
1178
|
-
function buildOptimizeDepsForSvelte(svelteDeps, options,
|
|
1202
|
+
function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps2) {
|
|
1179
1203
|
const include = [];
|
|
1180
1204
|
const exclude = ["svelte-hmr"];
|
|
1181
1205
|
const isIncluded = (dep) => {
|
|
1182
1206
|
var _a;
|
|
1183
|
-
return include.includes(dep) || ((_a =
|
|
1207
|
+
return include.includes(dep) || ((_a = optimizeDeps2 == null ? void 0 : optimizeDeps2.include) == null ? void 0 : _a.includes(dep));
|
|
1184
1208
|
};
|
|
1185
1209
|
const isExcluded = (dep) => {
|
|
1186
1210
|
var _a;
|
|
1187
|
-
return exclude.includes(dep) || ((_a =
|
|
1211
|
+
return exclude.includes(dep) || ((_a = optimizeDeps2 == null ? void 0 : optimizeDeps2.exclude) == null ? void 0 : _a.some((id) => dep === id || id.startsWith(`${dep}/`)));
|
|
1188
1212
|
};
|
|
1189
1213
|
if (!isExcluded("svelte")) {
|
|
1190
1214
|
const svelteImportsToInclude = SVELTE_IMPORTS.filter((x) => x !== "svelte/ssr");
|
|
@@ -1250,10 +1274,11 @@ function patchResolvedViteConfig(viteConfig, options) {
|
|
|
1250
1274
|
// src/utils/vite-plugin-svelte-cache.ts
|
|
1251
1275
|
var VitePluginSvelteCache = class {
|
|
1252
1276
|
constructor() {
|
|
1253
|
-
this._css = new Map();
|
|
1254
|
-
this._js = new Map();
|
|
1255
|
-
this._dependencies = new Map();
|
|
1256
|
-
this._dependants = new Map();
|
|
1277
|
+
this._css = /* @__PURE__ */ new Map();
|
|
1278
|
+
this._js = /* @__PURE__ */ new Map();
|
|
1279
|
+
this._dependencies = /* @__PURE__ */ new Map();
|
|
1280
|
+
this._dependants = /* @__PURE__ */ new Map();
|
|
1281
|
+
this._resolvedSvelteFields = /* @__PURE__ */ new Map();
|
|
1257
1282
|
}
|
|
1258
1283
|
update(compileData) {
|
|
1259
1284
|
this.updateCSS(compileData);
|
|
@@ -1277,7 +1302,7 @@ var VitePluginSvelteCache = class {
|
|
|
1277
1302
|
const added = dependencies.filter((d) => !prevDependencies.includes(d));
|
|
1278
1303
|
added.forEach((d) => {
|
|
1279
1304
|
if (!this._dependants.has(d)) {
|
|
1280
|
-
this._dependants.set(d, new Set());
|
|
1305
|
+
this._dependants.set(d, /* @__PURE__ */ new Set());
|
|
1281
1306
|
}
|
|
1282
1307
|
this._dependants.get(d).add(compileData.filename);
|
|
1283
1308
|
});
|
|
@@ -1315,15 +1340,24 @@ var VitePluginSvelteCache = class {
|
|
|
1315
1340
|
return this._js.get(svelteRequest.normalizedFilename);
|
|
1316
1341
|
}
|
|
1317
1342
|
}
|
|
1318
|
-
getDependants(
|
|
1319
|
-
const dependants = this._dependants.get(
|
|
1343
|
+
getDependants(path7) {
|
|
1344
|
+
const dependants = this._dependants.get(path7);
|
|
1320
1345
|
return dependants ? [...dependants] : [];
|
|
1321
1346
|
}
|
|
1347
|
+
getResolvedSvelteField(name, importer) {
|
|
1348
|
+
return this._resolvedSvelteFields.get(this._getResolvedSvelteFieldKey(name, importer));
|
|
1349
|
+
}
|
|
1350
|
+
setResolvedSvelteField(importee, importer = void 0, resolvedSvelte) {
|
|
1351
|
+
this._resolvedSvelteFields.set(this._getResolvedSvelteFieldKey(importee, importer), resolvedSvelte);
|
|
1352
|
+
}
|
|
1353
|
+
_getResolvedSvelteFieldKey(importee, importer) {
|
|
1354
|
+
return importer ? `${importer} > ${importee}` : importee;
|
|
1355
|
+
}
|
|
1322
1356
|
};
|
|
1323
1357
|
|
|
1324
1358
|
// src/utils/watch.ts
|
|
1325
|
-
var import_fs4 =
|
|
1326
|
-
var import_path4 =
|
|
1359
|
+
var import_fs4 = __toESM(require("fs"), 1);
|
|
1360
|
+
var import_path4 = __toESM(require("path"), 1);
|
|
1327
1361
|
function setupWatchers(options, cache, requestParser) {
|
|
1328
1362
|
const { server, configFile: svelteConfigFile } = options;
|
|
1329
1363
|
if (!server) {
|
|
@@ -1350,7 +1384,6 @@ function setupWatchers(options, cache, requestParser) {
|
|
|
1350
1384
|
}
|
|
1351
1385
|
};
|
|
1352
1386
|
const triggerViteRestart = (filename) => {
|
|
1353
|
-
var _a;
|
|
1354
1387
|
if (serverConfig.middlewareMode) {
|
|
1355
1388
|
const message = "Svelte config change detected, restart your dev process to apply the changes.";
|
|
1356
1389
|
log.info(message, filename);
|
|
@@ -1360,7 +1393,7 @@ function setupWatchers(options, cache, requestParser) {
|
|
|
1360
1393
|
});
|
|
1361
1394
|
} else {
|
|
1362
1395
|
log.info(`svelte config changed: restarting vite server. - file: ${filename}`);
|
|
1363
|
-
server.restart(
|
|
1396
|
+
server.restart();
|
|
1364
1397
|
}
|
|
1365
1398
|
};
|
|
1366
1399
|
const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => import_path4.default.join(root, cfg));
|
|
@@ -1398,18 +1431,31 @@ function ensureWatchedFile(watcher, file, root) {
|
|
|
1398
1431
|
}
|
|
1399
1432
|
|
|
1400
1433
|
// src/utils/resolve.ts
|
|
1401
|
-
var import_path5 =
|
|
1402
|
-
var
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1434
|
+
var import_path5 = __toESM(require("path"), 1);
|
|
1435
|
+
var import_module4 = require("module");
|
|
1436
|
+
function resolveViaPackageJsonSvelte(importee, importer, cache) {
|
|
1437
|
+
if (importer && isBareImport(importee) && !isNodeInternal(importee) && !is_common_without_svelte_field(importee)) {
|
|
1438
|
+
const cached = cache.getResolvedSvelteField(importee, importer);
|
|
1439
|
+
if (cached) {
|
|
1440
|
+
return cached;
|
|
1441
|
+
}
|
|
1442
|
+
const localRequire = (0, import_module4.createRequire)(importer);
|
|
1443
|
+
const pkgData = resolveDependencyData(importee, localRequire);
|
|
1444
|
+
if (pkgData) {
|
|
1445
|
+
const { pkg, dir } = pkgData;
|
|
1446
|
+
if (pkg.svelte) {
|
|
1447
|
+
const result = import_path5.default.resolve(dir, pkg.svelte);
|
|
1448
|
+
cache.setResolvedSvelteField(importee, importer, result);
|
|
1449
|
+
return result;
|
|
1450
|
+
}
|
|
1451
|
+
} else {
|
|
1452
|
+
throw new Error(`failed to resolve package.json of ${importee} imported by ${importer}`);
|
|
1410
1453
|
}
|
|
1411
1454
|
}
|
|
1412
1455
|
}
|
|
1456
|
+
function isNodeInternal(importee) {
|
|
1457
|
+
return importee.startsWith("node:") || import_module4.builtinModules.includes(importee);
|
|
1458
|
+
}
|
|
1413
1459
|
function isBareImport(importee) {
|
|
1414
1460
|
if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || import_path5.default.isAbsolute(importee)) {
|
|
1415
1461
|
return false;
|
|
@@ -1425,6 +1471,44 @@ function isBareImport(importee) {
|
|
|
1425
1471
|
}
|
|
1426
1472
|
}
|
|
1427
1473
|
|
|
1474
|
+
// src/utils/optimizer.ts
|
|
1475
|
+
var import_fs5 = __toESM(require("fs"), 1);
|
|
1476
|
+
var import_path6 = __toESM(require("path"), 1);
|
|
1477
|
+
var import_vite4 = require("vite");
|
|
1478
|
+
var PREBUNDLE_SENSITIVE_OPTIONS = [
|
|
1479
|
+
"compilerOptions",
|
|
1480
|
+
"configFile",
|
|
1481
|
+
"experimental",
|
|
1482
|
+
"extensions",
|
|
1483
|
+
"ignorePluginPreprocessors",
|
|
1484
|
+
"preprocess"
|
|
1485
|
+
];
|
|
1486
|
+
async function handleOptimizeDeps(options, viteConfig) {
|
|
1487
|
+
if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir)
|
|
1488
|
+
return;
|
|
1489
|
+
const viteMetadataPath = import_path6.default.resolve(viteConfig.cacheDir, "_metadata.json");
|
|
1490
|
+
if (!import_fs5.default.existsSync(viteMetadataPath))
|
|
1491
|
+
return;
|
|
1492
|
+
const svelteMetadataPath = import_path6.default.resolve(viteConfig.cacheDir, "_svelte_metadata.json");
|
|
1493
|
+
const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
|
|
1494
|
+
return typeof value === "function" ? value.toString() : value;
|
|
1495
|
+
});
|
|
1496
|
+
if (import_fs5.default.existsSync(svelteMetadataPath)) {
|
|
1497
|
+
const existingSvelteMetadata = import_fs5.default.readFileSync(svelteMetadataPath, "utf8");
|
|
1498
|
+
if (existingSvelteMetadata === currentSvelteMetadata)
|
|
1499
|
+
return;
|
|
1500
|
+
}
|
|
1501
|
+
await (0, import_vite4.optimizeDeps)(viteConfig, true);
|
|
1502
|
+
import_fs5.default.writeFileSync(svelteMetadataPath, currentSvelteMetadata);
|
|
1503
|
+
}
|
|
1504
|
+
function generateSvelteMetadata(options) {
|
|
1505
|
+
const metadata = {};
|
|
1506
|
+
for (const key of PREBUNDLE_SENSITIVE_OPTIONS) {
|
|
1507
|
+
metadata[key] = options[key];
|
|
1508
|
+
}
|
|
1509
|
+
return metadata;
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1428
1512
|
// src/index.ts
|
|
1429
1513
|
function svelte(inlineOptions) {
|
|
1430
1514
|
if (process.env.DEBUG != null) {
|
|
@@ -1432,7 +1516,7 @@ function svelte(inlineOptions) {
|
|
|
1432
1516
|
}
|
|
1433
1517
|
validateInlineOptions(inlineOptions);
|
|
1434
1518
|
const cache = new VitePluginSvelteCache();
|
|
1435
|
-
const
|
|
1519
|
+
const pkg_resolve_errors = /* @__PURE__ */ new Set();
|
|
1436
1520
|
let requestParser;
|
|
1437
1521
|
let options;
|
|
1438
1522
|
let viteConfig;
|
|
@@ -1460,6 +1544,9 @@ function svelte(inlineOptions) {
|
|
|
1460
1544
|
viteConfig = config;
|
|
1461
1545
|
log.debug("resolved options", options);
|
|
1462
1546
|
},
|
|
1547
|
+
async buildStart() {
|
|
1548
|
+
await handleOptimizeDeps(options, viteConfig);
|
|
1549
|
+
},
|
|
1463
1550
|
configureServer(server) {
|
|
1464
1551
|
options.server = server;
|
|
1465
1552
|
setupWatchers(options, cache, requestParser);
|
|
@@ -1506,21 +1593,13 @@ function svelte(inlineOptions) {
|
|
|
1506
1593
|
return resolvedSvelteSSR;
|
|
1507
1594
|
}
|
|
1508
1595
|
try {
|
|
1509
|
-
const resolved = resolveViaPackageJsonSvelte(importee, importer);
|
|
1596
|
+
const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
|
|
1510
1597
|
if (resolved) {
|
|
1511
1598
|
log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
|
|
1512
1599
|
return resolved;
|
|
1513
1600
|
}
|
|
1514
1601
|
} catch (err) {
|
|
1515
|
-
|
|
1516
|
-
case "ERR_PACKAGE_PATH_NOT_EXPORTED":
|
|
1517
|
-
pkg_export_errors.add(importee);
|
|
1518
|
-
return null;
|
|
1519
|
-
case "MODULE_NOT_FOUND":
|
|
1520
|
-
return null;
|
|
1521
|
-
default:
|
|
1522
|
-
throw err;
|
|
1523
|
-
}
|
|
1602
|
+
pkg_resolve_errors.add(importee);
|
|
1524
1603
|
}
|
|
1525
1604
|
},
|
|
1526
1605
|
async transform(code, id, opts) {
|
|
@@ -1546,7 +1625,7 @@ function svelte(inlineOptions) {
|
|
|
1546
1625
|
try {
|
|
1547
1626
|
compileData = await compileSvelte2(svelteRequest, code, options);
|
|
1548
1627
|
} catch (e) {
|
|
1549
|
-
throw toRollupError(e);
|
|
1628
|
+
throw toRollupError(e, options);
|
|
1550
1629
|
}
|
|
1551
1630
|
logCompilerWarnings(compileData.compiled.warnings, options);
|
|
1552
1631
|
cache.update(compileData);
|
|
@@ -1568,12 +1647,15 @@ function svelte(inlineOptions) {
|
|
|
1568
1647
|
}
|
|
1569
1648
|
},
|
|
1570
1649
|
buildEnd() {
|
|
1571
|
-
if (
|
|
1572
|
-
log.warn(`
|
|
1650
|
+
if (pkg_resolve_errors.size > 0) {
|
|
1651
|
+
log.warn(`vite-plugin-svelte was unable to find package.json of the following packages and wasn't able to resolve via their "svelte" field.
|
|
1652
|
+
If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.
|
|
1653
|
+
${Array.from(pkg_resolve_errors, (s) => `- ${s}`).join("\n")}`.replace(/\t/g, ""));
|
|
1573
1654
|
}
|
|
1574
1655
|
}
|
|
1575
1656
|
};
|
|
1576
1657
|
}
|
|
1658
|
+
module.exports = __toCommonJS(src_exports);
|
|
1577
1659
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1578
1660
|
0 && (module.exports = {
|
|
1579
1661
|
svelte
|