change the way inject-by-tag work (javascript obfuscator compatible)

This commit is contained in:
Vinh Le 2019-04-04 10:47:26 +07:00
parent f754feae72
commit 1bb456eb6b
6 changed files with 13706 additions and 2622 deletions

File diff suppressed because it is too large Load diff

View file

@ -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",

View file

@ -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,8 +101,8 @@ 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,8 +115,8 @@ 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,11 +131,11 @@ 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;
} }
} }

View file

@ -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,44 +23,49 @@ 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))
let replaced = 0; return
const asset = compilation.assets[basename]; let replaced = 0;
var asset = compilation.assets[file];
const originalSource = asset.source(); var newSource = new ReplaceSource(asset);
if (!originalSource || typeof originalSource.replace !== 'function') { let AIVTagRegexp = this.context.config.componentsOptions.InjectByTag.AIVTagRegexp
continue; let source = asset.source();
} while (true) {
// source = newSource.source()
const modFile = originalSource.replace(this.context.config.componentsOptions.InjectByTag.AIVTagRegexp, (tag) => { let match = AIVTagRegexp.exec(source)
// handle version if (!match) break
tag = tag.replace(/(\{)(version)(\})/g, () => { replaced++
return this.context.version; let newVal = this.replace(match[0])
}); newSource.replace(match.index, match.index + match[0].length - 1, newVal)
}
// handle date compilation.assets[file] = newSource
tag = tag.replace(/(\{)(date)(\})/g, () => { log.info(`${chalk.red('InjectByTag')} : match : ${file} : replaced : ${replaced}`);
return dateFormat(new Date(), config.componentsOptions.InjectByTag.dateFormat);
});
// remove [AIV] and [/AIV]
tag = tag.replace(/(\[AIV])|(\[\/AIV])/g, '');
replaced++;
return tag;
}); });
});
})
asset.source = () => modFile; })
log.info(`InjectByTag : match : ${basename} : replaced : ${replaced}`);
}
}
cb();
});
return new Promise((resolve) => { resolve(); }); return new Promise((resolve) => { resolve(); });
} }
replace(tag) {
// handle version
tag = tag.replace(/(\{)(version)(\})/g, () => {
return this.context.version;
});
// handle date
tag = tag.replace(/(\{)(date)(\})/g, () => {
return dateFormat(new Date(), this.context.config.componentsOptions.InjectByTag.dateFormat);
});
// remove [AIV] and [/AIV]
tag = tag.replace(/(\[AIV])|(\[\/AIV])/g, '');
return tag;
}
} }

View file

@ -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,

4356
yarn.lock Normal file

File diff suppressed because it is too large Load diff