yapi/ykit.js

198 lines
5.2 KiB
JavaScript
Raw Normal View History

2017-07-05 21:08:51 +08:00
var path = require('path');
var AssetsPlugin = require('assets-webpack-plugin')
var CompressionPlugin = require('compression-webpack-plugin')
var assetsPluginInstance = new AssetsPlugin({
2017-07-25 15:07:55 +08:00
filename: 'static/prd/assets.js',
processOutput: function (assets) {
return 'window.WEBPACK_ASSETS = ' + JSON.stringify(assets);
}
2017-09-06 10:18:13 +08:00
})
var config = require('../config.json');
var compressPlugin = new CompressionPlugin({
asset: "[path].gz[query]",
algorithm: "gzip",
test: /\.(js|css)$/,
threshold: 10240,
2017-09-08 11:18:30 +08:00
minRatio: 0.8
2017-09-05 10:24:13 +08:00
});
function fileExist (filePath){
try {
return fs.statSync(filePath).isFile();
} catch (err) {
return false;
}
};
function initPlugins(){
if(config.plugins && Array.isArray(config.plugins)){
config.plugins = config.plugins.filter(item=>{
2017-09-07 11:02:21 +08:00
return fileExist(path.resolve(__dirname, 'plugins/yapi-plugin-' + item + '/client.js'))
2017-09-05 10:24:13 +08:00
})
}
}
initPlugins();
2017-08-10 21:20:57 +08:00
2017-08-10 21:20:57 +08:00
function handleCommonsChunk(webpackConfig) {
var commonsChunk = {
2017-08-22 10:43:40 +08:00
vendors: {
lib: ['react', 'redux',
'redux-thunk',
'react-dom',
'redux-promise',
2017-08-23 18:24:29 +08:00
'react-router',
2017-08-22 10:43:40 +08:00
'react-router-dom',
2017-08-23 18:24:29 +08:00
'prop-types',
2017-08-22 10:43:40 +08:00
'axios',
'moment'
2017-08-23 18:24:29 +08:00
],
2017-09-08 11:18:30 +08:00
lib2: [
2017-08-23 18:24:29 +08:00
'brace',
'mockjs',
'json5'
2017-08-22 10:43:40 +08:00
]
}
},
2017-08-10 21:20:57 +08:00
chunks = [],
filenameTpl = webpackConfig.output[this.env],
vendors;
if (typeof commonsChunk === 'object' && commonsChunk !== undefined) {
if (typeof commonsChunk.name === 'string' && commonsChunk) {
chunks.push(commonsChunk.name);
}
vendors = commonsChunk.vendors;
if (typeof vendors === 'object' && vendors !== undefined) {
var i = 0;
for (var name in vendors) {
if (vendors.hasOwnProperty(name) && vendors[name]) {
i++;
chunks.push(name);
webpackConfig.entry[name] = Array.isArray(vendors[name]) ? vendors[name] : [vendors[name]];
}
}
if (i > 0) {
chunks.push('manifest');
}
}
if (chunks.length > 0) {
let chunkFilename = filenameTpl.filename
chunkFilename = chunkFilename.replace("[ext]", '.js')
webpackConfig.plugins.push(
new this.webpack.optimize.CommonsChunkPlugin({
name: chunks,
filename: chunkFilename,
minChunks: commonsChunk.minChunks ? commonsChunk.minChunks : 2
})
);
}
}
}
2017-07-05 19:31:50 +08:00
module.exports = {
2017-09-06 10:18:13 +08:00
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: /node_modules/
}
}],
2017-07-14 12:18:55 +08:00
// devtool: 'cheap-source-map',
2017-07-25 15:07:55 +08:00
config: function (ykit) {
return {
2017-07-25 15:07:55 +08:00
exports: [
'./index.js'
],
2017-09-08 11:18:30 +08:00
modifyWebpackConfig: function (baseConfig) {
2017-08-31 18:04:45 +08:00
var ENV_PARAMS = {};
switch (this.env) {
case 'local':
2017-08-23 20:41:47 +08:00
ENV_PARAMS = 'dev';
break;
case 'dev':
2017-08-23 20:41:47 +08:00
ENV_PARAMS = 'dev';
break;
case 'prd':
2017-08-23 20:41:47 +08:00
ENV_PARAMS = 'production';
break;
default:
2017-08-22 10:43:40 +08:00
}
baseConfig.plugins.push(new this.webpack.DefinePlugin({
2017-09-05 10:24:13 +08:00
'process.env.NODE_ENV': JSON.stringify(ENV_PARAMS),
'process.env.config': JSON.stringify(config)
}))
2017-08-10 21:20:57 +08:00
//初始化配置
2017-07-25 15:07:55 +08:00
baseConfig.devtool = 'cheap-module-eval-source-map'
2017-08-31 18:04:45 +08:00
baseConfig.context = path.resolve(__dirname, './client');
baseConfig.resolve.alias.common = '/common';
2017-09-06 10:18:13 +08:00
baseConfig.resolve.alias.plugins = '/plugins';
2017-07-25 15:07:55 +08:00
baseConfig.output.prd.path = 'static/prd';
baseConfig.output.prd.publicPath = '';
2017-07-25 16:24:56 +08:00
baseConfig.output.prd.filename = '[name]@[chunkhash][ext]'
2017-08-10 21:20:57 +08:00
//commonsChunk
2017-09-06 10:18:13 +08:00
handleCommonsChunk.call(this, baseConfig)
2017-08-22 10:43:40 +08:00
baseConfig.module.loaders.push({
test: /\.less$/,
loader: ykit.ExtractTextPlugin.extract(
require.resolve('style-loader'),
require.resolve('css-loader')
+ '?sourceMap!'
+ require.resolve('less-loader') + '?sourceMap'
)
})
2017-07-25 15:07:55 +08:00
baseConfig.module.loaders.push({
test: /\.(sass|scss)$/,
loader: ykit.ExtractTextPlugin.extract(
require.resolve('css-loader')
+ '?sourceMap!'
+ require.resolve('fast-sass-loader-china') + '?sourceMap'
2017-07-25 15:07:55 +08:00
)
})
baseConfig.module.preLoaders.push({
2017-07-25 17:06:49 +08:00
test: /\.(js|jsx)$/,
2017-09-06 10:18:13 +08:00
exclude: /node_modules/,
loader: "eslint-loader"
2017-07-25 15:07:55 +08:00
});
2017-07-25 17:06:49 +08:00
if (this.env == 'prd') {
baseConfig.plugins.push(assetsPluginInstance)
baseConfig.plugins.push(compressPlugin)
2017-07-25 17:06:49 +08:00
}
2017-07-25 15:07:55 +08:00
return baseConfig;
}
2017-07-06 14:25:52 +08:00
}
},
server: {
// true/false默认 false效果相当于 ykit server --hot
2017-09-06 10:18:13 +08:00
hot: true,
2017-07-06 14:25:52 +08:00
// true/false默认 false开启后可在当前打开的页面提示打包错误
2017-09-06 10:18:13 +08:00
overlay: false
2017-07-06 14:25:52 +08:00
},
hooks: {},
commands: []
2017-09-08 11:18:30 +08:00
};