@sveltejs/vite-plugin-svelte 1.0.0-next.40 → 1.0.0-next.43
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 +5 -5
- package/dist/index.cjs +321 -222
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +47 -2
- package/dist/index.js +311 -205
- package/dist/index.js.map +1 -1
- package/package.json +13 -11
- package/src/index.ts +163 -130
- package/src/ui/inspector/Inspector.svelte +229 -0
- package/src/ui/inspector/icon.svg +5 -0
- package/src/ui/inspector/load-inspector.ts +16 -0
- package/src/ui/inspector/plugin.ts +101 -0
- package/src/utils/compile.ts +4 -0
- package/src/utils/esbuild.ts +3 -26
- package/src/utils/hash.ts +1 -1
- package/src/utils/load-svelte-config.ts +3 -0
- package/src/utils/optimizer.ts +17 -22
- package/src/utils/options.ts +94 -32
- package/src/utils/preprocess.ts +6 -2
- package/src/utils/watch.ts +22 -18
package/dist/index.cjs
CHANGED
|
@@ -21,35 +21,29 @@ var __spreadValues = (a, b) => {
|
|
|
21
21
|
return a;
|
|
22
22
|
};
|
|
23
23
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
-
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
25
24
|
var __export = (target, all) => {
|
|
26
25
|
for (var name in all)
|
|
27
26
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
28
27
|
};
|
|
29
|
-
var
|
|
30
|
-
if (
|
|
31
|
-
for (let key of __getOwnPropNames(
|
|
32
|
-
if (!__hasOwnProp.call(
|
|
33
|
-
__defProp(
|
|
28
|
+
var __copyProps = (to, from, except, desc) => {
|
|
29
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
30
|
+
for (let key of __getOwnPropNames(from))
|
|
31
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
32
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
34
33
|
}
|
|
35
|
-
return
|
|
34
|
+
return to;
|
|
36
35
|
};
|
|
37
|
-
var __toESM = (
|
|
38
|
-
|
|
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);
|
|
36
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
37
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
45
38
|
|
|
46
39
|
// src/index.ts
|
|
47
40
|
var src_exports = {};
|
|
48
41
|
__export(src_exports, {
|
|
49
42
|
svelte: () => svelte
|
|
50
43
|
});
|
|
44
|
+
module.exports = __toCommonJS(src_exports);
|
|
51
45
|
|
|
52
|
-
// ../../node_modules/.pnpm/tsup@5.12.
|
|
46
|
+
// ../../node_modules/.pnpm/tsup@5.12.5_typescript@4.6.3/node_modules/tsup/assets/cjs_shims.js
|
|
53
47
|
var getImportMetaUrl = () => typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
|
|
54
48
|
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
55
49
|
|
|
@@ -285,7 +279,7 @@ function safeBase64Hash(input) {
|
|
|
285
279
|
}
|
|
286
280
|
const md5 = crypto.createHash("md5");
|
|
287
281
|
md5.update(input);
|
|
288
|
-
const hash = toSafe(md5.digest("base64")).
|
|
282
|
+
const hash = toSafe(md5.digest("base64")).slice(0, hash_length);
|
|
289
283
|
hashes[input] = hash;
|
|
290
284
|
return hash;
|
|
291
285
|
}
|
|
@@ -300,8 +294,9 @@ function toSafe(base64) {
|
|
|
300
294
|
}
|
|
301
295
|
|
|
302
296
|
// src/utils/compile.ts
|
|
297
|
+
var scriptLangRE = /<script [^>]*lang=["']?([^"' >]+)["']?[^>]*>/;
|
|
303
298
|
var _createCompileSvelte = (makeHot) => async function compileSvelte2(svelteRequest, code, options) {
|
|
304
|
-
var _a, _b;
|
|
299
|
+
var _a, _b, _c;
|
|
305
300
|
const { filename, normalizedFilename, cssId, ssr } = svelteRequest;
|
|
306
301
|
const { emitCss = true } = options;
|
|
307
302
|
const dependencies = [];
|
|
@@ -364,6 +359,7 @@ import ${JSON.stringify(cssId)};
|
|
|
364
359
|
return {
|
|
365
360
|
filename,
|
|
366
361
|
normalizedFilename,
|
|
362
|
+
lang: ((_c = code.match(scriptLangRE)) == null ? void 0 : _c[1]) || "js",
|
|
367
363
|
compiled,
|
|
368
364
|
ssr,
|
|
369
365
|
dependencies
|
|
@@ -482,6 +478,9 @@ var knownSvelteConfigNames = [
|
|
|
482
478
|
];
|
|
483
479
|
var dynamicImportDefault = new Function("path", 'return import(path + "?t=" + Date.now()).then(m => m.default)');
|
|
484
480
|
async function loadSvelteConfig(viteConfig, inlineOptions) {
|
|
481
|
+
if (inlineOptions.configFile === false) {
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
485
484
|
const configFile = findConfigToLoad(viteConfig, inlineOptions);
|
|
486
485
|
if (configFile) {
|
|
487
486
|
let err;
|
|
@@ -562,7 +561,7 @@ var SVELTE_HMR_IMPORTS = [
|
|
|
562
561
|
];
|
|
563
562
|
|
|
564
563
|
// src/utils/options.ts
|
|
565
|
-
var
|
|
564
|
+
var import_path4 = __toESM(require("path"), 1);
|
|
566
565
|
|
|
567
566
|
// src/utils/dependencies.ts
|
|
568
567
|
var import_path2 = __toESM(require("path"), 1);
|
|
@@ -592,7 +591,7 @@ function findRootSvelteDependencies(root, cwdFallback = true) {
|
|
|
592
591
|
].filter((dep) => !is_common_without_svelte_field(dep));
|
|
593
592
|
return getSvelteDependencies(deps, root);
|
|
594
593
|
}
|
|
595
|
-
function getSvelteDependencies(deps, pkgDir,
|
|
594
|
+
function getSvelteDependencies(deps, pkgDir, path8 = []) {
|
|
596
595
|
const result = [];
|
|
597
596
|
const localRequire = (0, import_module2.createRequire)(`${pkgDir}/package.json`);
|
|
598
597
|
const resolvedDeps = deps.map((dep) => resolveDependencyData(dep, localRequire)).filter(Boolean);
|
|
@@ -600,18 +599,18 @@ function getSvelteDependencies(deps, pkgDir, path7 = []) {
|
|
|
600
599
|
const type = getSvelteDependencyType(pkg);
|
|
601
600
|
if (!type)
|
|
602
601
|
continue;
|
|
603
|
-
result.push({ name: pkg.name, type, pkg, dir, path:
|
|
602
|
+
result.push({ name: pkg.name, type, pkg, dir, path: path8 });
|
|
604
603
|
if (type === "component-library" && pkg.dependencies) {
|
|
605
604
|
let dependencyNames = Object.keys(pkg.dependencies);
|
|
606
|
-
const circular = dependencyNames.filter((name) =>
|
|
605
|
+
const circular = dependencyNames.filter((name) => path8.includes(name));
|
|
607
606
|
if (circular.length > 0) {
|
|
608
|
-
log.warn.enabled && log.warn(`skipping circular svelte dependencies in automated vite optimizeDeps handling`, circular.map((x) =>
|
|
609
|
-
dependencyNames = dependencyNames.filter((name) => !
|
|
607
|
+
log.warn.enabled && log.warn(`skipping circular svelte dependencies in automated vite optimizeDeps handling`, circular.map((x) => path8.concat(x).join(">")));
|
|
608
|
+
dependencyNames = dependencyNames.filter((name) => !path8.includes(name));
|
|
610
609
|
}
|
|
611
|
-
if (
|
|
612
|
-
log.debug.once(`encountered deep svelte dependency tree: ${
|
|
610
|
+
if (path8.length === 3) {
|
|
611
|
+
log.debug.once(`encountered deep svelte dependency tree: ${path8.join(">")}`);
|
|
613
612
|
}
|
|
614
|
-
result.push(...getSvelteDependencies(dependencyNames, dir,
|
|
613
|
+
result.push(...getSvelteDependencies(dependencyNames, dir, path8.concat(pkg.name)));
|
|
615
614
|
}
|
|
616
615
|
}
|
|
617
616
|
return result;
|
|
@@ -792,7 +791,9 @@ function esbuildSveltePlugin(options) {
|
|
|
792
791
|
return {
|
|
793
792
|
name: "vite-plugin-svelte:optimize-svelte",
|
|
794
793
|
setup(build) {
|
|
795
|
-
|
|
794
|
+
var _a;
|
|
795
|
+
if ((_a = build.initialOptions.plugins) == null ? void 0 : _a.some((v) => v.name === "vite:dep-scan"))
|
|
796
|
+
return;
|
|
796
797
|
const svelteExtensions = (options.extensions ?? [".svelte"]).map((ext) => ext.slice(1));
|
|
797
798
|
const svelteFilter = new RegExp(`\\.(` + svelteExtensions.join("|") + `)(\\?.*)?$`);
|
|
798
799
|
build.onLoad({ filter: svelteFilter }, async ({ path: filename }) => {
|
|
@@ -807,23 +808,6 @@ function esbuildSveltePlugin(options) {
|
|
|
807
808
|
}
|
|
808
809
|
};
|
|
809
810
|
}
|
|
810
|
-
function disableVitePrebundleSvelte(build) {
|
|
811
|
-
var _a;
|
|
812
|
-
const viteDepPrebundlePlugin = (_a = build.initialOptions.plugins) == null ? void 0 : _a.find((v) => v.name === "vite:dep-pre-bundle");
|
|
813
|
-
if (!viteDepPrebundlePlugin)
|
|
814
|
-
return;
|
|
815
|
-
const _setup = viteDepPrebundlePlugin.setup.bind(viteDepPrebundlePlugin);
|
|
816
|
-
viteDepPrebundlePlugin.setup = function(build2) {
|
|
817
|
-
const _onResolve = build2.onResolve.bind(build2);
|
|
818
|
-
build2.onResolve = function(options, callback) {
|
|
819
|
-
if (options.filter.source.includes("svelte")) {
|
|
820
|
-
options.filter = new RegExp(options.filter.source.replace("|svelte", ""), options.filter.flags);
|
|
821
|
-
}
|
|
822
|
-
return _onResolve(options, callback);
|
|
823
|
-
};
|
|
824
|
-
return _setup(build2);
|
|
825
|
-
};
|
|
826
|
-
}
|
|
827
811
|
async function compileSvelte(options, { filename, code }) {
|
|
828
812
|
var _a, _b;
|
|
829
813
|
const compileOptions = __spreadProps(__spreadValues({}, options.compilerOptions), {
|
|
@@ -909,6 +893,7 @@ async function buildSourceMap(from, to, filename) {
|
|
|
909
893
|
}
|
|
910
894
|
|
|
911
895
|
// src/utils/preprocess.ts
|
|
896
|
+
var import_path3 = __toESM(require("path"), 1);
|
|
912
897
|
var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"];
|
|
913
898
|
var supportedScriptLangs = ["ts"];
|
|
914
899
|
function createViteScriptPreprocessor() {
|
|
@@ -949,7 +934,7 @@ function createViteStylePreprocessor(config) {
|
|
|
949
934
|
const moduleId = `${filename}.${lang}`;
|
|
950
935
|
const transformResult = await pluginTransform(content, moduleId);
|
|
951
936
|
if (((_b = (_a = transformResult.map) == null ? void 0 : _a.sources) == null ? void 0 : _b[0]) === moduleId) {
|
|
952
|
-
transformResult.map.sources[0] = filename;
|
|
937
|
+
transformResult.map.sources[0] = import_path3.default.basename(filename);
|
|
953
938
|
}
|
|
954
939
|
return {
|
|
955
940
|
code: transformResult.code,
|
|
@@ -974,7 +959,10 @@ function createInjectScopeEverythingRulePreprocessorGroup() {
|
|
|
974
959
|
s.append(" *{}");
|
|
975
960
|
return {
|
|
976
961
|
code: s.toString(),
|
|
977
|
-
map: s.generateDecodedMap({
|
|
962
|
+
map: s.generateDecodedMap({
|
|
963
|
+
source: filename ? import_path3.default.basename(filename) : void 0,
|
|
964
|
+
hires: true
|
|
965
|
+
})
|
|
978
966
|
};
|
|
979
967
|
}
|
|
980
968
|
};
|
|
@@ -1085,6 +1073,7 @@ function validateSourceMapOutputWrapper(group, i) {
|
|
|
1085
1073
|
}
|
|
1086
1074
|
|
|
1087
1075
|
// src/utils/options.ts
|
|
1076
|
+
var import_deepmerge = __toESM(require("deepmerge"), 1);
|
|
1088
1077
|
var knownOptions = /* @__PURE__ */ new Set([
|
|
1089
1078
|
"configFile",
|
|
1090
1079
|
"include",
|
|
@@ -1097,7 +1086,8 @@ var knownOptions = /* @__PURE__ */ new Set([
|
|
|
1097
1086
|
"hot",
|
|
1098
1087
|
"ignorePluginPreprocessors",
|
|
1099
1088
|
"disableDependencyReinclusion",
|
|
1100
|
-
"experimental"
|
|
1089
|
+
"experimental",
|
|
1090
|
+
"kit"
|
|
1101
1091
|
]);
|
|
1102
1092
|
function validateInlineOptions(inlineOptions) {
|
|
1103
1093
|
const invalidKeys = Object.keys(inlineOptions || {}).filter((key) => !knownOptions.has(key));
|
|
@@ -1117,19 +1107,27 @@ async function preResolveOptions(inlineOptions = {}, viteUserConfig, viteEnv) {
|
|
|
1117
1107
|
}
|
|
1118
1108
|
};
|
|
1119
1109
|
const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions);
|
|
1120
|
-
const
|
|
1121
|
-
compilerOptions: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.compilerOptions), svelteConfig == null ? void 0 : svelteConfig.compilerOptions), inlineOptions == null ? void 0 : inlineOptions.compilerOptions),
|
|
1122
|
-
experimental: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.experimental), svelteConfig == null ? void 0 : svelteConfig.experimental), inlineOptions == null ? void 0 : inlineOptions.experimental),
|
|
1110
|
+
const extraOptions = {
|
|
1123
1111
|
root: viteConfigWithResolvedRoot.root,
|
|
1124
1112
|
isBuild: viteEnv.command === "build",
|
|
1125
1113
|
isServe: viteEnv.command === "serve",
|
|
1126
1114
|
isDebug: process.env.DEBUG != null
|
|
1127
|
-
}
|
|
1115
|
+
};
|
|
1116
|
+
const merged = mergeConfigs(defaultOptions, svelteConfig, inlineOptions, extraOptions);
|
|
1128
1117
|
if (svelteConfig == null ? void 0 : svelteConfig.configFile) {
|
|
1129
1118
|
merged.configFile = svelteConfig.configFile;
|
|
1130
1119
|
}
|
|
1131
1120
|
return merged;
|
|
1132
1121
|
}
|
|
1122
|
+
function mergeConfigs(...configs) {
|
|
1123
|
+
let result = {};
|
|
1124
|
+
for (const config of configs.filter(Boolean)) {
|
|
1125
|
+
result = (0, import_deepmerge.default)(result, config, {
|
|
1126
|
+
arrayMerge: (target, source) => source ?? target
|
|
1127
|
+
});
|
|
1128
|
+
}
|
|
1129
|
+
return result;
|
|
1130
|
+
}
|
|
1133
1131
|
function resolveOptions(preResolveOptions2, viteConfig) {
|
|
1134
1132
|
const defaultOptions = {
|
|
1135
1133
|
hot: viteConfig.isProduction ? false : { injectCss: !preResolveOptions2.emitCss },
|
|
@@ -1138,11 +1136,11 @@ function resolveOptions(preResolveOptions2, viteConfig) {
|
|
|
1138
1136
|
dev: !viteConfig.isProduction
|
|
1139
1137
|
}
|
|
1140
1138
|
};
|
|
1141
|
-
const
|
|
1142
|
-
compilerOptions: __spreadValues(__spreadValues({}, defaultOptions.compilerOptions), preResolveOptions2.compilerOptions),
|
|
1139
|
+
const extraOptions = {
|
|
1143
1140
|
root: viteConfig.root,
|
|
1144
1141
|
isProduction: viteConfig.isProduction
|
|
1145
|
-
}
|
|
1142
|
+
};
|
|
1143
|
+
const merged = mergeConfigs(defaultOptions, preResolveOptions2, extraOptions);
|
|
1146
1144
|
addExtraPreprocessors(merged, viteConfig);
|
|
1147
1145
|
enforceOptionsForHmr(merged);
|
|
1148
1146
|
enforceOptionsForProduction(merged);
|
|
@@ -1192,9 +1190,10 @@ function enforceOptionsForProduction(options) {
|
|
|
1192
1190
|
}
|
|
1193
1191
|
}
|
|
1194
1192
|
function resolveViteRoot(viteConfig) {
|
|
1195
|
-
return (0, import_vite3.normalizePath)(viteConfig.root ?
|
|
1193
|
+
return (0, import_vite3.normalizePath)(viteConfig.root ? import_path4.default.resolve(viteConfig.root) : process.cwd());
|
|
1196
1194
|
}
|
|
1197
1195
|
function buildExtraViteConfig(options, config) {
|
|
1196
|
+
var _a;
|
|
1198
1197
|
const svelteDeps = findRootSvelteDependencies(options.root);
|
|
1199
1198
|
const extraViteConfig = {
|
|
1200
1199
|
resolve: {
|
|
@@ -1205,19 +1204,29 @@ function buildExtraViteConfig(options, config) {
|
|
|
1205
1204
|
if (options.isServe) {
|
|
1206
1205
|
extraViteConfig.optimizeDeps = buildOptimizeDepsForSvelte(svelteDeps, options, config.optimizeDeps);
|
|
1207
1206
|
}
|
|
1207
|
+
if ((_a = options.experimental) == null ? void 0 : _a.prebundleSvelteLibraries) {
|
|
1208
|
+
extraViteConfig.optimizeDeps = __spreadProps(__spreadValues({}, extraViteConfig.optimizeDeps), {
|
|
1209
|
+
extensions: options.extensions ?? [".svelte"],
|
|
1210
|
+
esbuildOptions: {
|
|
1211
|
+
plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {
|
|
1212
|
+
} }]
|
|
1213
|
+
}
|
|
1214
|
+
});
|
|
1215
|
+
}
|
|
1208
1216
|
extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config, extraViteConfig);
|
|
1209
1217
|
return extraViteConfig;
|
|
1210
1218
|
}
|
|
1211
|
-
function buildOptimizeDepsForSvelte(svelteDeps, options,
|
|
1219
|
+
function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) {
|
|
1220
|
+
var _a;
|
|
1212
1221
|
const include = [];
|
|
1213
1222
|
const exclude = ["svelte-hmr"];
|
|
1214
1223
|
const isIncluded = (dep) => {
|
|
1215
|
-
var
|
|
1216
|
-
return include.includes(dep) || ((
|
|
1224
|
+
var _a2;
|
|
1225
|
+
return include.includes(dep) || ((_a2 = optimizeDeps == null ? void 0 : optimizeDeps.include) == null ? void 0 : _a2.includes(dep));
|
|
1217
1226
|
};
|
|
1218
1227
|
const isExcluded = (dep) => {
|
|
1219
|
-
var
|
|
1220
|
-
return exclude.includes(dep) || ((
|
|
1228
|
+
var _a2;
|
|
1229
|
+
return exclude.includes(dep) || ((_a2 = optimizeDeps == null ? void 0 : optimizeDeps.exclude) == null ? void 0 : _a2.some((id) => dep === id || id.startsWith(`${dep}/`)));
|
|
1221
1230
|
};
|
|
1222
1231
|
if (!isExcluded("svelte")) {
|
|
1223
1232
|
const svelteImportsToInclude = SVELTE_IMPORTS.filter((x) => x !== "svelte/ssr");
|
|
@@ -1226,15 +1235,8 @@ function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps2) {
|
|
|
1226
1235
|
} else {
|
|
1227
1236
|
log.debug('"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.');
|
|
1228
1237
|
}
|
|
1229
|
-
if (options.experimental.prebundleSvelteLibraries) {
|
|
1230
|
-
return {
|
|
1231
|
-
include,
|
|
1232
|
-
exclude,
|
|
1233
|
-
esbuildOptions: {
|
|
1234
|
-
plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {
|
|
1235
|
-
} }]
|
|
1236
|
-
}
|
|
1237
|
-
};
|
|
1238
|
+
if ((_a = options.experimental) == null ? void 0 : _a.prebundleSvelteLibraries) {
|
|
1239
|
+
return { include, exclude };
|
|
1238
1240
|
}
|
|
1239
1241
|
svelteDeps = svelteDeps.filter((dep) => dep.type === "component-library");
|
|
1240
1242
|
const svelteDepsToExclude = Array.from(new Set(svelteDeps.map((dep) => dep.name))).filter((dep) => !isIncluded(dep));
|
|
@@ -1356,8 +1358,8 @@ var VitePluginSvelteCache = class {
|
|
|
1356
1358
|
return this._js.get(svelteRequest.normalizedFilename);
|
|
1357
1359
|
}
|
|
1358
1360
|
}
|
|
1359
|
-
getDependants(
|
|
1360
|
-
const dependants = this._dependants.get(
|
|
1361
|
+
getDependants(path8) {
|
|
1362
|
+
const dependants = this._dependants.get(path8);
|
|
1361
1363
|
return dependants ? [...dependants] : [];
|
|
1362
1364
|
}
|
|
1363
1365
|
getResolvedSvelteField(name, importer) {
|
|
@@ -1373,7 +1375,7 @@ var VitePluginSvelteCache = class {
|
|
|
1373
1375
|
|
|
1374
1376
|
// src/utils/watch.ts
|
|
1375
1377
|
var import_fs4 = __toESM(require("fs"), 1);
|
|
1376
|
-
var
|
|
1378
|
+
var import_path5 = __toESM(require("path"), 1);
|
|
1377
1379
|
function setupWatchers(options, cache, requestParser) {
|
|
1378
1380
|
const { server, configFile: svelteConfigFile } = options;
|
|
1379
1381
|
if (!server) {
|
|
@@ -1412,27 +1414,29 @@ function setupWatchers(options, cache, requestParser) {
|
|
|
1412
1414
|
server.restart();
|
|
1413
1415
|
}
|
|
1414
1416
|
};
|
|
1415
|
-
const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => import_path4.default.join(root, cfg));
|
|
1416
|
-
const restartOnConfigAdd = (filename) => {
|
|
1417
|
-
if (possibleSvelteConfigs.includes(filename)) {
|
|
1418
|
-
triggerViteRestart(filename);
|
|
1419
|
-
}
|
|
1420
|
-
};
|
|
1421
|
-
const restartOnConfigChange = (filename) => {
|
|
1422
|
-
if (filename === svelteConfigFile) {
|
|
1423
|
-
triggerViteRestart(filename);
|
|
1424
|
-
}
|
|
1425
|
-
};
|
|
1426
1417
|
const listenerCollection = {
|
|
1427
1418
|
add: [],
|
|
1428
1419
|
change: [emitChangeEventOnDependants],
|
|
1429
1420
|
unlink: [removeUnlinkedFromCache, emitChangeEventOnDependants]
|
|
1430
1421
|
};
|
|
1431
|
-
if (svelteConfigFile) {
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1422
|
+
if (svelteConfigFile !== false) {
|
|
1423
|
+
const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => import_path5.default.join(root, cfg));
|
|
1424
|
+
const restartOnConfigAdd = (filename) => {
|
|
1425
|
+
if (possibleSvelteConfigs.includes(filename)) {
|
|
1426
|
+
triggerViteRestart(filename);
|
|
1427
|
+
}
|
|
1428
|
+
};
|
|
1429
|
+
const restartOnConfigChange = (filename) => {
|
|
1430
|
+
if (filename === svelteConfigFile) {
|
|
1431
|
+
triggerViteRestart(filename);
|
|
1432
|
+
}
|
|
1433
|
+
};
|
|
1434
|
+
if (svelteConfigFile) {
|
|
1435
|
+
listenerCollection.change.push(restartOnConfigChange);
|
|
1436
|
+
listenerCollection.unlink.push(restartOnConfigChange);
|
|
1437
|
+
} else {
|
|
1438
|
+
listenerCollection.add.push(restartOnConfigAdd);
|
|
1439
|
+
}
|
|
1436
1440
|
}
|
|
1437
1441
|
Object.entries(listenerCollection).forEach(([evt, listeners]) => {
|
|
1438
1442
|
if (listeners.length > 0) {
|
|
@@ -1442,12 +1446,12 @@ function setupWatchers(options, cache, requestParser) {
|
|
|
1442
1446
|
}
|
|
1443
1447
|
function ensureWatchedFile(watcher, file, root) {
|
|
1444
1448
|
if (file && !file.startsWith(root + "/") && !file.includes("\0") && import_fs4.default.existsSync(file)) {
|
|
1445
|
-
watcher.add(
|
|
1449
|
+
watcher.add(import_path5.default.resolve(file));
|
|
1446
1450
|
}
|
|
1447
1451
|
}
|
|
1448
1452
|
|
|
1449
1453
|
// src/utils/resolve.ts
|
|
1450
|
-
var
|
|
1454
|
+
var import_path6 = __toESM(require("path"), 1);
|
|
1451
1455
|
var import_module4 = require("module");
|
|
1452
1456
|
function resolveViaPackageJsonSvelte(importee, importer, cache) {
|
|
1453
1457
|
if (importer && isBareImport(importee) && !isNodeInternal(importee) && !is_common_without_svelte_field(importee)) {
|
|
@@ -1460,7 +1464,7 @@ function resolveViaPackageJsonSvelte(importee, importer, cache) {
|
|
|
1460
1464
|
if (pkgData) {
|
|
1461
1465
|
const { pkg, dir } = pkgData;
|
|
1462
1466
|
if (pkg.svelte) {
|
|
1463
|
-
const result =
|
|
1467
|
+
const result = import_path6.default.resolve(dir, pkg.svelte);
|
|
1464
1468
|
cache.setResolvedSvelteField(importee, importer, result);
|
|
1465
1469
|
return result;
|
|
1466
1470
|
}
|
|
@@ -1471,7 +1475,7 @@ function isNodeInternal(importee) {
|
|
|
1471
1475
|
return importee.startsWith("node:") || import_module4.builtinModules.includes(importee);
|
|
1472
1476
|
}
|
|
1473
1477
|
function isBareImport(importee) {
|
|
1474
|
-
if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") ||
|
|
1478
|
+
if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || import_path6.default.isAbsolute(importee)) {
|
|
1475
1479
|
return false;
|
|
1476
1480
|
}
|
|
1477
1481
|
const parts = importee.split("/");
|
|
@@ -1486,9 +1490,8 @@ function isBareImport(importee) {
|
|
|
1486
1490
|
}
|
|
1487
1491
|
|
|
1488
1492
|
// src/utils/optimizer.ts
|
|
1489
|
-
var import_fs5 =
|
|
1490
|
-
var
|
|
1491
|
-
var import_vite4 = require("vite");
|
|
1493
|
+
var import_fs5 = require("fs");
|
|
1494
|
+
var import_path7 = __toESM(require("path"), 1);
|
|
1492
1495
|
var PREBUNDLE_SENSITIVE_OPTIONS = [
|
|
1493
1496
|
"compilerOptions",
|
|
1494
1497
|
"configFile",
|
|
@@ -1497,23 +1500,20 @@ var PREBUNDLE_SENSITIVE_OPTIONS = [
|
|
|
1497
1500
|
"ignorePluginPreprocessors",
|
|
1498
1501
|
"preprocess"
|
|
1499
1502
|
];
|
|
1500
|
-
async function
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
const
|
|
1504
|
-
if (!viteMetadataPath)
|
|
1505
|
-
return;
|
|
1506
|
-
const svelteMetadataPath = import_path6.default.resolve(viteMetadataPath, "../_svelte_metadata.json");
|
|
1507
|
-
const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
|
|
1503
|
+
async function saveSvelteMetadata(cacheDir, options) {
|
|
1504
|
+
const svelteMetadata = generateSvelteMetadata(options);
|
|
1505
|
+
const svelteMetadataPath = import_path7.default.resolve(cacheDir, "_svelte_metadata.json");
|
|
1506
|
+
const currentSvelteMetadata = JSON.stringify(svelteMetadata, (_, value) => {
|
|
1508
1507
|
return typeof value === "function" ? value.toString() : value;
|
|
1509
1508
|
});
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1509
|
+
let existingSvelteMetadata;
|
|
1510
|
+
try {
|
|
1511
|
+
existingSvelteMetadata = await import_fs5.promises.readFile(svelteMetadataPath, "utf8");
|
|
1512
|
+
} catch {
|
|
1514
1513
|
}
|
|
1515
|
-
await
|
|
1516
|
-
import_fs5.
|
|
1514
|
+
await import_fs5.promises.mkdir(cacheDir, { recursive: true });
|
|
1515
|
+
await import_fs5.promises.writeFile(svelteMetadataPath, currentSvelteMetadata);
|
|
1516
|
+
return currentSvelteMetadata !== existingSvelteMetadata;
|
|
1517
1517
|
}
|
|
1518
1518
|
function generateSvelteMetadata(options) {
|
|
1519
1519
|
const metadata = {};
|
|
@@ -1522,13 +1522,94 @@ function generateSvelteMetadata(options) {
|
|
|
1522
1522
|
}
|
|
1523
1523
|
return metadata;
|
|
1524
1524
|
}
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1525
|
+
|
|
1526
|
+
// src/ui/inspector/plugin.ts
|
|
1527
|
+
var import_module5 = require("module");
|
|
1528
|
+
var defaultInspectorOptions = {
|
|
1529
|
+
toggleKeyCombo: process.platform === "win32" ? "control-shift" : "meta-shift",
|
|
1530
|
+
holdMode: false,
|
|
1531
|
+
showToggleButton: "active",
|
|
1532
|
+
toggleButtonPos: "top-right",
|
|
1533
|
+
customStyles: true
|
|
1534
|
+
};
|
|
1535
|
+
function svelteInspector() {
|
|
1536
|
+
let root;
|
|
1537
|
+
let rootRequire;
|
|
1538
|
+
let inspectorOptions;
|
|
1539
|
+
let append_to;
|
|
1540
|
+
return {
|
|
1541
|
+
name: "vite-plugin-svelte:inspector",
|
|
1542
|
+
apply: "serve",
|
|
1543
|
+
enforce: "pre",
|
|
1544
|
+
configResolved(config) {
|
|
1545
|
+
var _a, _b, _c;
|
|
1546
|
+
const vps = config.plugins.find((p) => p.name === "vite-plugin-svelte");
|
|
1547
|
+
if ((_c = (_b = (_a = vps == null ? void 0 : vps.api) == null ? void 0 : _a.options) == null ? void 0 : _b.experimental) == null ? void 0 : _c.inspector) {
|
|
1548
|
+
inspectorOptions = __spreadValues(__spreadValues({}, defaultInspectorOptions), vps.api.options.experimental.inspector);
|
|
1549
|
+
}
|
|
1550
|
+
if (!vps || !inspectorOptions) {
|
|
1551
|
+
this.resolveId = this.load = this.transformIndexHtml = this.transform = () => {
|
|
1552
|
+
};
|
|
1553
|
+
} else {
|
|
1554
|
+
root = config.root || process.cwd();
|
|
1555
|
+
rootRequire = (0, import_module5.createRequire)(root);
|
|
1556
|
+
if (vps.api.options.kit && !inspectorOptions.appendTo) {
|
|
1557
|
+
const out_dir = vps.api.options.kit.outDir || ".svelte-kit";
|
|
1558
|
+
inspectorOptions.appendTo = `${out_dir}/runtime/client/start.js`;
|
|
1559
|
+
}
|
|
1560
|
+
append_to = inspectorOptions.appendTo;
|
|
1561
|
+
}
|
|
1562
|
+
},
|
|
1563
|
+
async resolveId(importee, importer, options) {
|
|
1564
|
+
if (options == null ? void 0 : options.ssr) {
|
|
1565
|
+
return;
|
|
1566
|
+
}
|
|
1567
|
+
if (importee === "virtual:svelte-inspector-options") {
|
|
1568
|
+
return importee;
|
|
1569
|
+
}
|
|
1570
|
+
if (importee.startsWith("virtual:svelte-inspector:")) {
|
|
1571
|
+
const file = importee.replace("virtual:svelte-inspector:", "@sveltejs/vite-plugin-svelte/src/ui/inspector/");
|
|
1572
|
+
const path8 = rootRequire.resolve(file);
|
|
1573
|
+
if (path8) {
|
|
1574
|
+
return path8;
|
|
1575
|
+
} else {
|
|
1576
|
+
log.error.once(`failed to resolve ${file} for ${importee} from ${root}`);
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
},
|
|
1580
|
+
load(id) {
|
|
1581
|
+
if (id === "virtual:svelte-inspector-options") {
|
|
1582
|
+
return `export default ${JSON.stringify(inspectorOptions ?? {})}`;
|
|
1583
|
+
}
|
|
1584
|
+
},
|
|
1585
|
+
transform(code, id, options) {
|
|
1586
|
+
if ((options == null ? void 0 : options.ssr) || !append_to) {
|
|
1587
|
+
return;
|
|
1588
|
+
}
|
|
1589
|
+
if (id.endsWith(append_to)) {
|
|
1590
|
+
return { code: `${code}
|
|
1591
|
+
import 'virtual:svelte-inspector:load-inspector.ts'` };
|
|
1592
|
+
}
|
|
1593
|
+
},
|
|
1594
|
+
transformIndexHtml(html) {
|
|
1595
|
+
if (append_to) {
|
|
1596
|
+
return;
|
|
1597
|
+
}
|
|
1598
|
+
return {
|
|
1599
|
+
html,
|
|
1600
|
+
tags: [
|
|
1601
|
+
{
|
|
1602
|
+
tag: "script",
|
|
1603
|
+
injectTo: "body",
|
|
1604
|
+
attrs: {
|
|
1605
|
+
type: "module",
|
|
1606
|
+
src: "/@id/virtual:svelte-inspector:load-inspector.ts"
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
]
|
|
1610
|
+
};
|
|
1611
|
+
}
|
|
1612
|
+
};
|
|
1532
1613
|
}
|
|
1533
1614
|
|
|
1534
1615
|
// src/index.ts
|
|
@@ -1543,121 +1624,139 @@ function svelte(inlineOptions) {
|
|
|
1543
1624
|
let viteConfig;
|
|
1544
1625
|
let compileSvelte2;
|
|
1545
1626
|
let resolvedSvelteSSR;
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1627
|
+
const api = {};
|
|
1628
|
+
const plugins = [
|
|
1629
|
+
{
|
|
1630
|
+
name: "vite-plugin-svelte",
|
|
1631
|
+
enforce: "pre",
|
|
1632
|
+
api,
|
|
1633
|
+
async config(config, configEnv) {
|
|
1634
|
+
if (process.env.DEBUG) {
|
|
1635
|
+
log.setLevel("debug");
|
|
1636
|
+
} else if (config.logLevel) {
|
|
1637
|
+
log.setLevel(config.logLevel);
|
|
1638
|
+
}
|
|
1639
|
+
options = await preResolveOptions(inlineOptions, config, configEnv);
|
|
1640
|
+
const extraViteConfig = buildExtraViteConfig(options, config);
|
|
1641
|
+
log.debug("additional vite config", extraViteConfig);
|
|
1642
|
+
return extraViteConfig;
|
|
1643
|
+
},
|
|
1644
|
+
async configResolved(config) {
|
|
1645
|
+
options = resolveOptions(options, config);
|
|
1646
|
+
patchResolvedViteConfig(config, options);
|
|
1647
|
+
requestParser = buildIdParser(options);
|
|
1648
|
+
compileSvelte2 = createCompileSvelte(options);
|
|
1649
|
+
viteConfig = config;
|
|
1650
|
+
api.options = options;
|
|
1651
|
+
log.debug("resolved options", options);
|
|
1652
|
+
},
|
|
1653
|
+
async buildStart() {
|
|
1654
|
+
var _a;
|
|
1655
|
+
if (!((_a = options.experimental) == null ? void 0 : _a.prebundleSvelteLibraries))
|
|
1656
|
+
return;
|
|
1657
|
+
const isSvelteMetadataChanged = await saveSvelteMetadata(viteConfig.cacheDir, options);
|
|
1658
|
+
if (isSvelteMetadataChanged) {
|
|
1659
|
+
viteConfig.server.force = true;
|
|
1660
|
+
}
|
|
1661
|
+
},
|
|
1662
|
+
configureServer(server) {
|
|
1663
|
+
options.server = server;
|
|
1664
|
+
setupWatchers(options, cache, requestParser);
|
|
1665
|
+
},
|
|
1666
|
+
load(id, opts) {
|
|
1667
|
+
const ssr = opts === true || (opts == null ? void 0 : opts.ssr);
|
|
1668
|
+
const svelteRequest = requestParser(id, !!ssr);
|
|
1669
|
+
if (svelteRequest) {
|
|
1670
|
+
const { filename, query } = svelteRequest;
|
|
1671
|
+
if (query.svelte && query.type === "style") {
|
|
1672
|
+
const css = cache.getCSS(svelteRequest);
|
|
1673
|
+
if (css) {
|
|
1674
|
+
log.debug(`load returns css for ${filename}`);
|
|
1675
|
+
return css;
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
if (viteConfig.assetsInclude(filename)) {
|
|
1679
|
+
log.debug(`load returns raw content for ${filename}`);
|
|
1680
|
+
return import_fs6.default.readFileSync(filename, "utf-8");
|
|
1585
1681
|
}
|
|
1586
1682
|
}
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1683
|
+
},
|
|
1684
|
+
async resolveId(importee, importer, opts) {
|
|
1685
|
+
const ssr = !!(opts == null ? void 0 : opts.ssr);
|
|
1686
|
+
const svelteRequest = requestParser(importee, ssr);
|
|
1687
|
+
if (svelteRequest == null ? void 0 : svelteRequest.query.svelte) {
|
|
1688
|
+
if (svelteRequest.query.type === "style") {
|
|
1689
|
+
log.debug(`resolveId resolved virtual css module ${svelteRequest.cssId}`);
|
|
1690
|
+
return svelteRequest.cssId;
|
|
1691
|
+
}
|
|
1692
|
+
log.debug(`resolveId resolved ${importee}`);
|
|
1693
|
+
return importee;
|
|
1590
1694
|
}
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1695
|
+
if (ssr && importee === "svelte") {
|
|
1696
|
+
if (!resolvedSvelteSSR) {
|
|
1697
|
+
resolvedSvelteSSR = this.resolve("svelte/ssr", void 0, { skipSelf: true }).then((svelteSSR) => {
|
|
1698
|
+
log.debug("resolved svelte to svelte/ssr");
|
|
1699
|
+
return svelteSSR;
|
|
1700
|
+
}, (err) => {
|
|
1701
|
+
log.debug("failed to resolve svelte to svelte/ssr. Update svelte to a version that exports it", err);
|
|
1702
|
+
return null;
|
|
1703
|
+
});
|
|
1704
|
+
}
|
|
1705
|
+
return resolvedSvelteSSR;
|
|
1600
1706
|
}
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
}, (err) => {
|
|
1610
|
-
log.debug("failed to resolve svelte to svelte/ssr. Update svelte to a version that exports it", err);
|
|
1611
|
-
return null;
|
|
1612
|
-
});
|
|
1707
|
+
try {
|
|
1708
|
+
const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
|
|
1709
|
+
if (resolved) {
|
|
1710
|
+
log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
|
|
1711
|
+
return resolved;
|
|
1712
|
+
}
|
|
1713
|
+
} catch (e) {
|
|
1714
|
+
log.debug.once(`error trying to resolve ${importee} from ${importer} via package.json svelte field `, e);
|
|
1613
1715
|
}
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
const
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
return
|
|
1716
|
+
},
|
|
1717
|
+
async transform(code, id, opts) {
|
|
1718
|
+
var _a;
|
|
1719
|
+
const ssr = !!(opts == null ? void 0 : opts.ssr);
|
|
1720
|
+
const svelteRequest = requestParser(id, ssr);
|
|
1721
|
+
if (!svelteRequest || svelteRequest.query.svelte) {
|
|
1722
|
+
return;
|
|
1621
1723
|
}
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
compileData.dependencies.forEach((d) => {
|
|
1643
|
-
ensureWatchedFile(options.server.watcher, d, options.root);
|
|
1724
|
+
let compileData;
|
|
1725
|
+
try {
|
|
1726
|
+
compileData = await compileSvelte2(svelteRequest, code, options);
|
|
1727
|
+
} catch (e) {
|
|
1728
|
+
throw toRollupError(e, options);
|
|
1729
|
+
}
|
|
1730
|
+
logCompilerWarnings(compileData.compiled.warnings, options);
|
|
1731
|
+
cache.update(compileData);
|
|
1732
|
+
if (((_a = compileData.dependencies) == null ? void 0 : _a.length) && options.server) {
|
|
1733
|
+
compileData.dependencies.forEach((d) => {
|
|
1734
|
+
ensureWatchedFile(options.server.watcher, d, options.root);
|
|
1735
|
+
});
|
|
1736
|
+
}
|
|
1737
|
+
log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
|
|
1738
|
+
return __spreadProps(__spreadValues({}, compileData.compiled.js), {
|
|
1739
|
+
meta: {
|
|
1740
|
+
vite: {
|
|
1741
|
+
lang: compileData.lang
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1644
1744
|
});
|
|
1645
|
-
}
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
if (svelteRequest) {
|
|
1655
|
-
return handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options);
|
|
1745
|
+
},
|
|
1746
|
+
handleHotUpdate(ctx) {
|
|
1747
|
+
if (!options.hot || !options.emitCss) {
|
|
1748
|
+
return;
|
|
1749
|
+
}
|
|
1750
|
+
const svelteRequest = requestParser(ctx.file, false, ctx.timestamp);
|
|
1751
|
+
if (svelteRequest) {
|
|
1752
|
+
return handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options);
|
|
1753
|
+
}
|
|
1656
1754
|
}
|
|
1657
1755
|
}
|
|
1658
|
-
|
|
1756
|
+
];
|
|
1757
|
+
plugins.push(svelteInspector());
|
|
1758
|
+
return plugins.filter(Boolean);
|
|
1659
1759
|
}
|
|
1660
|
-
module.exports = __toCommonJS(src_exports);
|
|
1661
1760
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1662
1761
|
0 && (module.exports = {
|
|
1663
1762
|
svelte
|