Skip to content

Commit

Permalink
feat(legacy): normalize exports paths (#775)
Browse files Browse the repository at this point in the history
* feat(legacy): normalize exports paths

* test(legacy): normalized paths

* chore: update pnpm-lock.yaml
  • Loading branch information
patak-dev committed Jan 29, 2021
1 parent 2711aa8 commit 03e32d2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
3 changes: 3 additions & 0 deletions packages/legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
"peerDependencies": {
"rollup": "^1.20.0||^2.0.0"
},
"dependencies": {
"@rollup/pluginutils": "^4.1.0"
},
"devDependencies": {
"@rollup/plugin-buble": "^0.21.3",
"del-cli": "^3.0.1",
Expand Down
9 changes: 6 additions & 3 deletions packages/legacy/src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { resolve } from 'path';

import { normalizePath } from '@rollup/pluginutils';

const validName = /^[a-zA-Z_$][a-zA-Z$_0-9]*$/;

export default function legacy(options) {
const exports = {};
Object.keys(options).forEach((file) => {
exports[resolve(file)] = options[file];
exports[normalizePath(resolve(file))] = options[file];
});

return {
name: 'legacy',

transform(content, id) {
if (id in exports) {
const normalizedId = normalizePath(id);
if (normalizedId in exports) {
let code = content;
const value = exports[id];
const value = exports[normalizedId];

if (typeof value === 'string') {
// default export
Expand Down
26 changes: 26 additions & 0 deletions packages/legacy/test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const path = require('path');

const test = require('ava');
const { rollup } = require('rollup');

const { normalizePath } = require('@rollup/pluginutils');

const { testBundle } = require('../../../util/test');

const legacy = require('..');
Expand Down Expand Up @@ -64,3 +68,25 @@ test('adds a unchanged named export', async (t) => {
t.plan(1);
await testBundle(t, bundle);
});

test('normalized paths', async (t) => {
const bundle = await rollup({
input: 'fixtures/default-export/main.js',
plugins: [
{
name: 'NormalizedPath',
resolveId(importee) {
if (importee === './answer') {
return normalizePath(path.resolve('./fixtures/default-export/answer.js'));
}
return null;
}
},
legacy({
'./fixtures/default-export/answer.js': 'answer'
})
]
});
t.plan(1);
await testBundle(t, bundle);
});
33 changes: 17 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 03e32d2

Please sign in to comment.