UNPKG

5.37 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.initGenerate = exports.onBeforeCompile = exports.initOptions = exports.initClientConf = exports.initServerConf = exports.initDllConf = exports.initCommand = void 0;
2
3var _joi = _interopRequireDefault(require("@hapi/joi"));
4
5
6var _env = require("./utils/env");
7var _extend = _interopRequireDefault(require("./utils/extend"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}const cwd = process.cwd();const fs = require('fs');const path = require('path');
8const colors = require('colors');
9let tuarcPath = path.resolve(cwd, './.tuarc');
10let tuarcConf = {
11 plugins: [] };
12
13
14try {
15 tuarcConf = require(tuarcPath);
16} catch (err) {
17}
18
19const defaultPlugins = [
20'@tuax/cli',
21'@tuax/plugin-babel'
22
23// path.resolve(__dirname, '../packages/@tuax/cli'),
24// path.resolve(__dirname, '../packages/@tuax/plugin-test'),
25// path.resolve(__dirname, '../packages/@tuax/plugin-babel'),
26// path.resolve(__dirname, '../packages/@tuax/plugin-profile'),
27// path.resolve(__dirname, '../packages/@tuax/plugin-imgmin'),
28// path.resolve(__dirname, '../packages/@tuax/plugin-css-preprocessor'),
29];
30
31let pluginList = defaultPlugins.concat(tuarcConf.plugins || []);
32
33const resolveModule = plugin => {
34 let [pluginModule, ...options] = Array.isArray(plugin) ? plugin : [plugin];
35 if (typeof pluginModule === 'string') {//只适用于开发阶段
36 pluginModule = require(pluginModule);
37 }
38 return { pluginModule, options };
39};
40
41/**
42 * 初始化所有插件的扩展命令
43 * 在 bin/tua 中调用,初始化命令行之前
44 * 单独将 command 从plugin的index中抽出来,是为了让命令行变快
45 * @param {Object} yargv tua默认命令行 yargs 对象
46 */
47const initCommand = yargv => {
48 pluginList.forEach(plugin => {
49 let { pluginModule, options } = resolveModule(plugin);
50 if (typeof pluginModule.command === 'function')
51 pluginModule.command(yargv, options);
52 });
53};
54
55/**
56 * 初始化服务端配置
57 * @param {Object} chainedConf webpack-chain 对象
58 */exports.initCommand = initCommand;
59const initServerConf = chainedConf => {
60 let { config } = require('./options');
61 pluginList.forEach(plugin => {
62 let { pluginModule, options } = resolveModule(plugin);
63 if (typeof pluginModule.serverConf === 'function')
64 pluginModule.serverConf(chainedConf, _env.env, config, ...options);
65 });
66};
67/**
68 * 初始化客户端配置
69 * @param {Object} chainedConf webpack-chain 对象
70 */exports.initServerConf = initServerConf;
71const initClientConf = chainedConf => {
72 let { config } = require('./options');
73 pluginList.forEach(plugin => {
74 let { pluginModule, options } = resolveModule(plugin);
75 if (typeof pluginModule.clientConf === 'function')
76 pluginModule.clientConf(chainedConf, _env.env, config, ...options);
77 });
78};
79
80/**
81 * 初始化客户端配置
82 * @param {Object} chainedConf webpack-chain 对象
83 */exports.initClientConf = initClientConf;
84const initDllConf = chainedConf => {
85 let { config } = require('./options');
86 pluginList.forEach(plugin => {
87 let { pluginModule, options } = resolveModule(plugin);
88 if (typeof pluginModule.dllConf === 'function')
89 pluginModule.dllConf(chainedConf, _env.env, config, ...options);
90 });
91};
92
93/**
94 * 编译之前
95 */exports.initDllConf = initDllConf;
96const onBeforeCompile = () => {
97 let { config } = require('./options');
98 pluginList.forEach(plugin => {
99 let { pluginModule, options } = resolveModule(plugin);
100 if (typeof pluginModule.beforeCompile === 'function')
101 pluginModule.beforeCompile(_env.env, config, ...options);
102 });
103};
104
105/**
106 * 初始化option,设置扩展字段的校验和默认值
107 * @return {Object}
108 * {
109 * pluginScheme:
110 * pluginDefaultOptions:
111 * }
112 */exports.onBeforeCompile = onBeforeCompile;
113const initOptions = () => {
114 let pluginConfigs = {
115 pluginScheme: {},
116 pluginDefaultOptions: {} };
117
118 pluginList.forEach(plugin => {
119 let { pluginModule, options } = resolveModule(plugin);
120 if (typeof pluginModule.options === 'function') {
121 let pluginOption = pluginModule.options(_joi.default, ...options) || {};
122 let { pluginScheme, pluginDefaultOptions } = pluginOption;
123 if (typeof pluginScheme === 'undefined') {
124 // console.warn('pluginScheme 没有返回,没有参数校验')
125 } else {
126 (0, _extend.default)(true, pluginConfigs.pluginScheme, pluginScheme);
127 }
128 if (typeof pluginDefaultOptions === 'undefined') {
129 // console.warn('pluginOptions 没有返回,没有配置默认值')
130 } else {
131 (0, _extend.default)(true, pluginConfigs.pluginDefaultOptions, pluginDefaultOptions);
132 }
133 }
134 });
135 return pluginConfigs;
136};exports.initOptions = initOptions;
137
138const initGenerate = () => {
139 if (!fs.existsSync(path.resolve(cwd, 'package.json'))) {
140 throw new Error('请执行 npm init 创建 package.json');
141 return; //不存在 package.json 直接返回
142 }
143 let installPkg = require('./utils/installPkg');
144 pluginList.forEach(plugin => {
145 let { pluginModule, options } = resolveModule(plugin);
146 if (typeof pluginModule.generate === 'function')
147 installPkg.addPkg(pluginModule.generate(options));
148 });
149 installPkg.run();
150};exports.initGenerate = initGenerate;
\No newline at end of file