瀏覽代碼

Feature/webpack (#62)

* feat(webpack) chain with safe tap & load tauri config

* feat(config) add automaticStart option

* mergefix

* fix2
nothingismagick 5 年之前
父節點
當前提交
dc06913def
共有 6 個文件被更改,包括 50 次插入12 次删除
  1. 2 1
      mode/bin/tauri-build.js
  2. 5 0
      mode/helpers/tauri-config.js
  3. 31 7
      mode/webpack/index.js
  4. 2 1
      package.json
  5. 5 3
      templates/conf/tauri.conf.js
  6. 5 0
      yarn.lock

+ 2 - 1
mode/bin/tauri-build.js

@@ -26,7 +26,8 @@ const Runner = require('../runner')
 const tauri = new Runner({ modeDir: tauriDir })
 const tauriConfig = require('../helpers/tauri-config')({
   ctx: {
-    debug: argv.debug
+    debug: argv.debug,
+    prod: true
   }
 })
 

+ 5 - 0
mode/helpers/tauri-config.js

@@ -23,6 +23,11 @@ module.exports = cfg => {
       },
       security: {
         csp: 'default-src data: filesystem: ws: http: https: \'unsafe-eval\' \'unsafe-inline\''
+      },
+      automaticStart: {
+        active: false,
+        devArgs: [],
+        buildArgs: []
       }
     }
   }, tauriConf, cfg)

+ 31 - 7
mode/webpack/index.js

@@ -1,6 +1,21 @@
 const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin')
+const tauriConfig = require('../helpers/tauri-config')
+const WebpackShellPlugin = require('webpack-shell-plugin')
 
-module.exports.chain = function (chain, cfg) {
+const safeTap = (options, cb) => {
+  if (options !== undefined) {
+    cb()
+  }
+  return options
+}
+
+module.exports.chain = function (chain) {
+  const cfg = tauriConfig({
+    ctx: {
+      debug: process.env.NODE_ENV !== 'production',
+      prod: process.env.NODE_ENV === 'production'
+    }
+  })
   if (!cfg.tauri.embeddedServer.active) {
     chain.optimization.splitChunks({
       chunks: 'all',
@@ -41,14 +56,13 @@ module.exports.chain = function (chain, cfg) {
 
       chain.module.rule('babel')
         .use('babel-loader')
-        .tap(options => {
+        .tap(options => safeTap(() => {
           options.plugins.push([
             'system-import-transformer', { // needs constant attention
               modules: 'common'
             }
           ])
-          return options
-        })
+        }))
     }
 
     const modules = {
@@ -59,10 +73,9 @@ module.exports.chain = function (chain, cfg) {
     for (const module in modules) {
       chain.module.rule(module)
         .use(modules[module])
-        .tap(options => {
+        .tap(options => safeTap(options, () => {
           options.limit = undefined
-          return options
-        })
+        }))
     }
   }
 
@@ -70,4 +83,15 @@ module.exports.chain = function (chain, cfg) {
     chain.plugin('html-webpack-inline-source')
       .use(HtmlWebpackInlineSourcePlugin)
   }
+
+  if (cfg.tauri.automaticStart.active) {
+    chain.plugin('webpack-shell-plugin')
+      .use(WebpackShellPlugin, [{
+        onBuildEnd: [
+          cfg.ctx.prod
+            ? `tauri build${cfg.tauri.automaticStart.buildArgs.join(' ')}`
+            : `tauri dev${cfg.tauri.automaticStart.devArgs.join(' ')}`
+        ]
+      }])
+  }
 }

+ 2 - 1
package.json

@@ -66,7 +66,8 @@
     "jest": "24.9.0",
     "jest-mock-process": "^1.2.0",
     "lint-staged": "^9.4.2",
-    "promise": "^8.0.3"
+    "promise": "^8.0.3",
+    "webpack-shell-plugin": "^0.5.0"
   },
   "husky": {
     "hooks": {

+ 5 - 3
templates/conf/tauri.conf.js

@@ -1,5 +1,6 @@
-const path = require('path')
-const distDir = path.resolve(__dirname, './dist')
+const
+  path = require('path');
+  const distDir = path.resolve(__dirname, './dist')
 
 module.exports = function () {
   return {
@@ -24,6 +25,7 @@ module.exports = function () {
       security: {
         csp: 'default-src data: filesystem: ws: http: https: \'unsafe-eval\' \'unsafe-inline\''
       }
-    }
+    },
+    edge: true
   }
 }

+ 5 - 0
yarn.lock

@@ -6307,6 +6307,11 @@ webpack-merge@^4.2.1:
   dependencies:
     lodash "^4.17.5"
 
+webpack-shell-plugin@^0.5.0:
+  version "0.5.0"
+  resolved "https://registry.yarnpkg.com/webpack-shell-plugin/-/webpack-shell-plugin-0.5.0.tgz#29b8a1d80ddeae0ddb10e729667f728653c2c742"
+  integrity sha1-Kbih2A3erg3bEOcpZn9yhlPCx0I=
+
 whatwg-encoding@^1.0.1, whatwg-encoding@^1.0.3:
   version "1.0.5"
   resolved "https://registry.npmjs.org/whatwg-encoding/-/whatwg-encoding-1.0.5.tgz#5abacf777c32166a51d085d6b4f3e7d27113ddb0"