@sveltejs/vite-plugin-svelte 1.0.0-next.36 → 1.0.0-next.39

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/src/index.ts CHANGED
@@ -27,7 +27,6 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
27
27
  }
28
28
  validateInlineOptions(inlineOptions);
29
29
  const cache = new VitePluginSvelteCache();
30
- const pkg_resolve_errors = new Set();
31
30
  // updated in configResolved hook
32
31
  let requestParser: IdParser;
33
32
  let options: ResolvedOptions;
@@ -135,37 +134,28 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
135
134
  }
136
135
  return resolvedSvelteSSR;
137
136
  }
138
-
139
137
  try {
140
138
  const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
141
139
  if (resolved) {
142
140
  log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
143
141
  return resolved;
144
142
  }
145
- } catch (err) {
146
- pkg_resolve_errors.add(importee);
143
+ } catch (e) {
144
+ log.debug.once(
145
+ `error trying to resolve ${importee} from ${importer} via package.json svelte field `,
146
+ e
147
+ );
148
+ // this error most likely happens due to non-svelte related importee/importers so swallow it here
149
+ // in case it really way a svelte library, users will notice anyway. (lib not working due to failed resolve)
147
150
  }
148
151
  },
149
152
 
150
153
  async transform(code, id, opts) {
151
154
  const ssr = !!opts?.ssr;
152
155
  const svelteRequest = requestParser(id, ssr);
153
- if (!svelteRequest) {
156
+ if (!svelteRequest || svelteRequest.query.svelte) {
154
157
  return;
155
158
  }
156
- const { filename, query } = svelteRequest;
157
-
158
- if (query.svelte) {
159
- if (query.type === 'style') {
160
- const css = cache.getCSS(svelteRequest);
161
- if (css) {
162
- log.debug(`transform returns css for ${filename}`);
163
- return css; // TODO return code arg instead? it's the code from load hook.
164
- }
165
- }
166
- log.error('failed to transform tagged svelte request', svelteRequest);
167
- throw new Error(`failed to transform tagged svelte request for id ${id}`);
168
- }
169
159
  let compileData;
170
160
  try {
171
161
  compileData = await compileSvelte(svelteRequest, code, options);
@@ -179,7 +169,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
179
169
  ensureWatchedFile(options.server!.watcher, d, options.root);
180
170
  });
181
171
  }
182
- log.debug(`transform returns compiled js for ${filename}`);
172
+ log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
183
173
  return compileData.compiled.js;
184
174
  },
185
175
 
@@ -191,20 +181,6 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
191
181
  if (svelteRequest) {
192
182
  return handleHotUpdate(compileSvelte, ctx, svelteRequest, cache, options);
193
183
  }
194
- },
195
-
196
- /**
197
- * All resolutions done; display warnings wrt `package.json` access.
198
- */
199
- // TODO generateBundle isn't called by vite, is buildEnd enough or should it be logged once per violation in resolve
200
- buildEnd() {
201
- if (pkg_resolve_errors.size > 0) {
202
- log.warn(
203
- `vite-plugin-svelte was unable to find package.json of the following packages and wasn't able to resolve via their "svelte" field.
204
- If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.
205
- ${Array.from(pkg_resolve_errors, (s) => `- ${s}`).join('\n')}`.replace(/\t/g, '')
206
- );
207
- }
208
184
  }
209
185
  };
210
186
  }
@@ -1,15 +1,16 @@
1
- import { findRootSvelteDependencies } from '../dependencies';
1
+ import { findRootSvelteDependencies, needsOptimization } from '../dependencies';
2
2
  import * as path from 'path';
3
+ import { createRequire } from 'module';
3
4
 
4
5
  describe('dependencies', () => {
5
6
  describe('findRootSvelteDependencies', () => {
6
- it('should find svelte dependencies in packages/e2e-test/hmr', async () => {
7
+ it('should find svelte dependencies in packages/e2e-test/hmr', () => {
7
8
  const deps = findRootSvelteDependencies(path.resolve('packages/e2e-tests/hmr'));
8
9
  expect(deps).toHaveLength(1);
9
10
  expect(deps[0].name).toBe('e2e-test-dep-svelte-simple');
10
11
  expect(deps[0].path).toEqual([]);
11
12
  });
12
- it('should find nested svelte dependencies in packages/e2e-test/package-json-svelte-field', async () => {
13
+ it('should find nested svelte dependencies in packages/e2e-test/package-json-svelte-field', () => {
13
14
  const deps = findRootSvelteDependencies(
14
15
  path.resolve('packages/e2e-tests/package-json-svelte-field')
15
16
  );
@@ -26,4 +27,14 @@ describe('dependencies', () => {
26
27
  expect(simple.path[0]).toBe('e2e-test-dep-svelte-nested');
27
28
  });
28
29
  });
30
+ describe('needsOptimization', () => {
31
+ it('should optimize cjs deps only', () => {
32
+ const localRequire = createRequire(path.resolve('packages/e2e-tests/dependencies'));
33
+ expect(needsOptimization('e2e-test-dep-cjs-and-esm', localRequire)).toBe(false);
34
+ expect(needsOptimization('e2e-test-dep-cjs-only', localRequire)).toBe(true);
35
+ expect(needsOptimization('e2e-test-dep-esm-only', localRequire)).toBe(false);
36
+ expect(needsOptimization('e2e-test-dep-index-only', localRequire)).toBe(true);
37
+ expect(needsOptimization('e2e-test-dep-types-only', localRequire)).toBe(false);
38
+ });
39
+ });
29
40
  });
@@ -187,12 +187,23 @@ export function needsOptimization(dep: string, localRequire: NodeRequire): boole
187
187
  const pkg = depData.pkg;
188
188
  // only optimize if is cjs, using the below as heuristic
189
189
  // see https://github.com/sveltejs/vite-plugin-svelte/issues/162
190
- const isCjs = pkg.main && !pkg.module && !pkg.exports;
191
- if (!isCjs) return false;
192
- // ensure entry is js so vite can prebundle it
193
- // see https://github.com/sveltejs/vite-plugin-svelte/issues/233
194
- const entryExt = path.extname(pkg.main);
195
- return !entryExt || entryExt === '.js' || entryExt === '.cjs';
190
+ const hasEsmFields = pkg.module || pkg.exports;
191
+ if (hasEsmFields) return false;
192
+ if (pkg.main) {
193
+ // ensure entry is js so vite can prebundle it
194
+ // see https://github.com/sveltejs/vite-plugin-svelte/issues/233
195
+ const entryExt = path.extname(pkg.main);
196
+ return !entryExt || entryExt === '.js' || entryExt === '.cjs';
197
+ } else {
198
+ // check if has implicit index.js entrypoint
199
+ // https://github.com/sveltejs/vite-plugin-svelte/issues/281
200
+ try {
201
+ localRequire.resolve(`${dep}/index.js`);
202
+ return true;
203
+ } catch {
204
+ return false;
205
+ }
206
+ }
196
207
  }
197
208
 
198
209
  interface DependencyData {
@@ -16,11 +16,10 @@ const PREBUNDLE_SENSITIVE_OPTIONS: (keyof ResolvedOptions)[] = [
16
16
  export async function handleOptimizeDeps(options: ResolvedOptions, viteConfig: ResolvedConfig) {
17
17
  if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir) return;
18
18
 
19
- const viteMetadataPath = path.resolve(viteConfig.cacheDir, '_metadata.json');
19
+ const viteMetadataPath = findViteMetadataPath(viteConfig.cacheDir);
20
+ if (!viteMetadataPath) return;
20
21
 
21
- if (!fs.existsSync(viteMetadataPath)) return;
22
-
23
- const svelteMetadataPath = path.resolve(viteConfig.cacheDir, '_svelte_metadata.json');
22
+ const svelteMetadataPath = path.resolve(viteMetadataPath, '../_svelte_metadata.json');
24
23
  const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
25
24
  return typeof value === 'function' ? value.toString() : value;
26
25
  });
@@ -41,3 +40,11 @@ function generateSvelteMetadata(options: ResolvedOptions) {
41
40
  }
42
41
  return metadata;
43
42
  }
43
+
44
+ function findViteMetadataPath(cacheDir: string) {
45
+ const metadataPaths = ['_metadata.json', 'deps/_metadata.json'];
46
+ for (const metadataPath of metadataPaths) {
47
+ const viteMetadataPath = path.resolve(cacheDir, metadataPath);
48
+ if (fs.existsSync(viteMetadataPath)) return viteMetadataPath;
49
+ }
50
+ }
@@ -1,5 +1,5 @@
1
1
  import path from 'path';
2
- import { createRequire } from 'module';
2
+ import { builtinModules, createRequire } from 'module';
3
3
  import { is_common_without_svelte_field, resolveDependencyData } from './dependencies';
4
4
  import { VitePluginSvelteCache } from './vite-plugin-svelte-cache';
5
5
 
@@ -8,7 +8,12 @@ export function resolveViaPackageJsonSvelte(
8
8
  importer: string | undefined,
9
9
  cache: VitePluginSvelteCache
10
10
  ): string | void {
11
- if (importer && isBareImport(importee) && !is_common_without_svelte_field(importee)) {
11
+ if (
12
+ importer &&
13
+ isBareImport(importee) &&
14
+ !isNodeInternal(importee) &&
15
+ !is_common_without_svelte_field(importee)
16
+ ) {
12
17
  const cached = cache.getResolvedSvelteField(importee, importer);
13
18
  if (cached) {
14
19
  return cached;
@@ -22,12 +27,14 @@ export function resolveViaPackageJsonSvelte(
22
27
  cache.setResolvedSvelteField(importee, importer, result);
23
28
  return result;
24
29
  }
25
- } else {
26
- throw new Error(`failed to resolve package.json of ${importee} imported by ${importer}`);
27
30
  }
28
31
  }
29
32
  }
30
33
 
34
+ function isNodeInternal(importee: string) {
35
+ return importee.startsWith('node:') || builtinModules.includes(importee);
36
+ }
37
+
31
38
  function isBareImport(importee: string): boolean {
32
39
  if (
33
40
  !importee ||