♻️ Merge 'main.js' & 'start.mjs' scripts

This commit is contained in:
KaKi87 2022-08-07 18:14:10 +02:00
parent 88e4994342
commit 864bfa2a2c
5 changed files with 82 additions and 81 deletions

View File

@ -30,7 +30,6 @@ process.env['PARCEL_WORKER_BACKEND'] = 'process';
'.gitignore',
'build.mjs',
'config.example.js',
'start.mjs',
'yarn.lock',
...fs
.readFileSync('./.gitignore', 'utf8')

63
main.js
View File

@ -1,63 +0,0 @@
const
path = require('path'),
{
app,
BrowserWindow,
nativeTheme
} = require('electron'),
{
default: installExtension,
VUEJS3_DEVTOOLS
} = app.isPackaged ? {} : require('electron-devtools-installer'),
{
isSpellcheckEnabled,
isTitleBarEnabled,
isMenuBarEnabled,
isCorsEnabled,
devServerPort
} = require('./config.js');
(async () => {
nativeTheme.themeSource = 'dark';
await app.whenReady();
if(!app.isPackaged)
await installExtension(VUEJS3_DEVTOOLS);
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, './src-electron/preload.js'),
spellcheck: isSpellcheckEnabled
},
icon: path.join(__dirname, './src-electron/assets/icon.png'),
frame: isTitleBarEnabled,
show: false
});
mainWindow.setMenuBarVisibility(isMenuBarEnabled);
if(!isCorsEnabled){
const corsHeaders = [
'access-control-allow-origin',
'access-control-allow-methods',
'access-control-allow-headers'
];
mainWindow.webContents.session.webRequest.onHeadersReceived((details, callback) => callback({
responseHeaders: {
...Object.fromEntries(
Object
.entries(details.responseHeaders)
.filter(([header]) => !corsHeaders.includes(header.toLowerCase()))
),
...Object.fromEntries(corsHeaders.map(header => [header, ['*']]))
}
}));
}
mainWindow.webContents.openDevTools({
mode: 'detach',
activate: !app.isPackaged
});
if(app.isPackaged){
mainWindow.webContents.closeDevTools();
await mainWindow.loadFile('./dist/index.html');
}
else
await mainWindow.loadURL(`http://localhost:${devServerPort}/`);
mainWindow.show();
})();

View File

@ -1,7 +1,7 @@
{
"name": "template-electron17-vue3-parcel2",
"version": "0.1.0",
"main": "main.js",
"main": "start.js",
"repository": "ssh://kaki@git.kaki87.net:3021/KaKi87/template-electron17-vue3-parcel2.git",
"author": "KaKi87 <KaKi87@pm.me>",
"license": "MIT",
@ -16,7 +16,7 @@
"vue": "^3.2.30"
},
"scripts": {
"start": "node ./start.mjs",
"start": "node ./start.js",
"build": "node ./build.mjs"
},
"dependencies": {

80
start.js Normal file
View File

@ -0,0 +1,80 @@
const
electron = require('electron'),
{
isSpellcheckEnabled,
isTitleBarEnabled,
isMenuBarEnabled,
isCorsEnabled,
devServerPort
} = require('./config.js');
(async () => {
if(typeof electron === 'string'){
const
{ spawn } = require('node:child_process'),
{ Parcel } = require('@parcel/core');
await new Parcel({
entries: 'src-vue/index.html',
defaultConfig: '@parcel/config-default',
env: { NODE_ENV: 'development' },
serveOptions: { port: devServerPort },
hmrOptions: { port: devServerPort }
}).watch();
const child = spawn(
electron,
['start.js'],
{ stdio: 'inherit' }
);
child.on('close', () => process.exit());
}
else {
const
path = require('node:path'),
{
default: installExtension,
VUEJS3_DEVTOOLS
} = electron.app.isPackaged ? {} : require('electron-devtools-installer');
electron.nativeTheme.themeSource = 'dark';
await electron.app.whenReady();
if(!electron.app.isPackaged)
await installExtension(VUEJS3_DEVTOOLS);
const mainWindow = new electron.BrowserWindow({
webPreferences: {
preload: path.join(__dirname, './src-electron/preload.js'),
spellcheck: isSpellcheckEnabled
},
icon: path.join(__dirname, './src-electron/assets/icon.png'),
frame: isTitleBarEnabled,
show: false
});
mainWindow.setMenuBarVisibility(isMenuBarEnabled);
if(!isCorsEnabled){
const corsHeaders = [
'access-control-allow-origin',
'access-control-allow-methods',
'access-control-allow-headers'
];
mainWindow.webContents.session.webRequest.onHeadersReceived((details, callback) => callback({
responseHeaders: {
...Object.fromEntries(
Object
.entries(details.responseHeaders)
.filter(([header]) => !corsHeaders.includes(header.toLowerCase()))
),
...Object.fromEntries(corsHeaders.map(header => [header, ['*']]))
}
}));
}
mainWindow.webContents.openDevTools({
mode: 'detach',
activate: !electron.app.isPackaged
});
if(electron.app.isPackaged){
mainWindow.webContents.closeDevTools();
await mainWindow.loadFile('./dist/index.html');
}
else
await mainWindow.loadURL(`http://localhost:${devServerPort}/`);
mainWindow.show();
}
})().catch(console.error);

View File

@ -1,15 +0,0 @@
import { spawn } from 'child_process';
import { Parcel } from '@parcel/core';
import electron from 'electron';
import config from './config.js';
(async () => {
await new Parcel({
entries: 'src-vue/index.html',
defaultConfig: '@parcel/config-default',
env: { NODE_ENV: 'development' },
serveOptions: { port: config.devServerPort },
hmrOptions: { port: config.devServerPort }
}).watch();
const child = spawn(electron, ['main.js'], { stdio: 'inherit' });
child.on('close', () => process.exit());
})();