big update for webpack configs. use --env to select environment. now dev/prod will combine both web and electron webpack environments

This commit is contained in:
sawka 2023-10-25 13:32:15 -07:00
parent d19cd7c623
commit 083e2f8b6c
9 changed files with 169 additions and 291 deletions

View File

@ -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

30
webpack.config.js Normal file
View File

@ -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]");
};

View File

@ -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;

View File

@ -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};

View File

@ -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;

View File

@ -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;

View File

@ -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"],
},
};

View File

@ -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;

View File

@ -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};