Closed panlimin closed 5 years ago
找到了另类的DefinePlugin使用方法 改变myconfig.ts
/// 阅读 api.d.ts 查看文档
///<reference path="api.d.ts"/>
import * as path from 'path';
import { UglifyPlugin, CompilePlugin, ManifestPlugin, ExmlPlugin, EmitResConfigFilePlugin, TextureMergerPlugin, CleanPlugin, ResSplitPlugin } from 'built-in';
import { WxgamePlugin } from './wxgame/wxgame';
import { BricksPlugin } from './bricks/bricks';
import { CustomPlugin } from './myplugin';
const Options = {
NeedSplit: false,
IsEUI: false,
ISDEBUG: false,
ENV: "program",
};
export default (target, command, projectName, version): any[] => {
if (['build', 'publish'].indexOf(command) == -1) throw `错误编译参数:command=${command}`;
if (['wxgame', 'ios', 'android', 'bricks'].indexOf(target) == -1) throw `错误编译参数:target=${target}`;
let ReturnCommands: any = {};
if (command == 'publish') {
ReturnCommands[40] = new UglifyPlugin([{ sources: ['main.js'], target: 'main.js' }]);
if (Options.NeedSplit) ReturnCommands[70] = new ResSplitPlugin({ matchers: [{ from: 'resource/qiniu/**', to: `../${projectName}_resource_remote` }] });
}
ReturnCommands[20] = new CompilePlugin({ libraryType: command == 'publish' ? 'release' : 'debug' });
if (Options.IsEUI) ReturnCommands[30] = new ExmlPlugin('commonjs');
ReturnCommands[60] = new ManifestPlugin({ output: 'manifest.js' });
if (target == 'wxgame') {
ReturnCommands[10] = new CleanPlugin({ matchers: ['js', 'resource'] });
ReturnCommands[50] = new WxgamePlugin();
}
if (target == 'bricks') {
ReturnCommands[10] = new CleanPlugin({ matchers: ['js', 'resource'] });
ReturnCommands[50] = new BricksPlugin();
}
(<any>Object).assign(ReturnCommands[20].options.defines, Options);
return Object.keys(ReturnCommands)
.sort((a, b) => parseInt(a) - parseInt(b))
.map(k => ReturnCommands[k])
.filter(f => f);
};
代码中的Options即全局替换的变量 业务代码Main.ts开头增加
declare var ISDEBUG: number;
declare var ENV: string;
则ISDEBUG和ENV会被直接替换 IsEUI没有在Main中定义则不能使用
官方Compiler.ts中的getCompilerDefines函数,直接替换了defines而不是合并,用起来有点不舒服
感谢反馈
config.[target].ts
myconfig.ts