generated from cawa-93/vite-electron-builder
-
-
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
5fd07e8
commit 3ddc467
Showing
4 changed files
with
178 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/node | ||
const {build} = require('vite'); | ||
const {dirname} = require('path'); | ||
|
||
/** @type 'production' | 'development' | 'test' */ | ||
const mode = (process.env.MODE = process.env.MODE || 'production'); | ||
|
||
const packagesConfigs = [ | ||
'packages/main/vite.config.js', | ||
'packages/preload/vite.config.js', | ||
'packages/renderer/vite.config.js', | ||
]; | ||
|
||
/** | ||
* Run `vite build` for config file | ||
*/ | ||
const buildByConfig = configFile => build({configFile, mode}); | ||
(async () => { | ||
try { | ||
const totalTimeLabel = 'Total bundling time'; | ||
console.time(totalTimeLabel); | ||
|
||
for (const packageConfigPath of packagesConfigs) { | ||
const consoleGroupName = `${dirname(packageConfigPath)}/`; | ||
console.group(consoleGroupName); | ||
|
||
const timeLabel = 'Bundling time'; | ||
console.time(timeLabel); | ||
|
||
await buildByConfig(packageConfigPath); | ||
|
||
console.timeEnd(timeLabel); | ||
console.groupEnd(); | ||
console.log('\n'); // Just for pretty print | ||
} | ||
console.timeEnd(totalTimeLabel); | ||
} catch (e) { | ||
console.error(e); | ||
process.exit(1); | ||
} | ||
})(); |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env node | ||
|
||
const {resolveConfig} = require('vite'); | ||
const {writeFileSync, mkdirSync, existsSync} = require('fs'); | ||
const {resolve, dirname} = require('path'); | ||
|
||
const MODES = ['production', 'development', 'test']; | ||
|
||
const typeDefinitionFile = resolve(process.cwd(), './types/env.d.ts'); | ||
|
||
/** | ||
* | ||
* @return {string} | ||
*/ | ||
function getBaseInterface() { | ||
return 'interface IBaseEnv {[key: string]: string}'; | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} mode | ||
* @return {Promise<{name: string, declaration: string}>} | ||
*/ | ||
async function getInterfaceByMode(mode) { | ||
const interfaceName = `${mode}Env`; | ||
const {env: envForMode} = await resolveConfig({mode}, 'build'); | ||
return { | ||
name: interfaceName, | ||
declaration: `interface ${interfaceName} extends IBaseEnv ${JSON.stringify(envForMode)}`, | ||
}; | ||
} | ||
|
||
/** | ||
* @param {string[]} modes | ||
* @param {string} filePath | ||
*/ | ||
async function buildMode(modes, filePath) { | ||
const IBaseEnvDeclaration = getBaseInterface(); | ||
|
||
const interfaces = await Promise.all(modes.map(getInterfaceByMode)); | ||
|
||
const allDeclarations = interfaces.map(i => i.declaration); | ||
const allNames = interfaces.map(i => i.name); | ||
|
||
const ImportMetaEnvDeclaration = `type ImportMetaEnv = Readonly<${allNames.join(' | ')}>`; | ||
|
||
const content = ` | ||
${IBaseEnvDeclaration} | ||
${allDeclarations.join('\n')} | ||
${ImportMetaEnvDeclaration} | ||
`; | ||
|
||
const dir = dirname(filePath); | ||
if (!existsSync(dir)) { | ||
mkdirSync(dir); | ||
} | ||
|
||
writeFileSync(filePath, content, {encoding: 'utf-8', flag: 'w'}); | ||
} | ||
|
||
buildMode(MODES, typeDefinitionFile).catch(err => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
const {loadEnv} = require('vite'); | ||
|
||
/** | ||
* Load variables from `.env.[mode]` files in cwd | ||
* and set it to `process.env` | ||
* | ||
* @param {string} mode | ||
* @param {string} cwd | ||
* | ||
* @return {void} | ||
*/ | ||
export function loadAndSetEnv(mode, cwd) { | ||
const env = loadEnv(mode || 'production', cwd); | ||
for (const envKey in env) { | ||
if (process.env[envKey] === undefined && env.hasOwnProperty(envKey)) { | ||
process.env[envKey] = env[envKey]; | ||
} | ||
} | ||
} |
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,18 +1,60 @@ | ||
const {writeFile, readFile} = require('fs/promises'); | ||
const {execSync} = require('child_process'); | ||
const electron = require('electron'); | ||
const path = require('path'); | ||
|
||
/** | ||
* This script should be run in electron context | ||
* @example | ||
* ELECTRON_RUN_AS_NODE=1 electron scripts/update-electron-vendors.js | ||
* Returns versions of electron vendors | ||
* The performance of this feature is very poor and can be improved | ||
* @see https://github.com/electron/electron/issues/28006 | ||
* | ||
* @returns {NodeJS.ProcessVersions} | ||
*/ | ||
function getVendors() { | ||
const output = execSync(`${electron} -p "JSON.stringify(process.versions)"`, { | ||
env: {ELECTRON_RUN_AS_NODE: '1'}, | ||
encoding: 'utf-8', | ||
}); | ||
|
||
return JSON.parse(output); | ||
} | ||
|
||
function formattedJSON(obj) { | ||
return JSON.stringify(obj, null, 2) + '\n'; | ||
} | ||
|
||
function updateVendors() { | ||
const electronRelease = getVendors(); | ||
|
||
const nodeMajorVersion = electronRelease.node.split('.')[0]; | ||
const chromeMajorVersion = electronRelease.v8.split('.')[0] + electronRelease.v8.split('.')[1]; | ||
|
||
const packageJSONPath = path.resolve(process.cwd(), 'package.json'); | ||
|
||
import {writeFileSync} from 'fs'; | ||
import path from 'path'; | ||
return Promise.all([ | ||
writeFile( | ||
'./electron-vendors.config.json', | ||
formattedJSON({ | ||
chrome: chromeMajorVersion, | ||
node: nodeMajorVersion, | ||
}), | ||
), | ||
|
||
const electronRelease = process.versions; | ||
readFile(packageJSONPath) | ||
.then(JSON.parse) | ||
.then(packageJSON => { | ||
if (!packageJSON || !Array.isArray(packageJSON.browserslist)) { | ||
throw new Error(`Can't find browserslist in ${packageJSONPath}`); | ||
} | ||
|
||
const node = electronRelease.node.split('.')[0]; | ||
const chrome = electronRelease.v8.split('.').splice(0, 2).join(''); | ||
packageJSON.browserslist = [`Chrome ${chromeMajorVersion}`]; | ||
|
||
const browserslistrcPath = path.resolve(process.cwd(), '.browserslistrc'); | ||
return writeFile(packageJSONPath, formattedJSON(packageJSON)); | ||
}), | ||
]); | ||
} | ||
|
||
writeFileSync('./.electron-vendors.cache.json', JSON.stringify({chrome, node})); | ||
writeFileSync(browserslistrcPath, `Chrome ${chrome}`, 'utf8'); | ||
updateVendors().catch(err => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |