@fxext/cli
Version:
fanxing miniapp cli
103 lines (101 loc) • 3.17 kB
JavaScript
const path = require('path');
const fs = require('fs-extra');
const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin-hash');
const HappyPack = require('happypack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const { getLocalIP } = require('../utils');
const { VueLoaderPlugin } = require('vue-loader');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
module.exports = (config, builder, { framework = 'react', layoutType }) => {
const { isProduction } = config;
const { host, port, https, appMode = 0, sdkType } = builder;
const plugins = [
new HappyPack({
id: '1',
verbose: false,
loaders: [
{
path: 'babel-loader',
options: {
cacheDirectory: config.path.cache,
},
query: {
configFile: framework === 'vue' ? path.resolve(__dirname, 'vue-babel.config.json') : path.resolve(__dirname, 'babel.config.json'),
},
},
],
}),
new webpack.DefinePlugin(config.injectVar),
];
if (framework === 'vue') {
plugins.push(new VueLoaderPlugin());
} else if (framework === 'react') {
plugins.push(new ReactRefreshWebpackPlugin());
}
if (isProduction) {
plugins.push(
new webpack.HashedModuleIdsPlugin({
hashFunction: 'sha256',
hashDigest: 'hex',
hashDigestLength: 10,
})
);
} else {
const messages = [
`App running at:`,
`- 本地IP地址: ${getLocalIP()}`,
`- 开发端口号: ${port}`,
'请将该信息配置到开发者中心',
'',
`本地服务:`
];
if (fs.pathExistsSync(path.join(process.cwd(), './m/app/index.js'))) {
messages.push(`Web面板:${https ? 'https' : 'http'}://${host}:${port}/index.html`);
}
if (fs.pathExistsSync(path.join(process.cwd(), './streamer_pc/app/streamer_pc.js'))) {
messages.push(`伴侣面板:${https ? 'https' : 'http'}://${host}:${port}/streamer_pc.html`);
}
plugins.push(new FriendlyErrorsWebpackPlugin({
compilationSuccessInfo: {
messages
},
clearConsole: true,
additionalFormatters: [],
additionalTransformers: []
}));
}
if (config.clean) {
plugins.push(new CleanWebpackPlugin());
}
plugins.push(new CopyWebpackPlugin([
{
from: `*(m|streamer_pc)/app.json`,
to: `[path][name].[ext]`,
}
]));
config.html.forEach((page, key) => {
if (page.type === layoutType) {
let loadSdkType = 'fx-ext';
if (appMode === 1) {
loadSdkType = 'fx-ext-user';
}
if (sdkType === 'partner') {
loadSdkType = 'fx-ext-partner';
}
plugins.push(
new HtmlWebpackPlugin({
filename: page.key + '.html',
template: page.path,
isProduction,
sdkVersion: config.sdkVersion,
chunks: [`${page.type}/app/${page.key}`],
loadSdkType
})
);
}
});
return plugins;
};