Skip to content
Permalink
Browse files
feat(webpack): add styleExport option (#613)
Makes outputting the css as a var optional to save on bundle size where possible.
  • Loading branch information
kevinkace authored and tivac committed Jun 14, 2019
1 parent 490acfe commit 8fc3ae1d9f482d014f4ffae0e8962b60c77b296f
Showing with 164 additions and 5 deletions.
  1. +20 −0 packages/webpack/README.md
  2. +11 −5 packages/webpack/loader.js
  3. +115 −0 packages/webpack/test/__snapshots__/webpack.test.js.snap
  4. +18 −0 packages/webpack/test/webpack.test.js
@@ -93,3 +93,23 @@ By default this plugin will create both a default export and named `export`s for
},
...
```

#### `styleExport`

By default this plugin will export the style string, eg `import { styles } from "./style.css";`. You can disable this by setting `styleExport` to `false`.

```js
...
module : {
rules : [{
test : /\.css$/,
use : {
loader : "@modular-css/webpack/loader",
options : {
styleExport : false
}
}
}]
},
...
```
@@ -1,13 +1,17 @@
"use strict";

const utils = require("loader-utils");
const utils = require("loader-utils");
const { keyword } = require("esutils");

const output = require("@modular-css/processor/lib/output.js");

// Can't be an arrow function due to `this` usage :(
module.exports = async function(source) {
const options = utils.getOptions(this) || false;
const defaults = {
styleExport : true,
namedExports : true
};
const options = Object.assign(Object.create(null), defaults, utils.getOptions(this)) || false;
const done = this.async();
const processor = this.options ?
// Webpack 2 & 3
@@ -34,7 +38,7 @@ module.exports = async function(source) {
processor.dependencies(this.resourcePath).forEach(this.addDependency);

// Just default object export in this case
if(options.namedExports === false) {
if(!options.namedExports) {
return done(null, out.join("\n"));
}

@@ -50,7 +54,9 @@ module.exports = async function(source) {
out.push(`export var ${ident} = ${JSON.stringify(exported[ident])};`);
});

out.push(`export var styles = ${JSON.stringify(result.details.result.css)};`);
if(options.styleExport) {
out.push(`export var styles = ${JSON.stringify(result.details.result.css)};`);
}

return done(null, out.join("\n"));
} catch(e) {

Some generated files are not rendered by default. Learn more.

@@ -206,6 +206,24 @@ describe("/webpack.js", () => {
});
});

it("should support disabling styleExport when the option is set", (done) => {
webpack(config({
entry : "./packages/webpack/test/specimens/simple.js",
use : {
loader,
options : {
styleExport : false,
},
},
}), (err, stats) => {
success(err, stats);

expect(read("output.js")).toMatchSnapshot();

done();
});
});

it("should generate correct builds in watch mode when files change", (done) => {
var changed = 0,
compiler, watcher;

0 comments on commit 8fc3ae1

Please sign in to comment.