fixed a compile error when using webpack serve
This commit is contained in:
parent
cce73043a6
commit
b7dc3f4fdc
1 changed files with 86 additions and 75 deletions
|
@ -1,5 +1,6 @@
|
||||||
import * as webpack from "webpack";
|
import * as webpack from "webpack";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
|
import {Compilation, NormalModule} from "webpack";
|
||||||
|
|
||||||
interface Options {
|
interface Options {
|
||||||
outputFileName?: string;
|
outputFileName?: string;
|
||||||
|
@ -14,8 +15,17 @@ class ManifestGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
apply(compiler: webpack.Compiler) {
|
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 = {};
|
const chunkData = {};
|
||||||
|
|
||||||
for(const chunkGroup of compilation.chunkGroups) {
|
for(const chunkGroup of compilation.chunkGroups) {
|
||||||
const fileJs = [];
|
const fileJs = [];
|
||||||
const filesCss = [];
|
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/")) {
|
if(!module.type.startsWith("javascript/")) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -70,16 +80,21 @@ class ManifestGenerator {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!module.resource) {
|
if(!(module instanceof NormalModule)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(module.resource.indexOf("webpack-dev-server") !== -1) {
|
||||||
|
/* Don't include dev server files */
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(module.context !== path.dirname(module.resource)) {
|
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({
|
modules.push({
|
||||||
id: module.id,
|
id: compilation.chunkGraph.getModuleId(module),
|
||||||
context: path.relative(this.options.context, module.context).replace(/\\/g, "/"),
|
context: path.relative(this.options.context, module.context).replace(/\\/g, "/"),
|
||||||
resource: path.basename(module.resource)
|
resource: path.basename(module.resource)
|
||||||
});
|
});
|
||||||
|
@ -99,11 +114,7 @@ class ManifestGenerator {
|
||||||
});
|
});
|
||||||
|
|
||||||
const fileName = this.options.outputFileName || "manifest.json";
|
const fileName = this.options.outputFileName || "manifest.json";
|
||||||
compilation.assets[fileName] = {
|
compilation.emitAsset(fileName, new webpack.sources.RawSource(payload));
|
||||||
size() { return payload.length; },
|
|
||||||
source() { return payload; }
|
|
||||||
} as any;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue