-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bc67e5
commit b3c6076
Showing
12 changed files
with
159 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,56 @@ | ||
/** | ||
* electron 打包 | ||
*/ | ||
import { join } from 'path' | ||
import { spawn, ChildProcess } from 'child_process' | ||
import { watch, rollup, OutputOptions } from 'rollup' | ||
import minimist from 'minimist' | ||
import chalk from 'chalk' | ||
import ora from 'ora' | ||
import electron from 'electron' | ||
import dotenv from 'dotenv' | ||
import { waitOn } from './utils' | ||
import options from './rollup.config' | ||
import { main } from '../package.json' | ||
import { join } from "path"; | ||
import { spawn, ChildProcess } from "child_process"; | ||
import { watch, rollup, OutputOptions } from "rollup"; | ||
import minimist from "minimist"; | ||
import chalk from "chalk"; | ||
import ora from "ora"; | ||
import electron from "electron"; | ||
import dotenv from "dotenv"; | ||
import { waitOn } from "./utils"; | ||
import options from "./rollup.config"; | ||
import { main } from "../package.json"; | ||
|
||
dotenv.config({ path: join(__dirname, '../.env') }) | ||
dotenv.config({ path: join(__dirname, "../.env") }); | ||
|
||
const argv = minimist(process.argv.slice(2)) | ||
const opts = options(argv.env) | ||
const TAG = '[build-main.ts]' | ||
const spinner = ora(`${TAG} Electron build...`) | ||
const argv = minimist(process.argv.slice(2)); | ||
const opts = options(argv.env); | ||
const TAG = "[build-main.ts]"; | ||
const spinner = ora(`${TAG} Electron build...`); | ||
|
||
if (argv.watch) { | ||
waitOn({ port: process.env.PORT as string }).then(msg => { | ||
const watcher = watch(opts) | ||
let child: ChildProcess | ||
watcher.on('change', filename => { | ||
const log = chalk.green(`change -- ${filename}`) | ||
console.log(TAG, log) | ||
}) | ||
watcher.on('event', ev => { | ||
if (ev.code === 'END') { | ||
if (child) child.kill() | ||
child = spawn(electron as any, [join(__dirname, `../${main}`), '--inspect'], { stdio: 'inherit' }) | ||
} else if (ev.code === 'ERROR') { | ||
console.log(ev.error) | ||
waitOn({ port: process.env.PORT as string }).then((msg) => { | ||
const watcher = watch(opts); | ||
let child: ChildProcess; | ||
watcher.on("change", (filename) => { | ||
const log = chalk.green(`change -- ${filename}`); | ||
console.log(TAG, log); | ||
}); | ||
watcher.on("event", (ev) => { | ||
if (ev.code === "END") { | ||
if (child) child.kill(); | ||
child = spawn( | ||
electron as any, | ||
[join(__dirname, `../${main}`), "--inspect"], | ||
{ stdio: "inherit" } | ||
); | ||
} else if (ev.code === "ERROR") { | ||
console.log(ev.error); | ||
} | ||
}) | ||
}) | ||
}); | ||
}); | ||
} else { | ||
spinner.start() | ||
spinner.start(); | ||
rollup(opts) | ||
.then(build => { | ||
spinner.stop() | ||
console.log(TAG, chalk.green('Electron build successed.')) | ||
build.write(opts.output as OutputOptions) | ||
}) | ||
.catch(error => { | ||
spinner.stop() | ||
console.log(`\n${TAG} ${chalk.red('构建报错')}\n`, error, '\n') | ||
.then((build) => { | ||
spinner.stop(); | ||
console.log(TAG, chalk.green("Electron build successed.")); | ||
build.write(opts.output as OutputOptions); | ||
}) | ||
.catch((error) => { | ||
spinner.stop(); | ||
console.log(`\n${TAG} ${chalk.red("构建报错")}\n`, error, "\n"); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
|
||
/** HelloWorld.vue?vue&type=style&index=0&scoped=true&lang.css -> HelloWorld.vue */ | ||
export function cleanUrl(url: string) { | ||
return url.replace(/(\?|#).*$/, '') | ||
return url.replace(/(\?|#).*$/, ""); | ||
} | ||
|
||
export const vue_js_ts_extensions = ['.vue', '.js', '.jsx', '.ts', '.tsx'] | ||
export const vue_js_ts_extensions = [".vue", ".js", ".jsx", ".ts", ".tsx"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,39 @@ | ||
import { join } from 'path' | ||
import { RollupOptions } from 'rollup' | ||
import nodeResolve from '@rollup/plugin-node-resolve' | ||
import commonjs from '@rollup/plugin-commonjs' | ||
import typescript from '@rollup/plugin-typescript' | ||
import alias from '@rollup/plugin-alias' | ||
import json from '@rollup/plugin-json' | ||
import { builtins } from './utils' | ||
import { join } from "path"; | ||
import { RollupOptions } from "rollup"; | ||
import nodeResolve from "@rollup/plugin-node-resolve"; | ||
import commonjs from "@rollup/plugin-commonjs"; | ||
import typescript from "@rollup/plugin-typescript"; | ||
import alias from "@rollup/plugin-alias"; | ||
import json from "@rollup/plugin-json"; | ||
import { builtins } from "./utils"; | ||
|
||
export default (env = 'production') => { | ||
export default (env = "production") => { | ||
const options: RollupOptions = { | ||
input: join(__dirname, '../src/main/index.ts'), | ||
input: join(__dirname, "../src/main/index.ts"), | ||
output: { | ||
file: join(__dirname, '../dist/main/index.js'), | ||
format: 'cjs', | ||
name: 'ElectronMainBundle', | ||
file: join(__dirname, "../dist/main/index.js"), | ||
format: "cjs", | ||
name: "ElectronMainBundle", | ||
sourcemap: true, | ||
}, | ||
plugins: [ | ||
nodeResolve(), | ||
commonjs(), | ||
json(), | ||
typescript({ | ||
module: 'ESNext', | ||
module: "ESNext", | ||
}), | ||
alias({ | ||
entries: [ | ||
{ find: '@', replacement: join(__dirname, '../src/render') }, | ||
{ find: '@main', replacement: join(__dirname, '../src/main') }, | ||
{ find: '@src', replacement: join(__dirname, '../src') }, | ||
{ find: '@root', replacement: join(__dirname, '..') }, | ||
] | ||
{ find: "@", replacement: join(__dirname, "../src/render") }, | ||
{ find: "@main", replacement: join(__dirname, "../src/main") }, | ||
{ find: "@src", replacement: join(__dirname, "../src") }, | ||
{ find: "@root", replacement: join(__dirname, "..") }, | ||
], | ||
}), | ||
], | ||
external: [ | ||
...builtins(), | ||
'electron', | ||
], | ||
} | ||
external: [...builtins(), "electron"], | ||
}; | ||
|
||
return options | ||
} | ||
return options; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,29 @@ | ||
import { builtinModules } from 'module' | ||
import { get } from 'http' | ||
import { green } from 'chalk' | ||
import { builtinModules } from "module"; | ||
import { get } from "http"; | ||
import { green } from "chalk"; | ||
|
||
/** 轮询监听 vite 启动 */ | ||
export function waitOn(arg0: { port: string | number; interval?: number; }) { | ||
return new Promise(resolve => { | ||
const { port, interval = 149 } = arg0 | ||
const url = `http://localhost:${port}` | ||
let counter = 0 | ||
export function waitOn(arg0: { port: string | number; interval?: number }) { | ||
return new Promise((resolve) => { | ||
const { port, interval = 149 } = arg0; | ||
const url = `http://localhost:${port}`; | ||
let counter = 0; | ||
const timer: NodeJS.Timer = setInterval(() => { | ||
get(url, res => { | ||
clearInterval(timer) | ||
console.log('[waitOn]', green(`"${url}" are already responsive.`), `(${res.statusCode}: ${res.statusMessage})`) | ||
resolve(res.statusCode) | ||
}).on('error', err => { | ||
console.log('[waitOn]', `counter: ${counter++}`) | ||
}) | ||
}, interval) | ||
}) | ||
get(url, (res) => { | ||
clearInterval(timer); | ||
console.log( | ||
"[waitOn]", | ||
green(`"${url}" are already responsive.`), | ||
`(${res.statusCode}: ${res.statusMessage})` | ||
); | ||
resolve(res.statusCode); | ||
}).on("error", (err) => { | ||
console.log("[waitOn]", `counter: ${counter++}`); | ||
}); | ||
}, interval); | ||
}); | ||
} | ||
|
||
/** node.js builtins module */ | ||
export const builtins = () => builtinModules.filter(x => !/^_|^(internal|v8|node-inspect)\/|\//.test(x)) | ||
export const builtins = () => | ||
builtinModules.filter((x) => !/^_|^(internal|v8|node-inspect)\/|\//.test(x)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.