egret-labs / egret-core

Egret is a brand new open mobile game and application engine which allows you to quickly build mobile games and apps on Android,iOS and Windows.
http://www.egret.com
Other
3.92k stars 795 forks source link

统合各环境编译插件配置,不能使用webpack.DefinePlugin有点纠结 #296

Closed panlimin closed 5 years ago

panlimin commented 6 years ago

config.[target].ts

/// 阅读 api.d.ts 查看文档
///<reference path="api.d.ts"/>

import * as defaultConfig from './config';
import GetCommands from './myconfig';

const config: ResourceManagerConfig = {
    buildConfig: params => {
        const { target, command, projectName, version } = params;
        const outputDir = `../${projectName}_${target}`;
        return {
            outputDir,
            commands: GetCommands(target, command, projectName, version),
        };
    },

    mergeSelector: defaultConfig.mergeSelector,

    typeSelector: defaultConfig.typeSelector,
};

export = config;

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 = {
    DEBUG: true,
    RELEASE: false,
    NeedSplit: false,
    IsEUI: false,
};
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') {
        (<any>Object).assign(Options, { DEBUG: false, RELEASE: true });
        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', defines: Options });
    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();
    }
    return Object.keys(ReturnCommands)
        .sort((a, b) => parseInt(a) - parseInt(b))
        .map(k => ReturnCommands[k])
        .filter(f => f);
};
panlimin commented 6 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中定义则不能使用

panlimin commented 6 years ago

官方Compiler.ts中的getCompilerDefines函数,直接替换了defines而不是合并,用起来有点不舒服

runinspring commented 5 years ago

感谢反馈