Skip to content

Commit fbb311e

Browse files
feat: add support for named exports (#38)
1 parent 3ab3653 commit fbb311e

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

lib/plugin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ export default new Proxy(${classNamesMapString}, {
8787
`
8888
: `export default ${classNamesMapString};`;
8989

90-
const js = `${importStatement}\n${exportStatement};`;
90+
const namedExportStatements = Object.entries(cssModulesJSON).map(
91+
([camelCaseClassName, className]) => `export const ${camelCaseClassName} = "${className}";`
92+
).join('\n');
93+
94+
const js = `${importStatement}\n${exportStatement};\n${namedExportStatements}`;
9195

9296
return {
9397
js,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import * as styles from '../styles/app.modules.css';
3+
import * as styles2 from '../styles/deep/styles/hello.modules.css';
4+
5+
export const HelloWorld = () => (
6+
<>
7+
<h3 className={styles.helloWorld}>Hello World!</h3>
8+
<p className={styles2.helloText}>hi...</p>
9+
</>
10+
);

test/named-exports.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import ReactDom from 'react-dom';
3+
4+
import { HelloWorld } from './components/named-exports.world';
5+
6+
const App = () => {
7+
return <HelloWorld/>;
8+
};
9+
10+
ReactDom.render(<App/>, document.body);

test/test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ fse.emptyDirSync('./dist');
7474

7575
await esbuild.build({
7676
entryPoints: {
77-
['custom-entry-name']: 'app.jsx'
77+
['custom-entry-name']: 'app.jsx',
78+
['named-exports']: 'named-exports.jsx'
7879
},
7980
entryNames: '[name]-[hash]',
8081
format: 'esm',
@@ -99,7 +100,8 @@ fse.emptyDirSync('./dist');
99100

100101
await esbuild.build({
101102
entryPoints: {
102-
['custom-entry-name']: 'app.jsx'
103+
['custom-entry-name']: 'app.jsx',
104+
['named-exports']: 'named-exports.jsx'
103105
},
104106
entryNames: '[name]-[hash]',
105107
format: 'esm',
@@ -133,7 +135,7 @@ fse.emptyDirSync('./dist');
133135
console.log('[test][esbuild:bundle:v2] done, please check `test/dist/bundle-v2-custom-inject`', '\n');
134136

135137
await esbuild.build({
136-
entryPoints: ['app.jsx'],
138+
entryPoints: ['app.jsx', 'named-exports.jsx'],
137139
entryNames: '[name]-[hash]',
138140
format: 'esm',
139141
target: ['esnext'],

0 commit comments

Comments
 (0)