Browse Source

修改项目配置,为electron打包做准备

dengdx 3 weeks ago
parent
commit
05c77c03b9
4 changed files with 84 additions and 6 deletions
  1. 36 5
      config/index.ts
  2. 31 0
      main.js
  3. 13 0
      package-lock.json
  4. 4 1
      package.json

+ 36 - 5
config/index.ts

@@ -2,6 +2,9 @@ import { defineConfig, type UserConfigExport } from '@tarojs/cli';
 import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
 import devConfig from './dev';
 import prodConfig from './prod';
+import path from 'path';
+import TerserPlugin from 'terser-webpack-plugin';
+import webpack, { DefinePlugin } from 'webpack';
 
 // https://taro-docs.jd.com/docs/next/config#defineconfig-辅助函数
 export default defineConfig<'webpack5'>(async (merge) => {
@@ -47,15 +50,15 @@ export default defineConfig<'webpack5'>(async (merge) => {
       },
     },
     h5: {
-      publicPath: '/',
+      publicPath: './',
       staticDirectory: 'static',
       output: {
-        filename: 'js/[name].[hash:8].js',
+        filename: '[name].js',
         chunkFilename: 'js/[name].[chunkhash:8].js',
       },
       miniCssExtractPluginOption: {
         ignoreOrder: true,
-        filename: 'css/[name].[hash].css',
+        filename: 'css/[name].[fullhash].css',
         chunkFilename: 'css/[name].[chunkhash].css',
       },
       postcss: {
@@ -67,12 +70,22 @@ export default defineConfig<'webpack5'>(async (merge) => {
           enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
           config: {
             namingPattern: 'module', // 转换模式,取值为 global/module
-            generateScopedName: '[name]__[local]___[hash:base64:5]',
+            generateScopedName: '[name]__[local]___[fullhash:base64:5]',
           },
         },
       },
       webpackChain(chain) {
-        chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin);
+        chain.cache(false);
+        chain.output.path(path.resolve(__dirname, '../dist/h5'));
+        chain.optimization.minimize(false); // 彻底关闭压缩
+        // chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin);
+        chain.resolve
+          .plugin('tsconfig-paths')
+          .use(TsconfigPathsPlugin, [{
+            configFile: path.resolve(__dirname, '../tsconfig.json'), // 必须指向存在的 tsconfig
+          }]);
+        // 打开详细日志
+        chain.mode('production').stats('verbose');
         chain.merge({
           resolve: {
             fallback: {
@@ -83,7 +96,25 @@ export default defineConfig<'webpack5'>(async (merge) => {
             },
           },
         });
+        chain.optimization
+          .minimizer('terser')
+          .use(TerserPlugin, [{
+            parallel: false,           // 单线程,错误不会丢
+            terserOptions: {
+              compress: false,         // 先不压缩,只混淆
+              mangle: false,
+              format: {
+                comments: false,
+              },
+            },
+          } as any]);
+        chain.plugin('define-plugin')
+          .use(DefinePlugin as any, [{
+            'process.env.USE_MSW': JSON.stringify(process.env.USE_MSW || 'false'),
+            'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production'),
+          }]);
       },
+
     },
     rn: {
       appName: 'taroDemo',

+ 31 - 0
main.js

@@ -0,0 +1,31 @@
+const { app, BrowserWindow } = require('electron');
+const path = require('path');
+
+function createWindow() {
+  const win = new BrowserWindow({
+    width: 800,
+    height: 600,
+    webPreferences: {
+      nodeIntegration: true,
+      contextIsolation: false,
+    },
+  });
+
+  // 加载 Taro 编译后的 H5 页面
+  // win.loadFile('./dist/index.html');
+  win.loadFile(path.join(__dirname, './dist/h5/index.html'));
+}
+
+app.whenReady().then(createWindow);
+
+app.on('window-all-closed', () => {
+  if (process.platform !== 'darwin') {
+    app.quit();
+  }
+});
+
+app.on('activate', () => {
+  if (BrowserWindow.getAllWindows().length === 0) {
+    createWindow();
+  }
+});

+ 13 - 0
package-lock.json

@@ -52,6 +52,7 @@
         "@tarojs/webpack5-runner": "4.1.1",
         "@types/node": "^18",
         "@types/react": "^18.0.0",
+        "@types/webpack": "^5.28.5",
         "@types/webpack-env": "^1.13.6",
         "@typescript-eslint/eslint-plugin": "^8.33.1",
         "@typescript-eslint/parser": "^8.33.1",
@@ -73,6 +74,7 @@
         "stream-browserify": "^3.0.0",
         "stylelint": "^16.4.0",
         "tailwindcss": "^3.4.17",
+        "terser-webpack-plugin": "^5.3.14",
         "tsconfig-paths-webpack-plugin": "^4.1.0",
         "typescript": "^5.4.5",
         "typescript-eslint": "^8.33.1",
@@ -5600,6 +5602,17 @@
       "resolved": "https://registry.npmjs.org/@types/use-sync-external-store/-/use-sync-external-store-0.0.6.tgz",
       "integrity": "sha512-zFDAD+tlpf2r4asuHEj0XH6pY6i0g5NeAHPn+15wk3BV6JA69eERFXC1gyGThDkVa1zCyKr5jox1+2LbV/AMLg=="
     },
+    "node_modules/@types/webpack": {
+      "version": "5.28.5",
+      "resolved": "https://registry.npmjs.org/@types/webpack/-/webpack-5.28.5.tgz",
+      "integrity": "sha512-wR87cgvxj3p6D0Crt1r5avwqffqPXUkNlnQ1mjU93G7gCuFjufZR4I6j8cz5g1F1tTYpfOOFvly+cmIQwL9wvw==",
+      "dev": true,
+      "dependencies": {
+        "@types/node": "*",
+        "tapable": "^2.2.0",
+        "webpack": "^5"
+      }
+    },
     "node_modules/@types/webpack-env": {
       "version": "1.18.8",
       "resolved": "https://registry.npmjs.org/@types/webpack-env/-/webpack-env-1.18.8.tgz",

+ 4 - 1
package.json

@@ -3,6 +3,7 @@
   "version": "1.0.0",
   "private": true,
   "description": "医学成像系统",
+  "main": "main.js",
   "templateInfo": {
     "name": "default",
     "typescript": true,
@@ -33,7 +34,7 @@
     "dev:harmony-hybrid": "npm run build:harmony-hybrid -- --watch",
     "prepare": "husky",
     "start:electron": "electron .",
-    "build:electron": "electron-builder --config electron-builder.json  --linux"
+    "build:electron": "electron-builder --config electron-builder.json  --win"
   },
   "browserslist": [
     "defaults and fully supports es6-module",
@@ -85,6 +86,7 @@
     "@tarojs/webpack5-runner": "4.1.1",
     "@types/node": "^18",
     "@types/react": "^18.0.0",
+    "@types/webpack": "^5.28.5",
     "@types/webpack-env": "^1.13.6",
     "@typescript-eslint/eslint-plugin": "^8.33.1",
     "@typescript-eslint/parser": "^8.33.1",
@@ -106,6 +108,7 @@
     "stream-browserify": "^3.0.0",
     "stylelint": "^16.4.0",
     "tailwindcss": "^3.4.17",
+    "terser-webpack-plugin": "^5.3.14",
     "tsconfig-paths-webpack-plugin": "^4.1.0",
     "typescript": "^5.4.5",
     "typescript-eslint": "^8.33.1",