Skip to content

Commit 967f8d5

Browse files
authored
Add --css inline option (#769)
* Add --css inline option Fixes #744 * Add CLI option * Add docs for --css and update --css-modules docs * Create rude-keys-eat.md * update snapshot
1 parent 25b2b62 commit 967f8d5

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

.changeset/rude-keys-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"microbundle": minor
3+
---
4+
5+
Add `--css inline` option. The default CSS output for all formats is now external files (as it was supposed to be).

README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,24 @@ Just point the input to a `.ts` file through either the cli or the `source` key
163163

164164
Microbundle will generally respect your TypeScript config defined in a `tsconfig.json` file with notable exceptions being the "[target](https://www.typescriptlang.org/tsconfig#target)" and "[module](https://www.typescriptlang.org/tsconfig#module)" settings. To ensure your TypeScript configuration matches the configuration that Microbundle uses internally it's strongly recommended that you set `"module": "ESNext"` and `"target": "ESNext"` in your `tsconfig.json`.
165165

166-
### Using CSS Modules
166+
### CSS and CSS Modules
167167

168-
By default any css file imported as `.module.css`, will be treated as a css-module. If you wish to treat all .css
169-
imports as a module, specify the cli flag `--css-modules true`. If you wish to disable all css-module behaviours set the
170-
flag to `false`.
168+
Importing CSS files is supported via `import "./foo.css"`. By default, generated CSS output is written to disk. The `--css inline` command line option will inline generated CSS into your bundles as a string, returning the CSS string from the import:
171169

172-
The default scope name when css-modules is turned on will be, in watch mode `_[name]__[local]__[hash:base64:5]` and when
173-
you build `_[hash:base64:5]`. This can be overriden by specifying the flag, eg
174-
`--css-modules "_something_[hash:base64:7]"`. _Note:_ by setting this, it will be treated as a true, and thus, all .css
175-
imports will be scoped.
170+
```js
171+
// with the default external CSS:
172+
import './foo.css'; // generates a minified .css file in the output directory
173+
174+
// with `microbundle --css inline`:
175+
import css from './foo.css';
176+
console.log(css); // the generated minified stylesheet
177+
```
178+
179+
**CSS Modules:** CSS files with names ending in `.module.css` are treated as a [CSS Modules](https://github.com/css-modules/css-modules).
180+
To instead treat imported `.css` files as modules, run Microbundle with `--css-modules true`. To disable CSS Modules for your project, pass `--no-css-modules` or `--css-modules false`.
181+
182+
The default scope name for CSS Modules is`_[name]__[local]__[hash:base64:5]` in watch mode, and `_[hash:base64:5]` for production builds.
183+
This can be customized by passing the command line argument `--css-modules "[name]_[hash:base64:7]"`, using [these fields and naming conventions](https://github.com/webpack/loader-utils#interpolatename).
176184

177185
| flag | import | is css module? |
178186
| ----- | ------------------------------ | :----------------: |
@@ -265,6 +273,7 @@ Options
265273
--jsx A custom JSX pragma like React.createElement (default: h)
266274
--jsxImportSource Specify the automatic import source for JSX like preact
267275
--tsconfig Specify the path to a custom tsconfig.json
276+
--css Where to output CSS: "inline" or "external" (default: "external")
268277
--css-modules Configures .css to be treated as modules (default: null)
269278
-h, --help Displays this message
270279

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ function createConfig(options, entry, format, writeMeta) {
445445
modules: cssModulesConfig(options),
446446
// only write out CSS for the first bundle (avoids pointless extra files):
447447
inject: false,
448-
extract: !!writeMeta,
448+
extract: options.css !== 'inline',
449449
minimize: options.compress,
450450
}),
451451
moduleAliases.length > 0 &&

src/prog.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export default handler => {
5656
.option('--name', 'Specify name exposed in UMD builds')
5757
.option('--cwd', 'Use an alternative working directory', '.')
5858
.option('--sourcemap', 'Generate source map')
59+
.option('--css', 'Where to output CSS: "inline" or "external"', 'external')
5960
.option(
6061
'--css-modules',
6162
'Turns on css-modules for all .css imports. Passing a string will override the scopeName. eg --css-modules="_[hash]"',

test/__snapshots__/index.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,8 +1696,8 @@ Build \\"inlineSourceMap\\" to dist:
16961696
138 B: inline-source-map.js.br
16971697
188 B: inline-source-map.esm.js.gz
16981698
139 B: inline-source-map.esm.js.br
1699-
279 B: inline-source-map.umd.js.gz
1700-
219 B: inline-source-map.umd.js.br"
1699+
278 B: inline-source-map.umd.js.gz
1700+
217 B: inline-source-map.umd.js.br"
17011701
`;
17021702
17031703
exports[`fixtures build inline-source-map with microbundle 2`] = `3`;
@@ -1715,8 +1715,8 @@ exports[`fixtures build inline-source-map with microbundle 4`] = `
17151715
`;
17161716
17171717
exports[`fixtures build inline-source-map with microbundle 5`] = `
1718-
"!function(e,r){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=r():\\"function\\"==typeof define&&define.amd?define(r):(e=e||self).inlineSourceMap=r()}(this,function(){var e=function(){try{var e=arguments;return Promise.resolve([].slice.call(e).reduce(function(e,r){return e+r},0))}catch(e){return Promise.reject(e)}};return function(){try{var r=arguments,n=[].slice.call(r);return Promise.resolve(e.apply(void 0,n)).then(function(r){return Promise.resolve(e.apply(void 0,n)).then(function(e){return[r,e]})})}catch(e){return Promise.reject(e)}}});
1719-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAudW1kLmpzIiwic291cmNlcyI6WyIuLi9zcmMvdHdvLmpzIiwiLi4vc3JjL2luZGV4LmpzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBhc3luYyBmdW5jdGlvbiB0d28oLi4uYXJncykge1xuXHRyZXR1cm4gYXJncy5yZWR1Y2UoKHRvdGFsLCB2YWx1ZSkgPT4gdG90YWwgKyB2YWx1ZSwgMCk7XG59XG4iLCJpbXBvcnQgeyB0d28gfSBmcm9tICcuL3R3byc7XG5cbmV4cG9ydCBkZWZhdWx0IGFzeW5jIGZ1bmN0aW9uKC4uLmFyZ3MpIHtcblx0cmV0dXJuIFthd2FpdCB0d28oLi4uYXJncyksIGF3YWl0IHR3byguLi5hcmdzKV07XG59XG4iXSwibmFtZXMiOlsidHdvIiwicmVkdWNlIiwidG90YWwiLCJ2YWx1ZSIsImFyZ3MiXSwibWFwcGluZ3MiOiI0TEFBc0JBLGlDQUNyQix1QkFBTyxpQkFBS0MsT0FBTyxTQUFDQyxFQUFPQyxVQUFVRCxFQUFRQyxHQUFPLElBRHJELDBFQ0VpQ0MsMENBQ2xCSixlQUFPSSw0Q0FBYUosZUFBT0kscUJBQXpDLE1BQU8sVUFEUiJ9
1718+
"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=n():\\"function\\"==typeof define&&define.amd?define(n):(e||self).inlineSourceMap=n()}(this,function(){var e=function(){try{var e=arguments;return Promise.resolve([].slice.call(e).reduce(function(e,n){return e+n},0))}catch(e){return Promise.reject(e)}};return function(){try{var n=arguments,r=[].slice.call(n);return Promise.resolve(e.apply(void 0,r)).then(function(n){return Promise.resolve(e.apply(void 0,r)).then(function(e){return[n,e]})})}catch(e){return Promise.reject(e)}}});
1719+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAudW1kLmpzIiwic291cmNlcyI6WyIuLi9zcmMvdHdvLmpzIiwiLi4vc3JjL2luZGV4LmpzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBhc3luYyBmdW5jdGlvbiB0d28oLi4uYXJncykge1xuXHRyZXR1cm4gYXJncy5yZWR1Y2UoKHRvdGFsLCB2YWx1ZSkgPT4gdG90YWwgKyB2YWx1ZSwgMCk7XG59XG4iLCJpbXBvcnQgeyB0d28gfSBmcm9tICcuL3R3byc7XG5cbmV4cG9ydCBkZWZhdWx0IGFzeW5jIGZ1bmN0aW9uKC4uLmFyZ3MpIHtcblx0cmV0dXJuIFthd2FpdCB0d28oLi4uYXJncyksIGF3YWl0IHR3byguLi5hcmdzKV07XG59XG4iXSwibmFtZXMiOlsidHdvIiwicmVkdWNlIiwidG90YWwiLCJ2YWx1ZSIsImFyZ3MiXSwibWFwcGluZ3MiOiJzT0FBc0JBLGlDQUNyQix1QkFBTyxpQkFBS0MsT0FBTyxTQUFDQyxFQUFPQyxVQUFVRCxFQUFRQyxHQUFPLElBRHJELDBFQ0VpQ0MsMENBQ2xCSixlQUFPSSw0Q0FBYUosZUFBT0kscUJBQXpDLE1BQU8sVUFEUiJ9
17201720
"
17211721
`;
17221722

0 commit comments

Comments
 (0)