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

Support dynamic import scanning #3581

Merged
merged 2 commits into from Jul 13, 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/curly-dancers-double.md
@@ -0,0 +1,5 @@
---
'snowpack': patch
---

Fixes scanning of dynamic imports on packages
2 changes: 1 addition & 1 deletion snowpack/src/scan-imports.ts
Expand Up @@ -83,7 +83,7 @@ export function matchDynamicImportValue(importStatement: string) {
return matched?.[2] || matched?.[3] || null;
}

function getWebModuleSpecifierFromCode(code: string, imp: ImportSpecifier) {
export function getWebModuleSpecifierFromCode(code: string, imp: ImportSpecifier): string | null {
// import.meta: we can ignore
if (imp.d === -2) {
return null;
Expand Down
11 changes: 9 additions & 2 deletions snowpack/src/sources/local.ts
Expand Up @@ -16,7 +16,7 @@ import slash from 'slash';
import {getBuiltFileUrls} from '../build/file-urls';
import {logger} from '../logger';
import {scanCodeImportsExports, transformFileImports} from '../rewrite-imports';
import {getInstallTargets} from '../scan-imports';
import {getInstallTargets, getWebModuleSpecifierFromCode} from '../scan-imports';
import {ImportMap, PackageOptionsLocal, PackageSource, SnowpackConfig} from '../types';
import {
createInstallTarget,
Expand Down Expand Up @@ -627,7 +627,14 @@ export class PackageSourceLocal implements PackageSource {
const packageImports = new Set<string>();
const code = loadedFile.toString('utf8');
for (const imp of await scanCodeImportsExports(code)) {
const spec = code.substring(imp.s, imp.e).replace(/(\/|\\)+$/, ''); // remove trailing slash from end of specifier (easier for Node to resolve)
let spec = getWebModuleSpecifierFromCode(code, imp);
if(spec === null) {
continue;
}

// remove trailing slash from end of specifier (easier for Node to resolve)
spec = spec.replace(/(\/|\\)+$/, '');

if (isRemoteUrl(spec)) {
continue;
}
Expand Down
42 changes: 42 additions & 0 deletions test/snowpack/runtime/runtime.test.js
Expand Up @@ -238,4 +238,46 @@ describe('runtime', () => {
await fixture.cleanup();
}
});

it('Installs dynamic imports', async () => {
const fixture = await testRuntimeFixture({
'packages/@owner/dyn/package.json': dedent`
{
"version": "1.0.0",
"name": "@owner/dyn",
"module": "main.js"
}
`,
'packages/@owner/dyn/sub/path.js': dedent`
export default 2;
`,
'package.json': dedent`
{
"version": "1.0.1",
"name": "@snowpack/test-runtime-import-dynamic-pkg",
"dependencies": {
"@owner/dyn": "file:./packages/@owner/dyn"
}
}
`,
'main.js': dedent`
const promise = import('@owner/dyn/sub/path.js');

export default async function() {
let mod = await promise;
return mod.default;
}
`
});

try {
let mod = await fixture.runtime.importModule('/main.js');
let promise = mod.exports.default();
let value = await promise;
console.log(value);
expect(value).toEqual(2);
} finally {
await fixture.cleanup();
}
});
});