@sveltejs/vite-plugin-svelte 1.0.0-next.44 → 1.0.0-next.47

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/vite-plugin-svelte",
3
- "version": "1.0.0-next.44",
3
+ "version": "1.0.0-next.47",
4
4
  "license": "MIT",
5
5
  "author": "dominikg",
6
6
  "files": [
@@ -45,8 +45,8 @@
45
45
  "debug": "^4.3.4",
46
46
  "deepmerge": "^4.2.2",
47
47
  "kleur": "^4.1.4",
48
- "magic-string": "^0.26.1",
49
- "svelte-hmr": "^0.14.11"
48
+ "magic-string": "^0.26.2",
49
+ "svelte-hmr": "^0.14.12"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "diff-match-patch": "^1.0.5",
@@ -62,15 +62,15 @@
62
62
  "@types/debug": "^4.1.7",
63
63
  "@types/diff-match-patch": "^1.0.32",
64
64
  "diff-match-patch": "^1.0.5",
65
- "esbuild": "^0.14.38",
66
- "rollup": "^2.72.1",
65
+ "esbuild": "^0.14.42",
66
+ "rollup": "^2.75.4",
67
67
  "svelte": "^3.48.0",
68
- "tsup": "^5.12.7",
69
- "vite": "^2.9.8"
68
+ "tsup": "^6.0.1",
69
+ "vite": "^2.9.9"
70
70
  },
71
71
  "scripts": {
72
72
  "dev": "pnpm build:ci --sourcemap --watch src",
73
- "build:ci": "rimraf dist && tsup-node src/index.ts --format esm,cjs --no-splitting --target node14",
73
+ "build:ci": "rimraf dist && tsup-node src/index.ts --format esm,cjs --no-splitting --shims",
74
74
  "build": "pnpm build:ci --dts --sourcemap"
75
75
  }
76
76
  }
package/src/index.ts CHANGED
@@ -218,6 +218,8 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
218
218
  return plugins.filter(Boolean);
219
219
  }
220
220
 
221
+ export { loadSvelteConfig } from './utils/load-svelte-config';
222
+
221
223
  export {
222
224
  Options,
223
225
  Preprocessor,
@@ -99,7 +99,7 @@
99
99
  }
100
100
 
101
101
  function keydown(event) {
102
- if (event.repeat) {
102
+ if (event.repeat || event.key === undefined) {
103
103
  return;
104
104
  }
105
105
 
@@ -43,7 +43,7 @@ export function svelteInspector(): Plugin {
43
43
  disabled = true;
44
44
  } else {
45
45
  if (vps.api.options.kit && !inspectorOptions.appendTo) {
46
- const out_dir = vps.api.options.kit.outDir || '.svelte-kit';
46
+ const out_dir = path.basename(vps.api.options.kit.outDir || '.svelte-kit');
47
47
  inspectorOptions.appendTo = `${out_dir}/runtime/client/start.js`;
48
48
  }
49
49
  appendTo = inspectorOptions.appendTo;
@@ -1,6 +1,10 @@
1
+ import { describe, it, expect } from 'vitest';
1
2
  import { findRootSvelteDependencies, needsOptimization } from '../dependencies';
2
3
  import * as path from 'path';
3
4
  import { createRequire } from 'module';
5
+ import { fileURLToPath } from 'url';
6
+ const __dir = path.dirname(fileURLToPath(import.meta.url));
7
+ const e2eTestRoot = path.resolve(__dir, '../../../../../packages/e2e-tests');
4
8
 
5
9
  describe('dependencies', () => {
6
10
  describe('findRootSvelteDependencies', () => {
@@ -11,9 +15,7 @@ describe('dependencies', () => {
11
15
  expect(deps[0].path).toEqual([]);
12
16
  });
13
17
  it('should find nested svelte dependencies in packages/e2e-test/package-json-svelte-field', () => {
14
- const deps = findRootSvelteDependencies(
15
- path.resolve('packages/e2e-tests/package-json-svelte-field')
16
- );
18
+ const deps = findRootSvelteDependencies(path.join(e2eTestRoot, 'package-json-svelte-field'));
17
19
  expect(deps).toHaveLength(3);
18
20
  const hybrid = deps.find((dep) => dep.name === 'e2e-test-dep-svelte-hybrid');
19
21
  expect(hybrid).toBeTruthy();
@@ -29,7 +31,8 @@ describe('dependencies', () => {
29
31
  });
30
32
  describe('needsOptimization', () => {
31
33
  it('should optimize cjs deps only', () => {
32
- const localRequire = createRequire(path.resolve('packages/e2e-tests/dependencies'));
34
+ const testDepsPath = path.join(e2eTestRoot, 'dependencies/package.json');
35
+ const localRequire = createRequire(testDepsPath);
33
36
  expect(needsOptimization('e2e-test-dep-cjs-and-esm', localRequire)).toBe(false);
34
37
  expect(needsOptimization('e2e-test-dep-cjs-only', localRequire)).toBe(true);
35
38
  expect(needsOptimization('e2e-test-dep-esm-only', localRequire)).toBe(false);
@@ -1,3 +1,4 @@
1
+ import { describe, it, expect } from 'vitest';
1
2
  import { buildMagicString, buildSourceMap } from '../sourcemap';
2
3
 
3
4
  describe('sourcemap', () => {
@@ -21,7 +21,8 @@ const _createCompileSvelte = (makeHot: Function) =>
21
21
  const compileOptions: CompileOptions = {
22
22
  ...options.compilerOptions,
23
23
  filename,
24
- generate: ssr ? 'ssr' : 'dom'
24
+ generate: ssr ? 'ssr' : 'dom',
25
+ format: 'esm'
25
26
  };
26
27
  if (options.hot && options.emitCss) {
27
28
  const hash = `s-${safeBase64Hash(normalizedFilename)}`;
@@ -22,14 +22,15 @@ export const knownSvelteConfigNames = [
22
22
  // also use timestamp query to avoid caching on reload
23
23
  const dynamicImportDefault = new Function(
24
24
  'path',
25
- 'return import(path + "?t=" + Date.now()).then(m => m.default)'
25
+ 'timestamp',
26
+ 'return import(path + "?t=" + timestamp).then(m => m.default)'
26
27
  );
27
28
 
28
29
  export async function loadSvelteConfig(
29
- viteConfig: UserConfig,
30
- inlineOptions: Partial<Options>
30
+ viteConfig?: UserConfig,
31
+ inlineOptions?: Partial<Options>
31
32
  ): Promise<Partial<Options> | undefined> {
32
- if (inlineOptions.configFile === false) {
33
+ if (inlineOptions?.configFile === false) {
33
34
  return;
34
35
  }
35
36
  const configFile = findConfigToLoad(viteConfig, inlineOptions);
@@ -38,7 +39,10 @@ export async function loadSvelteConfig(
38
39
  // try to use dynamic import for svelte.config.js first
39
40
  if (configFile.endsWith('.js') || configFile.endsWith('.mjs')) {
40
41
  try {
41
- const result = await dynamicImportDefault(pathToFileURL(configFile).href);
42
+ const result = await dynamicImportDefault(
43
+ pathToFileURL(configFile).href,
44
+ fs.statSync(configFile).mtimeMs
45
+ );
42
46
  if (result != null) {
43
47
  return {
44
48
  ...result,
@@ -83,9 +87,9 @@ export async function loadSvelteConfig(
83
87
  }
84
88
  }
85
89
 
86
- function findConfigToLoad(viteConfig: UserConfig, inlineOptions: Partial<Options>) {
87
- const root = viteConfig.root || process.cwd();
88
- if (inlineOptions.configFile) {
90
+ function findConfigToLoad(viteConfig?: UserConfig, inlineOptions?: Partial<Options>) {
91
+ const root = viteConfig?.root || process.cwd();
92
+ if (inlineOptions?.configFile) {
89
93
  const abolutePath = path.isAbsolute(inlineOptions.configFile)
90
94
  ? inlineOptions.configFile
91
95
  : path.resolve(root, inlineOptions.configFile);
@@ -61,10 +61,7 @@ export async function preResolveOptions(
61
61
  };
62
62
  const defaultOptions: Partial<Options> = {
63
63
  extensions: ['.svelte'],
64
- emitCss: true,
65
- compilerOptions: {
66
- format: 'esm'
67
- }
64
+ emitCss: true
68
65
  };
69
66
  const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions);
70
67
  const extraOptions: Partial<PreResolvedOptions> = {
@@ -118,6 +115,7 @@ export function resolveOptions(
118
115
  };
119
116
  const merged: ResolvedOptions = mergeConfigs(defaultOptions, preResolveOptions, extraOptions);
120
117
 
118
+ removeIgnoredOptions(merged);
121
119
  addExtraPreprocessors(merged, viteConfig);
122
120
  enforceOptionsForHmr(merged);
123
121
  enforceOptionsForProduction(merged);
@@ -177,6 +175,26 @@ function enforceOptionsForProduction(options: ResolvedOptions) {
177
175
  }
178
176
  }
179
177
 
178
+ function removeIgnoredOptions(options: ResolvedOptions) {
179
+ const ignoredCompilerOptions = ['generate', 'format', 'filename'];
180
+ if (options.hot && options.emitCss) {
181
+ ignoredCompilerOptions.push('cssHash');
182
+ }
183
+ const passedCompilerOptions = Object.keys(options.compilerOptions || {});
184
+ const passedIgnored = passedCompilerOptions.filter((o) => ignoredCompilerOptions.includes(o));
185
+ if (passedIgnored.length) {
186
+ log.warn(
187
+ `The following Svelte compilerOptions are controlled by vite-plugin-svelte and essential to its functionality. User-specified values are ignored. Please remove them from your configuration: ${passedIgnored.join(
188
+ ', '
189
+ )}`
190
+ );
191
+ passedIgnored.forEach((ignored) => {
192
+ // @ts-expect-error string access
193
+ delete options.compilerOptions[ignored];
194
+ });
195
+ }
196
+ }
197
+
180
198
  // vite passes unresolved `root`option to config hook but we need the resolved value, so do it here
181
199
  // https://github.com/sveltejs/vite-plugin-svelte/issues/113
182
200
  // https://github.com/vitejs/vite/blob/43c957de8a99bb326afd732c962f42127b0a4d1e/packages/vite/src/node/config.ts#L293
@@ -401,11 +419,13 @@ export interface Options {
401
419
  emitCss?: boolean;
402
420
 
403
421
  /**
404
- * The options to be passed to the Svelte compiler
422
+ * The options to be passed to the Svelte compiler. A few options are set by default,
423
+ * including `dev` and `css`. However, some options are non-configurable, like
424
+ * `filename`, `format`, `generate`, and `cssHash` (in dev).
405
425
  *
406
426
  * @see https://svelte.dev/docs#svelte_compile
407
427
  */
408
- compilerOptions?: CompileOptions;
428
+ compilerOptions?: Omit<CompileOptions, 'filename' | 'format' | 'generate'>;
409
429
 
410
430
  /**
411
431
  * Handles warning emitted from the Svelte compiler