mtl-js-sdk
Version:
ynf-fw-mtl-api
293 lines (274 loc) • 9.16 kB
JavaScript
/*
* @Author: wangyingliang@yonyou.com
* @Date: 2023-09-06 11:17:55
* @LastEditors: wangyingliang wangyingliang@yonyou.com
* @LastEditTime: 2025-01-03 14:41:02
* @FilePath: /mtl-api-project/webpack/release.js
* @Description: webpack 配置
* Copyright (c) 2023 by Yonyou, All Rights Reserved.
*/
const TerserWebpackPlugin = require("terser-webpack-plugin");
var buildConfig = require("../scripts/build-config")(process.env.platform);
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
var config = [];
const path = require("path")
const APP_PATH = path.resolve(__dirname, "../")
const APP_SRC = path.join(APP_PATH, "/src")
const APP_PUB = path.join(APP_PATH, "/public")
const APP_DIST = path.join(APP_PATH, "/dist")
const APP_NDIST = path.join(APP_PATH, "/ndist")
function generateConfig(name) {
var uglify = name.indexOf("min") > -1;
let fileName = name
if (uglify && process.env.platform && "YouZone" != process.env.platform) {
fileName = name.replace('min', process.env.platform)
}
var config = {
mode: uglify ? 'production' : 'development',
entry: ["./index.js"],
output: {
path: process.env.platform ? `${APP_NDIST}/${process.env.platform}` : APP_DIST,
filename: fileName + ".js",
sourceMapFilename: fileName + ".js.map",
libraryTarget: "umd",
library: 'mtl',
umdNamedDefine: true
},
devtool: false,
performance: {
hints: false
},
plugins: [
// new BundleAnalyzerPlugin()
],
optimization: {
usedExports: true, // 启用Tree Shaking
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",//只用一个
options: {
presets: ['@babel/preset-env'],
cacheDirectory: true, // 开启babel编译缓存
cacheCompression: false, // 缓存文件不要压缩,压缩需要时间
plugins: ["@babel/plugin-transform-runtime"], // 减少代码体积
},
}
]
}
],
},
stats: {
errorDetails: true,
},
};
if (uglify) {
config.plugins.push(new TerserWebpackPlugin({
test: /\.js(\?.*)?$/i,
// 开启多进程压缩,提升速度
parallel: true,
// 是否将压缩后的代码注释删除
extractComments: true,
// 压缩选项
terserOptions: {
// 是否保留注释
output: {
comments: false,
},
// 压缩选项
compress: {
// 删除所有console
drop_console: false,
// 删除所有debugger
drop_debugger: true
}
}
}))
}
return config;
}
function generatePluginConfig(name) {
var config = {
mode: 'production',
entry: [`./lib/plugins/${name}/index.js`],
output: {
path: APP_DIST,
filename: `mtl.${name}.js`,
sourceMapFilename: `mtl.${name}.js.map`,
libraryTarget: "umd"
},
devtool: false,
optimization: {
usedExports: true, // 启用Tree Shaking
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",//只用一个
options: {
presets: ['@babel/preset-env'],
cacheDirectory: true, // 开启babel编译缓存
cacheCompression: false, // 缓存文件不要压缩,压缩需要时间
plugins: ["@babel/plugin-transform-runtime"], // 减少代码体积
},
}
]
}
],
},
plugins: [
new TerserWebpackPlugin({
test: /\.js(\?.*)?$/i,
// 开启多进程压缩,提升速度
parallel: true,
// 是否将压缩后的代码注释删除
extractComments: true,
// 压缩选项
terserOptions: {
// 是否保留注释
output: {
comments: false,
},
// 压缩选项
compress: {
// 删除所有console
drop_console: false,
// 删除所有debugger
drop_debugger: true
}
}
})
]
}
return config;
}
// 用于流水线
function nGenerateConfig(name, entry) {
var config = {
mode: 'production',
entry: [`./src/${entry}.js`],
output: {
path: APP_DIST,
filename: name + ".js",
sourceMapFilename: name + ".js.map",
libraryTarget: "umd",
library: 'mtl$cdn',
umdNamedDefine: true
},
devtool: false,
performance: {
hints: false
},
plugins: [
new TerserWebpackPlugin({
test: /\.js(\?.*)?$/i,
// 开启多进程压缩,提升速度
parallel: true,
// 是否将压缩后的代码注释删除
extractComments: true,
// 压缩选项
terserOptions: {
// 是否保留注释
output: {
comments: false,
},
// 压缩选项
compress: {
// 删除所有console
drop_console: false,
// 删除所有debugger
drop_debugger: true
}
}
})
],
optimization: {
usedExports: true, // 启用Tree Shaking
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",//只用一个
options: {
presets: ['@babel/preset-env'],
cacheDirectory: true, // 开启babel编译缓存
cacheCompression: false, // 缓存文件不要压缩,压缩需要时间
plugins: ["@babel/plugin-transform-runtime"], // 减少代码体积
},
}
]
}
],
}
};
return config;
}
// 编译插件
buildConfig.libs.optional.forEach(function (key) {
config.push(generatePluginConfig(key))
});
if (process.env.platform) {
["mtl.min"].forEach(function (key) {
config.push(generateConfig(key))
});
// 流水线
let key = "dyn"
config.push(nGenerateConfig(key, 'nindex'))
} else {
["mtl", "mtl.min"].forEach(function (key) {
config.push(generateConfig(key))
});
}
// copy src文件夹到 lib
function copyFolder(source, target) {
const fs = require("fs")
const path = require("path")
if (!fs.existsSync(target)) {
fs.mkdirSync(target)
}
const files = fs.readdirSync(source)
files.forEach(file => {
const sourcePath = path.join(source, file)
const targetPath = path.join(target, file)
const stat = fs.statSync(sourcePath)
if (stat.isFile()) {
fs.copyFileSync(sourcePath, targetPath)
} else if (stat.isDirectory()) {
copyFolder(sourcePath, targetPath)
}
})
}
copyFolder("./src", "./lib")
// 利用hook文件处理 lib 中的index.
const { replaceDynamicCode } = require("../scripts/hook");
[
{
config: buildConfig,
src: "./lib/index.js"
}
].forEach(file => {
const { config, src } = file;
replaceDynamicCode({ file: src, config: config });
});
// 预处理可选插件
var preaction = require("../scripts/plugin-pre-action");
buildConfig.libs.optional.forEach(file => {
preaction(`./lib/plugins/${file}/index.js`);
});
// console.log('-------------');
// console.log(config);
// console.log('-------------');
module.exports = [config];