From 87fb6c11c3ef29ff59a6010c999b50a68cbd0f70 Mon Sep 17 00:00:00 2001 From: * <8> Date: Mon, 6 Jun 2022 14:26:22 +0800 Subject: [PATCH] Match-id-e58f29c586ec81e77c9f8885f24ebc69bd5d1f3c --- package.json | 14 +++---- scripts/gen3rdLib.js | 37 +++++++++-------- scripts/rollup/copy-plugin.js | 13 ++++++ scripts/rollup/rollup.config.js | 67 +++++++++++++++++++++++++++++++ scripts/webpack/webpack.base.js | 28 ------------- scripts/webpack/webpack.config.js | 4 -- scripts/webpack/webpack.dev.js | 45 --------------------- scripts/webpack/webpack.pro.js | 61 ---------------------------- 8 files changed, 108 insertions(+), 161 deletions(-) create mode 100644 scripts/rollup/copy-plugin.js create mode 100644 scripts/rollup/rollup.config.js delete mode 100644 scripts/webpack/webpack.base.js delete mode 100644 scripts/webpack/webpack.config.js delete mode 100644 scripts/webpack/webpack.dev.js delete mode 100644 scripts/webpack/webpack.pro.js diff --git a/package.json b/package.json index fa79a858..58bf2f9f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ ], "scripts": { "lint": "eslint . --ext .ts", - "build": " webpack --config ./scripts/webpack/webpack.config.js", + "build": " rollup --config ./scripts/rollup/rollup.config.js", "build-3rdLib": "node ./scripts/gen3rdLib.js", "build-3rdLib-dev": "npm run build & node ./scripts/gen3rdLib.js --dev", "build-horizon3rdLib-dev": "npm run build & node ./scripts/gen3rdLib.js --dev --type horizon", @@ -51,6 +51,9 @@ "@babel/register": "^7.14.5", "@babel/traverse": "^7.11.0", "@mattiasbuelens/web-streams-polyfill": "^0.3.2", + "@rollup/plugin-babel": "^5.3.1", + "@rollup/plugin-node-resolve": "^13.3.0", + "@rollup/plugin-replace": "^4.0.0", "@types/jest": "^26.0.24", "@types/node": "^17.0.18", "@typescript-eslint/eslint-plugin": "^5.15.0", @@ -58,13 +61,11 @@ "babel-core": "^6.26.3", "babel-eslint": "^10.0.3", "babel-jest": "^27.5.1", - "babel-loader": "^8.2.2", "babel-plugin-syntax-trailing-function-commas": "^6.5.0", "babel-preset-env": "^1.7.0", "babel-preset-react": "^6.24.1", "chalk": "^3.0.0", "confusing-browser-globals": "^1.0.9", - "copy-webpack-plugin": "5.0.4", "core-js": "^3.6.4", "danger": "^9.2.10", "ejs": "^3.1.6", @@ -77,7 +78,6 @@ "eslint-plugin-no-for-of-loops": "^1.0.0", "eslint-plugin-no-function-declare-after-return": "^1.0.0", "eslint-plugin-react": "^6.7.1", - "eslint-webpack-plugin": "^3.0.1", "jest": "^25.5.4", "jest-cli": "^25.2.7", "jest-diff": "^25.2.6", @@ -90,9 +90,9 @@ "react-lifecycles-compat": "^3.0.4", "regenerator-runtime": "^0.13.9", "rimraf": "^3.0.0", - "typescript": "^3.9.7", - "webpack": "^4.46.0", - "webpack-cli": "^4.7.2" + "rollup": "^2.75.5", + "rollup-plugin-terser": "^7.0.2", + "typescript": "^3.9.7" }, "engines": { "node": ">=10.x", diff --git a/scripts/gen3rdLib.js b/scripts/gen3rdLib.js index 3b446cd8..f2953691 100644 --- a/scripts/gen3rdLib.js +++ b/scripts/gen3rdLib.js @@ -1,4 +1,4 @@ -'use strict' +'use strict'; const ejs = require('ejs'); const fs = require('fs'); const path = require('path'); @@ -10,25 +10,30 @@ const argv = require('minimist')(process.argv.slice(2)); const libPathPrefix = '../build'; const suffix = argv.dev ? 'development.js' : 'production.js'; const template = argv.type === 'horizon' ? 'horizon3rdTemplate.ejs' : 'template.ejs'; -const readLib = (lib) => { +const readLib = lib => { const libName = lib.split('.')[0]; const libPath = path.resolve(__dirname, `${libPathPrefix}/${libName}/umd/${lib}`); if (fs.existsSync(libPath)) { - return fs.readFileSync(libPath,'utf-8'); + return fs.readFileSync(libPath, 'utf-8'); } else { - console.log(chalk.red(`Error: "${libPath}" 文件不存在\n先运行 npm run build`)) + console.log(chalk.red(`Error: "${libPath}" 文件不存在\n先运行 npm run build`)); } }; -ejs.renderFile(path.resolve(__dirname, `./${template}`), { - Horizon: readLib(`horizon.${suffix}`), -}, null, function(err, result) { - const common3rdLibPath = path.resolve(__dirname, `${libPathPrefix}/horizonCommon3rdlib.min.js`) - rimRaf(common3rdLibPath, e => { - if (e) { - console.log(e) - } - fs.writeFileSync(common3rdLibPath, result); - console.log(chalk.green(`成功生成: ${common3rdLibPath}`)) - }) -}); +ejs.renderFile( + path.resolve(__dirname, `./${template}`), + { + Horizon: readLib(`horizon.${suffix}`), + }, + null, + function(err, result) { + const common3rdLibPath = path.resolve(__dirname, `${libPathPrefix}/horizonCommon3rdlib.min.js`); + rimRaf(common3rdLibPath, e => { + if (e) { + console.log(e); + } + fs.writeFileSync(common3rdLibPath, result); + console.log(chalk.green(`成功生成: ${common3rdLibPath}`)); + }); + } +); diff --git a/scripts/rollup/copy-plugin.js b/scripts/rollup/copy-plugin.js new file mode 100644 index 00000000..45d9652f --- /dev/null +++ b/scripts/rollup/copy-plugin.js @@ -0,0 +1,13 @@ +import fs from 'fs'; + +export default function copyFiles(copyPairs) { + return { + name: 'copy-files', + generateBundle() { + copyPairs.forEach(({ from, to }) => { + console.log(`copy files: ${from} → ${to}`); + fs.copyFileSync(from, to); + }); + }, + }; +} diff --git a/scripts/rollup/rollup.config.js b/scripts/rollup/rollup.config.js new file mode 100644 index 00000000..fb40e560 --- /dev/null +++ b/scripts/rollup/rollup.config.js @@ -0,0 +1,67 @@ +import nodeResolve from '@rollup/plugin-node-resolve'; +import babel from '@rollup/plugin-babel'; +import path from 'path'; +import replace from '@rollup/plugin-replace'; +import copy from './copy-plugin'; +import { terser } from 'rollup-plugin-terser'; +import { version as horizonVersion } from '@cloudsop/horizon/package.json'; + +const extensions = ['.js', '.ts']; + +const libDir = path.join(__dirname, '../../libs/horizon'); +const rootDir = path.join(__dirname, '../..'); +const outDir = path.join(rootDir, 'build', 'horizon'); +const outputResolve = (...p) => path.resolve(outDir, ...p); + +function genConfig(mode) { + const sourcemap = mode === 'development' ? 'inline' : false; + return { + input: path.resolve(libDir, 'index.ts'), + output: [ + { + file: outputResolve('cjs', `horizon.${mode}.js`), + sourcemap, + format: 'cjs', + }, + { + file: outputResolve('umd', `horizon.${mode}.js`), + sourcemap, + name: 'Horizon', + format: 'umd', + }, + ], + plugins: [ + nodeResolve({ + extensions, + modulesOnly: true, + }), + babel({ + exclude: 'node_modules/**', + configFile: path.join(__dirname, '../../babel.config.js'), + babelHelpers: 'runtime', + extensions, + }), + replace({ + values: { + 'process.env.NODE_ENV': `"${mode}"`, + isDev: 'true', + __VERSION__: `"${horizonVersion}"`, + }, + preventAssignment: true, + }), + mode === 'production' && terser(), + copy([ + { + from: path.join(libDir, 'index.js'), + to: path.join(outDir, 'index.js'), + }, + { + from: path.join(libDir, 'package.json'), + to: path.join(outDir, 'package.json'), + }, + ]), + ], + }; +} + +export default [genConfig('development'), genConfig('production')]; diff --git a/scripts/webpack/webpack.base.js b/scripts/webpack/webpack.base.js deleted file mode 100644 index 8bae9049..00000000 --- a/scripts/webpack/webpack.base.js +++ /dev/null @@ -1,28 +0,0 @@ -const path = require('path'); - -const libPath = path.join(__dirname, '../../libs/horizon'); -const baseConfig = { - entry: path.resolve(libPath, 'index.ts'), - module: { - rules: [ - { - test: /\.(js)|ts$/, - exclude: /node_modules/, - use: [ - { - loader: 'babel-loader' - } - ] - }, - ] - }, - resolve: { - extensions: ['.js', '.ts'], - alias: { - 'horizon-external': path.join(libPath, './horizon-external'), - 'horizon': path.join(libPath, './horizon'), - } - }, -}; - -module.exports = baseConfig; diff --git a/scripts/webpack/webpack.config.js b/scripts/webpack/webpack.config.js deleted file mode 100644 index ca762982..00000000 --- a/scripts/webpack/webpack.config.js +++ /dev/null @@ -1,4 +0,0 @@ -const dev = require('./webpack.dev'); -const pro = require('./webpack.pro'); - -module.exports = [...dev, ...pro]; diff --git a/scripts/webpack/webpack.dev.js b/scripts/webpack/webpack.dev.js deleted file mode 100644 index 7a537a9c..00000000 --- a/scripts/webpack/webpack.dev.js +++ /dev/null @@ -1,45 +0,0 @@ -const webpack = require('webpack'); -const ESLintPlugin = require('eslint-webpack-plugin'); -const baseConfig = require('./webpack.base'); -const path = require('path'); -const horizonVersion = require('../../libs/horizon/package.json').version; - -const mode = 'development'; -const devtool = 'inline-source-map'; -const filename = 'horizon.development.js'; - -const plugins = [ - new ESLintPlugin({fix: true}), - new webpack.DefinePlugin({ - 'process.env.NODE_ENV': '"development"', - isDev: 'true', - __VERSION__: `"${horizonVersion}"`, - }), -]; - -const umd = { - ...baseConfig, - mode, - devtool, - output: { - path: path.resolve(__dirname, '../../build/horizon/umd'), - filename, - libraryTarget: 'umd', - library: 'Horizon', - }, - plugins, -}; - -const cjs = { - ...baseConfig, - mode, - devtool, - output: { - path: path.resolve(__dirname, '../../build/horizon/cjs'), - filename, - libraryTarget: 'commonjs', - }, - plugins, -}; - -module.exports = [umd, cjs]; diff --git a/scripts/webpack/webpack.pro.js b/scripts/webpack/webpack.pro.js deleted file mode 100644 index 64bb472b..00000000 --- a/scripts/webpack/webpack.pro.js +++ /dev/null @@ -1,61 +0,0 @@ -const webpack = require('webpack'); -const CopyWebpackPlugin = require('copy-webpack-plugin'); -const baseConfig = require('./webpack.base'); -const path = require('path'); -const horizonVersion = require('../../libs/horizon/package.json').version; - -const mode = 'production'; -const devtool = 'none'; -const filename = 'horizon.production.js'; - -const plugins = [ - new webpack.DefinePlugin({ - 'process.env.NODE_ENV': '"production"', - isDev: 'false', - __VERSION__: `"${horizonVersion}"`, - }) -]; - -const proBaseConfig = { - ...baseConfig, - mode, - devtool, - plugins, - optimization: { - minimize: true - }, -}; - -const umd = { - ...proBaseConfig, - output: { - path: path.resolve(__dirname, '../../build/horizon/umd'), - filename, - libraryTarget: 'umd', - library: 'Horizon', - }, -}; - -const cjs = { - ...proBaseConfig, - output: { - path: path.resolve(__dirname, '../../build/horizon/cjs'), - filename, - libraryTarget: 'commonjs', - }, - plugins: [ - ...plugins, - new CopyWebpackPlugin([ - { - from: path.join(__dirname, '../../libs/horizon/index.js'), - to: path.join(__dirname, '../../build/horizon/index.js'), - }, - { - from: path.join(__dirname, '../../libs/horizon/package.json'), - to: path.join(__dirname, '../../build/horizon/package.json'), - } - ]) - ] -}; - -module.exports = [umd, cjs];