Added support for watch mode
parent
73f07f6bea
commit
403becbf78
|
@ -76,6 +76,15 @@
|
||||||
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.46.tgz",
|
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.46.tgz",
|
||||||
"integrity": "sha512-laIjwTQaD+5DukBZaygQ79K1Z0jb1bPEMRrkXSLjtCcZm+abyp5YbrqpSLzD42FwWW6gK/aS4NYpJ804nG2brg=="
|
"integrity": "sha512-laIjwTQaD+5DukBZaygQ79K1Z0jb1bPEMRrkXSLjtCcZm+abyp5YbrqpSLzD42FwWW6gK/aS4NYpJ804nG2brg=="
|
||||||
},
|
},
|
||||||
|
"@types/fs-extra": {
|
||||||
|
"version": "9.0.8",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.8.tgz",
|
||||||
|
"integrity": "sha512-bnlTVTwq03Na7DpWxFJ1dvnORob+Otb8xHyUqUWhqvz/Ksg8+JXPlR52oeMSZ37YEOa5PyccbgUNutiQdi13TA==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"@types/node": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
"@types/glob": {
|
"@types/glob": {
|
||||||
"version": "7.1.3",
|
"version": "7.1.3",
|
||||||
"resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.3.tgz",
|
"resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.3.tgz",
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
"type": "git"
|
"type": "git"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/fs-extra": "^9.0.8",
|
||||||
"@types/react": "^17.0.3",
|
"@types/react": "^17.0.3",
|
||||||
"@types/react-dom": "^17.0.2",
|
"@types/react-dom": "^17.0.2",
|
||||||
"@types/tapable": "^1.0.6",
|
"@types/tapable": "^1.0.6",
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import * as fs from "fs-extra";
|
import * as SystemFs from "fs-extra";
|
||||||
import sha1 from "sha1";
|
import sha1 from "sha1";
|
||||||
|
import util from "util";
|
||||||
|
|
||||||
|
import webpack, {Compiler, Module, RuntimeGlobals} from "webpack";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
GeneratedSprite,
|
GeneratedSprite,
|
||||||
|
@ -10,13 +13,10 @@ import {
|
||||||
SpriteCssOptions,
|
SpriteCssOptions,
|
||||||
SpriteDtsOptions
|
SpriteDtsOptions
|
||||||
} from "./generator";
|
} from "./generator";
|
||||||
|
import Compilation = webpack.Compilation;
|
||||||
|
|
||||||
const Module = require("webpack/lib/Module");
|
|
||||||
const RuntimeGlobals = require("webpack/lib/RuntimeGlobals");
|
|
||||||
const { RawSource } = require("webpack-sources");
|
const { RawSource } = require("webpack-sources");
|
||||||
|
|
||||||
import {Compiler} from "webpack";
|
|
||||||
|
|
||||||
export interface Options {
|
export interface Options {
|
||||||
modulePrefix?: string, /* defaults to svg-sprites/ */
|
modulePrefix?: string, /* defaults to svg-sprites/ */
|
||||||
dtsOutputFolder: string,
|
dtsOutputFolder: string,
|
||||||
|
@ -74,17 +74,31 @@ class SvgSpriteModule extends Module {
|
||||||
}
|
}
|
||||||
|
|
||||||
needBuild(context, callback) {
|
needBuild(context, callback) {
|
||||||
callback(null, !this.buildMeta);
|
context.fileSystemInfo.getContextHash(this.config.folder, (error, hash) => {
|
||||||
|
if(error) {
|
||||||
|
callback(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const needBuild = this.buildMeta?.directoryHash !== hash;
|
||||||
|
callback(null, needBuild);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
build(options, compilation, resolver, fs_, callback) {
|
build(options, compilation: Compilation, resolver, fs, callback) {
|
||||||
console.info("Building SVG sprite for configuration %s", this.configName);
|
this.buildAsync(options, compilation).then(() => {
|
||||||
|
callback();
|
||||||
|
}).catch(error => {
|
||||||
|
callback(error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
this.built = true;
|
private async buildAsync(options_, compilation: Compilation) {
|
||||||
this.buildMeta = {
|
this.buildMeta = {
|
||||||
async: false,
|
async: false,
|
||||||
exportsType: undefined
|
exportsType: undefined
|
||||||
};
|
};
|
||||||
|
|
||||||
this.buildInfo = {
|
this.buildInfo = {
|
||||||
cacheable: true,
|
cacheable: true,
|
||||||
assets: {},
|
assets: {},
|
||||||
|
@ -94,31 +108,36 @@ class SvgSpriteModule extends Module {
|
||||||
delete compilation.assets[this.spriteAssetName];
|
delete compilation.assets[this.spriteAssetName];
|
||||||
}
|
}
|
||||||
|
|
||||||
(async () => {
|
this.buildMeta.directoryHash = await new Promise((resolve, reject) => {
|
||||||
const files = (await fs.readdir(this.config.folder)).map(e => path.join(this.config.folder, e));
|
compilation.fileSystemInfo.getContextHash(this.config.folder, (err, hash) => {
|
||||||
this.sprite = await generateSprite(files);
|
if(err) {
|
||||||
|
reject(err);
|
||||||
this.spriteSvg = await generateSpriteSvg(this.sprite);
|
} else {
|
||||||
this.spriteAssetName = "sprite-" + sha1(this.spriteSvg).substr(-20) + ".svg";
|
resolve(hash);
|
||||||
this.spriteAssetUrl = this.spriteAssetName;
|
}
|
||||||
|
});
|
||||||
this.buildInfo.assets[this.spriteAssetName] = new RawSource(this.spriteSvg);
|
|
||||||
|
|
||||||
this.spriteCss = "";
|
|
||||||
for(const cssOption of this.config.cssOptions) {
|
|
||||||
this.spriteCss += await generateSpriteCss(cssOption, this.config.cssClassPrefix, this.sprite, this.spriteAssetUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.spriteJs = await generateSpriteJs(this.config.dtsOptions, this.sprite, this.spriteAssetUrl, this.config.cssClassPrefix);
|
|
||||||
|
|
||||||
const dtsContent = await generateSpriteDts(this.config.dtsOptions, this.configName, this.sprite, this.config.cssClassPrefix, this.pluginConfig.modulePrefix, this.config.folder);
|
|
||||||
await fs.writeFile(path.join(this.pluginConfig.dtsOutputFolder, this.configName + ".d.ts"), dtsContent);
|
|
||||||
})().then(() => {
|
|
||||||
callback();
|
|
||||||
}).catch(error => {
|
|
||||||
console.error(error);
|
|
||||||
callback("failed to generate sprite");
|
|
||||||
});
|
});
|
||||||
|
compilation.logger.info("Building SVG sprite for configuration %s (Hash: %s).", this.configName, this.buildMeta.directoryHash);
|
||||||
|
|
||||||
|
const files = await SystemFs.readdir(this.config.folder);
|
||||||
|
this.sprite = await generateSprite(files.map(file => path.join(this.config.folder, file)));
|
||||||
|
|
||||||
|
this.spriteSvg = await generateSpriteSvg(this.sprite);
|
||||||
|
this.spriteAssetName = "sprite-" + sha1(this.spriteSvg).substr(-20) + ".svg";
|
||||||
|
this.spriteAssetUrl = this.spriteAssetName;
|
||||||
|
|
||||||
|
this.buildInfo.assets[this.spriteAssetName] = new RawSource(this.spriteSvg);
|
||||||
|
|
||||||
|
this.spriteCss = "";
|
||||||
|
for(const cssOption of this.config.cssOptions) {
|
||||||
|
this.spriteCss += await generateSpriteCss(cssOption, this.config.cssClassPrefix, this.sprite, this.spriteAssetUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.spriteJs = await generateSpriteJs(this.config.dtsOptions, this.sprite, this.spriteAssetUrl, this.config.cssClassPrefix);
|
||||||
|
|
||||||
|
const dtsContent = await generateSpriteDts(this.config.dtsOptions, this.configName, this.sprite, this.config.cssClassPrefix, this.pluginConfig.modulePrefix, this.config.folder);
|
||||||
|
await SystemFs.writeFile(path.join(this.pluginConfig.dtsOutputFolder, this.configName + ".d.ts"), dtsContent);
|
||||||
|
compilation.logger.info("SVG sprite configuration %s contains %d/%d sprites", this.configName, this.sprite.entries.length, files.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
codeGeneration(context) {
|
codeGeneration(context) {
|
||||||
|
@ -155,6 +174,15 @@ class SvgSpriteModule extends Module {
|
||||||
}
|
}
|
||||||
|
|
||||||
addReason(_requestModule, _dependency) { }
|
addReason(_requestModule, _dependency) { }
|
||||||
|
|
||||||
|
addCacheDependencies(
|
||||||
|
fileDependencies,
|
||||||
|
contextDependencies,
|
||||||
|
missingDependencies,
|
||||||
|
buildDependencies
|
||||||
|
) {
|
||||||
|
contextDependencies.add(this.config.folder);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue