Browse Source

bug fixed

master
1549469775 4 years ago
parent
commit
a8166b1bdb
  1. 7
      a.md
  2. 89
      package-lock.json
  3. 46
      package.json
  4. BIN
      resource/electron/static/icon.png
  5. 5
      script/build.js
  6. 24
      script/build.ts
  7. 23
      script/run.js
  8. 4
      script/webpack/runMain.js
  9. 10
      script/webpack/webpack.main.config.js
  10. 19
      src/main/index.ts
  11. 0
      src/main/share/index.ts
  12. 1
      src/render/index.html
  13. 2
      src/render/main.tsx
  14. 3
      src/render/views/Home/index.tsx
  15. 2
      tsconfig.json
  16. 2
      types/global.d.ts
  17. 17
      vite.config.ts

7
a.md

@ -7,4 +7,9 @@ https://www.jianshu.com/p/4699b825d285
// ],
https://www.wyr.me/post/680
https://www.wyr.me/post/680
<!-- 切换 -->
https://github.com/webpack/webpack/issues/11767
https://vite-rollup-plugins.patak.dev/

89
package-lock.json

@ -1500,6 +1500,35 @@
}
}
},
"@rollup/plugin-replace": {
"version": "3.0.0",
"resolved": "https://registry.nlark.com/@rollup/plugin-replace/download/@rollup/plugin-replace-3.0.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40rollup%2Fplugin-replace%2Fdownload%2F%40rollup%2Fplugin-replace-3.0.0.tgz",
"integrity": "sha1-OkyWZdTnpM4sNgzwISMnhIkvP6w=",
"dev": true,
"requires": {
"@rollup/pluginutils": "^3.1.0",
"magic-string": "^0.25.7"
},
"dependencies": {
"@rollup/pluginutils": {
"version": "3.1.0",
"resolved": "https://registry.nlark.com/@rollup/pluginutils/download/@rollup/pluginutils-3.1.0.tgz?cache=0&sync_timestamp=1626393703548&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40rollup%2Fpluginutils%2Fdownload%2F%40rollup%2Fpluginutils-3.1.0.tgz",
"integrity": "sha1-cGtFJO5tyLEDs8mVUz5a1oDAK5s=",
"dev": true,
"requires": {
"@types/estree": "0.0.39",
"estree-walker": "^1.0.1",
"picomatch": "^2.2.2"
}
},
"estree-walker": {
"version": "1.0.1",
"resolved": "https://registry.npm.taobao.org/estree-walker/download/estree-walker-1.0.1.tgz?cache=0&sync_timestamp=1611956983677&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Festree-walker%2Fdownload%2Festree-walker-1.0.1.tgz",
"integrity": "sha1-MbxdYSyWtwQQa0d+bdXYqhOMtwA=",
"dev": true
}
}
},
"@rollup/plugin-typescript": {
"version": "8.2.3",
"resolved": "https://registry.nlark.com/@rollup/plugin-typescript/download/@rollup/plugin-typescript-8.2.3.tgz",
@ -1674,7 +1703,7 @@
},
"@types/node": {
"version": "15.14.3",
"resolved": "https://registry.nlark.com/@types/node/download/@types/node-15.14.3.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-15.14.3.tgz",
"resolved": "https://registry.nlark.com/@types/node/download/@types/node-15.14.3.tgz?cache=0&sync_timestamp=1627257813519&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-15.14.3.tgz",
"integrity": "sha1-MwdjuXPQrKy3/czHWU4fsoHtGxw=",
"dev": true
},
@ -4432,7 +4461,7 @@
},
"ejs": {
"version": "3.1.6",
"resolved": "https://registry.npm.taobao.org/ejs/download/ejs-3.1.6.tgz",
"resolved": "https://registry.nlark.com/ejs/download/ejs-3.1.6.tgz",
"integrity": "sha1-W/0KBol0O7UmizVQzO7rvBcCgio=",
"dev": true,
"requires": {
@ -6233,7 +6262,7 @@
},
"jake": {
"version": "10.8.2",
"resolved": "https://registry.npm.taobao.org/jake/download/jake-10.8.2.tgz?cache=0&sync_timestamp=1591684013969&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fjake%2Fdownload%2Fjake-10.8.2.tgz",
"resolved": "https://registry.npm.taobao.org/jake/download/jake-10.8.2.tgz",
"integrity": "sha1-68nehVgWCmbYLQ6txqLlj7xQCns=",
"dev": true,
"requires": {
@ -6286,7 +6315,7 @@
},
"escape-string-regexp": {
"version": "1.0.5",
"resolved": "https://registry.npm.taobao.org/escape-string-regexp/download/escape-string-regexp-1.0.5.tgz?cache=0&sync_timestamp=1618677243201&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fescape-string-regexp%2Fdownload%2Fescape-string-regexp-1.0.5.tgz",
"resolved": "https://registry.nlark.com/escape-string-regexp/download/escape-string-regexp-1.0.5.tgz",
"integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=",
"dev": true
},
@ -7031,7 +7060,7 @@
},
"object.assign": {
"version": "4.1.2",
"resolved": "https://registry.npm.taobao.org/object.assign/download/object.assign-4.1.2.tgz?cache=0&sync_timestamp=1604115158081&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fobject.assign%2Fdownload%2Fobject.assign-4.1.2.tgz",
"resolved": "https://registry.nlark.com/object.assign/download/object.assign-4.1.2.tgz",
"integrity": "sha1-DtVKNC7Os3s4/3brgxoOeIy2OUA=",
"dev": true,
"requires": {
@ -7139,6 +7168,15 @@
"integrity": "sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4=",
"dev": true
},
"p-limit": {
"version": "3.1.0",
"resolved": "https://registry.nlark.com/p-limit/download/p-limit-3.1.0.tgz",
"integrity": "sha1-4drMvnjQ0TiMoYxk/qOOPlfjcGs=",
"dev": true,
"requires": {
"yocto-queue": "^0.1.0"
}
},
"p-map": {
"version": "2.1.0",
"resolved": "https://registry.nlark.com/p-map/download/p-map-2.1.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fp-map%2Fdownload%2Fp-map-2.1.0.tgz",
@ -8971,19 +9009,10 @@
"dependencies": {
"commander": {
"version": "2.20.3",
"resolved": "https://registry.nlark.com/commander/download/commander-2.20.3.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fcommander%2Fdownload%2Fcommander-2.20.3.tgz",
"resolved": "https://registry.nlark.com/commander/download/commander-2.20.3.tgz?cache=0&sync_timestamp=1627358203890&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fcommander%2Fdownload%2Fcommander-2.20.3.tgz",
"integrity": "sha1-/UhehMA+tIgcIHIrpIA16FMa6zM=",
"dev": true
},
"p-limit": {
"version": "3.1.0",
"resolved": "https://registry.nlark.com/p-limit/download/p-limit-3.1.0.tgz",
"integrity": "sha1-4drMvnjQ0TiMoYxk/qOOPlfjcGs=",
"dev": true,
"requires": {
"yocto-queue": "^0.1.0"
}
},
"schema-utils": {
"version": "3.1.1",
"resolved": "https://registry.nlark.com/schema-utils/download/schema-utils-3.1.1.tgz?cache=0&sync_timestamp=1626694740261&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fschema-utils%2Fdownload%2Fschema-utils-3.1.1.tgz",
@ -8997,7 +9026,7 @@
},
"terser": {
"version": "5.7.1",
"resolved": "https://registry.nlark.com/terser/download/terser-5.7.1.tgz",
"resolved": "https://registry.nlark.com/terser/download/terser-5.7.1.tgz?cache=0&sync_timestamp=1624884102109&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fterser%2Fdownload%2Fterser-5.7.1.tgz",
"integrity": "sha1-LcemEAm2a7Y4MFyyqCR2OxFr94Q=",
"dev": true,
"requires": {
@ -9008,7 +9037,7 @@
"dependencies": {
"source-map": {
"version": "0.7.3",
"resolved": "https://registry.npm.taobao.org/source-map/download/source-map-0.7.3.tgz",
"resolved": "https://registry.nlark.com/source-map/download/source-map-0.7.3.tgz",
"integrity": "sha1-UwL4FpAxc1ImVECS5kmB91F1A4M=",
"dev": true
}
@ -9561,7 +9590,7 @@
},
"vite": {
"version": "2.4.3",
"resolved": "https://registry.nlark.com/vite/download/vite-2.4.3.tgz?cache=0&sync_timestamp=1626783291368&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fvite%2Fdownload%2Fvite-2.4.3.tgz",
"resolved": "https://registry.nlark.com/vite/download/vite-2.4.3.tgz",
"integrity": "sha1-/kqnjp3X02vLEuzL1SMTsmz633c=",
"dev": true,
"requires": {
@ -9657,9 +9686,9 @@
}
},
"webpack": {
"version": "5.46.0",
"resolved": "https://registry.nlark.com/webpack/download/webpack-5.46.0.tgz",
"integrity": "sha1-EF0g2W9521mzFrCuVDFvD2MDFLU=",
"version": "5.47.0",
"resolved": "https://registry.nlark.com/webpack/download/webpack-5.47.0.tgz?cache=0&sync_timestamp=1627396117693&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fwebpack%2Fdownload%2Fwebpack-5.47.0.tgz",
"integrity": "sha1-PBOGK117QoeSv+dsX2eg9Dumhfg=",
"dev": true,
"requires": {
"@types/eslint-scope": "^3.7.0",
@ -9684,7 +9713,7 @@
"tapable": "^2.1.1",
"terser-webpack-plugin": "^5.1.3",
"watchpack": "^2.2.0",
"webpack-sources": "^2.3.1"
"webpack-sources": "^3.0.1"
},
"dependencies": {
"@types/estree": {
@ -9705,14 +9734,10 @@
}
},
"webpack-sources": {
"version": "2.3.1",
"resolved": "https://registry.nlark.com/webpack-sources/download/webpack-sources-2.3.1.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fwebpack-sources%2Fdownload%2Fwebpack-sources-2.3.1.tgz",
"integrity": "sha1-Vw3grxY5Sf4nIjPCzv4bVvdFEf0=",
"dev": true,
"requires": {
"source-list-map": "^2.0.1",
"source-map": "^0.6.1"
}
"version": "3.0.1",
"resolved": "https://registry.nlark.com/webpack-sources/download/webpack-sources-3.0.1.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fwebpack-sources%2Fdownload%2Fwebpack-sources-3.0.1.tgz",
"integrity": "sha1-UYz6vb3jli91u+y6zRHYirMgUlI=",
"dev": true
}
}
},
@ -9739,7 +9764,7 @@
"dependencies": {
"commander": {
"version": "7.2.0",
"resolved": "https://registry.nlark.com/commander/download/commander-7.2.0.tgz?cache=0&sync_timestamp=1624609539421&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fcommander%2Fdownload%2Fcommander-7.2.0.tgz",
"resolved": "https://registry.nlark.com/commander/download/commander-7.2.0.tgz?cache=0&sync_timestamp=1627358203890&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fcommander%2Fdownload%2Fcommander-7.2.0.tgz",
"integrity": "sha1-o2y1fQtQHOEI5NIFWaFQo5HZerc=",
"dev": true
}
@ -10331,7 +10356,7 @@
},
"websocket-driver": {
"version": "0.7.4",
"resolved": "https://registry.npm.taobao.org/websocket-driver/download/websocket-driver-0.7.4.tgz",
"resolved": "https://registry.nlark.com/websocket-driver/download/websocket-driver-0.7.4.tgz",
"integrity": "sha1-ia1Slbv2S0gKvLox5JU6ynBvV2A=",
"dev": true,
"requires": {

46
package.json

@ -4,10 +4,12 @@
"description": "description",
"main": "dist/electron/entry.js",
"scripts": {
"dev": "node script/run",
"dev": "node script/run.js",
"build": "node script/run.js --build",
"build:in": "npm run build:vite && node script/build.js && npm run package",
"buildaa": "npm run build:vite && ts-node -r tsconfig-paths/register script/build --env=production && npm run package",
"webpack": "node script/webpack/runMain.js",
"deva": "ts-node -r tsconfig-paths/register script/dev-runner --env=development --watch",
"build": "ts-node -r tsconfig-paths/register script/build --env=production && npm run package",
"devaa": "npm run dev:all",
"dev:all": "concurrently -n=vue,ele -c=green,blue \"npm run dev:vue\" \"npm run dev:ele\"",
"dev:vue": "node script/before",
@ -16,7 +18,7 @@
"package": "electron-builder build --x64 --win",
"packageaaa": "electron-forge package",
"make": "electron-forge make",
"buildaa": "tsc && vite build",
"build:vite": "vite build",
"serve": "vite preview"
},
"keywords": [],
@ -31,37 +33,30 @@
"@rollup/plugin-commonjs": "^19.0.1",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"@rollup/plugin-replace": "^3.0.0",
"@rollup/plugin-typescript": "^8.2.3",
"@types/minimist": "^1.2.2",
"@types/node": "^15.14.3",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/react-router-dom": "^5.1.7",
"@vitejs/plugin-react-refresh": "^1.3.1",
"axios": "^0.21.1",
"babel-loader": "^8.2.2",
"babel-minify-webpack-plugin": "^0.3.1",
"cfonts": "^2.9.3",
"chalk": "^4.1.1",
"classnames": "^2.3.1",
"concurrently": "^6.2.0",
"cross-env": "^7.0.3",
"dotenv": "^10.0.0",
"electron": "^13.1.7",
"electron-builder": "^22.11.7",
"execa": "^5.1.1",
"node-loader": "^2.0.0",
"ts-loader": "^9.2.4",
"ts-node": "^10.1.0",
"tsconfig-paths": "^3.10.1",
"vite-plugin-html": "^2.0.7",
"vitejs-plugin-electron": "^0.1.3",
"webpack": "^5.46.0",
"webpack-cli": "^4.7.2",
"webpack-dev-server": "^3.11.2",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/react-router-dom": "^5.1.7",
"@vitejs/plugin-react-refresh": "^1.3.1",
"axios": "^0.21.1",
"classnames": "^2.3.1",
"electron-squirrel-startup": "^1.0.0",
"execa": "^5.1.1",
"i18next": "^20.3.4",
"i18next-browser-languagedetector": "^6.1.2",
"node-loader": "^2.0.0",
"react": "^17.0.0",
"react-dom": "^17.0.0",
"react-i18next": "^11.11.3",
@ -72,9 +67,17 @@
"redux-saga": "^1.1.3",
"sass": "^1.35.1",
"styled-jsx": "^3.4.4",
"ts-loader": "^9.2.4",
"ts-node": "^10.1.0",
"tsconfig-paths": "^3.10.1",
"typescript": "^4.3.2",
"vite": "^2.4.3",
"vite-plugin-html": "^2.0.7",
"vite-plugin-windicss": "^1.2.0",
"vitejs-plugin-electron": "^0.1.3",
"webpack": "^5.47.0",
"webpack-cli": "^4.7.2",
"webpack-dev-server": "^3.11.2",
"windicss": "^3.1.3"
},
"dependencies": {},
@ -109,10 +112,15 @@
}
]
},
"extraResources": {
"from": "src/preload",
"to": "src/preload"
},
"mac": {
"icon": "build/icons/icon.icns"
},
"win": {
"icon": "resource/electron/static/icon.png",
"target": [
{
"target": "nsis",

BIN
resource/electron/static/icon.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

5
script/build.js

@ -0,0 +1,5 @@
// @ts-nocheck
const {buildMain} = require("./webpack/runMain")
buildMain()

24
script/build.ts

@ -1,24 +0,0 @@
// @ts-nocheck
import { main } from "../package.json"
import { join } from "path"
import * as electron from "electron"
import * as chalk from "chalk"
import * as minimist from "minimist"
import { buildVite } from "./code/runVite"
import { buildElectron } from "./code/runElectron"
import { tscCheck } from "./code/runCommon"
const {buildMain} = require("./webpack/runMain")
import { exec, spawn, ChildProcess } from "child_process"
import { watch, rollup, OutputOptions } from "rollup"
import { waitOn } from "./utils"
import options from "./rollup.config"
;(async () => {
await tscCheck()
await buildVite()
await buildMain()
// await buildElectron()
})()

23
script/run.js

@ -1,8 +1,19 @@
const crossEnv = require("cross-env")
const minimist = require("minimist")
const {join} = require("path")
crossEnv([
"NO=123",
"PORT=3344",
"NODE_ENV=development",
"ts-node -r tsconfig-paths/register script/dev-runner --watch"
])
const argv = minimist(process.argv.slice(2))
if (argv.build) {
crossEnv([
"NODE_ENV=production",
"npm run build:in"
])
} else {
crossEnv([
`STATIC=${join(__dirname, '../resource/electron/static')}`,
"PORT=3344",
"NODE_ENV=development",
"ts-node -r tsconfig-paths/register script/dev-runner --watch"
])
}

4
script/webpack/runMain.js

@ -43,7 +43,6 @@ function buildMain () {
if (err) reject(err.stack || err)
else if (stats.hasErrors()) {
let err = ''
stats.toString({
chunks: false,
colors: true
@ -54,7 +53,8 @@ function buildMain () {
})
reject(err)
} else {
}
else {
resolve(stats.toString({
chunks: false,
colors: true

10
script/webpack/webpack.main.config.js

@ -17,7 +17,6 @@ let mainConfig = {
],
module: {
rules: [
// { test: /\.ts?$/, loader: "ts-loader", exclude: /node_modules/ },
{
test: /\.js$/,
use: ['babel-loader'],
@ -55,12 +54,11 @@ let mainConfig = {
target: 'electron-main'
}
/**
* Adjust mainConfig for production settings
*/
if (process.env.NODE_ENV === 'production') {
if (process.env.NODE_ENV !== 'production') {
mainConfig.plugins.push(
new MinifyPlugin()
new webpack.DefinePlugin({
'__static': `"${process.env.STATIC.replace(/\\/g, '\\\\')}"`
})
)
}

19
src/main/index.ts

@ -2,7 +2,7 @@
* electron
*/
// import "@src/common/patch"
import Shared from "../share"
import Shared from "./share"
const { join } = require("path")
const { app, BrowserWindow } = require("electron")
@ -12,33 +12,36 @@ Shared.data = {
miniWindow: null,
forceClose: false,
}
console.log(23422);
let isDev = process.env.NODE_ENV == "development" ? true : false
if (!isDev) {
// global.__static = require('path')
// .join(__dirname, '/resource')
// .replace(/\\/g, '\\\\')
// @ts-ignore
global.__static = require("path").join(__dirname, "/static").replace(/\\/g, "\\\\")
}
let win;
let win
function createWin() {
// 创建浏览器窗口dsaasdad
console.log(process.env.NODE_ENV);
console.log(__dirname);
console.log(join(__dirname, "../../src/preload/index.js"));
win = new BrowserWindow({
width: 1024,
height: 768,
icon: __static + "/icon.png",
webPreferences: {
// 下面两个必须这么用,看a.md的文档
nodeIntegration: true,
contextIsolation: false,
// preload: join(__dirname, "../../src/preload/index.js"),
preload: join(__dirname, "../../src/preload/index.js"),
},
})
const URL = app.isPackaged
? `file://${join(__dirname, "./index.html")}` // vite 构建后的静态文件地址
? `file://${__dirname}/index.html` // vite 构建后的静态文件地址
: `http://localhost:${process.env.PORT}/#/` // vite 启动的服务器地址
win?.loadURL(URL)

0
src/share/index.ts → src/main/share/index.ts

1
src/render/index.html

@ -11,6 +11,5 @@
<body>
<div id="root"></div>
<script type="module" src="/main.tsx"></script>
</body>
</html>

2
src/render/main.tsx

@ -9,7 +9,9 @@ import electron from "@/plugins/electron"
import Router from "./AppRouter";
// 静态资源地址变量请用"__static",在html中使用请用__static
console.log(electron);
console.log(__staticVar);
ReactDOM.render(

3
src/render/views/Home/index.tsx

@ -27,6 +27,9 @@ function Home(props: HomeProps) {
<NavLink to="/home"></NavLink> <br />
<NavLink to="/about"></NavLink><br />
<NavLink to="/login"></NavLink><br />
<div>
<img src='__static/icon.png' style={{width:"50px",height: "50px"}} alt=""/>
</div>
</div>
)
}

2
tsconfig.json

@ -25,7 +25,7 @@
"@root/*": ["./*"]
},
},
"include": ["src/render", "types"],
"include": ["src", "types"],
"exclude": ["node_modules"],
"ts-node": {
"compilerOptions": {

2
types/global.d.ts

@ -8,3 +8,5 @@ interface IAction extends IAny{
}
declare const __static: string;
declare const __staticVar: string;

17
vite.config.ts

@ -4,18 +4,28 @@ import reactRefresh from "@vitejs/plugin-react-refresh"
const { resolve, join } = require("path")
import electron from "vitejs-plugin-electron"
import { minifyHtml, injectHtml } from "vite-plugin-html"
import replace from '@rollup/plugin-replace';
console.log(process.env.NO)
let isDev = process.env.NODE_ENV === "development"
console.log('--->',process.env.STATIC);
let plugins = []
// plugins.push(electron())
let staticPath = isDev? '/static': 'static'
plugins.push(
replace({
preventAssignment: true,
"__static": staticPath,
"__staticVar": `"${staticPath}"`
// "__static": `file:///${process.env.STATIC.replace(/\\/g, '\\\\')}`
})
)
// https://vitejs.dev/config/
export default defineConfig({
root: resolve(__dirname, "src/render"),
base: "./",
publicDir: resolve(__dirname, "resource/electron"),
css: {
preprocessorOptions: {
scss: {
@ -50,8 +60,7 @@ export default defineConfig({
minifyHtml(),
injectHtml({
injectData: {
title: "vite-plugin-htmlaa-example",
// injectScript: '<script src="./inject.js"></script>',
title: "vite-plugin-htmlaa-example"
},
}),
],

Loading…
Cancel
Save