Skip to content

Commit

Permalink
use a rollup plugin to add style import to output bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
csr632 committed Dec 12, 2022
1 parent 0852281 commit 735266f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
3 changes: 2 additions & 1 deletion packages/theme-doc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"build": "npm run clean && concurrently \"npm:build-*\"",
"build-dts": "tsc",
"build-copy-files": "node ./scripts/copy.mjs",
"build-rollup": "rollup -c && node scripts/add-css-import.js",
"build-rollup": "rollup -c",
"dev": "npm run clean && concurrently \"npm:dev-*\"",
"dev-dts": "tsc --watch",
"dev-rollup": "rollup -wc",
Expand All @@ -50,6 +50,7 @@
"github-slugger": "^2.0.0",
"globby": "^13.1.2",
"less": "^4.1.3",
"magic-string": "^0.27.0",
"postcss": "^8.4.19",
"prism-react-renderer": "^1.3.5",
"rc-footer": "^0.6.6",
Expand Down
25 changes: 25 additions & 0 deletions packages/theme-doc/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import postcss from 'rollup-plugin-postcss'
import babel from '@rollup/plugin-babel'
import commonjs from '@rollup/plugin-commonjs'
import resolve from '@rollup/plugin-node-resolve'
import MagicString from 'magic-string'

const extensions = ['.js', '.jsx', '.ts', '.tsx']

Expand Down Expand Up @@ -64,5 +65,29 @@ export default {
},
extract: 'index.css',
}),
{
name: 'add-css-import',
async renderChunk(code, chunk, options, meta) {
debugger
if (chunk.fileName === 'index.js' && chunk.isEntry) {
chunk.imports.push('./index.css')
chunk.importedBindings['./index.css'] = []
const s = new MagicString(code)
if (options.format === 'cjs') {
if (code.startsWith(`'use strict';`)) {
s.remove(0, `'use strict';`.length)
}
s.prepend(`'use strict';\nrequire('./index.css');\n`)
} else {
s.prepend(`import './index.css';\n`)
}
const map = s.generateMap({ hires: true })
return {
code: s.toString(),
map,
}
}
},
},
],
}
17 changes: 0 additions & 17 deletions packages/theme-doc/scripts/add-css-import.js

This file was deleted.

2 changes: 2 additions & 0 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 735266f

Please sign in to comment.