Skip to content

Commit

Permalink
feat: add support for named export list (#3)
Browse files Browse the repository at this point in the history
* add support for named exports in curly braces

e.g. export { foo };

* allow named exports to coexist with default exports

* add "default" named export to CJS

* Revert "add "default" named export to CJS"

This reverts commit 31ae209

* add tests
  • Loading branch information
d-fischer committed Aug 19, 2020
1 parent 6315dae commit ac1d229
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
5 changes: 4 additions & 1 deletion index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,14 @@ function run(filepath, isMode) {
.replace(/(^|\s|;)export default/, '$1module.exports =')
.replace(/(^|\s|;)export (const|(?:async )?function|class|let|var) (.+?)(?=(\(|\s|\=))/gi, (_, x, type, name) => {
return keys.push(name) && `${x}${type} ${name}`;
})
.replace(/(^|\s|;)export \{(.+?)}(?=(;|\s|$))/, (_, x, names) => {
return keys.push(...(names.split(',').map(name => name.trim()))) && x;
});

if (keys.length > 0) {
keys.sort().forEach(key => {
CJS += `\nexports.${key} = ${key};`;
CJS += `\nmodule.exports.${key} = ${key};`;
});
}

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/curly-named-exports/expects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cjs": "dist/curly-named.js",
"esm": "dist/curly-named.mjs",
"umd": "dist/curly-named.min.js"
}
6 changes: 6 additions & 0 deletions test/fixtures/curly-named-exports/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "named-exports",
"unpkg": "dist/curly-named.min.js",
"module": "dist/curly-named.mjs",
"main": "dist/curly-named.js"
}
19 changes: 19 additions & 0 deletions test/fixtures/curly-named-exports/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function foo(a) {
return a + a;
}

const bar = b => b + b;

async function baz(a) {
return a + a;
}

let hello = 'world';

var abc = 123;

class Foo extends Component {
//
}

export { foo, bar, baz, hello, abc, Foo };
2 changes: 1 addition & 1 deletion test/index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const fixtures = join(__dirname, 'fixtures');

const tests = {
cjs: /(module\.)?exports(\.?)/,
esm: /export (default )?(function|const|class|let|var)/,
esm: /export ((default )?(function|const|class|let|var)|{)/,
umd: new RegExp(`"object"==typeof exports&&"undefined"!=typeof module`)
};

Expand Down

0 comments on commit ac1d229

Please sign in to comment.