fixed a compile error when using webpack serve
parent
cce73043a6
commit
b7dc3f4fdc
|
@ -1,5 +1,6 @@
|
|||
import * as webpack from "webpack";
|
||||
import * as path from "path";
|
||||
import {Compilation, NormalModule} from "webpack";
|
||||
|
||||
interface Options {
|
||||
outputFileName?: string;
|
||||
|
@ -14,96 +15,106 @@ class ManifestGenerator {
|
|||
}
|
||||
|
||||
apply(compiler: webpack.Compiler) {
|
||||
compiler.hooks.emit.tap("ManifestGenerator", compilation => {
|
||||
const chunkData = {};
|
||||
for(const chunkGroup of compilation.chunkGroups) {
|
||||
const fileJs = [];
|
||||
const filesCss = [];
|
||||
const modules = [];
|
||||
compiler.hooks.thisCompilation.tap({
|
||||
name: "ManifestGenerator",
|
||||
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
|
||||
}, compilation => {
|
||||
compilation.hooks.processAssets.tap("ManifestGenerator", () => this.emitAssets(compilation));
|
||||
});
|
||||
}
|
||||
|
||||
for(const chunk of chunkGroup.chunks) {
|
||||
if(!chunk.files.size) {
|
||||
continue;
|
||||
}
|
||||
emitAssets(compilation: webpack.Compilation) {
|
||||
const chunkData = {};
|
||||
|
||||
for(const file of chunk.files) {
|
||||
const extension = path.extname(file);
|
||||
switch (extension) {
|
||||
case ".js":
|
||||
fileJs.push({
|
||||
hash: chunk.hash,
|
||||
file: file
|
||||
});
|
||||
break;
|
||||
for(const chunkGroup of compilation.chunkGroups) {
|
||||
const fileJs = [];
|
||||
const filesCss = [];
|
||||
const modules = [];
|
||||
|
||||
case ".css":
|
||||
filesCss.push({
|
||||
hash: chunk.hash,
|
||||
file: file
|
||||
});
|
||||
break;
|
||||
for(const chunk of chunkGroup.chunks) {
|
||||
if(!chunk.files.size) {
|
||||
continue;
|
||||
}
|
||||
|
||||
case ".wasm":
|
||||
break;
|
||||
|
||||
default:
|
||||
throw "Unknown chunk file with extension " + extension;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
for(const file of chunk.files) {
|
||||
const extension = path.extname(file);
|
||||
switch (extension) {
|
||||
case ".js":
|
||||
fileJs.push({
|
||||
hash: chunk.hash,
|
||||
file: file
|
||||
});
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
|
||||
if(!module.resource) {
|
||||
continue;
|
||||
}
|
||||
case ".css":
|
||||
filesCss.push({
|
||||
hash: chunk.hash,
|
||||
file: file
|
||||
});
|
||||
break;
|
||||
|
||||
if(module.context !== path.dirname(module.resource)) {
|
||||
throw "invalid context/resource relation";
|
||||
}
|
||||
case ".wasm":
|
||||
break;
|
||||
|
||||
modules.push({
|
||||
id: module.id,
|
||||
context: path.relative(this.options.context, module.context).replace(/\\/g, "/"),
|
||||
resource: path.basename(module.resource)
|
||||
});
|
||||
default:
|
||||
throw "Unknown chunk file with extension " + extension;
|
||||
}
|
||||
}
|
||||
|
||||
chunkData[chunkGroup.options.name] = {
|
||||
files: fileJs,
|
||||
css_files: filesCss,
|
||||
modules: modules
|
||||
};
|
||||
for(const module of compilation.chunkGraph.getChunkModules(chunk)) {
|
||||
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;
|
||||
}
|
||||
|
||||
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({
|
||||
version: 2,
|
||||
chunks: chunkData
|
||||
});
|
||||
chunkData[chunkGroup.options.name] = {
|
||||
files: fileJs,
|
||||
css_files: filesCss,
|
||||
modules: modules
|
||||
};
|
||||
}
|
||||
|
||||
const fileName = this.options.outputFileName || "manifest.json";
|
||||
compilation.assets[fileName] = {
|
||||
size() { return payload.length; },
|
||||
source() { return payload; }
|
||||
} as any;
|
||||
const payload = JSON.stringify({
|
||||
version: 2,
|
||||
chunks: chunkData
|
||||
});
|
||||
|
||||
const fileName = this.options.outputFileName || "manifest.json";
|
||||
compilation.emitAsset(fileName, new webpack.sources.RawSource(payload));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue