Skip to content

Commit ec92d34

Browse files
feat(node-resolve): preserve search params and hashes (#487)
1 parent 30a8c91 commit ec92d34

File tree

5 files changed

+98
-2
lines changed

5 files changed

+98
-2
lines changed

packages/node-resolve/src/index.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ export function nodeResolve(opts = {}) {
9090
// ignore IDs with null character, these belong to other plugins
9191
if (/\0/.test(importee)) return null;
9292

93+
// strip hash and query params from import
94+
const [withoutHash, hash] = importee.split('#');
95+
const [importPath, params] = withoutHash.split('?');
96+
const importSuffix = `${params ? `?${params}` : ''}${hash ? `#${hash}` : ''}`;
97+
importee = importPath;
98+
9399
const basedir = !importer || dedupe(importee) ? rootDir : dirname(importer);
94100

95101
// https://github.com/defunctzombie/package-browser-field-spec
@@ -244,11 +250,17 @@ export function nodeResolve(opts = {}) {
244250
if (resolved && options.modulesOnly) {
245251
const code = await readFile(resolved, 'utf-8');
246252
if (isModule(code)) {
247-
return { id: resolved, moduleSideEffects: hasModuleSideEffects(resolved) };
253+
return {
254+
id: `${resolved}${importSuffix}`,
255+
moduleSideEffects: hasModuleSideEffects(resolved)
256+
};
248257
}
249258
return null;
250259
}
251-
const result = { id: resolved, moduleSideEffects: hasModuleSideEffects(resolved) };
260+
const result = {
261+
id: `${resolved}${importSuffix}`,
262+
moduleSideEffects: hasModuleSideEffects(resolved)
263+
};
252264
return result;
253265
} catch (error) {
254266
return null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import test from 'test#foo';
2+
3+
export default test;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import test from 'test?foo=bar&lorem=ipsum#foo';
2+
3+
export default test;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import test from 'test?foo=bar&lorem=ipsum';
2+
3+
export default test;

packages/node-resolve/test/test.js

+75
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,78 @@ test('handles package side-effects', async (t) => {
239239

240240
delete global.sideEffects;
241241
});
242+
243+
test('can resolve imports with hashes', async (t) => {
244+
const bundle = await rollup({
245+
input: 'hash.js',
246+
onwarn: () => t.fail('No warnings were expected'),
247+
plugins: [
248+
nodeResolve(),
249+
{
250+
load(id) {
251+
if (id === resolve(__dirname, 'fixtures', 'node_modules', 'test', 'index.js#foo')) {
252+
return 'export default "resolved with hash"';
253+
}
254+
return null;
255+
}
256+
}
257+
]
258+
});
259+
const { module } = await testBundle(t, bundle);
260+
261+
t.is(module.exports, 'resolved with hash');
262+
});
263+
264+
test('can resolve imports with search params', async (t) => {
265+
const bundle = await rollup({
266+
input: 'search-params.js',
267+
onwarn: () => t.fail('No warnings were expected'),
268+
plugins: [
269+
nodeResolve(),
270+
{
271+
load(id) {
272+
if (
273+
id ===
274+
resolve(__dirname, 'fixtures', 'node_modules', 'test', 'index.js?foo=bar&lorem=ipsum')
275+
) {
276+
return 'export default "resolved with search params"';
277+
}
278+
return null;
279+
}
280+
}
281+
]
282+
});
283+
const { module } = await testBundle(t, bundle);
284+
285+
t.is(module.exports, 'resolved with search params');
286+
});
287+
288+
test('can resolve imports with search params and hash', async (t) => {
289+
const bundle = await rollup({
290+
input: 'search-params-and-hash.js',
291+
onwarn: () => t.fail('No warnings were expected'),
292+
plugins: [
293+
nodeResolve(),
294+
{
295+
load(id) {
296+
if (
297+
id ===
298+
resolve(
299+
__dirname,
300+
'fixtures',
301+
'node_modules',
302+
'test',
303+
'index.js?foo=bar&lorem=ipsum#foo'
304+
)
305+
) {
306+
return 'export default "resolved with search params and hash"';
307+
}
308+
return null;
309+
}
310+
}
311+
]
312+
});
313+
const { module } = await testBundle(t, bundle);
314+
315+
t.is(module.exports, 'resolved with search params and hash');
316+
});

0 commit comments

Comments
 (0)