Skip to content

Commit

Permalink
Check externals before resolving entrypoint (#3479)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Jun 23, 2021
1 parent c1ee4d1 commit 110043e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
13 changes: 7 additions & 6 deletions snowpack/src/sources/local.ts
Expand Up @@ -677,6 +677,13 @@ export class PackageSourceLocal implements PackageSource {
spec = spec.replace(from, to);
}

const [packageName] = parsePackageImportSpecifier(spec);

// If this import is marked as external, do not transform the original spec
if (this.isExternal(packageName, spec)) {
return spec;
}

const entrypoint = resolveEntrypoint(spec, {
cwd: source,
packageLookupFields: [
Expand All @@ -694,12 +701,6 @@ export class PackageSourceLocal implements PackageSource {
return path.posix.join(config.buildOptions.metaUrlPath, 'pkg', memoizedResolve[entrypoint]);
}
}
const [packageName] = parsePackageImportSpecifier(spec);

// If this import is marked as external, do not transform the original spec
if (this.isExternal(packageName, spec)) {
return spec;
}

const isSymlink = !entrypoint.includes(path.join('node_modules', packageName));
const isWithinRoot = config.workspaceRoot && entrypoint.startsWith(config.workspaceRoot);
Expand Down
26 changes: 25 additions & 1 deletion test/snowpack/runtime/runtime.test.js
Expand Up @@ -53,24 +53,47 @@ describe('runtime', () => {
// https://github.com/nodejs/node/issues/35889
it.skip('Provides import.meta.fileURL in SSR', async () => {
const fixture = await testRuntimeFixture({
'packages/dep/package.json': dedent`
{
"name": "@snowpack/test-runtime-metaurl-dep",
"version": "0.0.1",
"main": "main.js"
}
`,
'packages/dep/main.js': dedent`
import fs from 'node:fs';
const readFile = fs.promises.readFile;
export async function getData(url) {
const json = await fs.readFile(url, 'utf-8');
const data = JSON.parse(json);
return data;
}
`,
'main.js': dedent`
import fs from 'node:fs/promises';
import { getData as getDataDepFn } from '@snowpack/test-runtime-metaurl-dep';
const url = new URL('./data.json', import.meta.url);
const depUrl = new URL('./packages/dep/package.json', import.meta.url);
export async function getData() {
const json = await fs.readFile(url, 'utf-8');
const data = JSON.parse(json);
return data;
}
export async function getDepVersion() {
return (await getDataDepFn(depUrl)).version;
}
`,
'data.json': dedent`
[ 1, 2 ]
`,
'package.json': dedent`
{
"version": "1.0.1",
"name": "@snowpack/test-runtime-invalidate"
"name": "@snowpack/test-runtime-metaurl"
}
`,
'snowpack.config.json': dedent`
Expand All @@ -86,6 +109,7 @@ describe('runtime', () => {
let mod = await fixture.runtime.importModule('/main.js');

expect(await mod.exports.getData()).toStrictEqual([1, 2]);
expect(await mod.exports.getDepVersion()).equal('0.0.1');
} finally {
await fixture.cleanup();
}
Expand Down

1 comment on commit 110043e

@vercel
Copy link

@vercel vercel bot commented on 110043e Jun 23, 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.