diff --git a/.changeset/itchy-coins-sort.md b/.changeset/itchy-coins-sort.md new file mode 100644 index 0000000000..e5c1dbcf98 --- /dev/null +++ b/.changeset/itchy-coins-sort.md @@ -0,0 +1,5 @@ +--- +'snowpack': patch +--- + +Fix for dynamic import scanning diff --git a/snowpack/src/sources/local.ts b/snowpack/src/sources/local.ts index 2f629b7a0f..a365575c07 100644 --- a/snowpack/src/sources/local.ts +++ b/snowpack/src/sources/local.ts @@ -648,18 +648,20 @@ export class PackageSourceLocal implements PackageSource { continue; } - const scannedImport = code.substring(imp.s, imp.e).replace(/(\/|\\)+$/, ''); // remove trailing slash from end of specifier (easier for Node to resolve) - if (isRemoteUrl(scannedImport)) { + // remove trailing slash from end of specifier (easier for Node to resolve) + spec = spec.replace(/(\/|\\)+$/, ''); + + if (isRemoteUrl(spec)) { continue; // ignore remote files } - if (isPathImport(scannedImport)) { + if (isPathImport(spec)) { continue; } - const [scannedPackageName] = parsePackageImportSpecifier(scannedImport); + const [scannedPackageName] = parsePackageImportSpecifier(spec); if (scannedPackageName && memoizedImportMap[scannedPackageName]) { continue; // if we’ve already installed this, then don’t reinstall } - newPackageImports.add(scannedImport); + newPackageImports.add(spec); } for (const packageImport of newPackageImports) { diff --git a/test/snowpack/runtime/runtime.test.js b/test/snowpack/runtime/runtime.test.js index 8c0e894978..686a5527a3 100644 --- a/test/snowpack/runtime/runtime.test.js +++ b/test/snowpack/runtime/runtime.test.js @@ -241,31 +241,39 @@ describe('runtime', () => { it('Installs dynamic imports', async () => { const fixture = await testRuntimeFixture({ - 'packages/@owner/dyn/package.json': dedent` + 'packages/@owner/dyn2/package.json': dedent` { "version": "1.0.0", - "name": "@owner/dyn", + "name": "@owner/dyn2", "module": "main.js" } `, - 'packages/@owner/dyn/sub/path.js': dedent` - export default 2; + 'packages/@owner/dyn2/sub/sibling.js': dedent` + export default 3; + `, + 'packages/@owner/dyn2/sub/path.js': dedent` + const promise = import('./sibling.js'); + + export default async function() { + let mod = await promise; + return mod.default; + } `, 'package.json': dedent` { "version": "1.0.1", "name": "@snowpack/test-runtime-import-dynamic-pkg", "dependencies": { - "@owner/dyn": "file:./packages/@owner/dyn" + "@owner/dyn2": "file:./packages/@owner/dyn2" } } `, 'main.js': dedent` - const promise = import('@owner/dyn/sub/path.js'); + import fn from '@owner/dyn2/sub/path.js'; export default async function() { - let mod = await promise; - return mod.default; + let value = await fn(); + return value; } `, }); @@ -274,8 +282,7 @@ describe('runtime', () => { let mod = await fixture.runtime.importModule('/main.js'); let promise = mod.exports.default(); let value = await promise; - console.log(value); - expect(value).toEqual(2); + expect(value).toEqual(3); } finally { await fixture.cleanup(); }