Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes dynamic import regression #3684

Merged
merged 2 commits into from Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/itchy-coins-sort.md
@@ -0,0 +1,5 @@
---
'snowpack': patch
---

Fix for dynamic import scanning
12 changes: 7 additions & 5 deletions snowpack/src/sources/local.ts
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
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