Skip to content

Commit c4a5555

Browse files
authored
Support dynamic import scanning (#3581)
* Support dynamic import scanning This fixes dynamic import scanning of packages. * Adding a changeset
1 parent bc3a48e commit c4a5555

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

.changeset/curly-dancers-double.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'snowpack': patch
3+
---
4+
5+
Fixes scanning of dynamic imports on packages

snowpack/src/scan-imports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function matchDynamicImportValue(importStatement: string) {
8383
return matched?.[2] || matched?.[3] || null;
8484
}
8585

86-
function getWebModuleSpecifierFromCode(code: string, imp: ImportSpecifier) {
86+
export function getWebModuleSpecifierFromCode(code: string, imp: ImportSpecifier): string | null {
8787
// import.meta: we can ignore
8888
if (imp.d === -2) {
8989
return null;

snowpack/src/sources/local.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import slash from 'slash';
1616
import {getBuiltFileUrls} from '../build/file-urls';
1717
import {logger} from '../logger';
1818
import {scanCodeImportsExports, transformFileImports} from '../rewrite-imports';
19-
import {getInstallTargets} from '../scan-imports';
19+
import {getInstallTargets, getWebModuleSpecifierFromCode} from '../scan-imports';
2020
import {ImportMap, PackageOptionsLocal, PackageSource, SnowpackConfig} from '../types';
2121
import {
2222
createInstallTarget,
@@ -627,7 +627,14 @@ export class PackageSourceLocal implements PackageSource {
627627
const packageImports = new Set<string>();
628628
const code = loadedFile.toString('utf8');
629629
for (const imp of await scanCodeImportsExports(code)) {
630-
const spec = code.substring(imp.s, imp.e).replace(/(\/|\\)+$/, ''); // remove trailing slash from end of specifier (easier for Node to resolve)
630+
let spec = getWebModuleSpecifierFromCode(code, imp);
631+
if(spec === null) {
632+
continue;
633+
}
634+
635+
// remove trailing slash from end of specifier (easier for Node to resolve)
636+
spec = spec.replace(/(\/|\\)+$/, '');
637+
631638
if (isRemoteUrl(spec)) {
632639
continue;
633640
}

test/snowpack/runtime/runtime.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,46 @@ describe('runtime', () => {
238238
await fixture.cleanup();
239239
}
240240
});
241+
242+
it('Installs dynamic imports', async () => {
243+
const fixture = await testRuntimeFixture({
244+
'packages/@owner/dyn/package.json': dedent`
245+
{
246+
"version": "1.0.0",
247+
"name": "@owner/dyn",
248+
"module": "main.js"
249+
}
250+
`,
251+
'packages/@owner/dyn/sub/path.js': dedent`
252+
export default 2;
253+
`,
254+
'package.json': dedent`
255+
{
256+
"version": "1.0.1",
257+
"name": "@snowpack/test-runtime-import-dynamic-pkg",
258+
"dependencies": {
259+
"@owner/dyn": "file:./packages/@owner/dyn"
260+
}
261+
}
262+
`,
263+
'main.js': dedent`
264+
const promise = import('@owner/dyn/sub/path.js');
265+
266+
export default async function() {
267+
let mod = await promise;
268+
return mod.default;
269+
}
270+
`
271+
});
272+
273+
try {
274+
let mod = await fixture.runtime.importModule('/main.js');
275+
let promise = mod.exports.default();
276+
let value = await promise;
277+
console.log(value);
278+
expect(value).toEqual(2);
279+
} finally {
280+
await fixture.cleanup();
281+
}
282+
});
241283
});

0 commit comments

Comments
 (0)