Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash with the exportLocalsConvention option #844

Merged
merged 1 commit into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ export function pitch(request) {

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

if (namedExport) {
Object.keys(originalExports).forEach((key) => {
Expand Down
89 changes: 89 additions & 0 deletions test/cases/export-only-locals-and-es-named-export/expected/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ([
/* 0 */,
/* 1 */
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "aClass": () => (/* binding */ aClass),
/* harmony export */ "bClass": () => (/* binding */ bClass),
/* harmony export */ "cClass": () => (/* binding */ cClass)
/* harmony export */ });
// extracted by mini-css-extract-plugin
var aClass = "foo__style__a-class";
var bClass = "foo__style__b__class";
var cClass = "foo__style__cClass";

/***/ })
/******/ ]);
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _style_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);


// eslint-disable-next-line no-console
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 });

})();

/******/ })()
;
4 changes: 4 additions & 0 deletions test/cases/export-only-locals-and-es-named-export/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { aClass, bClass, cClass } from "./style.css";

// eslint-disable-next-line no-console
console.log({ aClass, bClass, cClass });
11 changes: 11 additions & 0 deletions test/cases/export-only-locals-and-es-named-export/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.a-class {
background: red;
}

.b__class {
color: green;
}

.cClass {
color: blue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Self from "../../../src";

module.exports = {
entry: "./index.js",
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: Self.loader,
},
{
loader: "css-loader",
options: {
modules: {
namedExport: true,
localIdentName: "foo__[name]__[local]",
exportOnlyLocals: true,
},
},
},
],
},
],
},
plugins: [
new Self({
filename: "[name].css",
}),
],
};
65 changes: 65 additions & 0 deletions test/cases/export-only-locals-commonjs/expected/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ([
/* 0 */,
/* 1 */
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
// extracted by mini-css-extract-plugin


/***/ })
/******/ ]);
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _style_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);


// eslint-disable-next-line no-console
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 });

})();

/******/ })()
;
4 changes: 4 additions & 0 deletions test/cases/export-only-locals-commonjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { aClass, bClass, cClass } from "./style.css";

// eslint-disable-next-line no-console
console.log({ aClass, bClass, cClass });
11 changes: 11 additions & 0 deletions test/cases/export-only-locals-commonjs/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.a-class {
background: red;
}

.b__class {
color: green;
}

.cClass {
color: blue;
}
32 changes: 32 additions & 0 deletions test/cases/export-only-locals-commonjs/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Self from "../../../src";

module.exports = {
entry: "./index.js",
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: Self.loader,
},
{
loader: "css-loader",
options: {
esModule: false,
modules: {
localIdentName: "foo__[name]__[local]",
exportOnlyLocals: true,
},
},
},
],
},
],
},
plugins: [
new Self({
filename: "[name].css",
}),
],
};
65 changes: 65 additions & 0 deletions test/cases/export-only-locals/expected/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ([
/* 0 */,
/* 1 */
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
// extracted by mini-css-extract-plugin


/***/ })
/******/ ]);
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _style_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);


// eslint-disable-next-line no-console
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 });

})();

/******/ })()
;
4 changes: 4 additions & 0 deletions test/cases/export-only-locals/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { aClass, bClass, cClass } from "./style.css";

// eslint-disable-next-line no-console
console.log({ aClass, bClass, cClass });
11 changes: 11 additions & 0 deletions test/cases/export-only-locals/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.a-class {
background: red;
}

.b__class {
color: green;
}

.cClass {
color: blue;
}
31 changes: 31 additions & 0 deletions test/cases/export-only-locals/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Self from "../../../src";

module.exports = {
entry: "./index.js",
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: Self.loader,
},
{
loader: "css-loader",
options: {
modules: {
localIdentName: "foo__[name]__[local]",
exportOnlyLocals: true,
},
},
},
],
},
],
},
plugins: [
new Self({
filename: "[name].css",
}),
],
};