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

Normalize ids before looking up in named export map #406

Merged
merged 4 commits into from Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/index.js
@@ -1,5 +1,5 @@
import { realpathSync, existsSync } from 'fs';
import { extname, resolve } from 'path';
import { extname, resolve, normalize } from 'path';
import { sync as nodeResolveSync, isCore } from 'resolve';
import { createFilter } from 'rollup-pluginutils';
import { peerDependencies } from '../package.json';
Expand Down Expand Up @@ -40,6 +40,10 @@ export default function commonjs(options = {}) {
} catch (err) {
resolvedId = resolve(id);
}

// Note: customNamedExport's keys must be normalized file paths.
// resolve and nodeResolveSync both return normalized file paths
// so no additional normalization is necessary.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm cool with this, this is educational and will help people not familiar with the code 👍

customNamedExports[resolvedId] = options.namedExports[id];

if (existsSync(resolvedId)) {
Expand Down Expand Up @@ -81,14 +85,16 @@ export default function commonjs(options = {}) {
return null;
}

const normalizedId = normalize(id);

const transformed = transformCommonjs(
this.parse,
code,
id,
this.getModuleInfo(id).isEntry,
ignoreGlobal,
ignoreRequire,
customNamedExports[id],
customNamedExports[normalizedId],
sourceMap,
allowDynamicRequire,
ast
Expand Down
34 changes: 34 additions & 0 deletions test/test.js
Expand Up @@ -822,5 +822,39 @@ exports.shuffleArray = shuffleArray_1;
`
);
});

it('normalizes paths used in the named export map', async () => {
// Deliberately denormalizes file paths and ensures named exports
// continue to work.
function hookedResolve() {
const resolvePlugin = resolve();
const oldResolve = resolvePlugin.resolveId;
resolvePlugin.resolveId = async function() {
const result = await oldResolve.apply(resolvePlugin, arguments);
if (result) {
result.id = result.id.replace(/\/|\\/, path.sep);
}

return result;
};

return resolvePlugin;
}

const bundle = await rollup({
input: 'samples/custom-named-exports/main.js',
plugins: [
hookedResolve(),
commonjs({
namedExports: {
'samples/custom-named-exports/secret-named-exporter.js': ['named'],
external: ['message']
}
})
]
});

await executeBundle(bundle);
});
});
});