Skip to content

Commit 8f77e19

Browse files
authored
fix: add export default {} when CSS modules enabled and a file is empty for the defaultExport option
1 parent 3df97b6 commit 8f77e19

File tree

6 files changed

+132
-6
lines changed

6 files changed

+132
-6
lines changed

src/loader.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ function pitch(request) {
255255
}
256256

257257
const result = (function makeResult() {
258+
const defaultExport =
259+
typeof options.defaultExport !== "undefined"
260+
? options.defaultExport
261+
: false;
262+
258263
if (locals) {
259264
if (namedExport) {
260265
const identifiers = Array.from(
@@ -281,11 +286,6 @@ function pitch(request) {
281286
.map(([id, key]) => `${id} as ${JSON.stringify(key)}`)
282287
.join(", ")} }`;
283288

284-
const defaultExport =
285-
typeof options.defaultExport !== "undefined"
286-
? options.defaultExport
287-
: false;
288-
289289
return defaultExport
290290
? `${localsString}\n${exportsString}\nexport default { ${identifiers
291291
.map(([id, key]) => `${JSON.stringify(key)}: ${id}`)
@@ -297,7 +297,9 @@ function pitch(request) {
297297
esModule ? "export default" : "module.exports = "
298298
} ${JSON.stringify(locals)};`;
299299
} else if (esModule) {
300-
return "\nexport {};";
300+
return defaultExport
301+
? "\nexport {};export default {};"
302+
: "\nexport {};";
301303
}
302304
return "";
303305
})();

test/cases/es-named-and-default-export-1/empty.css

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/******/ (() => { // webpackBootstrap
2+
/******/ "use strict";
3+
/******/ var __webpack_modules__ = ([
4+
/* 0 */,
5+
/* 1 */
6+
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
7+
8+
__webpack_require__.r(__webpack_exports__);
9+
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
10+
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
11+
/* harmony export */ });
12+
// extracted by mini-css-extract-plugin
13+
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ({});
14+
15+
/***/ })
16+
/******/ ]);
17+
/************************************************************************/
18+
/******/ // The module cache
19+
/******/ var __webpack_module_cache__ = {};
20+
/******/
21+
/******/ // The require function
22+
/******/ function __webpack_require__(moduleId) {
23+
/******/ // Check if module is in cache
24+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
25+
/******/ if (cachedModule !== undefined) {
26+
/******/ return cachedModule.exports;
27+
/******/ }
28+
/******/ // Create a new module (and put it into the cache)
29+
/******/ var module = __webpack_module_cache__[moduleId] = {
30+
/******/ // no module.id needed
31+
/******/ // no module.loaded needed
32+
/******/ exports: {}
33+
/******/ };
34+
/******/
35+
/******/ // Execute the module function
36+
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
37+
/******/
38+
/******/ // Return the exports of the module
39+
/******/ return module.exports;
40+
/******/ }
41+
/******/
42+
/************************************************************************/
43+
/******/ /* webpack/runtime/define property getters */
44+
/******/ (() => {
45+
/******/ // define getter functions for harmony exports
46+
/******/ __webpack_require__.d = (exports, definition) => {
47+
/******/ for(var key in definition) {
48+
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
49+
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
50+
/******/ }
51+
/******/ }
52+
/******/ };
53+
/******/ })();
54+
/******/
55+
/******/ /* webpack/runtime/hasOwnProperty shorthand */
56+
/******/ (() => {
57+
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
58+
/******/ })();
59+
/******/
60+
/******/ /* webpack/runtime/make namespace object */
61+
/******/ (() => {
62+
/******/ // define __esModule on exports
63+
/******/ __webpack_require__.r = (exports) => {
64+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
65+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
66+
/******/ }
67+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
68+
/******/ };
69+
/******/ })();
70+
/******/
71+
/************************************************************************/
72+
var __webpack_exports__ = {};
73+
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
74+
(() => {
75+
__webpack_require__.r(__webpack_exports__);
76+
/* harmony import */ var _empty_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
77+
78+
79+
// eslint-disable-next-line no-console
80+
console.log({ css: _empty_css__WEBPACK_IMPORTED_MODULE_0__["default"] });
81+
82+
})();
83+
84+
/******/ })()
85+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import css from "./empty.css";
2+
3+
// eslint-disable-next-line no-console
4+
console.log({ css });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Self from "../../../src";
2+
3+
module.exports = {
4+
entry: "./index.js",
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [
10+
{
11+
loader: Self.loader,
12+
options: {
13+
defaultExport: true,
14+
},
15+
},
16+
{
17+
loader: "css-loader",
18+
options: {
19+
esModule: true,
20+
modules: {
21+
namedExport: true,
22+
},
23+
},
24+
},
25+
],
26+
},
27+
],
28+
},
29+
plugins: [
30+
new Self({
31+
filename: "[name].css",
32+
}),
33+
],
34+
};

0 commit comments

Comments
 (0)