refactor: 调整注入能力适配新 cli

This commit is contained in:
jeasonnow
2023-06-25 10:24:57 +08:00
parent cc2c658bfb
commit 2af4c6c5a3
2 changed files with 18 additions and 1 deletions

View File

@@ -1,17 +0,0 @@
import path from 'path';
import fs from 'fs';
import { npmDirectory } from '@/utils/dir.js';
export default async function combineFiles(files: string[]) {
const output = path.join(npmDirectory, `src-tauri/src/inject/_INJECT_.js`);
const contents = files.map(file => {
const fileContent = fs.readFileSync(file);
if (file.endsWith('.css')) {
return "window.addEventListener('DOMContentLoaded', (_event) => { const css = `" + fileContent + "`; const style = document.createElement('style'); style.innerHTML = css; document.head.appendChild(style); });";
}
return fileContent;
});
fs.writeFileSync(output, contents.join('\n'));
return files;
}

19
bin/helpers/merge.ts vendored
View File

@@ -2,6 +2,7 @@ import path from 'path';
import fsExtra from 'fs-extra';
import { npmDirectory } from '@/utils/dir';
import combineFiles from '@/utils/combine';
import logger from '@/options/logger';
import { PakeAppOptions, PlatformMap } from '@/types';
@@ -19,6 +20,7 @@ export async function mergeConfig(url: string, options: PakeAppOptions, tauriCon
identifier,
name,
resizable = true,
inject,
} = options;
const { platform } = process;
@@ -68,7 +70,11 @@ export async function mergeConfig(url: string, options: PakeAppOptions, tauriCon
} else {
tauriConf.pake.windows[0].url_type = 'web';
// Set the secure domain for calling window.__TAURI__ to the application domain that has been set.
tauriConf.tauri.security.dangerousRemoteDomainIpcAccess[0].domain = new URL(url).hostname;
tauriConf.tauri.security.dangerousRemoteDomainIpcAccess = [{
domain: new URL(url).hostname,
windows: ["pake"],
enableTauriAPI: true,
}];
}
const platformMap: PlatformMap = {
@@ -172,6 +178,17 @@ export async function mergeConfig(url: string, options: PakeAppOptions, tauriCon
tauriConf.tauri.systemTray.iconPath = trayIconPath;
// inject js or css files
if (inject?.length > 0) {
if (!inject.every(item => item.endsWith('.css') || item.endsWith('.js'))) {
logger.error("The injected file must be in either CSS or JS format.");
return;
}
const files = inject.map(relativePath => path.join(process.cwd(), relativePath));
tauriConf.pake.inject = files;
combineFiles(files);
}
// Save config file.
const platformConfigPaths: PlatformMap = {
win32: 'src-tauri/tauri.windows.conf.json',