Skip to content

Commit 3c7e7f6

Browse files
committed
update: 完善开发模式
1 parent a597af4 commit 3c7e7f6

File tree

9 files changed

+103
-26
lines changed

9 files changed

+103
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ dist
3737
src-tauri/.cargo/config
3838
src-tauri/.cargo/
3939
.next
40+
src-tauri/.pake/

bin/helpers/merge.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { npmDirectory } from '@/utils/dir';
55
import combineFiles from '@/utils/combine';
66
import logger from '@/options/logger';
77
import { PakeAppOptions, PlatformMap } from '@/types';
8+
import { tauriConfigDirectory } from '../utils/dir';
89

910
export async function mergeConfig(url: string, options: PakeAppOptions, tauriConf: any) {
1011
const {
@@ -200,20 +201,23 @@ export async function mergeConfig(url: string, options: PakeAppOptions, tauriCon
200201

201202
// Save config file.
202203
const platformConfigPaths: PlatformMap = {
203-
win32: 'src-tauri/tauri.windows.conf.json',
204-
darwin: 'src-tauri/tauri.macos.conf.json',
205-
linux: 'src-tauri/tauri.linux.conf.json',
204+
win32: 'tauri.windows.conf.json',
205+
darwin: 'tauri.macos.conf.json',
206+
linux: 'tauri.linux.conf.json',
206207
};
207-
const configPath = path.join(npmDirectory, platformConfigPaths[platform]);
208+
const configPath = path.join(tauriConfigDirectory, platformConfigPaths[platform]);
208209

209210
const bundleConf = { tauri: { bundle: tauriConf.tauri.bundle } };
210-
await fsExtra.writeJson(configPath, bundleConf, { spaces: 4 });
211-
const pakeConfigPath = path.join(npmDirectory, 'src-tauri/pake.json');
212-
await fsExtra.writeJson(pakeConfigPath, tauriConf.pake, { spaces: 4 });
211+
await fsExtra.outputJSON(configPath, bundleConf, { spaces: 4 });
212+
const pakeConfigPath = path.join(tauriConfigDirectory, 'pake.json');
213+
await fsExtra.outputJSON(pakeConfigPath, tauriConf.pake, { spaces: 4 });
213214

214215
let tauriConf2 = JSON.parse(JSON.stringify(tauriConf));
215216
delete tauriConf2.pake;
216217
delete tauriConf2.tauri.bundle;
217-
const configJsonPath = path.join(npmDirectory, 'src-tauri/tauri.conf.json');
218-
await fsExtra.writeJson(configJsonPath, tauriConf2, { spaces: 4 });
218+
if (process.env.NODE_ENV === 'development') {
219+
tauriConf2.tauri.bundle = bundleConf.tauri.bundle;
220+
}
221+
const configJsonPath = path.join(tauriConfigDirectory, 'tauri.conf.json');
222+
await fsExtra.outputJSON(configJsonPath, tauriConf2, { spaces: 4 });
219223
}

bin/utils/dir.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ import { fileURLToPath } from 'url';
55
const currentModulePath = fileURLToPath(import.meta.url);
66

77
// Resolve the parent directory of the current module
8-
export const npmDirectory = process.env.DEV === 'true' ? process.cwd() : path.join(path.dirname(currentModulePath), '..');
8+
export const npmDirectory = path.join(path.dirname(currentModulePath), '..');
9+
10+
export const tauriConfigDirectory = process.env.NODE_ENV === 'development' ? path.join(npmDirectory, 'src-tauri', '.pake') : path.join(npmDirectory, 'src-tauri');

dist/cli.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var scripts = {
6060
analyze: "cd src-tauri && cargo bloat --release --crates",
6161
tauri: "tauri",
6262
cli: "rollup -c rollup.config.js --watch",
63-
"cli:dev": "cross-env DEV=true tsx watch ./bin/dev.ts & npm run dev",
63+
"cli:dev": "cross-env NODE_ENV=development rollup -c rollup.config.js -w",
6464
"cli:build": "cross-env NODE_ENV=production rollup -c rollup.config.js",
6565
prepublishOnly: "npm run cli:build"
6666
};
@@ -88,6 +88,7 @@ var devDependencies = {
8888
"@rollup/plugin-alias": "^4.0.2",
8989
"@rollup/plugin-commonjs": "^23.0.2",
9090
"@rollup/plugin-json": "^5.0.2",
91+
"@rollup/plugin-replace": "^5.0.2",
9192
"@rollup/plugin-terser": "^0.1.0",
9293
"@types/fs-extra": "^9.0.13",
9394
"@types/is-url": "^1.2.30",
@@ -362,7 +363,8 @@ const IS_LINUX = platform$1 === 'linux';
362363
// Convert the current module URL to a file path
363364
const currentModulePath = fileURLToPath(import.meta.url);
364365
// Resolve the parent directory of the current module
365-
const npmDirectory = process.env.DEV === 'true' ? process.cwd() : path.join(path.dirname(currentModulePath), '..');
366+
const npmDirectory = path.join(path.dirname(currentModulePath), '..');
367+
const tauriConfigDirectory = path.join(npmDirectory, 'src-tauri');
366368

367369
function shellExec(command) {
368370
return new Promise((resolve, reject) => {
@@ -639,20 +641,20 @@ async function mergeConfig(url, options, tauriConf) {
639641
}
640642
// Save config file.
641643
const platformConfigPaths = {
642-
win32: 'src-tauri/tauri.windows.conf.json',
643-
darwin: 'src-tauri/tauri.macos.conf.json',
644-
linux: 'src-tauri/tauri.linux.conf.json',
644+
win32: 'tauri.windows.conf.json',
645+
darwin: 'tauri.macos.conf.json',
646+
linux: 'tauri.linux.conf.json',
645647
};
646-
const configPath = path.join(npmDirectory, platformConfigPaths[platform]);
648+
const configPath = path.join(tauriConfigDirectory, platformConfigPaths[platform]);
647649
const bundleConf = { tauri: { bundle: tauriConf.tauri.bundle } };
648-
await fsExtra.writeJson(configPath, bundleConf, { spaces: 4 });
649-
const pakeConfigPath = path.join(npmDirectory, 'src-tauri/pake.json');
650-
await fsExtra.writeJson(pakeConfigPath, tauriConf.pake, { spaces: 4 });
650+
await fsExtra.outputJSON(configPath, bundleConf, { spaces: 4 });
651+
const pakeConfigPath = path.join(tauriConfigDirectory, 'pake.json');
652+
await fsExtra.outputJSON(pakeConfigPath, tauriConf.pake, { spaces: 4 });
651653
let tauriConf2 = JSON.parse(JSON.stringify(tauriConf));
652654
delete tauriConf2.pake;
653655
delete tauriConf2.tauri.bundle;
654-
const configJsonPath = path.join(npmDirectory, 'src-tauri/tauri.conf.json');
655-
await fsExtra.writeJson(configJsonPath, tauriConf2, { spaces: 4 });
656+
const configJsonPath = path.join(tauriConfigDirectory, 'tauri.conf.json');
657+
await fsExtra.outputJSON(configJsonPath, tauriConf2, { spaces: 4 });
656658
}
657659

658660
class BaseBuilder {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"analyze": "cd src-tauri && cargo bloat --release --crates",
4141
"tauri": "tauri",
4242
"cli": "rollup -c rollup.config.js --watch",
43-
"cli:dev": "cross-env DEV=true tsx watch ./bin/dev.ts & npm run dev",
43+
"cli:dev": "cross-env NODE_ENV=development rollup -c rollup.config.js -w",
4444
"cli:build": "cross-env NODE_ENV=production rollup -c rollup.config.js",
4545
"prepublishOnly": "npm run cli:build"
4646
},
@@ -68,6 +68,7 @@
6868
"@rollup/plugin-alias": "^4.0.2",
6969
"@rollup/plugin-commonjs": "^23.0.2",
7070
"@rollup/plugin-json": "^5.0.2",
71+
"@rollup/plugin-replace": "^5.0.2",
7172
"@rollup/plugin-terser": "^0.1.0",
7273
"@types/fs-extra": "^9.0.13",
7374
"@types/is-url": "^1.2.30",

plugins/pakeCliDevPlugin.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import chalk from 'chalk';
2+
import {spawn, exec} from 'child_process';
3+
4+
5+
// just run in development mode
6+
export default function pakeCliDevPlugin() {
7+
return {
8+
name: 'pake-cli-dev-plugin',
9+
buildEnd() {
10+
11+
const command = 'node';
12+
const cliCmdArgs = ['./dist/dev.js'];
13+
const cliChildProcess = spawn(command, cliCmdArgs);
14+
15+
cliChildProcess.stdout.on('data', (data) => {
16+
console.log(chalk.green(data.toString()));
17+
});
18+
19+
cliChildProcess.stderr.on('data', (data) => {
20+
console.error(chalk.yellow(data.toString()));
21+
});
22+
23+
cliChildProcess.on('close', async (code) => {
24+
console.log(chalk.yellow(`cli running end with code: ${code}`));
25+
cliChildProcess.kill();
26+
const dev = await exec('npm run tauri dev -- --config ./src-tauri/.pake/tauri.conf.json --features cli-build');
27+
28+
dev.stdout.on('data', (data) => {
29+
console.error(chalk.green(data.toString()));
30+
});
31+
32+
dev.stderr.on('data', (data) => {
33+
console.error(chalk.yellow(data.toString()));
34+
});
35+
36+
dev.on('close', () => {
37+
dev.kill();
38+
console.log(chalk.green('rebuild start'));
39+
});
40+
});
41+
}
42+
}
43+
}

rollup.config.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,23 @@ import typescript from 'rollup-plugin-typescript2';
44
import alias from '@rollup/plugin-alias';
55
import commonjs from '@rollup/plugin-commonjs';
66
import json from '@rollup/plugin-json';
7+
import replace from '@rollup/plugin-replace';
8+
9+
import pakeCliDevPlugin from './plugins/pakeCliDevPlugin.js';
10+
11+
const isProduction = process.env.NODE_ENV === 'production';
12+
const devPlugins = !isProduction ? [pakeCliDevPlugin()] : [];
713

814
export default {
9-
input: 'bin/cli.ts',
15+
input: isProduction ? 'bin/cli.ts' : 'bin/dev.ts',
1016
output: {
11-
file: 'dist/cli.js',
12-
format: 'es'
17+
file: isProduction ? 'dist/cli.js' : 'dist/dev.js',
18+
format: 'es',
19+
sourcemap: !isProduction,
20+
},
21+
watch: {
22+
include: 'bin/**',
23+
exclude: 'node_modules/**',
1324
},
1425
plugins: [
1526
json(),
@@ -18,8 +29,12 @@ export default {
1829
clean: true, // 清理缓存
1930
}),
2031
commonjs(),
32+
replace({
33+
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
34+
}),
2135
alias({
2236
entries: [{ find: '@', replacement: path.join(appRootPath.path, 'bin') }],
2337
}),
38+
...devPlugins,
2439
],
2540
};

src-tauri/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-works
2525
cargo-bloat = "0.11.1"
2626

2727
[features]
28+
# this feature is used for development builds from development cli
29+
cli-build = []
2830
# by default Tauri runs in production mode
2931
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL
3032
default = ["custom-protocol"]

src-tauri/src/util.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ use std::path::PathBuf;
44
use tauri::{api, Config, Window};
55

66
pub fn get_pake_config() -> (PakeConfig, Config) {
7+
#[cfg(feature = "cli-build")]
8+
let pake_config: PakeConfig =
9+
serde_json::from_str(include_str!("../.pake/pake.json")).expect("Failed to parse pake config");
10+
#[cfg(not(feature = "cli-build"))]
711
let pake_config: PakeConfig =
812
serde_json::from_str(include_str!("../pake.json")).expect("Failed to parse pake config");
9-
13+
#[cfg(feature = "cli-build")]
14+
let tauri_config: Config = serde_json::from_str(include_str!("../.pake/tauri.conf.json"))
15+
.expect("Failed to parse tauri config");
16+
#[cfg(not(feature = "cli-build"))]
1017
let tauri_config: Config = serde_json::from_str(include_str!("../tauri.conf.json"))
1118
.expect("Failed to parse tauri config");
1219

0 commit comments

Comments
 (0)