Skip to content

Commit

Permalink
fix: missing auxiliary assets (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Dec 4, 2020
1 parent fed2dea commit f28c1e1
Show file tree
Hide file tree
Showing 23 changed files with 174 additions and 56 deletions.
40 changes: 23 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"npm-run-all": "^4.1.5",
"prettier": "^2.1.2",
"standard-version": "^9.0.0",
"webpack": "^5.4.0",
"webpack": "^5.9.0",
"webpack-cli": "^4.2.0",
"webpack-dev-server": "^3.7.2"
},
Expand Down
12 changes: 12 additions & 0 deletions src/CssDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class CssDependency extends webpack.Dependency {
this.media = media;
this.sourceMap = sourceMap;
this.context = context;
// eslint-disable-next-line no-undefined
this.assets = undefined;
// eslint-disable-next-line no-undefined
this.assetsInfo = undefined;
}

getResourceIdentifier() {
Expand All @@ -34,6 +38,8 @@ class CssDependency extends webpack.Dependency {
write(this.sourceMap);
write(this.context);
write(this.identifierIndex);
write(this.assets);
write(this.assetsInfo);

super.serialize(context);
}
Expand Down Expand Up @@ -65,6 +71,12 @@ if (webpack.util && webpack.util.serialization) {
read()
);

const assets = read();
const assetsInfo = read();

dep.assets = assets;
dep.assetsInfo = assetsInfo;

dep.deserialize(context);

return dep;
Expand Down
30 changes: 23 additions & 7 deletions src/CssModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class CssModule extends webpack.Module {
content,
media,
sourceMap,
assets,
assetsInfo,
}) {
super(MODULE_TYPE, context);

Expand All @@ -26,7 +28,10 @@ class CssModule extends webpack.Module {
this.content = content;
this.media = media;
this.sourceMap = sourceMap;
this.buildInfo = {};
this.buildInfo = {
assets,
assetsInfo,
};
this.buildMeta = {};
}

Expand Down Expand Up @@ -107,6 +112,7 @@ class CssModule extends webpack.Module {
write(this.content);
write(this.media);
write(this.sourceMap);
write(this.buildInfo);

super.serialize(context);
}
Expand All @@ -128,13 +134,23 @@ if (webpack.util && webpack.util.serialization) {
deserialize(context) {
const { read } = context;

const contextModule = read();
const identifier = read();
const identifierIndex = read();
const content = read();
const media = read();
const sourceMap = read();
const { assets, assetsInfo } = read();

const dep = new CssModule({
context: read(),
identifier: read(),
identifierIndex: read(),
content: read(),
media: read(),
sourceMap: read(),
context: contextModule,
identifier,
identifierIndex,
content,
media,
sourceMap,
assets,
assetsInfo,
});

dep.deserialize(context);
Expand Down
26 changes: 13 additions & 13 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,12 @@ export function pitch(request) {
const callback = this.async();

childCompiler.runAsChild((err, entries, compilation) => {
for (const asset of compilation.getAssets()) {
const { buildInfo } = this._module;

if (!buildInfo.assets) {
buildInfo.assets = Object.create(null);
}

if (!buildInfo.assetsInfo) {
buildInfo.assetsInfo = new Map();
}
const assets = Object.create(null);
const assetsInfo = new Map();

buildInfo.assets[asset.name] = asset.source;
buildInfo.assetsInfo.set(asset.name, asset.info);
for (const asset of compilation.getAssets()) {
assets[asset.name] = asset.source;
assetsInfo.set(asset.name, asset.info);
}

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

const identifierCountMap = new Map();

let lastDep;

for (const dependency of dependencies) {
if (!dependency.identifier) {
// eslint-disable-next-line no-continue
Expand All @@ -171,10 +166,15 @@ export function pitch(request) {
const count = identifierCountMap.get(dependency.identifier) || 0;

this._module.addDependency(
new CssDependency(dependency, dependency.context, count)
(lastDep = new CssDependency(dependency, dependency.context, count))
);
identifierCountMap.set(dependency.identifier, count + 1);
}

if (lastDep) {
lastDep.assets = assets;
lastDep.assetsInfo = assetsInfo;
}
};

if (err) {
Expand Down
5 changes: 5 additions & 0 deletions test/TestCases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ describe('TestCases', () => {
clearDirectory(outputDirectory);

for (const directory of tests) {
if (directory === 'auxiliaryAssets' && webpack.version[0] === '4') {
// eslint-disable-next-line no-continue
continue;
}

if (!/^(\.|_)/.test(directory)) {
// eslint-disable-next-line no-loop-func
it(`${directory} should compile to the expected result`, (done) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
auxiliaryAssets-815babffe97b00a1f30b.woff2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
auxiliaryAssets-a0de923c946d4ccb38b0.ttf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
auxiliaryAssets-d4c409dfe110747b1763.woff
10 changes: 10 additions & 0 deletions test/cases/auxiliaryAssets/fonts.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 300;
font-display: swap;
src: local('Roboto Light'), local('Roboto-Light'),
url('fonts/roboto-v18-latin-300.woff2') format('woff2'),
url('fonts/roboto-v18-latin-300.woff') format('woff'),
url('fonts/roboto-v18-latin-300.ttf') format('truetype');
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions test/cases/auxiliaryAssets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './fonts.css';
64 changes: 64 additions & 0 deletions test/cases/auxiliaryAssets/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Self from '../../../src';

class AssetsPlugin {
// eslint-disable-next-line class-methods-use-this
apply(compiler) {
compiler.hooks.emit.tapAsync('AssetsPlugin', (compilation, cb) => {
const stats = compilation.getStats().toJson({
all: true,
});

const { RawSource } = compiler.webpack.sources;

for (const file of stats.entrypoints.main.auxiliaryAssets) {
const newFile = `auxiliaryAssets-${file.name}`;
compilation.emitAsset(newFile, new RawSource(newFile), {});
}

cb();
});
}
}

module.exports = {
entry: './index.js',
mode: 'development',
output: {
publicPath: '/',
},
optimization: {
sideEffects: true,
},
module: {
rules: [
{
test: /\.(woff2?|ttf|eot|otf|png|jpe?g|gif|ico|svg|webp)$/,
type: 'asset',
},
{
test: /\.css$/,
use: [
{
loader: Self.loader,
options: {
esModule: true,
},
},
{
loader: 'css-loader',
options: {
esModule: true,
},
},
],
},
],
},
plugins: [
new Self({
filename: '[name].css',
chunkFilename: '[id].[name].css',
}),
new AssetsPlugin(),
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _styleC_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
;



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

__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _styleD_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
;



/***/ }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _styleB_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5);
;



/***/ }),
Expand All @@ -28,7 +28,7 @@ __webpack_require__.r(__webpack_exports__);
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _styleA_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(7);
;



/***/ }),
Expand Down
2 changes: 1 addition & 1 deletion test/cases/dependOn/expected/webpack-5/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _styleB_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
;



/***/ }),
Expand Down
2 changes: 1 addition & 1 deletion test/cases/dependOn/expected/webpack-5/entry1.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _styleA_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
;



/***/ }),
Expand Down
Loading

0 comments on commit f28c1e1

Please sign in to comment.