Fixes #22 : Call to apply can be asynchronous, but the ‘emit’ callback function shall only be used once from the main class.
This commit is contained in:
parent
ee443b5db9
commit
a00a1fb1cc
5 changed files with 2526 additions and 2281 deletions
4673
dist/WebpackAutoInjectVersion.js
vendored
4673
dist/WebpackAutoInjectVersion.js
vendored
File diff suppressed because it is too large
Load diff
|
@ -18,20 +18,15 @@ export default class AutoIncreaseVersion {
|
|||
* @protected
|
||||
* @returns {Promise}
|
||||
*/
|
||||
apply() {
|
||||
async apply() {
|
||||
// when runInWatchMode
|
||||
// we have to register AutoIncreaseVersion instead of firing it straight away
|
||||
if (config.componentsOptions.AutoIncreaseVersion.runInWatchMode) {
|
||||
if (this.context.compiler) {
|
||||
this.context.compiler.plugin('emit', async (compilation, cb) => {
|
||||
await new Promise((resolve, reject) => {
|
||||
this.resolve = resolve;
|
||||
this.reject = reject;
|
||||
this.start();
|
||||
});
|
||||
cb();
|
||||
});
|
||||
}
|
||||
await new Promise((resolve, reject) => {
|
||||
this.resolve = resolve;
|
||||
this.reject = reject;
|
||||
this.start();
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -24,27 +24,25 @@ export default class InjectAsComment {
|
|||
* @returns {Promise}
|
||||
*/
|
||||
apply() {
|
||||
this.context.compiler.plugin('emit', (compilation, cb) => {
|
||||
for (let basename in compilation.assets) {
|
||||
let ext = path.extname(basename);
|
||||
let asset = compilation.assets[basename];
|
||||
switch (ext) {
|
||||
case '.js' :
|
||||
this.injectIntoJs(asset);
|
||||
break;
|
||||
case '.html' :
|
||||
this.injectIntoHtml(asset);
|
||||
break;
|
||||
case '.css' :
|
||||
this.injectIntoCss(asset);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
log.info(`InjectAsComment : match : ${basename} : injected : ${this.context.version}`);
|
||||
for (let basename in this.context.compilation.assets) {
|
||||
let ext = path.extname(basename);
|
||||
let asset = this.context.compilation.assets[basename];
|
||||
switch (ext) {
|
||||
case '.js' :
|
||||
this.injectIntoJs(asset);
|
||||
break;
|
||||
case '.html' :
|
||||
this.injectIntoHtml(asset);
|
||||
break;
|
||||
case '.css' :
|
||||
this.injectIntoCss(asset);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
cb();
|
||||
});
|
||||
log.info(`InjectAsComment : match : ${basename} : injected : ${this.context.version}`);
|
||||
}
|
||||
|
||||
return new Promise((resolve) => { resolve(); });
|
||||
}
|
||||
|
||||
|
|
|
@ -22,49 +22,46 @@ export default class InjectByTag {
|
|||
* @returns {Promise}
|
||||
*/
|
||||
apply() {
|
||||
this.context.compiler.plugin('emit', (compilation, cb) => {
|
||||
// for every output file
|
||||
for (let basename in compilation.assets) {
|
||||
// only if match regex
|
||||
if (this.context.config.componentsOptions.InjectByTag.fileRegex.test(basename)) {
|
||||
let replaced = 0;
|
||||
let asset = compilation.assets[basename];
|
||||
// for every output file
|
||||
for (let basename in this.context.compilation.assets) {
|
||||
// only if match regex
|
||||
if (this.context.config.componentsOptions.InjectByTag.fileRegex.test(basename)) {
|
||||
let replaced = 0;
|
||||
let asset = this.context.compilation.assets[basename];
|
||||
|
||||
const originalSource = asset.source();
|
||||
if (!originalSource || typeof originalSource.replace !== 'function') {
|
||||
continue;
|
||||
}
|
||||
const originalSource = asset.source();
|
||||
if (!originalSource || typeof originalSource.replace !== 'function') {
|
||||
continue;
|
||||
}
|
||||
|
||||
let modFile = originalSource.replace(InjectByTag.AIVTagRegexp, (tag) => {
|
||||
// handle version
|
||||
tag = tag.replace(/(\{)(version)(\})/g, () => {
|
||||
return this.context.version;
|
||||
});
|
||||
|
||||
// handle date
|
||||
tag = tag.replace(/(\{)(date)(\})/g, () => {
|
||||
return dateFormat(new Date(), config.componentsOptions.InjectByTag.dateFormat);
|
||||
});
|
||||
|
||||
// remove [AIV] and [/AIV]
|
||||
tag = tag.replace(/(\[AIV])|(\[\/AIV])/g, '');
|
||||
|
||||
replaced++;
|
||||
|
||||
return tag;
|
||||
let modFile = originalSource.replace(InjectByTag.AIVTagRegexp, (tag) => {
|
||||
// handle version
|
||||
tag = tag.replace(/(\{)(version)(\})/g, () => {
|
||||
return this.context.version;
|
||||
});
|
||||
|
||||
// let modFile = originalSource.replace(/(\[AIV\]{version}\[\/AIV\])/g, () => {
|
||||
// replaced++;
|
||||
// return this.context.version;
|
||||
// });
|
||||
// handle date
|
||||
tag = tag.replace(/(\{)(date)(\})/g, () => {
|
||||
return dateFormat(new Date(), config.componentsOptions.InjectByTag.dateFormat);
|
||||
});
|
||||
|
||||
asset.source = () => modFile;
|
||||
log.info(`InjectByTag : match : ${basename} : replaced : ${replaced}`);
|
||||
}
|
||||
// remove [AIV] and [/AIV]
|
||||
tag = tag.replace(/(\[AIV])|(\[\/AIV])/g, '');
|
||||
|
||||
replaced++;
|
||||
|
||||
return tag;
|
||||
});
|
||||
|
||||
// let modFile = originalSource.replace(/(\[AIV\]{version}\[\/AIV\])/g, () => {
|
||||
// replaced++;
|
||||
// return this.context.version;
|
||||
// });
|
||||
|
||||
asset.source = () => modFile;
|
||||
log.info(`InjectByTag : match : ${basename} : replaced : ${replaced}`);
|
||||
}
|
||||
cb();
|
||||
});
|
||||
}
|
||||
return new Promise((resolve) => { resolve(); });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,9 +49,13 @@ export default class WebpackAutoInject {
|
|||
* @param compiler
|
||||
* @protected
|
||||
*/
|
||||
async apply(compiler) {
|
||||
apply(compiler) {
|
||||
this.compiler = compiler;
|
||||
await this.executeWebpackComponents();
|
||||
compiler.plugin('emit', async (compilation, cb) => {
|
||||
this.compilation = compilation;
|
||||
await this.executeWebpackComponents();
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue