@sveltejs/vite-plugin-svelte 1.0.0-next.33 → 1.0.0-next.34

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.js CHANGED
@@ -26,7 +26,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
26
26
  });
27
27
 
28
28
  // src/index.ts
29
- import fs7 from "fs";
29
+ import fs8 from "fs";
30
30
 
31
31
  // src/utils/log.ts
32
32
  import { cyan, yellow, red } from "kleur/colors";
@@ -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
- createHash
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;
@@ -562,7 +558,7 @@ function findRootSvelteDependencies(root, cwdFallback = true) {
562
558
  ].filter((dep) => !is_common_without_svelte_field(dep));
563
559
  return getSvelteDependencies(deps, root);
564
560
  }
565
- function getSvelteDependencies(deps, pkgDir, path6 = []) {
561
+ function getSvelteDependencies(deps, pkgDir, path7 = []) {
566
562
  const result = [];
567
563
  const localRequire = createRequire2(`${pkgDir}/package.json`);
568
564
  const resolvedDeps = deps.map((dep) => resolveDependencyData(dep, localRequire)).filter(Boolean);
@@ -570,18 +566,18 @@ function getSvelteDependencies(deps, pkgDir, path6 = []) {
570
566
  const type = getSvelteDependencyType(pkg);
571
567
  if (!type)
572
568
  continue;
573
- result.push({ name: pkg.name, type, pkg, dir, path: path6 });
569
+ result.push({ name: pkg.name, type, pkg, dir, path: path7 });
574
570
  if (type === "component-library" && pkg.dependencies) {
575
571
  let dependencyNames = Object.keys(pkg.dependencies);
576
- const circular = dependencyNames.filter((name) => path6.includes(name));
572
+ const circular = dependencyNames.filter((name) => path7.includes(name));
577
573
  if (circular.length > 0) {
578
- log.warn.enabled && log.warn(`skipping circular svelte dependencies in automated vite optimizeDeps handling`, circular.map((x) => path6.concat(x).join(">")));
579
- dependencyNames = dependencyNames.filter((name) => !path6.includes(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));
580
576
  }
581
- if (path6.length === 3) {
582
- log.debug.once(`encountered deep svelte dependency tree: ${path6.join(">")}`);
577
+ if (path7.length === 3) {
578
+ log.debug.once(`encountered deep svelte dependency tree: ${path7.join(">")}`);
583
579
  }
584
- result.push(...getSvelteDependencies(dependencyNames, dir, path6.concat(pkg.name)));
580
+ result.push(...getSvelteDependencies(dependencyNames, dir, path7.concat(pkg.name)));
585
581
  }
586
582
  }
587
583
  return result;
@@ -875,7 +871,8 @@ function createViteScriptPreprocessor() {
875
871
  loader: lang,
876
872
  tsconfigRaw: {
877
873
  compilerOptions: {
878
- importsNotUsedAsValues: "preserve"
874
+ importsNotUsedAsValues: "preserve",
875
+ preserveValueImports: true
879
876
  }
880
877
  }
881
878
  });
@@ -1039,7 +1036,7 @@ function validateSourceMapOutputWrapper(group, i) {
1039
1036
  }
1040
1037
 
1041
1038
  // src/utils/options.ts
1042
- var knownOptions = new Set([
1039
+ var knownOptions = /* @__PURE__ */ new Set([
1043
1040
  "configFile",
1044
1041
  "include",
1045
1042
  "exclude",
@@ -1093,6 +1090,7 @@ function resolveOptions(preResolveOptions2, viteConfig) {
1093
1090
  };
1094
1091
  const merged = __spreadProps(__spreadValues(__spreadValues({}, defaultOptions), preResolveOptions2), {
1095
1092
  compilerOptions: __spreadValues(__spreadValues({}, defaultOptions.compilerOptions), preResolveOptions2.compilerOptions),
1093
+ root: viteConfig.root,
1096
1094
  isProduction: viteConfig.isProduction
1097
1095
  });
1098
1096
  addExtraPreprocessors(merged, viteConfig);
@@ -1160,16 +1158,16 @@ function buildExtraViteConfig(options, config, configEnv) {
1160
1158
  extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config);
1161
1159
  return extraViteConfig;
1162
1160
  }
1163
- function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) {
1161
+ function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps2) {
1164
1162
  const include = [];
1165
1163
  const exclude = ["svelte-hmr"];
1166
1164
  const isIncluded = (dep) => {
1167
1165
  var _a;
1168
- return include.includes(dep) || ((_a = optimizeDeps == null ? void 0 : optimizeDeps.include) == null ? void 0 : _a.includes(dep));
1166
+ return include.includes(dep) || ((_a = optimizeDeps2 == null ? void 0 : optimizeDeps2.include) == null ? void 0 : _a.includes(dep));
1169
1167
  };
1170
1168
  const isExcluded = (dep) => {
1171
1169
  var _a;
1172
- return exclude.includes(dep) || ((_a = optimizeDeps == null ? void 0 : optimizeDeps.exclude) == null ? void 0 : _a.some((id) => dep === id || id.startsWith(`${dep}/`)));
1170
+ return exclude.includes(dep) || ((_a = optimizeDeps2 == null ? void 0 : optimizeDeps2.exclude) == null ? void 0 : _a.some((id) => dep === id || id.startsWith(`${dep}/`)));
1173
1171
  };
1174
1172
  if (!isExcluded("svelte")) {
1175
1173
  const svelteImportsToInclude = SVELTE_IMPORTS.filter((x) => x !== "svelte/ssr");
@@ -1235,10 +1233,10 @@ function patchResolvedViteConfig(viteConfig, options) {
1235
1233
  // src/utils/vite-plugin-svelte-cache.ts
1236
1234
  var VitePluginSvelteCache = class {
1237
1235
  constructor() {
1238
- this._css = new Map();
1239
- this._js = new Map();
1240
- this._dependencies = new Map();
1241
- 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();
1242
1240
  }
1243
1241
  update(compileData) {
1244
1242
  this.updateCSS(compileData);
@@ -1262,7 +1260,7 @@ var VitePluginSvelteCache = class {
1262
1260
  const added = dependencies.filter((d) => !prevDependencies.includes(d));
1263
1261
  added.forEach((d) => {
1264
1262
  if (!this._dependants.has(d)) {
1265
- this._dependants.set(d, new Set());
1263
+ this._dependants.set(d, /* @__PURE__ */ new Set());
1266
1264
  }
1267
1265
  this._dependants.get(d).add(compileData.filename);
1268
1266
  });
@@ -1300,8 +1298,8 @@ var VitePluginSvelteCache = class {
1300
1298
  return this._js.get(svelteRequest.normalizedFilename);
1301
1299
  }
1302
1300
  }
1303
- getDependants(path6) {
1304
- const dependants = this._dependants.get(path6);
1301
+ getDependants(path7) {
1302
+ const dependants = this._dependants.get(path7);
1305
1303
  return dependants ? [...dependants] : [];
1306
1304
  }
1307
1305
  };
@@ -1335,7 +1333,6 @@ function setupWatchers(options, cache, requestParser) {
1335
1333
  }
1336
1334
  };
1337
1335
  const triggerViteRestart = (filename) => {
1338
- var _a;
1339
1336
  if (serverConfig.middlewareMode) {
1340
1337
  const message = "Svelte config change detected, restart your dev process to apply the changes.";
1341
1338
  log.info(message, filename);
@@ -1345,7 +1342,7 @@ function setupWatchers(options, cache, requestParser) {
1345
1342
  });
1346
1343
  } else {
1347
1344
  log.info(`svelte config changed: restarting vite server. - file: ${filename}`);
1348
- server.restart(!!((_a = options.experimental) == null ? void 0 : _a.prebundleSvelteLibraries));
1345
+ server.restart();
1349
1346
  }
1350
1347
  };
1351
1348
  const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path4.join(root, cfg));
@@ -1410,6 +1407,44 @@ function isBareImport(importee) {
1410
1407
  }
1411
1408
  }
1412
1409
 
1410
+ // src/utils/optimizer.ts
1411
+ import fs7 from "fs";
1412
+ import path6 from "path";
1413
+ import { optimizeDeps } from "vite";
1414
+ var PREBUNDLE_SENSITIVE_OPTIONS = [
1415
+ "compilerOptions",
1416
+ "configFile",
1417
+ "experimental",
1418
+ "extensions",
1419
+ "ignorePluginPreprocessors",
1420
+ "preprocess"
1421
+ ];
1422
+ async function handleOptimizeDeps(options, viteConfig) {
1423
+ if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir)
1424
+ return;
1425
+ const viteMetadataPath = path6.resolve(viteConfig.cacheDir, "_metadata.json");
1426
+ if (!fs7.existsSync(viteMetadataPath))
1427
+ return;
1428
+ const svelteMetadataPath = path6.resolve(viteConfig.cacheDir, "_svelte_metadata.json");
1429
+ const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
1430
+ return typeof value === "function" ? value.toString() : value;
1431
+ });
1432
+ if (fs7.existsSync(svelteMetadataPath)) {
1433
+ const existingSvelteMetadata = fs7.readFileSync(svelteMetadataPath, "utf8");
1434
+ if (existingSvelteMetadata === currentSvelteMetadata)
1435
+ return;
1436
+ }
1437
+ await optimizeDeps(viteConfig, true);
1438
+ fs7.writeFileSync(svelteMetadataPath, currentSvelteMetadata);
1439
+ }
1440
+ function generateSvelteMetadata(options) {
1441
+ const metadata = {};
1442
+ for (const key of PREBUNDLE_SENSITIVE_OPTIONS) {
1443
+ metadata[key] = options[key];
1444
+ }
1445
+ return metadata;
1446
+ }
1447
+
1413
1448
  // src/index.ts
1414
1449
  function svelte(inlineOptions) {
1415
1450
  if (process.env.DEBUG != null) {
@@ -1417,7 +1452,7 @@ function svelte(inlineOptions) {
1417
1452
  }
1418
1453
  validateInlineOptions(inlineOptions);
1419
1454
  const cache = new VitePluginSvelteCache();
1420
- const pkg_export_errors = new Set();
1455
+ const pkg_export_errors = /* @__PURE__ */ new Set();
1421
1456
  let requestParser;
1422
1457
  let options;
1423
1458
  let viteConfig;
@@ -1445,6 +1480,9 @@ function svelte(inlineOptions) {
1445
1480
  viteConfig = config;
1446
1481
  log.debug("resolved options", options);
1447
1482
  },
1483
+ async buildStart() {
1484
+ await handleOptimizeDeps(options, viteConfig);
1485
+ },
1448
1486
  configureServer(server) {
1449
1487
  options.server = server;
1450
1488
  setupWatchers(options, cache, requestParser);
@@ -1463,7 +1501,7 @@ function svelte(inlineOptions) {
1463
1501
  }
1464
1502
  if (viteConfig.assetsInclude(filename)) {
1465
1503
  log.debug(`load returns raw content for ${filename}`);
1466
- return fs7.readFileSync(filename, "utf-8");
1504
+ return fs8.readFileSync(filename, "utf-8");
1467
1505
  }
1468
1506
  }
1469
1507
  },