Skip to content

Commit 0f8d501

Browse files
fix: crash with the exportLocalsConvention option (#844)
1 parent e751080 commit 0f8d501

File tree

13 files changed

+361
-1
lines changed

13 files changed

+361
-1
lines changed

Diff for: src/loader.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ export function pitch(request) {
9898

9999
namedExport =
100100
// eslint-disable-next-line no-underscore-dangle
101-
originalExports.__esModule && !("locals" in originalExports.default);
101+
originalExports.__esModule &&
102+
(!originalExports.default || !("locals" in originalExports.default));
102103

103104
if (namedExport) {
104105
Object.keys(originalExports).forEach((key) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 */ "aClass": () => (/* binding */ aClass),
11+
/* harmony export */ "bClass": () => (/* binding */ bClass),
12+
/* harmony export */ "cClass": () => (/* binding */ cClass)
13+
/* harmony export */ });
14+
// extracted by mini-css-extract-plugin
15+
var aClass = "foo__style__a-class";
16+
var bClass = "foo__style__b__class";
17+
var cClass = "foo__style__cClass";
18+
19+
/***/ })
20+
/******/ ]);
21+
/************************************************************************/
22+
/******/ // The module cache
23+
/******/ var __webpack_module_cache__ = {};
24+
/******/
25+
/******/ // The require function
26+
/******/ function __webpack_require__(moduleId) {
27+
/******/ // Check if module is in cache
28+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
29+
/******/ if (cachedModule !== undefined) {
30+
/******/ return cachedModule.exports;
31+
/******/ }
32+
/******/ // Create a new module (and put it into the cache)
33+
/******/ var module = __webpack_module_cache__[moduleId] = {
34+
/******/ // no module.id needed
35+
/******/ // no module.loaded needed
36+
/******/ exports: {}
37+
/******/ };
38+
/******/
39+
/******/ // Execute the module function
40+
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
41+
/******/
42+
/******/ // Return the exports of the module
43+
/******/ return module.exports;
44+
/******/ }
45+
/******/
46+
/************************************************************************/
47+
/******/ /* webpack/runtime/define property getters */
48+
/******/ (() => {
49+
/******/ // define getter functions for harmony exports
50+
/******/ __webpack_require__.d = (exports, definition) => {
51+
/******/ for(var key in definition) {
52+
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
53+
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
54+
/******/ }
55+
/******/ }
56+
/******/ };
57+
/******/ })();
58+
/******/
59+
/******/ /* webpack/runtime/hasOwnProperty shorthand */
60+
/******/ (() => {
61+
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
62+
/******/ })();
63+
/******/
64+
/******/ /* webpack/runtime/make namespace object */
65+
/******/ (() => {
66+
/******/ // define __esModule on exports
67+
/******/ __webpack_require__.r = (exports) => {
68+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
69+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
70+
/******/ }
71+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
72+
/******/ };
73+
/******/ })();
74+
/******/
75+
/************************************************************************/
76+
var __webpack_exports__ = {};
77+
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
78+
(() => {
79+
__webpack_require__.r(__webpack_exports__);
80+
/* harmony import */ var _style_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
81+
82+
83+
// eslint-disable-next-line no-console
84+
console.log({ aClass: _style_css__WEBPACK_IMPORTED_MODULE_0__.aClass, bClass: _style_css__WEBPACK_IMPORTED_MODULE_0__.bClass, cClass: _style_css__WEBPACK_IMPORTED_MODULE_0__.cClass });
85+
86+
})();
87+
88+
/******/ })()
89+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { aClass, bClass, cClass } from "./style.css";
2+
3+
// eslint-disable-next-line no-console
4+
console.log({ aClass, bClass, cClass });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.a-class {
2+
background: red;
3+
}
4+
5+
.b__class {
6+
color: green;
7+
}
8+
9+
.cClass {
10+
color: blue;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
},
13+
{
14+
loader: "css-loader",
15+
options: {
16+
modules: {
17+
namedExport: true,
18+
localIdentName: "foo__[name]__[local]",
19+
exportOnlyLocals: true,
20+
},
21+
},
22+
},
23+
],
24+
},
25+
],
26+
},
27+
plugins: [
28+
new Self({
29+
filename: "[name].css",
30+
}),
31+
],
32+
};
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
// extracted by mini-css-extract-plugin
10+
11+
12+
/***/ })
13+
/******/ ]);
14+
/************************************************************************/
15+
/******/ // The module cache
16+
/******/ var __webpack_module_cache__ = {};
17+
/******/
18+
/******/ // The require function
19+
/******/ function __webpack_require__(moduleId) {
20+
/******/ // Check if module is in cache
21+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
22+
/******/ if (cachedModule !== undefined) {
23+
/******/ return cachedModule.exports;
24+
/******/ }
25+
/******/ // Create a new module (and put it into the cache)
26+
/******/ var module = __webpack_module_cache__[moduleId] = {
27+
/******/ // no module.id needed
28+
/******/ // no module.loaded needed
29+
/******/ exports: {}
30+
/******/ };
31+
/******/
32+
/******/ // Execute the module function
33+
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
34+
/******/
35+
/******/ // Return the exports of the module
36+
/******/ return module.exports;
37+
/******/ }
38+
/******/
39+
/************************************************************************/
40+
/******/ /* webpack/runtime/make namespace object */
41+
/******/ (() => {
42+
/******/ // define __esModule on exports
43+
/******/ __webpack_require__.r = (exports) => {
44+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
45+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
46+
/******/ }
47+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
48+
/******/ };
49+
/******/ })();
50+
/******/
51+
/************************************************************************/
52+
var __webpack_exports__ = {};
53+
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
54+
(() => {
55+
__webpack_require__.r(__webpack_exports__);
56+
/* harmony import */ var _style_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
57+
58+
59+
// eslint-disable-next-line no-console
60+
console.log({ aClass: _style_css__WEBPACK_IMPORTED_MODULE_0__.aClass, bClass: _style_css__WEBPACK_IMPORTED_MODULE_0__.bClass, cClass: _style_css__WEBPACK_IMPORTED_MODULE_0__.cClass });
61+
62+
})();
63+
64+
/******/ })()
65+
;

Diff for: test/cases/export-only-locals-commonjs/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { aClass, bClass, cClass } from "./style.css";
2+
3+
// eslint-disable-next-line no-console
4+
console.log({ aClass, bClass, cClass });

Diff for: test/cases/export-only-locals-commonjs/style.css

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.a-class {
2+
background: red;
3+
}
4+
5+
.b__class {
6+
color: green;
7+
}
8+
9+
.cClass {
10+
color: blue;
11+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
},
13+
{
14+
loader: "css-loader",
15+
options: {
16+
esModule: false,
17+
modules: {
18+
localIdentName: "foo__[name]__[local]",
19+
exportOnlyLocals: true,
20+
},
21+
},
22+
},
23+
],
24+
},
25+
],
26+
},
27+
plugins: [
28+
new Self({
29+
filename: "[name].css",
30+
}),
31+
],
32+
};

Diff for: test/cases/export-only-locals/expected/main.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
// extracted by mini-css-extract-plugin
10+
11+
12+
/***/ })
13+
/******/ ]);
14+
/************************************************************************/
15+
/******/ // The module cache
16+
/******/ var __webpack_module_cache__ = {};
17+
/******/
18+
/******/ // The require function
19+
/******/ function __webpack_require__(moduleId) {
20+
/******/ // Check if module is in cache
21+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
22+
/******/ if (cachedModule !== undefined) {
23+
/******/ return cachedModule.exports;
24+
/******/ }
25+
/******/ // Create a new module (and put it into the cache)
26+
/******/ var module = __webpack_module_cache__[moduleId] = {
27+
/******/ // no module.id needed
28+
/******/ // no module.loaded needed
29+
/******/ exports: {}
30+
/******/ };
31+
/******/
32+
/******/ // Execute the module function
33+
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
34+
/******/
35+
/******/ // Return the exports of the module
36+
/******/ return module.exports;
37+
/******/ }
38+
/******/
39+
/************************************************************************/
40+
/******/ /* webpack/runtime/make namespace object */
41+
/******/ (() => {
42+
/******/ // define __esModule on exports
43+
/******/ __webpack_require__.r = (exports) => {
44+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
45+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
46+
/******/ }
47+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
48+
/******/ };
49+
/******/ })();
50+
/******/
51+
/************************************************************************/
52+
var __webpack_exports__ = {};
53+
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
54+
(() => {
55+
__webpack_require__.r(__webpack_exports__);
56+
/* harmony import */ var _style_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
57+
58+
59+
// eslint-disable-next-line no-console
60+
console.log({ aClass: _style_css__WEBPACK_IMPORTED_MODULE_0__.aClass, bClass: _style_css__WEBPACK_IMPORTED_MODULE_0__.bClass, cClass: _style_css__WEBPACK_IMPORTED_MODULE_0__.cClass });
61+
62+
})();
63+
64+
/******/ })()
65+
;

Diff for: test/cases/export-only-locals/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { aClass, bClass, cClass } from "./style.css";
2+
3+
// eslint-disable-next-line no-console
4+
console.log({ aClass, bClass, cClass });

Diff for: test/cases/export-only-locals/style.css

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.a-class {
2+
background: red;
3+
}
4+
5+
.b__class {
6+
color: green;
7+
}
8+
9+
.cClass {
10+
color: blue;
11+
}

Diff for: test/cases/export-only-locals/webpack.config.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
},
13+
{
14+
loader: "css-loader",
15+
options: {
16+
modules: {
17+
localIdentName: "foo__[name]__[local]",
18+
exportOnlyLocals: true,
19+
},
20+
},
21+
},
22+
],
23+
},
24+
],
25+
},
26+
plugins: [
27+
new Self({
28+
filename: "[name].css",
29+
}),
30+
],
31+
};

0 commit comments

Comments
 (0)