Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(legacy): normalize exports paths #775

Merged
merged 3 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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.