Skip to content

Commit

Permalink
Support dynamic import scanning (#3581)
Browse files Browse the repository at this point in the history
* Support dynamic import scanning

This fixes dynamic import scanning of packages.

* Adding a changeset
  • Loading branch information
matthewp authored Jul 13, 2021
1 parent bc3a48e commit c4a5555
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-dancers-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'snowpack': patch
---

Fixes scanning of dynamic imports on packages
2 changes: 1 addition & 1 deletion snowpack/src/scan-imports.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
}
});
});

1 comment on commit c4a5555

@vercel
Copy link

@vercel vercel bot commented on c4a5555 Jul 13, 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.