From 083e2f8b6c0e6166a3a26d5da6b71fec028ca606 Mon Sep 17 00:00:00 2001 From: sawka Date: Wed, 25 Oct 2023 13:32:15 -0700 Subject: [PATCH] big update for webpack configs. use --env to select environment. now dev/prod will combine both web and electron webpack environments --- scripthaus.md | 55 ++---------- webpack.config.js | 30 +++++++ webpack/webpack.dev.js | 36 -------- webpack/webpack.electron.js | 66 +++++++++++--- webpack/webpack.prod.js | 44 ---------- webpack/webpack.share.dev.js | 44 ---------- webpack/webpack.share.js | 62 -------------- webpack/webpack.share.prod.js | 38 --------- webpack/{webpack.common.js => webpack.web.js} | 85 ++++++++++++++++--- 9 files changed, 169 insertions(+), 291 deletions(-) create mode 100644 webpack.config.js delete mode 100644 webpack/webpack.dev.js delete mode 100644 webpack/webpack.prod.js delete mode 100644 webpack/webpack.share.dev.js delete mode 100644 webpack/webpack.share.js delete mode 100644 webpack/webpack.share.prod.js rename webpack/{webpack.common.js => webpack.web.js} (57%) diff --git a/scripthaus.md b/scripthaus.md index 32457750f..59281472d 100644 --- a/scripthaus.md +++ b/scripthaus.md @@ -3,25 +3,19 @@ ```bash # @scripthaus command webpack-watch # @scripthaus cd :playbook -node_modules/.bin/webpack --watch --config webpack/webpack.dev.js +node_modules/.bin/webpack --env dev --watch ``` ```bash # @scripthaus command webpack-build # @scripthaus cd :playbook -node_modules/.bin/webpack --config webpack/webpack.dev.js +node_modules/.bin/webpack --env dev ``` ```bash -# @scripthaus command webpack-electron-watch +# @scripthaus command webpack-build-prod # @scripthaus cd :playbook -node_modules/.bin/webpack --watch --config webpack/webpack.electron.js -``` - -```bash -# @scripthaus command webpack-electron-build -# @scripthaus cd :playbook -node_modules/.bin/webpack --config webpack/webpack.electron.js +node_modules/.bin/webpack --env prod ``` ```bash @@ -36,55 +30,24 @@ node_modules/.bin/electron-rebuild PROMPT_DEV=1 PCLOUD_ENDPOINT="https://ot2e112zx5.execute-api.us-west-2.amazonaws.com/dev" node_modules/.bin/electron dist-dev/emain.js ``` -```bash -# @scripthaus command devserver -# @scripthaus cd :playbook -node_modules/.bin/webpack-dev-server --config webpack/webpack.dev.js --host 0.0.0.0 -``` - -```bash -# @scripthaus command webshare-devserver -# @scripthaus cd :playbook -node_modules/.bin/webpack-dev-server --config webpack/webpack.share.dev.js --host 127.0.0.1 -``` - -```bash -# @scripthaus command webshare-build -# @scripthaus cd :playbook -node_modules/.bin/webpack --config webpack/webpack.share.dev.js -``` - -```bash -# @scripthaus command webshare-build-prod -# @scripthaus cd :playbook -node_modules/.bin/webpack --config webpack/webpack.share.prod.js -``` - ```bash # @scripthaus command typecheck # @scripthaus cd :playbook node_modules/.bin/tsc --jsx preserve --noEmit --esModuleInterop --target ES5 --experimentalDecorators --downlevelIteration src/index.ts ``` -```bash -# @scripthaus command typecheck-webshare -# @scripthaus cd :playbook -node_modules/.bin/tsc --jsx preserve --noEmit --esModuleInterop --target ES5 --experimentalDecorators --downlevelIteration src/webshare.ts -``` - ```bash # @scripthaus command build-package # @scripthaus cd :playbook rm -rf dist/ rm -rf bin/ rm -rf build/ -node_modules/.bin/webpack --config webpack/webpack.prod.js -node_modules/.bin/webpack --config webpack/webpack.electron.prod.js +node_modules/.bin/webpack --env prod GO_LDFLAGS="-s -w -X main.BuildTime=$(date +'%Y%m%d%H%M')" -(cd ../apishell; GOOS=darwin GOARCH=amd64 go build -ldflags="$GO_LDFLAGS" -o ../prompt-client/bin/mshell/mshell-v0.3-darwin.amd64 main-mshell.go) -(cd ../apishell; GOOS=darwin GOARCH=arm64 go build -ldflags="$GO_LDFLAGS" -o ../prompt-client/bin/mshell/mshell-v0.3-darwin.arm64 main-mshell.go) -(cd ../apishell; GOOS=linux GOARCH=amd64 go build -ldflags="$GO_LDFLAGS" -o ../prompt-client/bin/mshell/mshell-v0.3-linux.amd64 main-mshell.go) -(cd ../apishell; GOOS=linux GOARCH=arm64 go build -ldflags="$GO_LDFLAGS" -o ../prompt-client/bin/mshell/mshell-v0.3-linux.arm64 main-mshell.go) +(cd ../waveshell; GOOS=darwin GOARCH=amd64 go build -ldflags="$GO_LDFLAGS" -o ../bin/mshell/mshell-v0.3-darwin.amd64 main-waveshell.go) +(cd ../waveshell; GOOS=darwin GOARCH=arm64 go build -ldflags="$GO_LDFLAGS" -o ../bin/mshell/mshell-v0.3-darwin.arm64 main-waveshell.go) +(cd ../waveshell; GOOS=linux GOARCH=amd64 go build -ldflags="$GO_LDFLAGS" -o ../bin/mshell/mshell-v0.3-linux.amd64 main-waveshell.go) +(cd ../waveshell; GOOS=linux GOARCH=arm64 go build -ldflags="$GO_LDFLAGS" -o ../bin/mshell/mshell-v0.3-linux.arm64 main-waveshell.go) (cd wavesrv; GOOS=darwin GOARCH=amd64 go build -ldflags="$GO_LDFLAGS" -o ../build/wavesrv.amd64 ./cmd) (cd wavesrv; GOOS=darwin GOARCH=arm64 go build -ldflags="$GO_LDFLAGS" -o ../build/wavesrv.arm64 ./cmd) lipo -create -output bin/wavesrv build/wavesrv.amd64 build/wavesrv.arm64 diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 000000000..070ce8030 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,30 @@ +const {webDev, webProd} = require("./webpack/webpack.web.js"); +const {electronDev, electronProd} = require("./webpack/webpack.electron.js"); + +module.exports = (env) => { + if (env.prod) { + console.log("using PROD (web+electron) webpack environment"); + return [webProd, electronProd]; + } + if (env["prod:web"]) { + console.log("using PROD (web) webpack environment"); + return webProd; + } + if (env["prod:electron"]) { + console.log("using PROD (electron) webpack environment"); + return electronProd; + } + if (env.dev) { + console.log("using DEV (web+electron) webpack environment"); + return [webDev, electronDev]; + } + if (env["dev:web"]) { + console.log("using DEV (web) webpack environment"); + return webDev; + } + if (env["dev:electron"]) { + console.log("using DEV (electron) webpack environment"); + return electronDev; + } + console.log("must specify a webpack environment using --env [dev|prod]"); +}; diff --git a/webpack/webpack.dev.js b/webpack/webpack.dev.js deleted file mode 100644 index 7026cfd15..000000000 --- a/webpack/webpack.dev.js +++ /dev/null @@ -1,36 +0,0 @@ -const webpack = require("webpack"); -const merge = require("webpack-merge"); -const common = require("./webpack.common.js"); -const path = require("path"); -const CopyPlugin = require("copy-webpack-plugin"); -const VERSION = require("../version.js"); - -var merged = merge.merge(common, { - mode: "development", - output: { - path: path.resolve(__dirname, "../dist-dev"), - filename: "[name].js", - }, - devtool: "source-map", - devServer: { - static: { - directory: path.join(__dirname, "static"), - }, - port: 9000, - headers: { - "Cache-Control": "no-store", - }, - }, - watchOptions: { - aggregateTimeout: 200, - }, -}); - -var definePlugin = new webpack.DefinePlugin({ - __PROMPT_DEV__: "true", - __PROMPT_VERSION__: JSON.stringify(VERSION), - __PROMPT_BUILD__: JSON.stringify("devbuild"), -}); -merged.plugins.push(definePlugin); - -module.exports = merged; diff --git a/webpack/webpack.electron.js b/webpack/webpack.electron.js index 292f2d90a..771fd714f 100644 --- a/webpack/webpack.electron.js +++ b/webpack/webpack.electron.js @@ -1,20 +1,28 @@ +const webpack = require("webpack"); +const webpackMerge = require("webpack-merge"); const path = require("path"); +const moment = require("dayjs"); +const VERSION = require("../version.js"); const CopyPlugin = require("copy-webpack-plugin"); -module.exports = { - mode: "development", +function makeBuildStr() { + let buildStr = moment().format("YYYYMMDD-HHmmss"); + // console.log("waveterm:electron " + VERSION + " build " + buildStr); + return buildStr; +} + +const BUILD = makeBuildStr(); + +var electronCommon = { entry: { emain: ["./src/electron/emain.ts"], }, target: "electron-main", - output: { - path: path.resolve(__dirname, "../dist-dev"), - filename: "[name].js", - }, externals: { fs: "require('fs')", "fs-ext": "require('fs-ext')", }, + devtool: "source-map", module: { rules: [ { @@ -48,12 +56,48 @@ module.exports = { }, ], }, - plugins: [ - new CopyPlugin({ - patterns: [{ from: "src/electron/preload.js", to: "preload.js" }], - }), - ], resolve: { extensions: [".ts", ".tsx", ".js"], }, }; + +var electronDev = webpackMerge.merge(electronCommon, { + mode: "development", + output: { + path: path.resolve(__dirname, "../dist-dev"), + filename: "[name].js", + }, + plugins: [ + new CopyPlugin({ + patterns: [{ from: "src/electron/preload.js", to: "preload.js" }], + }), + new webpack.DefinePlugin({ + __PROMPT_DEV__: "true", + __PROMPT_VERSION__: JSON.stringify(VERSION), + __PROMPT_BUILD__: JSON.stringify("devbuild"), + }), + ], +}); + +var electronProd = webpackMerge.merge(electronCommon, { + mode: "production", + output: { + path: path.resolve(__dirname, "../dist"), + filename: "[name].js", + }, + plugins: [ + new CopyPlugin({ + patterns: [{ from: "src/electron/preload.js", to: "preload.js" }], + }), + new webpack.DefinePlugin({ + __PROMPT_DEV__: "false", + __PROMPT_VERSION__: JSON.stringify(VERSION), + __PROMPT_BUILD__: JSON.stringify(BUILD), + }), + ], + optimization: { + minimize: true, + }, +}); + +module.exports = {electronDev: electronDev, electronProd: electronProd}; diff --git a/webpack/webpack.prod.js b/webpack/webpack.prod.js deleted file mode 100644 index aa56bf838..000000000 --- a/webpack/webpack.prod.js +++ /dev/null @@ -1,44 +0,0 @@ -const webpack = require("webpack"); -const merge = require("webpack-merge"); -const common = require("./webpack.common.js"); -const moment = require("dayjs"); -const path = require("path"); -const VERSION = require("../version.js"); - -function makeBuildStr() { - let buildStr = moment().format("YYYYMMDD-HHmmss"); - console.log("Prompt " + VERSION + " build " + buildStr); - return buildStr; -} - -const BUILD = makeBuildStr(); - -let BundleAnalyzerPlugin = null; -if (process.env.WEBPACK_ANALYZE) { - BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin; -} - -let merged = merge.merge(common, { - mode: "production", - output: { - path: path.resolve(__dirname, "../dist"), - filename: "[name].js", - }, - devtool: false, - optimization: { - minimize: true, - }, -}); - -if (BundleAnalyzerPlugin != null) { - merged.plugins.push(new BundleAnalyzerPlugin()); -} -merged.plugins.push( - new webpack.DefinePlugin({ - __PROMPT_DEV__: "false", - __PROMPT_VERSION__: JSON.stringify(VERSION), - __PROMPT_BUILD__: JSON.stringify(BUILD), - }) -); - -module.exports = merged; diff --git a/webpack/webpack.share.dev.js b/webpack/webpack.share.dev.js deleted file mode 100644 index 2eb5015dd..000000000 --- a/webpack/webpack.share.dev.js +++ /dev/null @@ -1,44 +0,0 @@ -const webpack = require("webpack"); -const merge = require("webpack-merge"); -const common = require("./webpack.share.js"); -const path = require("path"); -const CopyPlugin = require("copy-webpack-plugin"); -const VERSION = require("../version.js"); - -var merged = merge.merge(common, { - mode: "development", - output: { - path: path.resolve(__dirname, "webshare/dist-dev"), - filename: "[name].js", - }, - devtool: "source-map", - devServer: { - static: { - directory: path.join(__dirname, "./webshare"), - }, - port: 9001, - headers: { - "Cache-Control": "no-store", - }, - devMiddleware: { - publicPath: "/dist-dev/", - }, - allowedHosts: "all", - hot: false, - liveReload: false, - }, - watchOptions: { - aggregateTimeout: 200, - }, -}); - -var definePlugin = new webpack.DefinePlugin({ - __PROMPT_DEV__: "true", - __PROMPT_VERSION__: JSON.stringify(VERSION), - __PROMPT_BUILD__: JSON.stringify("devbuild"), - __PROMPT_API_ENDPOINT__: JSON.stringify("https://ot2e112zx5.execute-api.us-west-2.amazonaws.com/dev"), - __PROMPT_WSAPI_ENDPOINT__: JSON.stringify("wss://5lfzlg5crl.execute-api.us-west-2.amazonaws.com/dev"), -}); -merged.plugins.push(definePlugin); - -module.exports = merged; diff --git a/webpack/webpack.share.js b/webpack/webpack.share.js deleted file mode 100644 index bc84ae3d3..000000000 --- a/webpack/webpack.share.js +++ /dev/null @@ -1,62 +0,0 @@ -const MiniCssExtractPlugin = require("mini-css-extract-plugin"); -const LodashModuleReplacementPlugin = require("lodash-webpack-plugin"); -const path = require("path"); - -module.exports = { - mode: "development", - entry: { - webshare: ["./src/webshare.ts", "./src/app/app.less"], - }, - output: { - path: path.resolve(__dirname, "webshare/dist"), - filename: "[name].js", - }, - module: { - rules: [ - { - test: /\.tsx?$/, - // exclude: /node_modules/, - use: { - loader: "babel-loader", - options: { - presets: [ - [ - "@babel/preset-env", - { - targets: - "defaults and not ie > 0 and not op_mini all and not op_mob > 0 and not kaios > 0 and not and_qq > 0 and not and_uc > 0 and not baidu > 0", - }, - ], - "@babel/preset-react", - "@babel/preset-typescript", - ], - plugins: [ - ["@babel/transform-runtime", { regenerator: true }], - "@babel/plugin-transform-react-jsx", - ["@babel/plugin-proposal-decorators", { legacy: true }], - ["@babel/plugin-proposal-class-properties", { loose: true }], - ["@babel/plugin-proposal-private-methods", { loose: true }], - ["@babel/plugin-proposal-private-property-in-object", { loose: true }], - "babel-plugin-jsx-control-statements", - ], - }, - }, - }, - { - test: /\.css$/, - use: ["style-loader", "css-loader"], - }, - { - test: /\.less$/, - use: [{ loader: MiniCssExtractPlugin.loader }, "css-loader", "less-loader"], - }, - ], - }, - plugins: [ - new MiniCssExtractPlugin({ filename: "[name].css", ignoreOrder: true }), - new LodashModuleReplacementPlugin(), - ], - resolve: { - extensions: [".ts", ".tsx", ".js", ".mjs", ".cjs", ".wasm", ".json", ".less", ".css"], - }, -}; diff --git a/webpack/webpack.share.prod.js b/webpack/webpack.share.prod.js deleted file mode 100644 index 8bbbeba0c..000000000 --- a/webpack/webpack.share.prod.js +++ /dev/null @@ -1,38 +0,0 @@ -const webpack = require("webpack"); -const merge = require("webpack-merge"); -const common = require("./webpack.share.js"); -const moment = require("dayjs"); -const path = require("path"); -const VERSION = require("../version.js"); - -function makeBuildStr() { - let buildStr = moment().format("YYYYMMDD-HHmmss"); - console.log("Prompt " + VERSION + " build " + buildStr); - return buildStr; -} - -const BUILD = makeBuildStr(); - -let merged = merge.merge(common, { - mode: "production", - output: { - path: path.resolve(__dirname, "webshare/dist"), - filename: "[name].js", - }, - devtool: false, - optimization: { - minimize: true, - }, -}); - -merged.plugins.push( - new webpack.DefinePlugin({ - __PROMPT_DEV__: "false", - __PROMPT_VERSION__: JSON.stringify(VERSION), - __PROMPT_BUILD__: JSON.stringify(BUILD), - __PROMPT_API_ENDPOINT__: JSON.stringify("https://share.getprompt.dev/api"), - __PROMPT_WSAPI_ENDPOINT__: JSON.stringify("wss://wsapi.getprompt.dev"), - }) -); - -module.exports = merged; diff --git a/webpack/webpack.common.js b/webpack/webpack.web.js similarity index 57% rename from webpack/webpack.common.js rename to webpack/webpack.web.js index a03e248da..f5cc0a2ca 100644 --- a/webpack/webpack.common.js +++ b/webpack/webpack.web.js @@ -1,16 +1,29 @@ +const webpack = require("webpack"); const MiniCssExtractPlugin = require("mini-css-extract-plugin"); const LodashModuleReplacementPlugin = require("lodash-webpack-plugin"); +const CopyPlugin = require("copy-webpack-plugin"); +const webpackMerge = require("webpack-merge"); const path = require("path"); +const moment = require("dayjs"); +const VERSION = require("../version.js"); -module.exports = { - mode: "development", +function makeBuildStr() { + let buildStr = moment().format("YYYYMMDD-HHmmss"); + // console.log("waveterm:web " + VERSION + " build " + buildStr); + return buildStr; +} + +const BUILD = makeBuildStr(); + +let BundleAnalyzerPlugin = null; +if (process.env.WEBPACK_ANALYZE) { + BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin; +} + +var webCommon = { entry: { prompt: ["./src/index.ts", "./src/app/app.less"], }, - output: { - path: path.resolve(__dirname, "../dist"), - filename: "[name].js", - }, module: { rules: [ { @@ -56,11 +69,63 @@ module.exports = { }, ], }, - plugins: [ - new MiniCssExtractPlugin({ filename: "[name].css", ignoreOrder: true }), - new LodashModuleReplacementPlugin(), - ], resolve: { extensions: [".ts", ".tsx", ".js", ".mjs", ".cjs", ".wasm", ".json", ".less", ".css"], }, }; + +var webDev = webpackMerge.merge(webCommon, { + mode: "development", + output: { + path: path.resolve(__dirname, "../dist-dev"), + filename: "[name].js", + }, + devtool: "source-map", + devServer: { + static: { + directory: path.join(__dirname, "../public"), + }, + port: 9000, + headers: { + "Cache-Control": "no-store", + }, + }, + plugins: [ + new MiniCssExtractPlugin({ filename: "[name].css", ignoreOrder: true }), + new LodashModuleReplacementPlugin(), + new webpack.DefinePlugin({ + __PROMPT_DEV__: "true", + __PROMPT_VERSION__: JSON.stringify(VERSION), + __PROMPT_BUILD__: JSON.stringify("devbuild"), + }), + ], + watchOptions: { + aggregateTimeout: 200, + }, +}); + +var webProd = webpackMerge.merge(webCommon, { + mode: "production", + output: { + path: path.resolve(__dirname, "../dist"), + filename: "[name].js", + }, + devtool: false, + plugins: [ + new MiniCssExtractPlugin({ filename: "[name].css", ignoreOrder: true }), + new LodashModuleReplacementPlugin(), + new webpack.DefinePlugin({ + __PROMPT_DEV__: "false", + __PROMPT_VERSION__: JSON.stringify(VERSION), + __PROMPT_BUILD__: JSON.stringify(BUILD), + }), + ], + optimization: { + minimize: true, + }, +}); +if (BundleAnalyzerPlugin != null) { + webProd.plugins.push(new BundleAnalyzerPlugin()); +} + +module.exports = {webDev: webDev, webProd: webProd};