您能否启动一个子进程并通过npx 运行灯塔?
允许npx解析依赖空间而不是依赖os
const {app, BrowserWindow} = require('electron')
const childproc = require('child_process')
const path = require('path')
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
mainWindow.webContents.openDevTools()
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
const child = childproc.spawn(
'npx',
[
'lighthouse',
`${website}`,
`--quiet`,
`--chrome-flags=--headless`,
`--output-path=${outputPath}`,
`--output`,
`html`,
`--emulated-form-factor=${strategy}`,
`--only-categories=${options}`
],
)
child.on('exit', function (code, signal) {
console.log(
'child process exited with ' + `code ${code} and signal ${signal}`
)
})
child.on('error', (err) => {
console.log(err)
})
child.stdout.on('data', (data) => {
console.log(data.toString())
})
child.stderr.on('data', (data) => {
console.error(`child stderr:\n${data}`)
})
不太确定您的整个设置,但使用启动器构建了此示例并在本地运行灯塔...
$ npm start
> electron-quick-start@1.0.0 start
> electron .
child stderr:
Please provide a url
child stderr:
Specify --help for available options
child process exited with code 1 and signal null