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
@@ -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) {
@@ -1491,3 +1491,118 @@ eval(\\"__webpack_require__(/*! ./simple.css */ \\\\\\"./packages/webpack/test/s

/******/ });"
`;

exports[`/webpack.js should support disabling styleExport when the option is set 1`] = `
"/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = \\"\\";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = \\"./packages/webpack/test/specimens/simple.js\\");
/******/ })
/************************************************************************/
/******/ ({

/***/ \\"./packages/webpack/test/specimens/simple.css\\":
/*!****************************************************!*\\\\
!*** ./packages/webpack/test/specimens/simple.css ***!
\\\\****************************************************/
/*! exports provided: default, wooga */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

\\"use strict\\";
eval(\\"__webpack_require__.r(__webpack_exports__);\\\\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \\\\\\"wooga\\\\\\", function() { return wooga; });\\\\n/* harmony default export */ __webpack_exports__[\\\\\\"default\\\\\\"] = ({\\\\n \\\\\\"wooga\\\\\\": \\\\\\"wooga\\\\\\"\\\\n});\\\\nvar wooga = \\\\\\"wooga\\\\\\";\\\\n\\\\n//# sourceURL=webpack:///./packages/webpack/test/specimens/simple.css?\\");

/***/ }),

/***/ \\"./packages/webpack/test/specimens/simple.js\\":
/*!***************************************************!*\\\\
!*** ./packages/webpack/test/specimens/simple.js ***!
\\\\***************************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

eval(\\"__webpack_require__(/*! ./simple.css */ \\\\\\"./packages/webpack/test/specimens/simple.css\\\\\\");\\\\n\\\\n\\\\n//# sourceURL=webpack:///./packages/webpack/test/specimens/simple.js?\\");

/***/ })

/******/ });"
`;
@@ -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.
You can’t perform that action at this time.