Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit fcd9826

Browse files
bterlsonlukastaegert
authored andcommitted
Normalize ids before looking up in named export map (#406)
* Normalize ids before looking up in named export map * Fix path normalization tests on linux * clarify normalization comments
1 parent e431c29 commit fcd9826

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { realpathSync, existsSync } from 'fs';
2-
import { extname, resolve } from 'path';
2+
import { extname, resolve, normalize } from 'path';
33
import { sync as nodeResolveSync, isCore } from 'resolve';
44
import { createFilter } from 'rollup-pluginutils';
55
import { peerDependencies } from '../package.json';
@@ -40,6 +40,10 @@ export default function commonjs(options = {}) {
4040
} catch (err) {
4141
resolvedId = resolve(id);
4242
}
43+
44+
// Note: customNamedExport's keys must be normalized file paths.
45+
// resolve and nodeResolveSync both return normalized file paths
46+
// so no additional normalization is necessary.
4347
customNamedExports[resolvedId] = options.namedExports[id];
4448

4549
if (existsSync(resolvedId)) {
@@ -81,14 +85,16 @@ export default function commonjs(options = {}) {
8185
return null;
8286
}
8387

88+
const normalizedId = normalize(id);
89+
8490
const transformed = transformCommonjs(
8591
this.parse,
8692
code,
8793
id,
8894
this.getModuleInfo(id).isEntry,
8995
ignoreGlobal,
9096
ignoreRequire,
91-
customNamedExports[id],
97+
customNamedExports[normalizedId],
9298
sourceMap,
9399
allowDynamicRequire,
94100
ast

test/test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,5 +822,39 @@ exports.shuffleArray = shuffleArray_1;
822822
`
823823
);
824824
});
825+
826+
it('normalizes paths used in the named export map', async () => {
827+
// Deliberately denormalizes file paths and ensures named exports
828+
// continue to work.
829+
function hookedResolve() {
830+
const resolvePlugin = resolve();
831+
const oldResolve = resolvePlugin.resolveId;
832+
resolvePlugin.resolveId = async function() {
833+
const result = await oldResolve.apply(resolvePlugin, arguments);
834+
if (result) {
835+
result.id = result.id.replace(/\/|\\/, path.sep);
836+
}
837+
838+
return result;
839+
};
840+
841+
return resolvePlugin;
842+
}
843+
844+
const bundle = await rollup({
845+
input: 'samples/custom-named-exports/main.js',
846+
plugins: [
847+
hookedResolve(),
848+
commonjs({
849+
namedExports: {
850+
'samples/custom-named-exports/secret-named-exporter.js': ['named'],
851+
external: ['message']
852+
}
853+
})
854+
]
855+
});
856+
857+
await executeBundle(bundle);
858+
});
825859
});
826860
});

0 commit comments

Comments
 (0)