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:
chabou-san 2018-01-16 13:24:46 +01:00
parent ee443b5db9
commit a00a1fb1cc
5 changed files with 2526 additions and 2281 deletions

File diff suppressed because it is too large Load diff

View file

@ -18,20 +18,15 @@ export default class AutoIncreaseVersion {
* @protected * @protected
* @returns {Promise} * @returns {Promise}
*/ */
apply() { async apply() {
// when runInWatchMode // when runInWatchMode
// we have to register AutoIncreaseVersion instead of firing it straight away // we have to register AutoIncreaseVersion instead of firing it straight away
if (config.componentsOptions.AutoIncreaseVersion.runInWatchMode) { if (config.componentsOptions.AutoIncreaseVersion.runInWatchMode) {
if (this.context.compiler) { await new Promise((resolve, reject) => {
this.context.compiler.plugin('emit', async (compilation, cb) => { this.resolve = resolve;
await new Promise((resolve, reject) => { this.reject = reject;
this.resolve = resolve; this.start();
this.reject = reject; });
this.start();
});
cb();
});
}
return null; return null;
} }

View file

@ -24,27 +24,25 @@ export default class InjectAsComment {
* @returns {Promise} * @returns {Promise}
*/ */
apply() { apply() {
this.context.compiler.plugin('emit', (compilation, cb) => { for (let basename in this.context.compilation.assets) {
for (let basename in compilation.assets) { let ext = path.extname(basename);
let ext = path.extname(basename); let asset = this.context.compilation.assets[basename];
let asset = compilation.assets[basename]; switch (ext) {
switch (ext) { case '.js' :
case '.js' : this.injectIntoJs(asset);
this.injectIntoJs(asset); break;
break; case '.html' :
case '.html' : this.injectIntoHtml(asset);
this.injectIntoHtml(asset); break;
break; case '.css' :
case '.css' : this.injectIntoCss(asset);
this.injectIntoCss(asset); break;
break; default:
default: break;
break;
}
log.info(`InjectAsComment : match : ${basename} : injected : ${this.context.version}`);
} }
cb(); log.info(`InjectAsComment : match : ${basename} : injected : ${this.context.version}`);
}); }
return new Promise((resolve) => { resolve(); }); return new Promise((resolve) => { resolve(); });
} }

View file

@ -22,49 +22,46 @@ export default class InjectByTag {
* @returns {Promise} * @returns {Promise}
*/ */
apply() { apply() {
this.context.compiler.plugin('emit', (compilation, cb) => { // for every output file
// for every output file for (let basename in this.context.compilation.assets) {
for (let basename in compilation.assets) { // only if match regex
// only if match regex if (this.context.config.componentsOptions.InjectByTag.fileRegex.test(basename)) {
if (this.context.config.componentsOptions.InjectByTag.fileRegex.test(basename)) { let replaced = 0;
let replaced = 0; let asset = this.context.compilation.assets[basename];
let asset = compilation.assets[basename];
const originalSource = asset.source(); const originalSource = asset.source();
if (!originalSource || typeof originalSource.replace !== 'function') { if (!originalSource || typeof originalSource.replace !== 'function') {
continue; continue;
} }
let modFile = originalSource.replace(InjectByTag.AIVTagRegexp, (tag) => { let modFile = originalSource.replace(InjectByTag.AIVTagRegexp, (tag) => {
// handle version // handle version
tag = tag.replace(/(\{)(version)(\})/g, () => { tag = tag.replace(/(\{)(version)(\})/g, () => {
return this.context.version; 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(/(\[AIV\]{version}\[\/AIV\])/g, () => { // handle date
// replaced++; tag = tag.replace(/(\{)(date)(\})/g, () => {
// return this.context.version; return dateFormat(new Date(), config.componentsOptions.InjectByTag.dateFormat);
// }); });
asset.source = () => modFile; // remove [AIV] and [/AIV]
log.info(`InjectByTag : match : ${basename} : replaced : ${replaced}`); 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(); }); return new Promise((resolve) => { resolve(); });
} }
} }

View file

@ -49,9 +49,13 @@ export default class WebpackAutoInject {
* @param compiler * @param compiler
* @protected * @protected
*/ */
async apply(compiler) { apply(compiler) {
this.compiler = compiler; this.compiler = compiler;
await this.executeWebpackComponents(); compiler.plugin('emit', async (compilation, cb) => {
this.compilation = compilation;
await this.executeWebpackComponents();
cb();
});
} }
/** /**