Skip to content

Commit f28c1e1

Browse files
fix: missing auxiliary assets (#662)
1 parent fed2dea commit f28c1e1

File tree

23 files changed

+174
-56
lines changed

23 files changed

+174
-56
lines changed

package-lock.json

+23-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"npm-run-all": "^4.1.5",
7575
"prettier": "^2.1.2",
7676
"standard-version": "^9.0.0",
77-
"webpack": "^5.4.0",
77+
"webpack": "^5.9.0",
7878
"webpack-cli": "^4.2.0",
7979
"webpack-dev-server": "^3.7.2"
8080
},

src/CssDependency.js

+12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class CssDependency extends webpack.Dependency {
1414
this.media = media;
1515
this.sourceMap = sourceMap;
1616
this.context = context;
17+
// eslint-disable-next-line no-undefined
18+
this.assets = undefined;
19+
// eslint-disable-next-line no-undefined
20+
this.assetsInfo = undefined;
1721
}
1822

1923
getResourceIdentifier() {
@@ -34,6 +38,8 @@ class CssDependency extends webpack.Dependency {
3438
write(this.sourceMap);
3539
write(this.context);
3640
write(this.identifierIndex);
41+
write(this.assets);
42+
write(this.assetsInfo);
3743

3844
super.serialize(context);
3945
}
@@ -65,6 +71,12 @@ if (webpack.util && webpack.util.serialization) {
6571
read()
6672
);
6773

74+
const assets = read();
75+
const assetsInfo = read();
76+
77+
dep.assets = assets;
78+
dep.assetsInfo = assetsInfo;
79+
6880
dep.deserialize(context);
6981

7082
return dep;

src/CssModule.js

+23-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class CssModule extends webpack.Module {
1616
content,
1717
media,
1818
sourceMap,
19+
assets,
20+
assetsInfo,
1921
}) {
2022
super(MODULE_TYPE, context);
2123

@@ -26,7 +28,10 @@ class CssModule extends webpack.Module {
2628
this.content = content;
2729
this.media = media;
2830
this.sourceMap = sourceMap;
29-
this.buildInfo = {};
31+
this.buildInfo = {
32+
assets,
33+
assetsInfo,
34+
};
3035
this.buildMeta = {};
3136
}
3237

@@ -107,6 +112,7 @@ class CssModule extends webpack.Module {
107112
write(this.content);
108113
write(this.media);
109114
write(this.sourceMap);
115+
write(this.buildInfo);
110116

111117
super.serialize(context);
112118
}
@@ -128,13 +134,23 @@ if (webpack.util && webpack.util.serialization) {
128134
deserialize(context) {
129135
const { read } = context;
130136

137+
const contextModule = read();
138+
const identifier = read();
139+
const identifierIndex = read();
140+
const content = read();
141+
const media = read();
142+
const sourceMap = read();
143+
const { assets, assetsInfo } = read();
144+
131145
const dep = new CssModule({
132-
context: read(),
133-
identifier: read(),
134-
identifierIndex: read(),
135-
content: read(),
136-
media: read(),
137-
sourceMap: read(),
146+
context: contextModule,
147+
identifier,
148+
identifierIndex,
149+
content,
150+
media,
151+
sourceMap,
152+
assets,
153+
assetsInfo,
138154
});
139155

140156
dep.deserialize(context);

src/loader.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,12 @@ export function pitch(request) {
136136
const callback = this.async();
137137

138138
childCompiler.runAsChild((err, entries, compilation) => {
139-
for (const asset of compilation.getAssets()) {
140-
const { buildInfo } = this._module;
141-
142-
if (!buildInfo.assets) {
143-
buildInfo.assets = Object.create(null);
144-
}
145-
146-
if (!buildInfo.assetsInfo) {
147-
buildInfo.assetsInfo = new Map();
148-
}
139+
const assets = Object.create(null);
140+
const assetsInfo = new Map();
149141

150-
buildInfo.assets[asset.name] = asset.source;
151-
buildInfo.assetsInfo.set(asset.name, asset.info);
142+
for (const asset of compilation.getAssets()) {
143+
assets[asset.name] = asset.source;
144+
assetsInfo.set(asset.name, asset.info);
152145
}
153146

154147
const addDependencies = (dependencies) => {
@@ -162,6 +155,8 @@ export function pitch(request) {
162155

163156
const identifierCountMap = new Map();
164157

158+
let lastDep;
159+
165160
for (const dependency of dependencies) {
166161
if (!dependency.identifier) {
167162
// eslint-disable-next-line no-continue
@@ -171,10 +166,15 @@ export function pitch(request) {
171166
const count = identifierCountMap.get(dependency.identifier) || 0;
172167

173168
this._module.addDependency(
174-
new CssDependency(dependency, dependency.context, count)
169+
(lastDep = new CssDependency(dependency, dependency.context, count))
175170
);
176171
identifierCountMap.set(dependency.identifier, count + 1);
177172
}
173+
174+
if (lastDep) {
175+
lastDep.assets = assets;
176+
lastDep.assetsInfo = assetsInfo;
177+
}
178178
};
179179

180180
if (err) {

test/TestCases.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ describe('TestCases', () => {
9090
clearDirectory(outputDirectory);
9191

9292
for (const directory of tests) {
93+
if (directory === 'auxiliaryAssets' && webpack.version[0] === '4') {
94+
// eslint-disable-next-line no-continue
95+
continue;
96+
}
97+
9398
if (!/^(\.|_)/.test(directory)) {
9499
// eslint-disable-next-line no-loop-func
95100
it(`${directory} should compile to the expected result`, (done) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
auxiliaryAssets-815babffe97b00a1f30b.woff2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
auxiliaryAssets-a0de923c946d4ccb38b0.ttf
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
auxiliaryAssets-d4c409dfe110747b1763.woff

test/cases/auxiliaryAssets/fonts.css

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@font-face {
2+
font-family: 'Roboto';
3+
font-style: normal;
4+
font-weight: 300;
5+
font-display: swap;
6+
src: local('Roboto Light'), local('Roboto-Light'),
7+
url('fonts/roboto-v18-latin-300.woff2') format('woff2'),
8+
url('fonts/roboto-v18-latin-300.woff') format('woff'),
9+
url('fonts/roboto-v18-latin-300.ttf') format('truetype');
10+
}
Binary file not shown.
Binary file not shown.
Binary file not shown.

test/cases/auxiliaryAssets/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './fonts.css';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import Self from '../../../src';
2+
3+
class AssetsPlugin {
4+
// eslint-disable-next-line class-methods-use-this
5+
apply(compiler) {
6+
compiler.hooks.emit.tapAsync('AssetsPlugin', (compilation, cb) => {
7+
const stats = compilation.getStats().toJson({
8+
all: true,
9+
});
10+
11+
const { RawSource } = compiler.webpack.sources;
12+
13+
for (const file of stats.entrypoints.main.auxiliaryAssets) {
14+
const newFile = `auxiliaryAssets-${file.name}`;
15+
compilation.emitAsset(newFile, new RawSource(newFile), {});
16+
}
17+
18+
cb();
19+
});
20+
}
21+
}
22+
23+
module.exports = {
24+
entry: './index.js',
25+
mode: 'development',
26+
output: {
27+
publicPath: '/',
28+
},
29+
optimization: {
30+
sideEffects: true,
31+
},
32+
module: {
33+
rules: [
34+
{
35+
test: /\.(woff2?|ttf|eot|otf|png|jpe?g|gif|ico|svg|webp)$/,
36+
type: 'asset',
37+
},
38+
{
39+
test: /\.css$/,
40+
use: [
41+
{
42+
loader: Self.loader,
43+
options: {
44+
esModule: true,
45+
},
46+
},
47+
{
48+
loader: 'css-loader',
49+
options: {
50+
esModule: true,
51+
},
52+
},
53+
],
54+
},
55+
],
56+
},
57+
plugins: [
58+
new Self({
59+
filename: '[name].css',
60+
chunkFilename: '[id].[name].css',
61+
}),
62+
new AssetsPlugin(),
63+
],
64+
};

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
__webpack_require__.r(__webpack_exports__);
88
/* harmony import */ var _styleC_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
9-
;
9+
1010

1111

1212
/***/ }),
@@ -23,7 +23,7 @@ __webpack_require__.r(__webpack_exports__);
2323

2424
__webpack_require__.r(__webpack_exports__);
2525
/* harmony import */ var _styleD_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
26-
;
26+
2727

2828

2929
/***/ }),

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"use strict";
1010
__webpack_require__.r(__webpack_exports__);
1111
/* harmony import */ var _styleB_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5);
12-
;
12+
1313

1414

1515
/***/ }),
@@ -28,7 +28,7 @@ __webpack_require__.r(__webpack_exports__);
2828
"use strict";
2929
__webpack_require__.r(__webpack_exports__);
3030
/* harmony import */ var _styleA_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(7);
31-
;
31+
3232

3333

3434
/***/ }),

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
__webpack_require__.r(__webpack_exports__);
88
/* harmony import */ var _styleB_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
9-
;
9+
1010

1111

1212
/***/ }),

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"use strict";
88
__webpack_require__.r(__webpack_exports__);
99
/* harmony import */ var _styleA_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
10-
;
10+
1111

1212

1313
/***/ }),

0 commit comments

Comments
 (0)