Skip to content
Permalink
Browse files
feat: exportDefault flag for webpack loader (#680)
  • Loading branch information
plesiecki authored and tivac committed Oct 28, 2019
1 parent 5789b89 commit 0179f99f5fcbc7d4cbd9cc07f447f85a45c550c6

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

0 packages/cli/cli.js 100644 → 100755
Empty file.
@@ -74,9 +74,29 @@ All other options are passed to the underlying `Processor` instance, see [Option

### Loader Options

#### `defaultExport`

By default this plugin will create both a default export and named `export`s for each class in a CSS file. You can disable `default` by setting `defaultExport` to `false`.

```js
...
module : {
rules : [{
test : /\.css$/,
use : {
loader : "@modular-css/webpack/loader",
options : {
defaultExport : false
}
}
}]
},
...
```

#### `namedExports`

By default this plugin will create both a default export and named `export`s for each class in a CSS file. You can disable this by setting `namedExports` to `false`.
By default this plugin will create both a default export and named `export`s for each class in a CSS file. You can disable named `export`s by setting `namedExports` to `false`.

```js
...
@@ -10,6 +10,7 @@ module.exports = async function(source) {
const defaults = {
styleExport : true,
namedExports : true,
defaultExport: true,
};
const options = Object.assign(Object.create(null), defaults, utils.getOptions(this)) || false;
const done = this.async();
@@ -31,9 +32,11 @@ module.exports = async function(source) {
const result = await processor.string(this.resourcePath, source);

const exported = output.join(result.exports);
const out = [
`export default ${JSON.stringify(exported, null, 4)};`,
];
const out = [];

if(options.defaultExport) {
out.push(`export default ${JSON.stringify(exported, null, 4)};`);
}

processor.dependencies(this.resourcePath).forEach(this.addDependency);

@@ -1377,6 +1377,121 @@ exports[`/webpack.js should support ES2015 named exports 2`] = `
"
`;

exports[`/webpack.js should support disabling defaultExport 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: wooga, styles */
/***/ (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 export (binding) */ __webpack_require__.d(__webpack_exports__, \\\\\\"styles\\\\\\", function() { return styles; });\\\\nvar wooga = \\\\\\"wooga\\\\\\";\\\\nvar styles = \\\\\\".wooga { color: red; }\\\\\\\\n\\\\\\";\\\\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?\\");
/***/ })
/******/ });"
`;

exports[`/webpack.js should support disabling namedExports when the option is set 1`] = `
"/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
@@ -224,6 +224,24 @@ describe("/webpack.js", () => {
});
});

it("should support disabling defaultExport when the option is set", (done) => {
webpack(config({
entry : "./packages/webpack/test/specimens/simple.js",
use : {
loader,
options : {
defaultExport : 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 0179f99

Please sign in to comment.