@sveltejs/vite-plugin-svelte 2.0.1 → 2.0.3

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": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "license": "MIT",
5
5
  "author": "dominikg",
6
6
  "files": [
@@ -38,11 +38,11 @@
38
38
  "homepage": "https://github.com/sveltejs/vite-plugin-svelte#readme",
39
39
  "dependencies": {
40
40
  "debug": "^4.3.4",
41
- "deepmerge": "^4.2.2",
41
+ "deepmerge": "^4.3.0",
42
42
  "kleur": "^4.1.5",
43
- "magic-string": "^0.27.0",
43
+ "magic-string": "^0.29.0",
44
44
  "svelte-hmr": "^0.15.1",
45
- "vitefu": "^0.2.3"
45
+ "vitefu": "^0.2.4"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "svelte": "^3.54.0",
@@ -50,11 +50,11 @@
50
50
  },
51
51
  "devDependencies": {
52
52
  "@types/debug": "^4.1.7",
53
- "esbuild": "^0.16.3",
53
+ "esbuild": "^0.17.9",
54
54
  "rollup": "^2.79.1",
55
- "svelte": "^3.54.0",
56
- "tsup": "^6.5.0",
57
- "vite": "^4.0.0"
55
+ "svelte": "^3.55.1",
56
+ "tsup": "^6.6.3",
57
+ "vite": "^4.1.4"
58
58
  },
59
59
  "scripts": {
60
60
  "dev": "pnpm build:ci --sourcemap --watch src",
@@ -4,6 +4,7 @@ import { log, logCompilerWarnings } from './utils/log';
4
4
  import { SvelteRequest } from './utils/id';
5
5
  import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache';
6
6
  import { ResolvedOptions } from './utils/options';
7
+ import { toRollupError } from './utils/error';
7
8
 
8
9
  /**
9
10
  * Vite-specific HMR handling
@@ -32,7 +33,7 @@ export async function handleHotUpdate(
32
33
  cache.update(compileData);
33
34
  } catch (e) {
34
35
  cache.setError(svelteRequest, e);
35
- throw e;
36
+ throw toRollupError(e, options);
36
37
  }
37
38
 
38
39
  const affectedModules = [...modules];
package/src/index.ts CHANGED
@@ -219,11 +219,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
219
219
  }
220
220
  const svelteRequest = requestParser(ctx.file, false, ctx.timestamp);
221
221
  if (svelteRequest) {
222
- try {
223
- return handleHotUpdate(compileSvelte, ctx, svelteRequest, cache, options);
224
- } catch (e) {
225
- throw toRollupError(e, options);
226
- }
222
+ return handleHotUpdate(compileSvelte, ctx, svelteRequest, cache, options);
227
223
  }
228
224
  },
229
225
  async buildEnd() {
package/src/preprocess.ts CHANGED
@@ -1,8 +1,8 @@
1
- import path from 'path';
2
1
  import { preprocessCSS, resolveConfig, transformWithEsbuild } from 'vite';
3
2
  import type { ESBuildOptions, InlineConfig, ResolvedConfig } from 'vite';
4
3
  // eslint-disable-next-line node/no-missing-import
5
4
  import type { Preprocessor, PreprocessorGroup } from 'svelte/types/compiler/preprocess';
5
+ import { mapSourcesToRelative } from './utils/sourcemaps';
6
6
 
7
7
  const supportedStyleLangs = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'postcss', 'sss'];
8
8
  const supportedScriptLangs = ['ts'];
@@ -27,7 +27,7 @@ function viteScript(): { script: Preprocessor } {
27
27
  async script({ attributes, content, filename = '' }) {
28
28
  const lang = attributes.lang as string;
29
29
  if (!supportedScriptLangs.includes(lang)) return;
30
- const transformResult = await transformWithEsbuild(content, filename, {
30
+ const { code, map } = await transformWithEsbuild(content, filename, {
31
31
  loader: lang as ESBuildOptions['loader'],
32
32
  target: 'esnext',
33
33
  tsconfigRaw: {
@@ -38,9 +38,12 @@ function viteScript(): { script: Preprocessor } {
38
38
  }
39
39
  }
40
40
  });
41
+
42
+ mapSourcesToRelative(map, filename);
43
+
41
44
  return {
42
- code: transformResult.code,
43
- map: transformResult.map
45
+ code,
46
+ map
44
47
  };
45
48
  }
46
49
  };
@@ -70,14 +73,13 @@ function viteStyle(config: InlineConfig | ResolvedConfig = {}): {
70
73
  transform = getCssTransformFn(resolvedConfig);
71
74
  }
72
75
  const moduleId = `${filename}.${lang}`;
73
- const result = await transform(content, moduleId);
74
- // patch sourcemap source to point back to original filename
75
- if (result.map?.sources?.[0] === moduleId) {
76
- result.map.sources[0] = path.basename(filename);
77
- }
76
+ const { code, map } = await transform(content, moduleId);
77
+
78
+ mapSourcesToRelative(map, moduleId);
79
+
78
80
  return {
79
- code: result.code,
80
- map: result.map ?? undefined
81
+ code,
82
+ map: map ?? undefined
81
83
  };
82
84
  };
83
85
  // @ts-expect-error tag so can be found by v-p-s
@@ -35,22 +35,23 @@ export function svelteInspector(): Plugin {
35
35
 
36
36
  configResolved(config) {
37
37
  const vps = config.plugins.find((p) => p.name === 'vite-plugin-svelte');
38
- if (vps?.api?.options?.experimental?.inspector) {
39
- inspectorOptions = {
40
- ...defaultInspectorOptions,
41
- ...vps.api.options.experimental.inspector
42
- };
43
- }
44
- if (!vps || !inspectorOptions) {
38
+ const options = vps?.api?.options?.experimental?.inspector;
39
+ if (!vps || !options) {
45
40
  log.debug('inspector disabled, could not find config');
46
41
  disabled = true;
47
- } else {
48
- if (vps.api.options.kit && !inspectorOptions.appendTo) {
49
- const out_dir = path.basename(vps.api.options.kit.outDir || '.svelte-kit');
50
- inspectorOptions.appendTo = `${out_dir}/generated/root.svelte`;
51
- }
52
- appendTo = inspectorOptions.appendTo;
42
+ return;
43
+ }
44
+ inspectorOptions = {
45
+ ...defaultInspectorOptions,
46
+ ...options
47
+ };
48
+ const isSvelteKit = config.plugins.some((p) => p.name.startsWith('vite-plugin-sveltekit'));
49
+ if (isSvelteKit && !inspectorOptions.appendTo) {
50
+ // this could append twice if a user had a file that ends with /generated/root.svelte
51
+ // but that should be rare and inspector doesn't execute twice
52
+ inspectorOptions.appendTo = `/generated/root.svelte`;
53
53
  }
54
+ appendTo = inspectorOptions.appendTo;
54
55
  },
55
56
 
56
57
  async resolveId(importee: string, importer, options) {
@@ -9,29 +9,12 @@ import { StatCollection } from './vite-plugin-svelte-stats';
9
9
  //eslint-disable-next-line node/no-missing-import
10
10
  import type { Processed } from 'svelte/types/compiler/preprocess';
11
11
  import { createInjectScopeEverythingRulePreprocessorGroup } from './preprocess';
12
- import path from 'path';
12
+ import { mapSourcesToRelative } from './sourcemaps';
13
13
 
14
14
  const scriptLangRE = /<script [^>]*lang=["']?([^"' >]+)["']?[^>]*>/;
15
15
 
16
16
  export type CompileSvelte = ReturnType<typeof _createCompileSvelte>;
17
17
 
18
- function mapSourcesToRelative(map: { sources?: string[] }, filename: string) {
19
- // sourcemap sources are relative to the sourcemap itself
20
- // assume the sourcemap location is the same as filename and turn absolute paths to relative
21
- // to avoid leaking fs information like vite root
22
- if (map?.sources) {
23
- map.sources = map.sources.map((s) => {
24
- if (path.isAbsolute(s)) {
25
- const relative = path.relative(filename, s);
26
- // empty string a source is not allowed, use simple filename
27
- return relative === '' ? path.basename(filename) : relative;
28
- } else {
29
- return s;
30
- }
31
- });
32
- }
33
- }
34
-
35
18
  const _createCompileSvelte = (makeHot: Function) => {
36
19
  let stats: StatCollection | undefined;
37
20
  const devStylePreprocessor = createInjectScopeEverythingRulePreprocessorGroup();
@@ -1,6 +1,6 @@
1
- const VITE_RESOLVE_MAIN_FIELDS = ['module', 'jsnext:main', 'jsnext'];
1
+ export const VITE_RESOLVE_MAIN_FIELDS = ['module', 'jsnext:main', 'jsnext'];
2
2
 
3
- export const SVELTE_RESOLVE_MAIN_FIELDS = ['svelte', ...VITE_RESOLVE_MAIN_FIELDS];
3
+ export const SVELTE_RESOLVE_MAIN_FIELDS = ['svelte'];
4
4
 
5
5
  export const SVELTE_IMPORTS = [
6
6
  'svelte/animate',
@@ -6,7 +6,8 @@ import {
6
6
  SVELTE_EXPORT_CONDITIONS,
7
7
  SVELTE_HMR_IMPORTS,
8
8
  SVELTE_IMPORTS,
9
- SVELTE_RESOLVE_MAIN_FIELDS
9
+ SVELTE_RESOLVE_MAIN_FIELDS,
10
+ VITE_RESOLVE_MAIN_FIELDS
10
11
  } from './constants';
11
12
  // eslint-disable-next-line node/no-missing-import
12
13
  import type { CompileOptions, Warning } from 'svelte/types/compiler/interfaces';
@@ -324,9 +325,18 @@ export async function buildExtraViteConfig(
324
325
  options: PreResolvedOptions,
325
326
  config: UserConfig
326
327
  ): Promise<Partial<UserConfig>> {
328
+ // make sure we only readd vite default mainFields when no other plugin has changed the config already
329
+ // see https://github.com/sveltejs/vite-plugin-svelte/issues/581
330
+ if (!config.resolve) {
331
+ config.resolve = {};
332
+ }
333
+ config.resolve.mainFields = [
334
+ ...SVELTE_RESOLVE_MAIN_FIELDS,
335
+ ...(config.resolve.mainFields ?? VITE_RESOLVE_MAIN_FIELDS)
336
+ ];
337
+
327
338
  const extraViteConfig: Partial<UserConfig> = {
328
339
  resolve: {
329
- mainFields: [...SVELTE_RESOLVE_MAIN_FIELDS],
330
340
  dedupe: [...SVELTE_IMPORTS, ...SVELTE_HMR_IMPORTS],
331
341
  conditions: [...SVELTE_EXPORT_CONDITIONS]
332
342
  }
@@ -0,0 +1,20 @@
1
+ import path from 'path';
2
+
3
+ /**
4
+ * sourcemap sources are relative to the sourcemap itself
5
+ * assume the sourcemap location is the same as filename and turn absolute paths to relative
6
+ * to avoid leaking fs information like vite root
7
+ */
8
+ export function mapSourcesToRelative(map: { sources?: string[] }, filename: string) {
9
+ if (map?.sources) {
10
+ map.sources = map.sources.map((s) => {
11
+ if (path.isAbsolute(s)) {
12
+ const relative = path.relative(filename, s);
13
+ // empty string as a source is not allowed, use simple filename
14
+ return relative === '' ? path.basename(filename) : relative;
15
+ } else {
16
+ return s;
17
+ }
18
+ });
19
+ }
20
+ }