Skip to content

Commit

Permalink
feat: exportDefault flag for webpack loader (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
plesiecki authored and tivac committed Oct 28, 2019
1 parent 5789b89 commit 0179f99
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 5 deletions.
14 changes: 13 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file modified packages/cli/cli.js
100644 → 100755
Empty file.
22 changes: 21 additions & 1 deletion packages/webpack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
...
Expand Down
9 changes: 6 additions & 3 deletions packages/webpack/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand Down
115 changes: 115 additions & 0 deletions packages/webpack/test/__snapshots__/webpack.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions packages/webpack/test/webpack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0179f99

Please sign in to comment.