-
Notifications
You must be signed in to change notification settings - Fork 17
/
ykit.config.js
executable file
·217 lines (198 loc) · 6.52 KB
/
ykit.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var path = require('path');
var AssetsPlugin = require('assets-webpack-plugin');
var CompressionPlugin = require('compression-webpack-plugin');
var commonLib = require('./common/plugin.js');
var assetsPluginInstance = new AssetsPlugin({
filename: 'static/prd/assets.js',
processOutput: function(assets) {
return 'window.WEBPACK_ASSETS = ' + JSON.stringify(assets);
}
});
var fs = require('fs');
var package = require('./package.json');
var yapi = require('./server/yapi');
var isWin = require('os').platform() === 'win32'
var compressPlugin = new CompressionPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: /\.(js|css)$/,
threshold: 10240,
minRatio: 0.8
});
function createScript(plugin, pathAlias) {
let options = plugin.options ? JSON.stringify(plugin.options) : null;
if (pathAlias === 'node_modules') {
return `"${plugin.name}" : {module: require('yapi-plugin-${
plugin.name
}/client.js'),options: ${options}}`;
}
return `"${plugin.name}" : {module: require('${pathAlias}/yapi-plugin-${
plugin.name
}/client.js'),options: ${options}}`;
}
function initPlugins(configPlugin) {
configPlugin = require('../config.json').plugins;
var systemConfigPlugin = require('./common/config.js').exts;
var scripts = [];
if (configPlugin && Array.isArray(configPlugin) && configPlugin.length) {
configPlugin = commonLib.initPlugins(configPlugin, 'plugin');
configPlugin.forEach(plugin => {
if (plugin.client && plugin.enable) {
scripts.push(createScript(plugin, 'node_modules'));
}
});
}
systemConfigPlugin = commonLib.initPlugins(systemConfigPlugin, 'ext');
systemConfigPlugin.forEach(plugin => {
if (plugin.client && plugin.enable) {
scripts.push(createScript(plugin, 'exts'));
}
});
scripts = 'module.exports = {' + scripts.join(',') + '}';
fs.writeFileSync('client/plugin-module.js', scripts);
}
initPlugins();
module.exports = {
plugins: [
{
name: 'antd',
options: {
modifyQuery: function(defaultQuery) {
// 可查看和编辑 defaultQuery
defaultQuery.plugins = [];
defaultQuery.plugins.push([
'transform-runtime',
{
polyfill: false,
regenerator: true
}
]);
defaultQuery.plugins.push('transform-decorators-legacy');
defaultQuery.plugins.push(['import', { libraryName: 'antd' }]);
return defaultQuery;
},
exclude: isWin ? /(tui-editor|node_modules\\(?!_?(yapi-plugin|json-schema-editor-visual)))/ : /(tui-editor|node_modules\/(?!_?(yapi-plugin|json-schema-editor-visual)))/
}
}
],
devtool: 'cheap-source-map',
config: function(ykit) {
return {
exports: ['./index.js'],
commonsChunk: {
vendors: {
lib: [
// 'anujs',
'react',
'react-dom',
'redux',
'redux-promise',
'react-router',
'react-router-dom',
'prop-types',
'react-dnd-html5-backend',
'react-dnd',
'reactabular-table',
'reactabular-dnd',
'table-resolver'
],
lib2: ['brace', 'json5', 'url', 'axios'],
lib3: ['mockjs', 'moment', 'recharts']
}
},
modifyWebpackConfig: function(baseConfig) {
var ENV_PARAMS = {};
switch (this.env) {
case 'local':
ENV_PARAMS = 'dev';
break;
case 'dev':
ENV_PARAMS = 'dev';
break;
case 'prd':
ENV_PARAMS = 'production';
break;
default:
}
baseConfig.plugins.push(
new this.webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(ENV_PARAMS),
'process.env.version': JSON.stringify(package.version),
'process.env.versionNotify': yapi.WEBCONFIG.versionNotify
})
);
//初始化配置
baseConfig.devtool = 'cheap-module-eval-source-map';
baseConfig.context = path.resolve(__dirname, './client');
baseConfig.resolve.alias.client = '/client';
baseConfig.resolve.alias.common = '/common';
baseConfig.resolve.alias.exts = '/exts';
// baseConfig.resolve.alias.react = 'anujs';
// baseConfig.resolve.alias['react-dom'] = 'anujs';
baseConfig.output.prd.path = 'static/prd';
baseConfig.output.prd.publicPath = '';
baseConfig.output.prd.filename = '[name]@[chunkhash][ext]';
baseConfig.module.noParse = /node_modules\/jsondiffpatch\/public\/build\/.*js/;
baseConfig.module.loaders.push({
test: /\.less$/,
loader: ykit.ExtractTextPlugin.extract(
require.resolve('style-loader'),
require.resolve('css-loader') +
'?sourceMap!' +
require.resolve('less-loader') +
'?sourceMap'
)
});
baseConfig.module.loaders.push({
test: /.(gif|jpg|jpeg|png|woff|woff2|eot|ttf|svg)$/,
loader: 'url-loader',
options: {
limit: 8192,
name: ['[path][name].[ext]?[sha256#base64:8]']
}
});
baseConfig.module.loaders.push({
test: /\.(sass|scss)$/,
loader: ykit.ExtractTextPlugin.extract(
require.resolve('css-loader') +
'?sourceMap!' +
require.resolve('sass-loader') +
'?sourceMap'
)
});
baseConfig.module.preLoaders.push({
test: /\.(js|jsx)$/,
exclude: /tui-editor|node_modules|google-diff.js/,
loader: 'eslint-loader'
});
baseConfig.module.preLoaders.push({
test: /\.json$/,
loader: 'json-loader'
});
if (this.env == 'prd') {
baseConfig.plugins.push(
new this.webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
);
baseConfig.plugins.push(assetsPluginInstance);
baseConfig.plugins.push(compressPlugin);
baseConfig.plugins.push(
new this.webpack.ContextReplacementPlugin(/moment[\\\/]locale$/, /^\.\/(zh-cn|en-gb)$/)
);
}
return baseConfig;
}
};
},
server: {
// true/false,默认 false,效果相当于 ykit server --hot
hot: true,
// true/false,默认 false,开启后可在当前打开的页面提示打包错误
overlay: false
},
hooks: {},
commands: []
};