Skip to content

Commit

Permalink
feat(node-resolve): preserve search params and hashes (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsDenBakker authored Jul 12, 2020
1 parent 30a8c91 commit ec92d34
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ export function nodeResolve(opts = {}) {
// ignore IDs with null character, these belong to other plugins
if (/\0/.test(importee)) return null;

// strip hash and query params from import
const [withoutHash, hash] = importee.split('#');
const [importPath, params] = withoutHash.split('?');
const importSuffix = `${params ? `?${params}` : ''}${hash ? `#${hash}` : ''}`;
importee = importPath;

const basedir = !importer || dedupe(importee) ? rootDir : dirname(importer);

// https://github.com/defunctzombie/package-browser-field-spec
Expand Down Expand Up @@ -244,11 +250,17 @@ export function nodeResolve(opts = {}) {
if (resolved && options.modulesOnly) {
const code = await readFile(resolved, 'utf-8');
if (isModule(code)) {
return { id: resolved, moduleSideEffects: hasModuleSideEffects(resolved) };
return {
id: `${resolved}${importSuffix}`,
moduleSideEffects: hasModuleSideEffects(resolved)
};
}
return null;
}
const result = { id: resolved, moduleSideEffects: hasModuleSideEffects(resolved) };
const result = {
id: `${resolved}${importSuffix}`,
moduleSideEffects: hasModuleSideEffects(resolved)
};
return result;
} catch (error) {
return null;
Expand Down
3 changes: 3 additions & 0 deletions packages/node-resolve/test/fixtures/hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import test from 'test#foo';

export default test;
3 changes: 3 additions & 0 deletions packages/node-resolve/test/fixtures/search-params-and-hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import test from 'test?foo=bar&lorem=ipsum#foo';

export default test;
3 changes: 3 additions & 0 deletions packages/node-resolve/test/fixtures/search-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import test from 'test?foo=bar&lorem=ipsum';

export default test;
75 changes: 75 additions & 0 deletions packages/node-resolve/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,78 @@ test('handles package side-effects', async (t) => {

delete global.sideEffects;
});

test('can resolve imports with hashes', async (t) => {
const bundle = await rollup({
input: 'hash.js',
onwarn: () => t.fail('No warnings were expected'),
plugins: [
nodeResolve(),
{
load(id) {
if (id === resolve(__dirname, 'fixtures', 'node_modules', 'test', 'index.js#foo')) {
return 'export default "resolved with hash"';
}
return null;
}
}
]
});
const { module } = await testBundle(t, bundle);

t.is(module.exports, 'resolved with hash');
});

test('can resolve imports with search params', async (t) => {
const bundle = await rollup({
input: 'search-params.js',
onwarn: () => t.fail('No warnings were expected'),
plugins: [
nodeResolve(),
{
load(id) {
if (
id ===
resolve(__dirname, 'fixtures', 'node_modules', 'test', 'index.js?foo=bar&lorem=ipsum')
) {
return 'export default "resolved with search params"';
}
return null;
}
}
]
});
const { module } = await testBundle(t, bundle);

t.is(module.exports, 'resolved with search params');
});

test('can resolve imports with search params and hash', async (t) => {
const bundle = await rollup({
input: 'search-params-and-hash.js',
onwarn: () => t.fail('No warnings were expected'),
plugins: [
nodeResolve(),
{
load(id) {
if (
id ===
resolve(
__dirname,
'fixtures',
'node_modules',
'test',
'index.js?foo=bar&lorem=ipsum#foo'
)
) {
return 'export default "resolved with search params and hash"';
}
return null;
}
}
]
});
const { module } = await testBundle(t, bundle);

t.is(module.exports, 'resolved with search params and hash');
});

0 comments on commit ec92d34

Please sign in to comment.