change the way inject-by-tag work (javascript obfuscator compatible)
This commit is contained in:
parent
f754feae72
commit
1bb456eb6b
6 changed files with 13706 additions and 2622 deletions
11863
dist/WebpackAutoInjectVersion.js
vendored
11863
dist/WebpackAutoInjectVersion.js
vendored
File diff suppressed because it is too large
Load diff
|
@ -22,7 +22,7 @@
|
||||||
"babel-preset-node5": "^11.0.1",
|
"babel-preset-node5": "^11.0.1",
|
||||||
"babel-preset-react": "^6.5.0",
|
"babel-preset-react": "^6.5.0",
|
||||||
"babel-preset-stage-2": "^6.22.0",
|
"babel-preset-stage-2": "^6.22.0",
|
||||||
"chalk": "^1.1.3",
|
"chalk": "^2.4.2",
|
||||||
"dateformat": "^2.0.0",
|
"dateformat": "^2.0.0",
|
||||||
"eslint": "^2.7.0",
|
"eslint": "^2.7.0",
|
||||||
"eslint-config-airbnb": "^6.2.0",
|
"eslint-config-airbnb": "^6.2.0",
|
||||||
|
|
|
@ -89,7 +89,7 @@ export default class InjectAsComment {
|
||||||
log.error(`unsupported tag in componentsOptions.InjectAsComment.tag [${tagName}]`);
|
log.error(`unsupported tag in componentsOptions.InjectAsComment.tag [${tagName}]`);
|
||||||
return tag;
|
return tag;
|
||||||
});
|
});
|
||||||
return `${baseOpen} ${tagPattern} ${baseClose}`;
|
return `${baseOpen.trim()} ${tagPattern} ${baseClose}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -101,7 +101,7 @@ export default class InjectAsComment {
|
||||||
* @param asset
|
* @param asset
|
||||||
*/
|
*/
|
||||||
injectIntoCss(asset) {
|
injectIntoCss(asset) {
|
||||||
let modAsset = this.parseTags(`/** [${config.SHORT}] `, ' **/ ');
|
let modAsset = this.parseTags(`/** ${config.SHORT}`, ' **/ ');
|
||||||
modAsset += `${endOfLine}${asset.source()} `;
|
modAsset += `${endOfLine}${asset.source()} `;
|
||||||
asset.source = () => modAsset;
|
asset.source = () => modAsset;
|
||||||
}
|
}
|
||||||
|
@ -115,7 +115,7 @@ export default class InjectAsComment {
|
||||||
* @param asset
|
* @param asset
|
||||||
*/
|
*/
|
||||||
injectIntoHtml(asset) {
|
injectIntoHtml(asset) {
|
||||||
let modAsset = this.parseTags(`<!-- [${config.SHORT}] `, ' --> ');
|
let modAsset = this.parseTags(`<!-- ${config.SHORT}`, ' --> ');
|
||||||
modAsset += `${endOfLine}${asset.source()} `;
|
modAsset += `${endOfLine}${asset.source()} `;
|
||||||
asset.source = () => modAsset;
|
asset.source = () => modAsset;
|
||||||
}
|
}
|
||||||
|
@ -131,9 +131,9 @@ export default class InjectAsComment {
|
||||||
injectIntoJs(asset) {
|
injectIntoJs(asset) {
|
||||||
let modAsset;
|
let modAsset;
|
||||||
if (this.context.config.componentsOptions.InjectAsComment.multiLineCommentType) {
|
if (this.context.config.componentsOptions.InjectAsComment.multiLineCommentType) {
|
||||||
modAsset = this.parseTags(`/** [${config.SHORT}] `, '*/ ');
|
modAsset = this.parseTags(`/** ${config.SHORT} `, '*/');
|
||||||
} else {
|
} else {
|
||||||
modAsset = this.parseTags(`// [${config.SHORT}] `, ' ');
|
modAsset = this.parseTags(`// ${config.SHORT} `, '');
|
||||||
}
|
}
|
||||||
modAsset += `${endOfLine}${asset.source()} `;
|
modAsset += `${endOfLine}${asset.source()} `;
|
||||||
asset.source = () => modAsset;
|
asset.source = () => modAsset;
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
import dateFormat from 'dateformat';
|
import dateFormat from 'dateformat';
|
||||||
import log from 'core/log';
|
import log from 'core/log';
|
||||||
import config from 'config';
|
import config from 'config';
|
||||||
|
import chalk from 'chalk'
|
||||||
/**
|
/**
|
||||||
* Inject version number into HTML
|
* Inject version number into HTML
|
||||||
* - done by parsing html file,
|
* - done by parsing html file,
|
||||||
* > replace: <{version}>
|
* > replace: <{version}>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { SourceMapSource, ReplaceSource, RawSource } from "webpack-sources";
|
||||||
export default class InjectByTag {
|
export default class InjectByTag {
|
||||||
|
|
||||||
static componentName = 'InjectByTag';
|
static componentName = 'InjectByTag';
|
||||||
|
@ -21,20 +23,35 @@ export default class InjectByTag {
|
||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
apply() {
|
apply() {
|
||||||
this.context.compiler.plugin('emit', (compilation, cb) => {
|
this.context.compiler.hooks.compilation.tap(InjectByTag.componentName, (compilation) => {
|
||||||
// for every output file
|
compilation.hooks.optimizeChunkAssets.tap(InjectByTag.componentName, (chunks) => {
|
||||||
for (const basename in compilation.assets) {
|
chunks.forEach(chunk => {
|
||||||
// only if match regex
|
chunk['files'].forEach((file) => {
|
||||||
if (this.context.config.componentsOptions.InjectByTag.fileRegex.test(basename)) {
|
if (!this.context.config.componentsOptions.InjectByTag.fileRegex.test(file))
|
||||||
|
return
|
||||||
let replaced = 0;
|
let replaced = 0;
|
||||||
const asset = compilation.assets[basename];
|
var asset = compilation.assets[file];
|
||||||
|
var newSource = new ReplaceSource(asset);
|
||||||
const originalSource = asset.source();
|
let AIVTagRegexp = this.context.config.componentsOptions.InjectByTag.AIVTagRegexp
|
||||||
if (!originalSource || typeof originalSource.replace !== 'function') {
|
let source = asset.source();
|
||||||
continue;
|
while (true) {
|
||||||
|
// source = newSource.source()
|
||||||
|
let match = AIVTagRegexp.exec(source)
|
||||||
|
if (!match) break
|
||||||
|
replaced++
|
||||||
|
let newVal = this.replace(match[0])
|
||||||
|
newSource.replace(match.index, match.index + match[0].length - 1, newVal)
|
||||||
}
|
}
|
||||||
|
compilation.assets[file] = newSource
|
||||||
|
log.info(`${chalk.red('InjectByTag')} : match : ${file} : replaced : ${replaced}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
const modFile = originalSource.replace(this.context.config.componentsOptions.InjectByTag.AIVTagRegexp, (tag) => {
|
})
|
||||||
|
return new Promise((resolve) => { resolve(); });
|
||||||
|
}
|
||||||
|
replace(tag) {
|
||||||
// handle version
|
// handle version
|
||||||
tag = tag.replace(/(\{)(version)(\})/g, () => {
|
tag = tag.replace(/(\{)(version)(\})/g, () => {
|
||||||
return this.context.version;
|
return this.context.version;
|
||||||
|
@ -42,23 +59,13 @@ export default class InjectByTag {
|
||||||
|
|
||||||
// handle date
|
// handle date
|
||||||
tag = tag.replace(/(\{)(date)(\})/g, () => {
|
tag = tag.replace(/(\{)(date)(\})/g, () => {
|
||||||
return dateFormat(new Date(), config.componentsOptions.InjectByTag.dateFormat);
|
return dateFormat(new Date(), this.context.config.componentsOptions.InjectByTag.dateFormat);
|
||||||
});
|
});
|
||||||
|
|
||||||
// remove [AIV] and [/AIV]
|
// remove [AIV] and [/AIV]
|
||||||
tag = tag.replace(/(\[AIV])|(\[\/AIV])/g, '');
|
tag = tag.replace(/(\[AIV])|(\[\/AIV])/g, '');
|
||||||
|
|
||||||
replaced++;
|
|
||||||
|
|
||||||
return tag;
|
return tag;
|
||||||
});
|
}
|
||||||
|
|
||||||
asset.source = () => modFile;
|
|
||||||
log.info(`InjectByTag : match : ${basename} : replaced : ${replaced}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cb();
|
|
||||||
});
|
|
||||||
return new Promise((resolve) => { resolve(); });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
export default {
|
export default {
|
||||||
SHORT: 'AIV_SHORT',
|
SHORT: '[AIV_SHORT]',
|
||||||
SILENT: false,
|
SILENT: false,
|
||||||
PACKAGE_JSON_PATH: './package.json',
|
PACKAGE_JSON_PATH: './package.json',
|
||||||
PACKAGE_JSON_INDENT: 4,
|
PACKAGE_JSON_INDENT: 4,
|
||||||
|
|
Loading…
Reference in a new issue