Skip to content

Commit 0179f99

Browse files
plesieckitivac
authored andcommitted
feat: exportDefault flag for webpack loader (#680)
1 parent 5789b89 commit 0179f99

File tree

6 files changed

+173
-5
lines changed

6 files changed

+173
-5
lines changed

package-lock.json

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/cli.js

100644100755
File mode changed.

packages/webpack/README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,29 @@ All other options are passed to the underlying `Processor` instance, see [Option
7474

7575
### Loader Options
7676

77+
#### `defaultExport`
78+
79+
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`.
80+
81+
```js
82+
...
83+
module : {
84+
rules : [{
85+
test : /\.css$/,
86+
use : {
87+
loader : "@modular-css/webpack/loader",
88+
options : {
89+
defaultExport : false
90+
}
91+
}
92+
}]
93+
},
94+
...
95+
```
96+
7797
#### `namedExports`
7898

79-
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`.
99+
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`.
80100

81101
```js
82102
...

packages/webpack/loader.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = async function(source) {
1010
const defaults = {
1111
styleExport : true,
1212
namedExports : true,
13+
defaultExport: true,
1314
};
1415
const options = Object.assign(Object.create(null), defaults, utils.getOptions(this)) || false;
1516
const done = this.async();
@@ -31,9 +32,11 @@ module.exports = async function(source) {
3132
const result = await processor.string(this.resourcePath, source);
3233

3334
const exported = output.join(result.exports);
34-
const out = [
35-
`export default ${JSON.stringify(exported, null, 4)};`,
36-
];
35+
const out = [];
36+
37+
if(options.defaultExport) {
38+
out.push(`export default ${JSON.stringify(exported, null, 4)};`);
39+
}
3740

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

packages/webpack/test/__snapshots__/webpack.test.js.snap

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,121 @@ exports[`/webpack.js should support ES2015 named exports 2`] = `
13771377
"
13781378
`;
13791379

1380+
exports[`/webpack.js should support disabling defaultExport when the option is set 1`] = `
1381+
"/******/ (function(modules) { // webpackBootstrap
1382+
/******/ // The module cache
1383+
/******/ var installedModules = {};
1384+
/******/
1385+
/******/ // The require function
1386+
/******/ function __webpack_require__(moduleId) {
1387+
/******/
1388+
/******/ // Check if module is in cache
1389+
/******/ if(installedModules[moduleId]) {
1390+
/******/ return installedModules[moduleId].exports;
1391+
/******/ }
1392+
/******/ // Create a new module (and put it into the cache)
1393+
/******/ var module = installedModules[moduleId] = {
1394+
/******/ i: moduleId,
1395+
/******/ l: false,
1396+
/******/ exports: {}
1397+
/******/ };
1398+
/******/
1399+
/******/ // Execute the module function
1400+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
1401+
/******/
1402+
/******/ // Flag the module as loaded
1403+
/******/ module.l = true;
1404+
/******/
1405+
/******/ // Return the exports of the module
1406+
/******/ return module.exports;
1407+
/******/ }
1408+
/******/
1409+
/******/
1410+
/******/ // expose the modules object (__webpack_modules__)
1411+
/******/ __webpack_require__.m = modules;
1412+
/******/
1413+
/******/ // expose the module cache
1414+
/******/ __webpack_require__.c = installedModules;
1415+
/******/
1416+
/******/ // define getter function for harmony exports
1417+
/******/ __webpack_require__.d = function(exports, name, getter) {
1418+
/******/ if(!__webpack_require__.o(exports, name)) {
1419+
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
1420+
/******/ }
1421+
/******/ };
1422+
/******/
1423+
/******/ // define __esModule on exports
1424+
/******/ __webpack_require__.r = function(exports) {
1425+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
1426+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1427+
/******/ }
1428+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
1429+
/******/ };
1430+
/******/
1431+
/******/ // create a fake namespace object
1432+
/******/ // mode & 1: value is a module id, require it
1433+
/******/ // mode & 2: merge all properties of value into the ns
1434+
/******/ // mode & 4: return value when already ns object
1435+
/******/ // mode & 8|1: behave like require
1436+
/******/ __webpack_require__.t = function(value, mode) {
1437+
/******/ if(mode & 1) value = __webpack_require__(value);
1438+
/******/ if(mode & 8) return value;
1439+
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
1440+
/******/ var ns = Object.create(null);
1441+
/******/ __webpack_require__.r(ns);
1442+
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
1443+
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
1444+
/******/ return ns;
1445+
/******/ };
1446+
/******/
1447+
/******/ // getDefaultExport function for compatibility with non-harmony modules
1448+
/******/ __webpack_require__.n = function(module) {
1449+
/******/ var getter = module && module.__esModule ?
1450+
/******/ function getDefault() { return module['default']; } :
1451+
/******/ function getModuleExports() { return module; };
1452+
/******/ __webpack_require__.d(getter, 'a', getter);
1453+
/******/ return getter;
1454+
/******/ };
1455+
/******/
1456+
/******/ // Object.prototype.hasOwnProperty.call
1457+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
1458+
/******/
1459+
/******/ // __webpack_public_path__
1460+
/******/ __webpack_require__.p = \\"\\";
1461+
/******/
1462+
/******/
1463+
/******/ // Load entry module and return exports
1464+
/******/ return __webpack_require__(__webpack_require__.s = \\"./packages/webpack/test/specimens/simple.js\\");
1465+
/******/ })
1466+
/************************************************************************/
1467+
/******/ ({
1468+
1469+
/***/ \\"./packages/webpack/test/specimens/simple.css\\":
1470+
/*!****************************************************!*\\\\
1471+
!*** ./packages/webpack/test/specimens/simple.css ***!
1472+
\\\\****************************************************/
1473+
/*! exports provided: wooga, styles */
1474+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
1475+
1476+
\\"use strict\\";
1477+
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?\\");
1478+
1479+
/***/ }),
1480+
1481+
/***/ \\"./packages/webpack/test/specimens/simple.js\\":
1482+
/*!***************************************************!*\\\\
1483+
!*** ./packages/webpack/test/specimens/simple.js ***!
1484+
\\\\***************************************************/
1485+
/*! no static exports found */
1486+
/***/ (function(module, exports, __webpack_require__) {
1487+
1488+
eval(\\"__webpack_require__(/*! ./simple.css */ \\\\\\"./packages/webpack/test/specimens/simple.css\\\\\\");\\\\n\\\\n\\\\n//# sourceURL=webpack:///./packages/webpack/test/specimens/simple.js?\\");
1489+
1490+
/***/ })
1491+
1492+
/******/ });"
1493+
`;
1494+
13801495
exports[`/webpack.js should support disabling namedExports when the option is set 1`] = `
13811496
"/******/ (function(modules) { // webpackBootstrap
13821497
/******/ // The module cache

packages/webpack/test/webpack.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,24 @@ describe("/webpack.js", () => {
224224
});
225225
});
226226

227+
it("should support disabling defaultExport when the option is set", (done) => {
228+
webpack(config({
229+
entry : "./packages/webpack/test/specimens/simple.js",
230+
use : {
231+
loader,
232+
options : {
233+
defaultExport : false,
234+
},
235+
},
236+
}), (err, stats) => {
237+
success(err, stats);
238+
239+
expect(read("output.js")).toMatchSnapshot();
240+
241+
done();
242+
});
243+
});
244+
227245
it("should generate correct builds in watch mode when files change", (done) => {
228246
var changed = 0,
229247
compiler, watcher;

0 commit comments

Comments
 (0)