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

Check externals before resolving entrypoint #3479

Merged
merged 1 commit into from
Jun 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions snowpack/src/sources/local.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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