Skip to content

Commit 5fafdf8

Browse files
authored
feat opt-in to transitive only side effects (webpack@5) (#599)
1 parent ef4bd79 commit 5fafdf8

File tree

19 files changed

+384
-132
lines changed

19 files changed

+384
-132
lines changed

src/CssDependency.js

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class CssDependency extends webpack.Dependency {
2020
return `css-module-${this.identifier}-${this.identifierIndex}`;
2121
}
2222

23+
// eslint-disable-next-line class-methods-use-this
24+
getModuleEvaluationSideEffectsState() {
25+
return webpack.ModuleGraphConnection.TRANSITIVE_ONLY;
26+
}
27+
2328
serialize(context) {
2429
const { write } = context;
2530

test/TestCases.test.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,23 @@ function compareDirectory(actual, expected) {
4747
const content = fs.readFileSync(path.resolve(expected, file), 'utf8');
4848
let actualContent;
4949

50-
try {
51-
actualContent = fs.readFileSync(path.resolve(actual, file), 'utf8');
52-
} catch (error) {
53-
// eslint-disable-next-line no-console
54-
console.log(error);
50+
if (/^MISSING/.test(content)) {
51+
expect(fs.existsSync(path.resolve(actual, file))).toBe(false);
52+
} else {
53+
try {
54+
actualContent = fs.readFileSync(path.resolve(actual, file), 'utf8');
55+
} catch (error) {
56+
// eslint-disable-next-line no-console
57+
console.log(error);
5558

56-
const dir = fs.readdirSync(actual);
59+
const dir = fs.readdirSync(actual);
5760

58-
// eslint-disable-next-line no-console
59-
console.log({ [actual]: dir });
60-
}
61+
// eslint-disable-next-line no-console
62+
console.log({ [actual]: dir });
63+
}
6164

62-
expect(actualContent).toEqual(content);
65+
expect(actualContent).toEqual(content);
66+
}
6367
}
6468
}
6569
}
@@ -119,6 +123,10 @@ describe('TestCases', () => {
119123
done(err);
120124
return;
121125
}
126+
if (stats.hasErrors()) {
127+
done(new Error(stats.toString()));
128+
return;
129+
}
122130

123131
done();
124132

test/cases/dependOn-multiple-files-per-entry/expected/webpack-5/common.js

+2-43
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,20 @@
11
/******/ (() => { // webpackBootstrap
2+
/******/ "use strict";
23
/******/ var __webpack_modules__ = ([
34
/* 0 */
45
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
56

6-
"use strict";
77
__webpack_require__.r(__webpack_exports__);
8-
/* harmony import */ var _styleC_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
9-
/* harmony import */ var _styleC_css__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_styleC_css__WEBPACK_IMPORTED_MODULE_0__);
108

119

1210

1311
/***/ }),
1412
/* 1 */
15-
/***/ (() => {
16-
17-
// extracted by mini-css-extract-plugin
18-
19-
/***/ }),
20-
/* 2 */
2113
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
2214

23-
"use strict";
2415
__webpack_require__.r(__webpack_exports__);
25-
/* harmony import */ var _styleD_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
26-
/* harmony import */ var _styleD_css__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_styleD_css__WEBPACK_IMPORTED_MODULE_0__);
27-
2816

2917

30-
/***/ }),
31-
/* 3 */
32-
/***/ (() => {
33-
34-
// extracted by mini-css-extract-plugin
3518

3619
/***/ })
3720
/******/ ]);
@@ -63,30 +46,6 @@ __webpack_require__.r(__webpack_exports__);
6346
/******/ __webpack_require__.m = __webpack_modules__;
6447
/******/
6548
/************************************************************************/
66-
/******/ /* webpack/runtime/compat get default export */
67-
/******/ (() => {
68-
/******/ // getDefaultExport function for compatibility with non-harmony modules
69-
/******/ __webpack_require__.n = (module) => {
70-
/******/ var getter = module && module.__esModule ?
71-
/******/ () => module['default'] :
72-
/******/ () => module;
73-
/******/ __webpack_require__.d(getter, { a: getter });
74-
/******/ return getter;
75-
/******/ };
76-
/******/ })();
77-
/******/
78-
/******/ /* webpack/runtime/define property getters */
79-
/******/ (() => {
80-
/******/ // define getter functions for harmony exports
81-
/******/ __webpack_require__.d = (exports, definition) => {
82-
/******/ for(var key in definition) {
83-
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
84-
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
85-
/******/ }
86-
/******/ }
87-
/******/ };
88-
/******/ })();
89-
/******/
9049
/******/ /* webpack/runtime/hasOwnProperty shorthand */
9150
/******/ (() => {
9251
/******/ __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
@@ -116,7 +75,7 @@ __webpack_require__.r(__webpack_exports__);
11675
/******/
11776
/******/ var deferredModules = [
11877
/******/ [0],
119-
/******/ [2]
78+
/******/ [1]
12079
/******/ ];
12180
/******/ // no chunk on demand loading
12281
/******/
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,23 @@
11
(self["webpackChunk"] = self["webpackChunk"] || []).push([[1],[
22
/* 0 */,
33
/* 1 */,
4-
/* 2 */,
5-
/* 3 */,
6-
/* 4 */
4+
/* 2 */
75
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
86

97
"use strict";
108
__webpack_require__.r(__webpack_exports__);
11-
/* harmony import */ var _styleB_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5);
12-
/* harmony import */ var _styleB_css__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_styleB_css__WEBPACK_IMPORTED_MODULE_0__);
139

1410

1511

1612
/***/ }),
17-
/* 5 */
18-
/***/ (() => {
19-
20-
// extracted by mini-css-extract-plugin
21-
22-
/***/ }),
23-
/* 6 */
13+
/* 3 */
2414
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
2515

2616
"use strict";
2717
__webpack_require__.r(__webpack_exports__);
28-
/* harmony import */ var _styleA_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(7);
29-
/* harmony import */ var _styleA_css__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_styleA_css__WEBPACK_IMPORTED_MODULE_0__);
3018

3119

3220

33-
/***/ }),
34-
/* 7 */
35-
/***/ (() => {
36-
37-
// extracted by mini-css-extract-plugin
38-
3921
/***/ })
4022
],
41-
0,[[6,0],[4,0]]]);
23+
0,[[3,0],[2,0]]]);

test/cases/dependOn/expected/webpack-5/common.js

+1-33
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
/******/ (() => { // webpackBootstrap
2+
/******/ "use strict";
23
/******/ var __webpack_modules__ = ([
34
/* 0 */
45
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
56

6-
"use strict";
77
__webpack_require__.r(__webpack_exports__);
8-
/* harmony import */ var _styleB_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
9-
/* harmony import */ var _styleB_css__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_styleB_css__WEBPACK_IMPORTED_MODULE_0__);
108

119

1210

13-
/***/ }),
14-
/* 1 */
15-
/***/ (() => {
16-
17-
// extracted by mini-css-extract-plugin
18-
1911
/***/ })
2012
/******/ ]);
2113
/************************************************************************/
@@ -46,30 +38,6 @@ __webpack_require__.r(__webpack_exports__);
4638
/******/ __webpack_require__.m = __webpack_modules__;
4739
/******/
4840
/************************************************************************/
49-
/******/ /* webpack/runtime/compat get default export */
50-
/******/ (() => {
51-
/******/ // getDefaultExport function for compatibility with non-harmony modules
52-
/******/ __webpack_require__.n = (module) => {
53-
/******/ var getter = module && module.__esModule ?
54-
/******/ () => module['default'] :
55-
/******/ () => module;
56-
/******/ __webpack_require__.d(getter, { a: getter });
57-
/******/ return getter;
58-
/******/ };
59-
/******/ })();
60-
/******/
61-
/******/ /* webpack/runtime/define property getters */
62-
/******/ (() => {
63-
/******/ // define getter functions for harmony exports
64-
/******/ __webpack_require__.d = (exports, definition) => {
65-
/******/ for(var key in definition) {
66-
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
67-
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
68-
/******/ }
69-
/******/ }
70-
/******/ };
71-
/******/ })();
72-
/******/
7341
/******/ /* webpack/runtime/hasOwnProperty shorthand */
7442
/******/ (() => {
7543
/******/ __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
(self["webpackChunk"] = self["webpackChunk"] || []).push([[1],[
22
/* 0 */,
3-
/* 1 */,
4-
/* 2 */
3+
/* 1 */
54
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
65

76
"use strict";
87
__webpack_require__.r(__webpack_exports__);
9-
/* harmony import */ var _styleA_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
10-
/* harmony import */ var _styleA_css__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_styleA_css__WEBPACK_IMPORTED_MODULE_0__);
118

129

1310

14-
/***/ }),
15-
/* 3 */
16-
/***/ (() => {
17-
18-
// extracted by mini-css-extract-plugin
19-
2011
/***/ })
2112
],
22-
0,[[2,0]]]);
13+
0,[[1,0]]]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.a {
2+
background: red;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.b {
2+
background: green;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.c {
2+
background: blue;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.foo__a {
2+
background: red;
3+
}
4+
5+
.foo__b {
6+
background: green;
7+
}
8+
9+
.foo__c {
10+
background: blue;
11+
}
12+

0 commit comments

Comments
 (0)