fixed a compile error when using webpack serve

master
WolverinDEV 2021-03-17 18:05:39 +01:00
parent cce73043a6
commit b7dc3f4fdc
1 changed files with 86 additions and 75 deletions

View File

@ -1,5 +1,6 @@
import * as webpack from "webpack";
import * as path from "path";
import {Compilation, NormalModule} from "webpack";
interface Options {
outputFileName?: string;
@ -14,8 +15,17 @@ class ManifestGenerator {
}
apply(compiler: webpack.Compiler) {
compiler.hooks.emit.tap("ManifestGenerator", compilation => {
compiler.hooks.thisCompilation.tap({
name: "ManifestGenerator",
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
}, compilation => {
compilation.hooks.processAssets.tap("ManifestGenerator", () => this.emitAssets(compilation));
});
}
emitAssets(compilation: webpack.Compilation) {
const chunkData = {};
for(const chunkGroup of compilation.chunkGroups) {
const fileJs = [];
const filesCss = [];
@ -51,7 +61,7 @@ class ManifestGenerator {
}
}
for(const module of chunk.getModules() as any[]) {
for(const module of compilation.chunkGraph.getChunkModules(chunk)) {
if(!module.type.startsWith("javascript/")) {
continue;
}
@ -70,16 +80,21 @@ class ManifestGenerator {
continue;
}
if(!module.resource) {
if(!(module instanceof NormalModule)) {
continue;
}
if(module.resource.indexOf("webpack-dev-server") !== -1) {
/* Don't include dev server files */
continue;
}
if(module.context !== path.dirname(module.resource)) {
throw "invalid context/resource relation";
throw "invalid context/resource relation (" + module.context + " <-> " + path.dirname(module.resource) + ")";
}
modules.push({
id: module.id,
id: compilation.chunkGraph.getModuleId(module),
context: path.relative(this.options.context, module.context).replace(/\\/g, "/"),
resource: path.basename(module.resource)
});
@ -99,11 +114,7 @@ class ManifestGenerator {
});
const fileName = this.options.outputFileName || "manifest.json";
compilation.assets[fileName] = {
size() { return payload.length; },
source() { return payload; }
} as any;
});
compilation.emitAsset(fileName, new webpack.sources.RawSource(payload));
}
}