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,96 +15,106 @@ class ManifestGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
apply(compiler: webpack.Compiler) {
|
apply(compiler: webpack.Compiler) {
|
||||||
compiler.hooks.emit.tap("ManifestGenerator", compilation => {
|
compiler.hooks.thisCompilation.tap({
|
||||||
const chunkData = {};
|
name: "ManifestGenerator",
|
||||||
for(const chunkGroup of compilation.chunkGroups) {
|
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
|
||||||
const fileJs = [];
|
}, compilation => {
|
||||||
const filesCss = [];
|
compilation.hooks.processAssets.tap("ManifestGenerator", () => this.emitAssets(compilation));
|
||||||
const modules = [];
|
});
|
||||||
|
}
|
||||||
|
|
||||||
for(const chunk of chunkGroup.chunks) {
|
emitAssets(compilation: webpack.Compilation) {
|
||||||
if(!chunk.files.size) {
|
const chunkData = {};
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(const file of chunk.files) {
|
for(const chunkGroup of compilation.chunkGroups) {
|
||||||
const extension = path.extname(file);
|
const fileJs = [];
|
||||||
switch (extension) {
|
const filesCss = [];
|
||||||
case ".js":
|
const modules = [];
|
||||||
fileJs.push({
|
|
||||||
hash: chunk.hash,
|
|
||||||
file: file
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ".css":
|
for(const chunk of chunkGroup.chunks) {
|
||||||
filesCss.push({
|
if(!chunk.files.size) {
|
||||||
hash: chunk.hash,
|
continue;
|
||||||
file: file
|
}
|
||||||
});
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ".wasm":
|
for(const file of chunk.files) {
|
||||||
break;
|
const extension = path.extname(file);
|
||||||
|
switch (extension) {
|
||||||
default:
|
case ".js":
|
||||||
throw "Unknown chunk file with extension " + extension;
|
fileJs.push({
|
||||||
}
|
hash: chunk.hash,
|
||||||
}
|
file: file
|
||||||
|
|
||||||
for(const module of chunk.getModules() as any[]) {
|
|
||||||
if(!module.type.startsWith("javascript/")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!module.context) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(module.context.startsWith("svg-sprites/")) {
|
|
||||||
/* custom svg sprite handler */
|
|
||||||
modules.push({
|
|
||||||
id: module.id,
|
|
||||||
context: "svg-sprites",
|
|
||||||
resource: module.context.substring("svg-sprites/".length)
|
|
||||||
});
|
});
|
||||||
continue;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
if(!module.resource) {
|
case ".css":
|
||||||
continue;
|
filesCss.push({
|
||||||
}
|
hash: chunk.hash,
|
||||||
|
file: file
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
|
||||||
if(module.context !== path.dirname(module.resource)) {
|
case ".wasm":
|
||||||
throw "invalid context/resource relation";
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
modules.push({
|
default:
|
||||||
id: module.id,
|
throw "Unknown chunk file with extension " + extension;
|
||||||
context: path.relative(this.options.context, module.context).replace(/\\/g, "/"),
|
|
||||||
resource: path.basename(module.resource)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
chunkData[chunkGroup.options.name] = {
|
for(const module of compilation.chunkGraph.getChunkModules(chunk)) {
|
||||||
files: fileJs,
|
if(!module.type.startsWith("javascript/")) {
|
||||||
css_files: filesCss,
|
continue;
|
||||||
modules: modules
|
}
|
||||||
};
|
|
||||||
|
if(!module.context) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(module.context.startsWith("svg-sprites/")) {
|
||||||
|
/* custom svg sprite handler */
|
||||||
|
modules.push({
|
||||||
|
id: module.id,
|
||||||
|
context: "svg-sprites",
|
||||||
|
resource: module.context.substring("svg-sprites/".length)
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (" + module.context + " <-> " + path.dirname(module.resource) + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
modules.push({
|
||||||
|
id: compilation.chunkGraph.getModuleId(module),
|
||||||
|
context: path.relative(this.options.context, module.context).replace(/\\/g, "/"),
|
||||||
|
resource: path.basename(module.resource)
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const payload = JSON.stringify({
|
chunkData[chunkGroup.options.name] = {
|
||||||
version: 2,
|
files: fileJs,
|
||||||
chunks: chunkData
|
css_files: filesCss,
|
||||||
});
|
modules: modules
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const fileName = this.options.outputFileName || "manifest.json";
|
const payload = JSON.stringify({
|
||||||
compilation.assets[fileName] = {
|
version: 2,
|
||||||
size() { return payload.length; },
|
chunks: chunkData
|
||||||
source() { return payload; }
|
|
||||||
} as any;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const fileName = this.options.outputFileName || "manifest.json";
|
||||||
|
compilation.emitAsset(fileName, new webpack.sources.RawSource(payload));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue