Skip to content

Commit

Permalink
Fixes dynamic import regression (#3684)
Browse files Browse the repository at this point in the history
* Fixes dynamic import regression

* Adds a changeset
  • Loading branch information
matthewp authored Aug 26, 2021
1 parent 96315b0 commit 57ee22f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-coins-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'snowpack': patch
---

Fix for dynamic import scanning
12 changes: 7 additions & 5 deletions snowpack/src/sources/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 17 additions & 10 deletions test/snowpack/runtime/runtime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
`,
});
Expand All @@ -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();
}
Expand Down

1 comment on commit 57ee22f

@vercel
Copy link

@vercel vercel bot commented on 57ee22f Aug 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.