@sveltejs/vite-plugin-svelte 1.0.0-next.31 → 1.0.0-next.35
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 +432 -355
- package/dist/index.cjs.map +1 -7
- package/dist/index.js +416 -348
- package/dist/index.js.map +1 -7
- package/package.json +11 -11
- package/src/index.ts +25 -28
- package/src/utils/dependencies.ts +11 -3
- package/src/utils/esbuild.ts +3 -0
- package/src/utils/optimizer.ts +43 -0
- package/src/utils/options.ts +98 -87
- package/src/utils/preprocess.ts +5 -8
- package/src/utils/resolve.ts +31 -10
- package/src/utils/vite-plugin-svelte-cache.ts +20 -0
- package/src/utils/watch.ts +7 -7
package/dist/index.js
CHANGED
|
@@ -89,7 +89,7 @@ function _log(logger, message, payload) {
|
|
|
89
89
|
function createLogger(level) {
|
|
90
90
|
const logger = loggers[level];
|
|
91
91
|
const logFn = _log.bind(null, logger);
|
|
92
|
-
const logged = new Set();
|
|
92
|
+
const logged = /* @__PURE__ */ new Set();
|
|
93
93
|
const once = function(message, payload) {
|
|
94
94
|
if (logged.has(message)) {
|
|
95
95
|
return;
|
|
@@ -181,7 +181,7 @@ async function handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, option
|
|
|
181
181
|
const content = await read();
|
|
182
182
|
const compileData = await compileSvelte2(svelteRequest, content, options);
|
|
183
183
|
cache.update(compileData);
|
|
184
|
-
const affectedModules = new Set();
|
|
184
|
+
const affectedModules = /* @__PURE__ */ new Set();
|
|
185
185
|
const cssModule = server.moduleGraph.getModuleById(svelteRequest.cssId);
|
|
186
186
|
const mainModule = server.moduleGraph.getModuleById(svelteRequest.id);
|
|
187
187
|
const cssUpdated = cssModule && cssChanged(cachedCss, compileData.compiled.css);
|
|
@@ -245,16 +245,14 @@ import { compile, preprocess, walk } from "svelte/compiler";
|
|
|
245
245
|
import { createMakeHot } from "svelte-hmr";
|
|
246
246
|
|
|
247
247
|
// src/utils/hash.ts
|
|
248
|
-
import
|
|
249
|
-
|
|
250
|
-
} from "crypto";
|
|
251
|
-
var hashes = Object.create(null);
|
|
248
|
+
import * as crypto from "crypto";
|
|
249
|
+
var hashes = /* @__PURE__ */ Object.create(null);
|
|
252
250
|
var hash_length = 12;
|
|
253
251
|
function safeBase64Hash(input) {
|
|
254
252
|
if (hashes[input]) {
|
|
255
253
|
return hashes[input];
|
|
256
254
|
}
|
|
257
|
-
const md5 = createHash("md5");
|
|
255
|
+
const md5 = crypto.createHash("md5");
|
|
258
256
|
md5.update(input);
|
|
259
257
|
const hash = toSafe(md5.digest("base64")).substr(0, hash_length);
|
|
260
258
|
hashes[input] = hash;
|
|
@@ -357,9 +355,7 @@ function createCompileSvelte(options) {
|
|
|
357
355
|
// src/utils/id.ts
|
|
358
356
|
import { createFilter } from "@rollup/pluginutils";
|
|
359
357
|
import { normalizePath } from "vite";
|
|
360
|
-
import
|
|
361
|
-
existsSync
|
|
362
|
-
} from "fs";
|
|
358
|
+
import * as fs from "fs";
|
|
363
359
|
var VITE_FS_PREFIX = "/@fs/";
|
|
364
360
|
var IS_WINDOWS = process.platform === "win32";
|
|
365
361
|
function splitId(id) {
|
|
@@ -413,7 +409,7 @@ function existsInRoot(filename, root) {
|
|
|
413
409
|
if (filename.startsWith(VITE_FS_PREFIX)) {
|
|
414
410
|
return false;
|
|
415
411
|
}
|
|
416
|
-
return existsSync(root + filename);
|
|
412
|
+
return fs.existsSync(root + filename);
|
|
417
413
|
}
|
|
418
414
|
function stripRoot(normalizedFilename, normalizedRoot) {
|
|
419
415
|
return normalizedFilename.startsWith(normalizedRoot + "/") ? normalizedFilename.slice(normalizedRoot.length) : normalizedFilename;
|
|
@@ -435,7 +431,9 @@ function buildIdParser(options) {
|
|
|
435
431
|
}
|
|
436
432
|
|
|
437
433
|
// src/utils/options.ts
|
|
438
|
-
import {
|
|
434
|
+
import {
|
|
435
|
+
normalizePath as normalizePath2
|
|
436
|
+
} from "vite";
|
|
439
437
|
|
|
440
438
|
// src/utils/load-svelte-config.ts
|
|
441
439
|
import { createRequire } from "module";
|
|
@@ -560,7 +558,7 @@ function findRootSvelteDependencies(root, cwdFallback = true) {
|
|
|
560
558
|
].filter((dep) => !is_common_without_svelte_field(dep));
|
|
561
559
|
return getSvelteDependencies(deps, root);
|
|
562
560
|
}
|
|
563
|
-
function getSvelteDependencies(deps, pkgDir,
|
|
561
|
+
function getSvelteDependencies(deps, pkgDir, path7 = []) {
|
|
564
562
|
const result = [];
|
|
565
563
|
const localRequire = createRequire2(`${pkgDir}/package.json`);
|
|
566
564
|
const resolvedDeps = deps.map((dep) => resolveDependencyData(dep, localRequire)).filter(Boolean);
|
|
@@ -568,18 +566,18 @@ function getSvelteDependencies(deps, pkgDir, path6 = []) {
|
|
|
568
566
|
const type = getSvelteDependencyType(pkg);
|
|
569
567
|
if (!type)
|
|
570
568
|
continue;
|
|
571
|
-
result.push({ name: pkg.name, type, pkg, dir, path:
|
|
569
|
+
result.push({ name: pkg.name, type, pkg, dir, path: path7 });
|
|
572
570
|
if (type === "component-library" && pkg.dependencies) {
|
|
573
571
|
let dependencyNames = Object.keys(pkg.dependencies);
|
|
574
|
-
const circular = dependencyNames.filter((name) =>
|
|
572
|
+
const circular = dependencyNames.filter((name) => path7.includes(name));
|
|
575
573
|
if (circular.length > 0) {
|
|
576
|
-
log.warn.enabled && log.warn(`skipping circular svelte dependencies in automated vite optimizeDeps handling`, circular.map((x) =>
|
|
577
|
-
dependencyNames = dependencyNames.filter((name) => !
|
|
574
|
+
log.warn.enabled && log.warn(`skipping circular svelte dependencies in automated vite optimizeDeps handling`, circular.map((x) => path7.concat(x).join(">")));
|
|
575
|
+
dependencyNames = dependencyNames.filter((name) => !path7.includes(name));
|
|
578
576
|
}
|
|
579
|
-
if (
|
|
580
|
-
log.debug.once(`encountered deep svelte dependency tree: ${
|
|
577
|
+
if (path7.length === 3) {
|
|
578
|
+
log.debug.once(`encountered deep svelte dependency tree: ${path7.join(">")}`);
|
|
581
579
|
}
|
|
582
|
-
result.push(...getSvelteDependencies(dependencyNames, dir,
|
|
580
|
+
result.push(...getSvelteDependencies(dependencyNames, dir, path7.concat(pkg.name)));
|
|
583
581
|
}
|
|
584
582
|
}
|
|
585
583
|
return result;
|
|
@@ -678,7 +676,11 @@ function needsOptimization(dep, localRequire) {
|
|
|
678
676
|
if (!depData)
|
|
679
677
|
return false;
|
|
680
678
|
const pkg = depData.pkg;
|
|
681
|
-
|
|
679
|
+
const isCjs = pkg.main && !pkg.module && !pkg.exports;
|
|
680
|
+
if (!isCjs)
|
|
681
|
+
return false;
|
|
682
|
+
const entryExt = path2.extname(pkg.main);
|
|
683
|
+
return !entryExt || entryExt === ".js" || entryExt === ".cjs";
|
|
682
684
|
}
|
|
683
685
|
|
|
684
686
|
// src/utils/options.ts
|
|
@@ -739,6 +741,7 @@ function formatFrameForVite(frame) {
|
|
|
739
741
|
}
|
|
740
742
|
|
|
741
743
|
// src/utils/esbuild.ts
|
|
744
|
+
var facadeEsbuildSveltePluginName = "vite-plugin-svelte:facade";
|
|
742
745
|
function esbuildSveltePlugin(options) {
|
|
743
746
|
return {
|
|
744
747
|
name: "vite-plugin-svelte:optimize-svelte",
|
|
@@ -780,6 +783,7 @@ async function compileSvelte(options, { filename, code }) {
|
|
|
780
783
|
const compileOptions = __spreadProps(__spreadValues({}, options.compilerOptions), {
|
|
781
784
|
css: true,
|
|
782
785
|
filename,
|
|
786
|
+
format: "esm",
|
|
783
787
|
generate: "dom"
|
|
784
788
|
});
|
|
785
789
|
let preprocessed;
|
|
@@ -802,8 +806,237 @@ async function compileSvelte(options, { filename, code }) {
|
|
|
802
806
|
return compiled.js.code + "//# sourceMappingURL=" + compiled.js.map.toUrl();
|
|
803
807
|
}
|
|
804
808
|
|
|
809
|
+
// src/utils/preprocess.ts
|
|
810
|
+
import {
|
|
811
|
+
transformWithEsbuild
|
|
812
|
+
} from "vite";
|
|
813
|
+
import MagicString2 from "magic-string";
|
|
814
|
+
import { preprocess as preprocess3 } from "svelte/compiler";
|
|
815
|
+
|
|
816
|
+
// src/utils/sourcemap.ts
|
|
817
|
+
import MagicString from "magic-string";
|
|
818
|
+
async function buildMagicString(from, to, options) {
|
|
819
|
+
let diff_match_patch, DIFF_DELETE, DIFF_INSERT;
|
|
820
|
+
try {
|
|
821
|
+
const dmpPkg = await import("diff-match-patch");
|
|
822
|
+
diff_match_patch = dmpPkg.diff_match_patch;
|
|
823
|
+
DIFF_INSERT = dmpPkg.DIFF_INSERT;
|
|
824
|
+
DIFF_DELETE = dmpPkg.DIFF_DELETE;
|
|
825
|
+
} catch (e) {
|
|
826
|
+
log.error.once('Failed to import optional dependency "diff-match-patch". Please install it to enable generated sourcemaps.');
|
|
827
|
+
return null;
|
|
828
|
+
}
|
|
829
|
+
const dmp = new diff_match_patch();
|
|
830
|
+
const diffs = dmp.diff_main(from, to);
|
|
831
|
+
dmp.diff_cleanupSemantic(diffs);
|
|
832
|
+
const m = new MagicString(from, options);
|
|
833
|
+
let pos = 0;
|
|
834
|
+
for (let i = 0; i < diffs.length; i++) {
|
|
835
|
+
const diff = diffs[i];
|
|
836
|
+
const nextDiff = diffs[i + 1];
|
|
837
|
+
if (diff[0] === DIFF_DELETE) {
|
|
838
|
+
if ((nextDiff == null ? void 0 : nextDiff[0]) === DIFF_INSERT) {
|
|
839
|
+
m.overwrite(pos, pos + diff[1].length, nextDiff[1]);
|
|
840
|
+
i++;
|
|
841
|
+
} else {
|
|
842
|
+
m.remove(pos, pos + diff[1].length);
|
|
843
|
+
}
|
|
844
|
+
pos += diff[1].length;
|
|
845
|
+
} else if (diff[0] === DIFF_INSERT) {
|
|
846
|
+
if (nextDiff) {
|
|
847
|
+
m.appendRight(pos, diff[1]);
|
|
848
|
+
} else {
|
|
849
|
+
m.append(diff[1]);
|
|
850
|
+
}
|
|
851
|
+
} else {
|
|
852
|
+
pos += diff[1].length;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
return m;
|
|
856
|
+
}
|
|
857
|
+
async function buildSourceMap(from, to, filename) {
|
|
858
|
+
const m = await buildMagicString(from, to, { filename });
|
|
859
|
+
return m ? m.generateDecodedMap({ source: filename, hires: true, includeContent: false }) : null;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// src/utils/preprocess.ts
|
|
863
|
+
var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"];
|
|
864
|
+
var supportedScriptLangs = ["ts"];
|
|
865
|
+
function createViteScriptPreprocessor() {
|
|
866
|
+
return async ({ attributes, content, filename = "" }) => {
|
|
867
|
+
const lang = attributes.lang;
|
|
868
|
+
if (!supportedScriptLangs.includes(lang))
|
|
869
|
+
return;
|
|
870
|
+
const transformResult = await transformWithEsbuild(content, filename, {
|
|
871
|
+
loader: lang,
|
|
872
|
+
tsconfigRaw: {
|
|
873
|
+
compilerOptions: {
|
|
874
|
+
importsNotUsedAsValues: "preserve",
|
|
875
|
+
preserveValueImports: true
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
});
|
|
879
|
+
return {
|
|
880
|
+
code: transformResult.code,
|
|
881
|
+
map: transformResult.map
|
|
882
|
+
};
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
function createViteStylePreprocessor(config) {
|
|
886
|
+
const pluginName = "vite:css";
|
|
887
|
+
const plugin = config.plugins.find((p) => p.name === pluginName);
|
|
888
|
+
if (!plugin) {
|
|
889
|
+
throw new Error(`failed to find plugin ${pluginName}`);
|
|
890
|
+
}
|
|
891
|
+
if (!plugin.transform) {
|
|
892
|
+
throw new Error(`plugin ${pluginName} has no transform`);
|
|
893
|
+
}
|
|
894
|
+
const pluginTransform = plugin.transform.bind(null);
|
|
895
|
+
return async ({ attributes, content, filename = "" }) => {
|
|
896
|
+
var _a, _b;
|
|
897
|
+
const lang = attributes.lang;
|
|
898
|
+
if (!supportedStyleLangs.includes(lang))
|
|
899
|
+
return;
|
|
900
|
+
const moduleId = `${filename}.${lang}`;
|
|
901
|
+
const transformResult = await pluginTransform(content, moduleId);
|
|
902
|
+
if (((_b = (_a = transformResult.map) == null ? void 0 : _a.sources) == null ? void 0 : _b[0]) === moduleId) {
|
|
903
|
+
transformResult.map.sources[0] = filename;
|
|
904
|
+
}
|
|
905
|
+
return {
|
|
906
|
+
code: transformResult.code,
|
|
907
|
+
map: transformResult.map ?? void 0
|
|
908
|
+
};
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
function createVitePreprocessorGroup(config) {
|
|
912
|
+
return {
|
|
913
|
+
markup({ content, filename }) {
|
|
914
|
+
return preprocess3(content, {
|
|
915
|
+
script: createViteScriptPreprocessor(),
|
|
916
|
+
style: createViteStylePreprocessor(config)
|
|
917
|
+
}, { filename });
|
|
918
|
+
}
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
function createInjectScopeEverythingRulePreprocessorGroup() {
|
|
922
|
+
return {
|
|
923
|
+
style({ content, filename }) {
|
|
924
|
+
const s = new MagicString2(content);
|
|
925
|
+
s.append(" *{}");
|
|
926
|
+
return {
|
|
927
|
+
code: s.toString(),
|
|
928
|
+
map: s.generateDecodedMap({ source: filename, hires: true })
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
function buildExtraPreprocessors(options, config) {
|
|
934
|
+
var _a, _b;
|
|
935
|
+
const prependPreprocessors = [];
|
|
936
|
+
const appendPreprocessors = [];
|
|
937
|
+
if ((_a = options.experimental) == null ? void 0 : _a.useVitePreprocess) {
|
|
938
|
+
log.debug("adding vite preprocessor");
|
|
939
|
+
prependPreprocessors.push(createVitePreprocessorGroup(config));
|
|
940
|
+
}
|
|
941
|
+
const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p == null ? void 0 : p.sveltePreprocess);
|
|
942
|
+
if (pluginsWithPreprocessorsDeprecated.length > 0) {
|
|
943
|
+
log.warn(`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(", ")}`);
|
|
944
|
+
pluginsWithPreprocessorsDeprecated.forEach((p) => {
|
|
945
|
+
if (!p.api) {
|
|
946
|
+
p.api = {};
|
|
947
|
+
}
|
|
948
|
+
if (p.api.sveltePreprocess === void 0) {
|
|
949
|
+
p.api.sveltePreprocess = p.sveltePreprocess;
|
|
950
|
+
} else {
|
|
951
|
+
log.error(`ignoring plugin.sveltePreprocess of ${p.name} because it already defined plugin.api.sveltePreprocess.`);
|
|
952
|
+
}
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
const pluginsWithPreprocessors = config.plugins.filter((p) => {
|
|
956
|
+
var _a2;
|
|
957
|
+
return (_a2 = p == null ? void 0 : p.api) == null ? void 0 : _a2.sveltePreprocess;
|
|
958
|
+
});
|
|
959
|
+
const ignored = [], included = [];
|
|
960
|
+
for (const p of pluginsWithPreprocessors) {
|
|
961
|
+
if (options.ignorePluginPreprocessors === true || Array.isArray(options.ignorePluginPreprocessors) && ((_b = options.ignorePluginPreprocessors) == null ? void 0 : _b.includes(p.name))) {
|
|
962
|
+
ignored.push(p);
|
|
963
|
+
} else {
|
|
964
|
+
included.push(p);
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
if (ignored.length > 0) {
|
|
968
|
+
log.debug(`Ignoring svelte preprocessors defined by these vite plugins: ${ignored.map((p) => p.name).join(", ")}`);
|
|
969
|
+
}
|
|
970
|
+
if (included.length > 0) {
|
|
971
|
+
log.debug(`Adding svelte preprocessors defined by these vite plugins: ${included.map((p) => p.name).join(", ")}`);
|
|
972
|
+
appendPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess));
|
|
973
|
+
}
|
|
974
|
+
if (options.hot && options.emitCss) {
|
|
975
|
+
appendPreprocessors.push(createInjectScopeEverythingRulePreprocessorGroup());
|
|
976
|
+
}
|
|
977
|
+
return { prependPreprocessors, appendPreprocessors };
|
|
978
|
+
}
|
|
979
|
+
function addExtraPreprocessors(options, config) {
|
|
980
|
+
var _a;
|
|
981
|
+
const { prependPreprocessors, appendPreprocessors } = buildExtraPreprocessors(options, config);
|
|
982
|
+
if (prependPreprocessors.length > 0 || appendPreprocessors.length > 0) {
|
|
983
|
+
if (!options.preprocess) {
|
|
984
|
+
options.preprocess = [...prependPreprocessors, ...appendPreprocessors];
|
|
985
|
+
} else if (Array.isArray(options.preprocess)) {
|
|
986
|
+
options.preprocess.unshift(...prependPreprocessors);
|
|
987
|
+
options.preprocess.push(...appendPreprocessors);
|
|
988
|
+
} else {
|
|
989
|
+
options.preprocess = [...prependPreprocessors, options.preprocess, ...appendPreprocessors];
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
const generateMissingSourceMaps = !!((_a = options.experimental) == null ? void 0 : _a.generateMissingPreprocessorSourcemaps);
|
|
993
|
+
if (options.preprocess && generateMissingSourceMaps) {
|
|
994
|
+
options.preprocess = Array.isArray(options.preprocess) ? options.preprocess.map((p, i) => validateSourceMapOutputWrapper(p, i)) : validateSourceMapOutputWrapper(options.preprocess, 0);
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
function validateSourceMapOutputWrapper(group, i) {
|
|
998
|
+
const wrapper = {};
|
|
999
|
+
for (const [processorType, processorFn] of Object.entries(group)) {
|
|
1000
|
+
wrapper[processorType] = async (options) => {
|
|
1001
|
+
var _a;
|
|
1002
|
+
const result = await processorFn(options);
|
|
1003
|
+
if (result && result.code !== options.content) {
|
|
1004
|
+
let invalidMap = false;
|
|
1005
|
+
if (!result.map) {
|
|
1006
|
+
invalidMap = true;
|
|
1007
|
+
log.warn.enabled && log.warn.once(`preprocessor at index ${i} did not return a sourcemap for ${processorType} transform`, {
|
|
1008
|
+
filename: options.filename,
|
|
1009
|
+
type: processorType,
|
|
1010
|
+
processor: processorFn.toString()
|
|
1011
|
+
});
|
|
1012
|
+
} else if (((_a = result.map) == null ? void 0 : _a.mappings) === "") {
|
|
1013
|
+
invalidMap = true;
|
|
1014
|
+
log.warn.enabled && log.warn.once(`preprocessor at index ${i} returned an invalid empty sourcemap for ${processorType} transform`, {
|
|
1015
|
+
filename: options.filename,
|
|
1016
|
+
type: processorType,
|
|
1017
|
+
processor: processorFn.toString()
|
|
1018
|
+
});
|
|
1019
|
+
}
|
|
1020
|
+
if (invalidMap) {
|
|
1021
|
+
try {
|
|
1022
|
+
const map = await buildSourceMap(options.content, result.code, options.filename);
|
|
1023
|
+
if (map) {
|
|
1024
|
+
log.debug.enabled && log.debug(`adding generated sourcemap to preprocesor result for ${options.filename}`);
|
|
1025
|
+
result.map = map;
|
|
1026
|
+
}
|
|
1027
|
+
} catch (e) {
|
|
1028
|
+
log.error(`failed to build sourcemap`, e);
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
return result;
|
|
1033
|
+
};
|
|
1034
|
+
}
|
|
1035
|
+
return wrapper;
|
|
1036
|
+
}
|
|
1037
|
+
|
|
805
1038
|
// src/utils/options.ts
|
|
806
|
-
var knownOptions = new Set([
|
|
1039
|
+
var knownOptions = /* @__PURE__ */ new Set([
|
|
807
1040
|
"configFile",
|
|
808
1041
|
"include",
|
|
809
1042
|
"exclude",
|
|
@@ -817,45 +1050,70 @@ var knownOptions = new Set([
|
|
|
817
1050
|
"disableDependencyReinclusion",
|
|
818
1051
|
"experimental"
|
|
819
1052
|
]);
|
|
820
|
-
function buildDefaultOptions(isProduction, emitCss = true) {
|
|
821
|
-
const hot = isProduction ? false : {
|
|
822
|
-
injectCss: !emitCss
|
|
823
|
-
};
|
|
824
|
-
const defaultOptions = {
|
|
825
|
-
extensions: [".svelte"],
|
|
826
|
-
hot,
|
|
827
|
-
emitCss,
|
|
828
|
-
compilerOptions: {
|
|
829
|
-
format: "esm",
|
|
830
|
-
css: !emitCss,
|
|
831
|
-
dev: !isProduction
|
|
832
|
-
}
|
|
833
|
-
};
|
|
834
|
-
log.debug(`default options for ${isProduction ? "production" : "development"}`, defaultOptions);
|
|
835
|
-
return defaultOptions;
|
|
836
|
-
}
|
|
837
1053
|
function validateInlineOptions(inlineOptions) {
|
|
838
1054
|
const invalidKeys = Object.keys(inlineOptions || {}).filter((key) => !knownOptions.has(key));
|
|
839
1055
|
if (invalidKeys.length) {
|
|
840
1056
|
log.warn(`invalid plugin options "${invalidKeys.join(", ")}" in config`, inlineOptions);
|
|
841
1057
|
}
|
|
842
1058
|
}
|
|
843
|
-
function
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
1059
|
+
async function preResolveOptions(inlineOptions = {}, viteUserConfig, viteEnv) {
|
|
1060
|
+
const viteConfigWithResolvedRoot = __spreadProps(__spreadValues({}, viteUserConfig), {
|
|
1061
|
+
root: resolveViteRoot(viteUserConfig)
|
|
1062
|
+
});
|
|
1063
|
+
const defaultOptions = {
|
|
1064
|
+
extensions: [".svelte"],
|
|
1065
|
+
emitCss: true,
|
|
1066
|
+
compilerOptions: {
|
|
1067
|
+
format: "esm"
|
|
848
1068
|
}
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
1069
|
+
};
|
|
1070
|
+
const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions);
|
|
1071
|
+
const merged = __spreadProps(__spreadValues(__spreadValues(__spreadValues({}, defaultOptions), svelteConfig), inlineOptions), {
|
|
1072
|
+
compilerOptions: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.compilerOptions), svelteConfig == null ? void 0 : svelteConfig.compilerOptions), inlineOptions == null ? void 0 : inlineOptions.compilerOptions),
|
|
1073
|
+
experimental: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.experimental), svelteConfig == null ? void 0 : svelteConfig.experimental), inlineOptions == null ? void 0 : inlineOptions.experimental),
|
|
1074
|
+
root: viteConfigWithResolvedRoot.root,
|
|
1075
|
+
isBuild: viteEnv.command === "build",
|
|
1076
|
+
isServe: viteEnv.command === "serve"
|
|
1077
|
+
});
|
|
1078
|
+
if (svelteConfig == null ? void 0 : svelteConfig.configFile) {
|
|
1079
|
+
merged.configFile = svelteConfig.configFile;
|
|
1080
|
+
}
|
|
1081
|
+
return merged;
|
|
1082
|
+
}
|
|
1083
|
+
function resolveOptions(preResolveOptions2, viteConfig) {
|
|
1084
|
+
const defaultOptions = {
|
|
1085
|
+
hot: viteConfig.isProduction ? false : { injectCss: !preResolveOptions2.emitCss },
|
|
1086
|
+
compilerOptions: {
|
|
1087
|
+
css: !preResolveOptions2.emitCss,
|
|
1088
|
+
dev: !viteConfig.isProduction
|
|
1089
|
+
}
|
|
1090
|
+
};
|
|
1091
|
+
const merged = __spreadProps(__spreadValues(__spreadValues({}, defaultOptions), preResolveOptions2), {
|
|
1092
|
+
compilerOptions: __spreadValues(__spreadValues({}, defaultOptions.compilerOptions), preResolveOptions2.compilerOptions),
|
|
1093
|
+
root: viteConfig.root,
|
|
1094
|
+
isProduction: viteConfig.isProduction
|
|
1095
|
+
});
|
|
1096
|
+
addExtraPreprocessors(merged, viteConfig);
|
|
1097
|
+
enforceOptionsForHmr(merged);
|
|
1098
|
+
enforceOptionsForProduction(merged);
|
|
1099
|
+
return merged;
|
|
1100
|
+
}
|
|
1101
|
+
function enforceOptionsForHmr(options) {
|
|
1102
|
+
if (options.hot) {
|
|
1103
|
+
if (!options.compilerOptions.dev) {
|
|
1104
|
+
log.warn("hmr is enabled but compilerOptions.dev is false, forcing it to true");
|
|
1105
|
+
options.compilerOptions.dev = true;
|
|
1106
|
+
}
|
|
1107
|
+
if (options.emitCss) {
|
|
1108
|
+
if (options.hot !== true && options.hot.injectCss) {
|
|
1109
|
+
log.warn("hmr and emitCss are enabled but hot.injectCss is true, forcing it to false");
|
|
1110
|
+
options.hot.injectCss = false;
|
|
1111
|
+
}
|
|
1112
|
+
if (options.compilerOptions.css) {
|
|
1113
|
+
log.warn("hmr and emitCss are enabled but compilerOptions.css is true, forcing it to false");
|
|
1114
|
+
options.compilerOptions.css = false;
|
|
1115
|
+
}
|
|
1116
|
+
} else {
|
|
859
1117
|
if (options.hot === true || !options.hot.injectCss) {
|
|
860
1118
|
log.warn("hmr with emitCss disabled requires option hot.injectCss to be enabled, forcing it to true");
|
|
861
1119
|
if (options.hot === true) {
|
|
@@ -883,31 +1141,6 @@ function enforceOptionsForProduction(options) {
|
|
|
883
1141
|
}
|
|
884
1142
|
}
|
|
885
1143
|
}
|
|
886
|
-
function mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfig, viteEnv) {
|
|
887
|
-
const merged = __spreadProps(__spreadValues(__spreadValues(__spreadValues({}, defaultOptions), svelteConfig), inlineOptions), {
|
|
888
|
-
compilerOptions: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions.compilerOptions), (svelteConfig == null ? void 0 : svelteConfig.compilerOptions) || {}), (inlineOptions == null ? void 0 : inlineOptions.compilerOptions) || {}),
|
|
889
|
-
experimental: __spreadValues(__spreadValues({}, (svelteConfig == null ? void 0 : svelteConfig.experimental) || {}), (inlineOptions == null ? void 0 : inlineOptions.experimental) || {}),
|
|
890
|
-
root: viteConfig.root,
|
|
891
|
-
isProduction: viteEnv.mode === "production",
|
|
892
|
-
isBuild: viteEnv.command === "build",
|
|
893
|
-
isServe: viteEnv.command === "serve"
|
|
894
|
-
});
|
|
895
|
-
if (svelteConfig == null ? void 0 : svelteConfig.configFile) {
|
|
896
|
-
merged.configFile = svelteConfig.configFile;
|
|
897
|
-
}
|
|
898
|
-
return merged;
|
|
899
|
-
}
|
|
900
|
-
async function resolveOptions(inlineOptions = {}, viteConfig, viteEnv) {
|
|
901
|
-
const viteConfigWithResolvedRoot = __spreadProps(__spreadValues({}, viteConfig), {
|
|
902
|
-
root: resolveViteRoot(viteConfig)
|
|
903
|
-
});
|
|
904
|
-
const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions) || {};
|
|
905
|
-
const defaultOptions = buildDefaultOptions(viteEnv.mode === "production", inlineOptions.emitCss ?? svelteConfig.emitCss);
|
|
906
|
-
const resolvedOptions = mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfigWithResolvedRoot, viteEnv);
|
|
907
|
-
enforceOptionsForProduction(resolvedOptions);
|
|
908
|
-
enforceOptionsForHmr(resolvedOptions);
|
|
909
|
-
return resolvedOptions;
|
|
910
|
-
}
|
|
911
1144
|
function resolveViteRoot(viteConfig) {
|
|
912
1145
|
return normalizePath2(viteConfig.root ? path3.resolve(viteConfig.root) : process.cwd());
|
|
913
1146
|
}
|
|
@@ -925,16 +1158,16 @@ function buildExtraViteConfig(options, config, configEnv) {
|
|
|
925
1158
|
extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config);
|
|
926
1159
|
return extraViteConfig;
|
|
927
1160
|
}
|
|
928
|
-
function buildOptimizeDepsForSvelte(svelteDeps, options,
|
|
1161
|
+
function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps2) {
|
|
929
1162
|
const include = [];
|
|
930
1163
|
const exclude = ["svelte-hmr"];
|
|
931
1164
|
const isIncluded = (dep) => {
|
|
932
1165
|
var _a;
|
|
933
|
-
return include.includes(dep) || ((_a =
|
|
1166
|
+
return include.includes(dep) || ((_a = optimizeDeps2 == null ? void 0 : optimizeDeps2.include) == null ? void 0 : _a.includes(dep));
|
|
934
1167
|
};
|
|
935
1168
|
const isExcluded = (dep) => {
|
|
936
1169
|
var _a;
|
|
937
|
-
return exclude.includes(dep) || ((_a =
|
|
1170
|
+
return exclude.includes(dep) || ((_a = optimizeDeps2 == null ? void 0 : optimizeDeps2.exclude) == null ? void 0 : _a.some((id) => dep === id || id.startsWith(`${dep}/`)));
|
|
938
1171
|
};
|
|
939
1172
|
if (!isExcluded("svelte")) {
|
|
940
1173
|
const svelteImportsToInclude = SVELTE_IMPORTS.filter((x) => x !== "svelte/ssr");
|
|
@@ -948,7 +1181,8 @@ function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) {
|
|
|
948
1181
|
include,
|
|
949
1182
|
exclude,
|
|
950
1183
|
esbuildOptions: {
|
|
951
|
-
plugins: [
|
|
1184
|
+
plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {
|
|
1185
|
+
} }]
|
|
952
1186
|
}
|
|
953
1187
|
};
|
|
954
1188
|
}
|
|
@@ -988,14 +1222,22 @@ function buildSSROptionsForSvelte(svelteDeps, options, config) {
|
|
|
988
1222
|
noExternal
|
|
989
1223
|
};
|
|
990
1224
|
}
|
|
1225
|
+
function patchResolvedViteConfig(viteConfig, options) {
|
|
1226
|
+
var _a, _b;
|
|
1227
|
+
const facadeEsbuildSveltePlugin = (_b = (_a = viteConfig.optimizeDeps.esbuildOptions) == null ? void 0 : _a.plugins) == null ? void 0 : _b.find((plugin) => plugin.name === facadeEsbuildSveltePluginName);
|
|
1228
|
+
if (facadeEsbuildSveltePlugin) {
|
|
1229
|
+
Object.assign(facadeEsbuildSveltePlugin, esbuildSveltePlugin(options));
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
991
1232
|
|
|
992
1233
|
// src/utils/vite-plugin-svelte-cache.ts
|
|
993
1234
|
var VitePluginSvelteCache = class {
|
|
994
1235
|
constructor() {
|
|
995
|
-
this._css = new Map();
|
|
996
|
-
this._js = new Map();
|
|
997
|
-
this._dependencies = new Map();
|
|
998
|
-
this._dependants = new Map();
|
|
1236
|
+
this._css = /* @__PURE__ */ new Map();
|
|
1237
|
+
this._js = /* @__PURE__ */ new Map();
|
|
1238
|
+
this._dependencies = /* @__PURE__ */ new Map();
|
|
1239
|
+
this._dependants = /* @__PURE__ */ new Map();
|
|
1240
|
+
this._resolvedSvelteFields = /* @__PURE__ */ new Map();
|
|
999
1241
|
}
|
|
1000
1242
|
update(compileData) {
|
|
1001
1243
|
this.updateCSS(compileData);
|
|
@@ -1019,7 +1261,7 @@ var VitePluginSvelteCache = class {
|
|
|
1019
1261
|
const added = dependencies.filter((d) => !prevDependencies.includes(d));
|
|
1020
1262
|
added.forEach((d) => {
|
|
1021
1263
|
if (!this._dependants.has(d)) {
|
|
1022
|
-
this._dependants.set(d, new Set());
|
|
1264
|
+
this._dependants.set(d, /* @__PURE__ */ new Set());
|
|
1023
1265
|
}
|
|
1024
1266
|
this._dependants.get(d).add(compileData.filename);
|
|
1025
1267
|
});
|
|
@@ -1057,10 +1299,19 @@ var VitePluginSvelteCache = class {
|
|
|
1057
1299
|
return this._js.get(svelteRequest.normalizedFilename);
|
|
1058
1300
|
}
|
|
1059
1301
|
}
|
|
1060
|
-
getDependants(
|
|
1061
|
-
const dependants = this._dependants.get(
|
|
1302
|
+
getDependants(path7) {
|
|
1303
|
+
const dependants = this._dependants.get(path7);
|
|
1062
1304
|
return dependants ? [...dependants] : [];
|
|
1063
1305
|
}
|
|
1306
|
+
getResolvedSvelteField(name, importer) {
|
|
1307
|
+
return this._resolvedSvelteFields.get(this._getResolvedSvelteFieldKey(name, importer));
|
|
1308
|
+
}
|
|
1309
|
+
setResolvedSvelteField(importee, importer = void 0, resolvedSvelte) {
|
|
1310
|
+
this._resolvedSvelteFields.set(this._getResolvedSvelteFieldKey(importee, importer), resolvedSvelte);
|
|
1311
|
+
}
|
|
1312
|
+
_getResolvedSvelteFieldKey(importee, importer) {
|
|
1313
|
+
return importer ? `${importer} > ${importee}` : importee;
|
|
1314
|
+
}
|
|
1064
1315
|
};
|
|
1065
1316
|
|
|
1066
1317
|
// src/utils/watch.ts
|
|
@@ -1072,7 +1323,7 @@ function setupWatchers(options, cache, requestParser) {
|
|
|
1072
1323
|
return;
|
|
1073
1324
|
}
|
|
1074
1325
|
const { watcher, ws } = server;
|
|
1075
|
-
const {
|
|
1326
|
+
const { root, server: serverConfig } = server.config;
|
|
1076
1327
|
const emitChangeEventOnDependants = (filename) => {
|
|
1077
1328
|
const dependants = cache.getDependants(filename);
|
|
1078
1329
|
dependants.forEach((dependant) => {
|
|
@@ -1092,16 +1343,16 @@ function setupWatchers(options, cache, requestParser) {
|
|
|
1092
1343
|
}
|
|
1093
1344
|
};
|
|
1094
1345
|
const triggerViteRestart = (filename) => {
|
|
1095
|
-
if (
|
|
1096
|
-
log.info(`svelte config changed: restarting vite server. - file: ${filename}`);
|
|
1097
|
-
watcher.emit("change", viteConfigFile);
|
|
1098
|
-
} else {
|
|
1346
|
+
if (serverConfig.middlewareMode) {
|
|
1099
1347
|
const message = "Svelte config change detected, restart your dev process to apply the changes.";
|
|
1100
1348
|
log.info(message, filename);
|
|
1101
1349
|
ws.send({
|
|
1102
1350
|
type: "error",
|
|
1103
1351
|
err: { message, stack: "", plugin: "vite-plugin-svelte", id: filename }
|
|
1104
1352
|
});
|
|
1353
|
+
} else {
|
|
1354
|
+
log.info(`svelte config changed: restarting vite server. - file: ${filename}`);
|
|
1355
|
+
server.restart();
|
|
1105
1356
|
}
|
|
1106
1357
|
};
|
|
1107
1358
|
const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path4.join(root, cfg));
|
|
@@ -1140,19 +1391,29 @@ function ensureWatchedFile(watcher, file, root) {
|
|
|
1140
1391
|
|
|
1141
1392
|
// src/utils/resolve.ts
|
|
1142
1393
|
import path5 from "path";
|
|
1143
|
-
import
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1394
|
+
import { createRequire as createRequire4 } from "module";
|
|
1395
|
+
function resolveViaPackageJsonSvelte(importee, importer, cache) {
|
|
1396
|
+
if (importer && isBareImport(importee) && !is_common_without_svelte_field(importee)) {
|
|
1397
|
+
const cached = cache.getResolvedSvelteField(importee, importer);
|
|
1398
|
+
if (cached) {
|
|
1399
|
+
return cached;
|
|
1400
|
+
}
|
|
1401
|
+
const localRequire = createRequire4(importer);
|
|
1402
|
+
const pkgData = resolveDependencyData(importee, localRequire);
|
|
1403
|
+
if (pkgData) {
|
|
1404
|
+
const { pkg, dir } = pkgData;
|
|
1405
|
+
if (pkg.svelte) {
|
|
1406
|
+
const result = path5.resolve(dir, pkg.svelte);
|
|
1407
|
+
cache.setResolvedSvelteField(importee, importer, result);
|
|
1408
|
+
return result;
|
|
1409
|
+
}
|
|
1410
|
+
} else {
|
|
1411
|
+
throw new Error(`failed to resolve package.json of ${importee} imported by ${importer}`);
|
|
1151
1412
|
}
|
|
1152
1413
|
}
|
|
1153
1414
|
}
|
|
1154
1415
|
function isBareImport(importee) {
|
|
1155
|
-
if (!importee || importee[0] === "." || importee[0] === "\0" || path5.isAbsolute(importee)) {
|
|
1416
|
+
if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || path5.isAbsolute(importee)) {
|
|
1156
1417
|
return false;
|
|
1157
1418
|
}
|
|
1158
1419
|
const parts = importee.split("/");
|
|
@@ -1166,233 +1427,42 @@ function isBareImport(importee) {
|
|
|
1166
1427
|
}
|
|
1167
1428
|
}
|
|
1168
1429
|
|
|
1169
|
-
// src/utils/
|
|
1170
|
-
import
|
|
1171
|
-
|
|
1172
|
-
} from "vite";
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
const diffs = dmp.diff_main(from, to);
|
|
1191
|
-
dmp.diff_cleanupSemantic(diffs);
|
|
1192
|
-
const m = new MagicString(from, options);
|
|
1193
|
-
let pos = 0;
|
|
1194
|
-
for (let i = 0; i < diffs.length; i++) {
|
|
1195
|
-
const diff = diffs[i];
|
|
1196
|
-
const nextDiff = diffs[i + 1];
|
|
1197
|
-
if (diff[0] === DIFF_DELETE) {
|
|
1198
|
-
if ((nextDiff == null ? void 0 : nextDiff[0]) === DIFF_INSERT) {
|
|
1199
|
-
m.overwrite(pos, pos + diff[1].length, nextDiff[1]);
|
|
1200
|
-
i++;
|
|
1201
|
-
} else {
|
|
1202
|
-
m.remove(pos, pos + diff[1].length);
|
|
1203
|
-
}
|
|
1204
|
-
pos += diff[1].length;
|
|
1205
|
-
} else if (diff[0] === DIFF_INSERT) {
|
|
1206
|
-
if (nextDiff) {
|
|
1207
|
-
m.appendRight(pos, diff[1]);
|
|
1208
|
-
} else {
|
|
1209
|
-
m.append(diff[1]);
|
|
1210
|
-
}
|
|
1211
|
-
} else {
|
|
1212
|
-
pos += diff[1].length;
|
|
1213
|
-
}
|
|
1214
|
-
}
|
|
1215
|
-
return m;
|
|
1216
|
-
}
|
|
1217
|
-
async function buildSourceMap(from, to, filename) {
|
|
1218
|
-
const m = await buildMagicString(from, to, { filename });
|
|
1219
|
-
return m ? m.generateDecodedMap({ source: filename, hires: true, includeContent: false }) : null;
|
|
1220
|
-
}
|
|
1221
|
-
|
|
1222
|
-
// src/utils/preprocess.ts
|
|
1223
|
-
var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"];
|
|
1224
|
-
var supportedScriptLangs = ["ts"];
|
|
1225
|
-
function createViteScriptPreprocessor() {
|
|
1226
|
-
return async ({ attributes, content, filename = "" }) => {
|
|
1227
|
-
const lang = attributes.lang;
|
|
1228
|
-
if (!supportedScriptLangs.includes(lang))
|
|
1229
|
-
return;
|
|
1230
|
-
const transformResult = await transformWithEsbuild(content, filename, {
|
|
1231
|
-
loader: lang,
|
|
1232
|
-
tsconfigRaw: {
|
|
1233
|
-
compilerOptions: {
|
|
1234
|
-
importsNotUsedAsValues: "preserve"
|
|
1235
|
-
}
|
|
1236
|
-
}
|
|
1237
|
-
});
|
|
1238
|
-
return {
|
|
1239
|
-
code: transformResult.code,
|
|
1240
|
-
map: transformResult.map
|
|
1241
|
-
};
|
|
1242
|
-
};
|
|
1243
|
-
}
|
|
1244
|
-
function createViteStylePreprocessor(config) {
|
|
1245
|
-
const pluginName = "vite:css";
|
|
1246
|
-
const plugin = config.plugins.find((p) => p.name === pluginName);
|
|
1247
|
-
if (!plugin) {
|
|
1248
|
-
throw new Error(`failed to find plugin ${pluginName}`);
|
|
1249
|
-
}
|
|
1250
|
-
if (!plugin.transform) {
|
|
1251
|
-
throw new Error(`plugin ${pluginName} has no transform`);
|
|
1252
|
-
}
|
|
1253
|
-
const pluginTransform = plugin.transform.bind(null);
|
|
1254
|
-
return async ({ attributes, content, filename = "" }) => {
|
|
1255
|
-
var _a, _b;
|
|
1256
|
-
const lang = attributes.lang;
|
|
1257
|
-
if (!supportedStyleLangs.includes(lang))
|
|
1258
|
-
return;
|
|
1259
|
-
const moduleId = `${filename}.${lang}`;
|
|
1260
|
-
const transformResult = await pluginTransform(content, moduleId);
|
|
1261
|
-
const hasMap = transformResult.map && transformResult.map.mappings !== "";
|
|
1262
|
-
if (hasMap && ((_b = (_a = transformResult.map) == null ? void 0 : _a.sources) == null ? void 0 : _b[0]) === moduleId) {
|
|
1263
|
-
transformResult.map.sources[0] = filename;
|
|
1264
|
-
}
|
|
1265
|
-
return {
|
|
1266
|
-
code: transformResult.code,
|
|
1267
|
-
map: hasMap ? transformResult.map : void 0
|
|
1268
|
-
};
|
|
1269
|
-
};
|
|
1270
|
-
}
|
|
1271
|
-
function createVitePreprocessorGroup(config) {
|
|
1272
|
-
return {
|
|
1273
|
-
markup({ content, filename }) {
|
|
1274
|
-
return preprocess3(content, {
|
|
1275
|
-
script: createViteScriptPreprocessor(),
|
|
1276
|
-
style: createViteStylePreprocessor(config)
|
|
1277
|
-
}, { filename });
|
|
1278
|
-
}
|
|
1279
|
-
};
|
|
1280
|
-
}
|
|
1281
|
-
function createInjectScopeEverythingRulePreprocessorGroup() {
|
|
1282
|
-
return {
|
|
1283
|
-
style({ content, filename }) {
|
|
1284
|
-
const s = new MagicString2(content);
|
|
1285
|
-
s.append(" *{}");
|
|
1286
|
-
return {
|
|
1287
|
-
code: s.toString(),
|
|
1288
|
-
map: s.generateDecodedMap({ source: filename, hires: true })
|
|
1289
|
-
};
|
|
1290
|
-
}
|
|
1291
|
-
};
|
|
1292
|
-
}
|
|
1293
|
-
function buildExtraPreprocessors(options, config) {
|
|
1294
|
-
var _a, _b;
|
|
1295
|
-
const prependPreprocessors = [];
|
|
1296
|
-
const appendPreprocessors = [];
|
|
1297
|
-
if ((_a = options.experimental) == null ? void 0 : _a.useVitePreprocess) {
|
|
1298
|
-
log.debug("adding vite preprocessor");
|
|
1299
|
-
prependPreprocessors.push(createVitePreprocessorGroup(config));
|
|
1300
|
-
}
|
|
1301
|
-
const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p == null ? void 0 : p.sveltePreprocess);
|
|
1302
|
-
if (pluginsWithPreprocessorsDeprecated.length > 0) {
|
|
1303
|
-
log.warn(`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(", ")}`);
|
|
1304
|
-
pluginsWithPreprocessorsDeprecated.forEach((p) => {
|
|
1305
|
-
if (!p.api) {
|
|
1306
|
-
p.api = {};
|
|
1307
|
-
}
|
|
1308
|
-
if (p.api.sveltePreprocess === void 0) {
|
|
1309
|
-
p.api.sveltePreprocess = p.sveltePreprocess;
|
|
1310
|
-
} else {
|
|
1311
|
-
log.error(`ignoring plugin.sveltePreprocess of ${p.name} because it already defined plugin.api.sveltePreprocess.`);
|
|
1312
|
-
}
|
|
1313
|
-
});
|
|
1314
|
-
}
|
|
1315
|
-
const pluginsWithPreprocessors = config.plugins.filter((p) => {
|
|
1316
|
-
var _a2;
|
|
1317
|
-
return (_a2 = p == null ? void 0 : p.api) == null ? void 0 : _a2.sveltePreprocess;
|
|
1430
|
+
// src/utils/optimizer.ts
|
|
1431
|
+
import fs6 from "fs";
|
|
1432
|
+
import path6 from "path";
|
|
1433
|
+
import { optimizeDeps } from "vite";
|
|
1434
|
+
var PREBUNDLE_SENSITIVE_OPTIONS = [
|
|
1435
|
+
"compilerOptions",
|
|
1436
|
+
"configFile",
|
|
1437
|
+
"experimental",
|
|
1438
|
+
"extensions",
|
|
1439
|
+
"ignorePluginPreprocessors",
|
|
1440
|
+
"preprocess"
|
|
1441
|
+
];
|
|
1442
|
+
async function handleOptimizeDeps(options, viteConfig) {
|
|
1443
|
+
if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir)
|
|
1444
|
+
return;
|
|
1445
|
+
const viteMetadataPath = path6.resolve(viteConfig.cacheDir, "_metadata.json");
|
|
1446
|
+
if (!fs6.existsSync(viteMetadataPath))
|
|
1447
|
+
return;
|
|
1448
|
+
const svelteMetadataPath = path6.resolve(viteConfig.cacheDir, "_svelte_metadata.json");
|
|
1449
|
+
const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
|
|
1450
|
+
return typeof value === "function" ? value.toString() : value;
|
|
1318
1451
|
});
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
if (
|
|
1322
|
-
|
|
1323
|
-
} else {
|
|
1324
|
-
included.push(p);
|
|
1325
|
-
}
|
|
1326
|
-
}
|
|
1327
|
-
if (ignored.length > 0) {
|
|
1328
|
-
log.debug(`Ignoring svelte preprocessors defined by these vite plugins: ${ignored.map((p) => p.name).join(", ")}`);
|
|
1329
|
-
}
|
|
1330
|
-
if (included.length > 0) {
|
|
1331
|
-
log.debug(`Adding svelte preprocessors defined by these vite plugins: ${included.map((p) => p.name).join(", ")}`);
|
|
1332
|
-
appendPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess));
|
|
1333
|
-
}
|
|
1334
|
-
if (options.hot && options.emitCss) {
|
|
1335
|
-
appendPreprocessors.push(createInjectScopeEverythingRulePreprocessorGroup());
|
|
1452
|
+
if (fs6.existsSync(svelteMetadataPath)) {
|
|
1453
|
+
const existingSvelteMetadata = fs6.readFileSync(svelteMetadataPath, "utf8");
|
|
1454
|
+
if (existingSvelteMetadata === currentSvelteMetadata)
|
|
1455
|
+
return;
|
|
1336
1456
|
}
|
|
1337
|
-
|
|
1457
|
+
await optimizeDeps(viteConfig, true);
|
|
1458
|
+
fs6.writeFileSync(svelteMetadataPath, currentSvelteMetadata);
|
|
1338
1459
|
}
|
|
1339
|
-
function
|
|
1340
|
-
|
|
1341
|
-
const
|
|
1342
|
-
|
|
1343
|
-
if (!options.preprocess) {
|
|
1344
|
-
options.preprocess = [...prependPreprocessors, ...appendPreprocessors];
|
|
1345
|
-
} else if (Array.isArray(options.preprocess)) {
|
|
1346
|
-
options.preprocess.unshift(...prependPreprocessors);
|
|
1347
|
-
options.preprocess.push(...appendPreprocessors);
|
|
1348
|
-
} else {
|
|
1349
|
-
options.preprocess = [...prependPreprocessors, options.preprocess, ...appendPreprocessors];
|
|
1350
|
-
}
|
|
1351
|
-
}
|
|
1352
|
-
const generateMissingSourceMaps = !!((_a = options.experimental) == null ? void 0 : _a.generateMissingPreprocessorSourcemaps);
|
|
1353
|
-
if (options.preprocess && generateMissingSourceMaps) {
|
|
1354
|
-
options.preprocess = Array.isArray(options.preprocess) ? options.preprocess.map((p, i) => validateSourceMapOutputWrapper(p, i)) : validateSourceMapOutputWrapper(options.preprocess, 0);
|
|
1460
|
+
function generateSvelteMetadata(options) {
|
|
1461
|
+
const metadata = {};
|
|
1462
|
+
for (const key of PREBUNDLE_SENSITIVE_OPTIONS) {
|
|
1463
|
+
metadata[key] = options[key];
|
|
1355
1464
|
}
|
|
1356
|
-
|
|
1357
|
-
function validateSourceMapOutputWrapper(group, i) {
|
|
1358
|
-
const wrapper = {};
|
|
1359
|
-
for (const [processorType, processorFn] of Object.entries(group)) {
|
|
1360
|
-
wrapper[processorType] = async (options) => {
|
|
1361
|
-
var _a;
|
|
1362
|
-
const result = await processorFn(options);
|
|
1363
|
-
if (result && result.code !== options.content) {
|
|
1364
|
-
let invalidMap = false;
|
|
1365
|
-
if (!result.map) {
|
|
1366
|
-
invalidMap = true;
|
|
1367
|
-
log.warn.enabled && log.warn.once(`preprocessor at index ${i} did not return a sourcemap for ${processorType} transform`, {
|
|
1368
|
-
filename: options.filename,
|
|
1369
|
-
type: processorType,
|
|
1370
|
-
processor: processorFn.toString()
|
|
1371
|
-
});
|
|
1372
|
-
} else if (((_a = result.map) == null ? void 0 : _a.mappings) === "") {
|
|
1373
|
-
invalidMap = true;
|
|
1374
|
-
log.warn.enabled && log.warn.once(`preprocessor at index ${i} returned an invalid empty sourcemap for ${processorType} transform`, {
|
|
1375
|
-
filename: options.filename,
|
|
1376
|
-
type: processorType,
|
|
1377
|
-
processor: processorFn.toString()
|
|
1378
|
-
});
|
|
1379
|
-
}
|
|
1380
|
-
if (invalidMap) {
|
|
1381
|
-
try {
|
|
1382
|
-
const map = await buildSourceMap(options.content, result.code, options.filename);
|
|
1383
|
-
if (map) {
|
|
1384
|
-
log.debug.enabled && log.debug(`adding generated sourcemap to preprocesor result for ${options.filename}`);
|
|
1385
|
-
result.map = map;
|
|
1386
|
-
}
|
|
1387
|
-
} catch (e) {
|
|
1388
|
-
log.error(`failed to build sourcemap`, e);
|
|
1389
|
-
}
|
|
1390
|
-
}
|
|
1391
|
-
}
|
|
1392
|
-
return result;
|
|
1393
|
-
};
|
|
1394
|
-
}
|
|
1395
|
-
return wrapper;
|
|
1465
|
+
return metadata;
|
|
1396
1466
|
}
|
|
1397
1467
|
|
|
1398
1468
|
// src/index.ts
|
|
@@ -1402,7 +1472,7 @@ function svelte(inlineOptions) {
|
|
|
1402
1472
|
}
|
|
1403
1473
|
validateInlineOptions(inlineOptions);
|
|
1404
1474
|
const cache = new VitePluginSvelteCache();
|
|
1405
|
-
const
|
|
1475
|
+
const pkg_resolve_errors = /* @__PURE__ */ new Set();
|
|
1406
1476
|
let requestParser;
|
|
1407
1477
|
let options;
|
|
1408
1478
|
let viteConfig;
|
|
@@ -1417,18 +1487,22 @@ function svelte(inlineOptions) {
|
|
|
1417
1487
|
} else if (config.logLevel) {
|
|
1418
1488
|
log.setLevel(config.logLevel);
|
|
1419
1489
|
}
|
|
1420
|
-
options = await
|
|
1490
|
+
options = await preResolveOptions(inlineOptions, config, configEnv);
|
|
1421
1491
|
const extraViteConfig = buildExtraViteConfig(options, config, configEnv);
|
|
1422
1492
|
log.debug("additional vite config", extraViteConfig);
|
|
1423
1493
|
return extraViteConfig;
|
|
1424
1494
|
},
|
|
1425
1495
|
async configResolved(config) {
|
|
1426
|
-
|
|
1496
|
+
options = resolveOptions(options, config);
|
|
1497
|
+
patchResolvedViteConfig(config, options);
|
|
1427
1498
|
requestParser = buildIdParser(options);
|
|
1428
1499
|
compileSvelte2 = createCompileSvelte(options);
|
|
1429
1500
|
viteConfig = config;
|
|
1430
1501
|
log.debug("resolved options", options);
|
|
1431
1502
|
},
|
|
1503
|
+
async buildStart() {
|
|
1504
|
+
await handleOptimizeDeps(options, viteConfig);
|
|
1505
|
+
},
|
|
1432
1506
|
configureServer(server) {
|
|
1433
1507
|
options.server = server;
|
|
1434
1508
|
setupWatchers(options, cache, requestParser);
|
|
@@ -1451,9 +1525,9 @@ function svelte(inlineOptions) {
|
|
|
1451
1525
|
}
|
|
1452
1526
|
}
|
|
1453
1527
|
},
|
|
1454
|
-
async resolveId(importee, importer, opts
|
|
1455
|
-
const ssr =
|
|
1456
|
-
const svelteRequest = requestParser(importee,
|
|
1528
|
+
async resolveId(importee, importer, opts) {
|
|
1529
|
+
const ssr = !!(opts == null ? void 0 : opts.ssr);
|
|
1530
|
+
const svelteRequest = requestParser(importee, ssr);
|
|
1457
1531
|
if (svelteRequest == null ? void 0 : svelteRequest.query.svelte) {
|
|
1458
1532
|
if (svelteRequest.query.type === "style") {
|
|
1459
1533
|
log.debug(`resolveId resolved virtual css module ${svelteRequest.cssId}`);
|
|
@@ -1475,27 +1549,19 @@ function svelte(inlineOptions) {
|
|
|
1475
1549
|
return resolvedSvelteSSR;
|
|
1476
1550
|
}
|
|
1477
1551
|
try {
|
|
1478
|
-
const resolved = resolveViaPackageJsonSvelte(importee, importer);
|
|
1552
|
+
const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
|
|
1479
1553
|
if (resolved) {
|
|
1480
1554
|
log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
|
|
1481
1555
|
return resolved;
|
|
1482
1556
|
}
|
|
1483
1557
|
} catch (err) {
|
|
1484
|
-
|
|
1485
|
-
case "ERR_PACKAGE_PATH_NOT_EXPORTED":
|
|
1486
|
-
pkg_export_errors.add(importee);
|
|
1487
|
-
return null;
|
|
1488
|
-
case "MODULE_NOT_FOUND":
|
|
1489
|
-
return null;
|
|
1490
|
-
default:
|
|
1491
|
-
throw err;
|
|
1492
|
-
}
|
|
1558
|
+
pkg_resolve_errors.add(importee);
|
|
1493
1559
|
}
|
|
1494
1560
|
},
|
|
1495
1561
|
async transform(code, id, opts) {
|
|
1496
1562
|
var _a;
|
|
1497
|
-
const ssr =
|
|
1498
|
-
const svelteRequest = requestParser(id,
|
|
1563
|
+
const ssr = !!(opts == null ? void 0 : opts.ssr);
|
|
1564
|
+
const svelteRequest = requestParser(id, ssr);
|
|
1499
1565
|
if (!svelteRequest) {
|
|
1500
1566
|
return;
|
|
1501
1567
|
}
|
|
@@ -1537,8 +1603,10 @@ function svelte(inlineOptions) {
|
|
|
1537
1603
|
}
|
|
1538
1604
|
},
|
|
1539
1605
|
buildEnd() {
|
|
1540
|
-
if (
|
|
1541
|
-
log.warn(`
|
|
1606
|
+
if (pkg_resolve_errors.size > 0) {
|
|
1607
|
+
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.
|
|
1608
|
+
If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.
|
|
1609
|
+
${Array.from(pkg_resolve_errors, (s) => `- ${s}`).join("\n")}`.replace(/\t/g, ""));
|
|
1542
1610
|
}
|
|
1543
1611
|
}
|
|
1544
1612
|
};
|
|
@@ -1546,4 +1614,4 @@ function svelte(inlineOptions) {
|
|
|
1546
1614
|
export {
|
|
1547
1615
|
svelte
|
|
1548
1616
|
};
|
|
1549
|
-
//# sourceMappingURL=index.js.map
|
|
1617
|
+
//# sourceMappingURL=index.js.map
|