Skip to content

Commit

Permalink
fix(react-styles): support esm import and export (#7084)
Browse files Browse the repository at this point in the history
* fix(react-styles): support esm import and export

The previous version are using `require()`, which is not fully
support esm

Add esm only output to support some pure esm project like vite or
snowpack

* fix(build): add inlineDynamicImports to rollup config
  • Loading branch information
kunyan authored Apr 11, 2022
1 parent 15786a5 commit b66ea14
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/react-styles/scripts/writeClassMaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ exports.default = ${JSON.stringify(classMap, null, 2)};
`.trim()
);

const writeESMExport = (file, classMap) =>
outputFileSync(
join(outDir, file.replace(/.css$/, '.mjs')),
`
import('./${basename(file, '.css.js')}');
export default ${JSON.stringify(classMap, null, 2)};
`.trim()
);

const writeDTSExport = (file, classMap) =>
outputFileSync(
join(outDir, file.replace(/.css$/, '.d.ts')),
Expand All @@ -36,6 +45,7 @@ function writeClassMaps(classMaps) {

writeCJSExport(outPath, classMap);
writeDTSExport(outPath, classMap);
writeESMExport(outPath, classMap);
copyFileSync(file, join(outDir, outPath));
});

Expand Down
1 change: 1 addition & 0 deletions packages/rollup.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = ({ packageName, name }) => ({
output: {
file: `dist/umd/${packageName}${isProduction ? '.min' : ''}.js`,
format: 'umd',
inlineDynamicImports: true,
name,
globals: {
react: 'React',
Expand Down

0 comments on commit b66ea14

Please sign in to comment.