Rollup code splitting

This commit is contained in:
Samuel Elliott 2022-07-02 19:57:52 +01:00
parent 169815588e
commit 0e630201c3
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
6 changed files with 43 additions and 20 deletions

View File

@ -94,7 +94,7 @@
"asar": false,
"extraMetadata": {
"name": "nxapi-app",
"main": "dist/bundle/app-main-bundle.cjs",
"main": "dist/bundle/app-entry.cjs",
"dependencies": {},
"optionalDependencies": {},
"devDependencies": {}

View File

@ -61,17 +61,23 @@ const watch = {
/**
* @type {import('rollup').RollupOptions}
*/
const cli = {
input: 'src/cli-entry.ts',
const main = {
input: ['src/cli-entry.ts', 'src/app/main/index.ts'],
output: {
file: 'dist/bundle/cli-bundle.js',
dir: 'dist/bundle',
format: 'es',
inlineDynamicImports: true,
sourcemap: true,
entryFileNames: chunk => {
if (chunk.name === 'cli-entry') return 'cli-bundle.js';
if (chunk.name === 'index') return 'app-main-bundle.js';
return 'entry-' + chunk.name + '.js';
},
chunkFileNames: 'chunk-[name].js',
},
plugins: [
replace(replace_options),
typescript({
outDir: 'dist/bundle/ts',
noEmit: true,
declaration: false,
}),
@ -99,16 +105,23 @@ const cli = {
/**
* @type {import('rollup').RollupOptions}
*/
const app = {
const app_entry = {
input: 'src/app/main/app-entry.cts',
output: {
file: 'dist/bundle/app-main-bundle.cjs',
format: 'cjs',
file: 'dist/bundle/app-entry.cjs',
format: 'iife',
inlineDynamicImports: true,
sourcemap: true,
},
plugins: [
replace(replace_options),
replace({
include: ['src/app/main/app-entry.cts'],
values: {
'__NXAPI_BUNDLE_APP_MAIN__': JSON.stringify('./app-main-bundle.js'),
},
preventAssignment: true,
}),
typescript({
noEmit: true,
declaration: false,
@ -129,6 +142,7 @@ const app = {
],
external: [
'electron',
path.resolve(__dirname, 'src/app/main/app-main-bundle.js'),
],
watch,
};
@ -235,8 +249,8 @@ const app_browser = {
};
export default [
cli,
app,
main,
app_entry,
app_preload,
app_preload_webservice,
app_browser,

View File

@ -1,7 +1,7 @@
Electron app
---
The Electron app is bundled into ~4 files in `dist/app/bundle` using Rollup. The main process code is not bundled for development, but is when packaging the app at `dist/bundle/app-main-bundle.cjs` (with the command line executable at `dist/bundle/cli-bundle.js`).
The Electron app is bundled into ~4 files in `dist/app/bundle` using Rollup. The main process code is not bundled for development, but is when packaging the app at `dist/bundle` (with the command line executable at `dist/bundle/cli-bundle.js`).
[electron.ts](electron.ts) exports all Electron APIs used in the main process. This is because the `electron` module doesn't actually exist - Electron patches the `require` function (but not the module importer). Additionally Electron does not support using a standard JavaScript module as the app entrypoint, so [main/app-entry.cts](main/app-entry.cts) (a CommonJS module) is used to import the actual app entrypoint after the `ready` event.

View File

@ -2,4 +2,7 @@ const electron = require('electron');
// Do anything that must be run before the app is ready...
electron.app.whenReady().then(() => import('./index.js')).then(m => m.init.call(null));
electron.app.whenReady()
// @ts-expect-error
.then(() => typeof __NXAPI_BUNDLE_APP_MAIN__ !== 'undefined' ? import(__NXAPI_BUNDLE_APP_MAIN__) : import('./index.js'))
.then(m => m.init.call(null));

View File

@ -28,6 +28,7 @@ export function createWindow<T extends WindowType>(
height: 600,
vibrancy: 'content',
autoHideMenuBar: true,
title: 'nxapi',
...options,
webPreferences: {
preload: path.join(bundlepath, 'preload.cjs'),

View File

@ -1,10 +1,13 @@
import process from 'node:process';
import * as path from 'node:path';
import { fileURLToPath } from 'node:url';
import * as fs from 'node:fs';
import * as fs from 'node:fs/promises';
import * as child_process from 'node:child_process';
import * as util from 'node:util';
import createDebug from 'debug';
const exec = util.promisify(child_process.exec);
const debug = createDebug('nxapi:util:product');
//
@ -32,21 +35,23 @@ const embedded_release = globalThis.__NXAPI_BUNDLE_RELEASE__;
export const dir = path.resolve(fileURLToPath(import.meta.url), '..', '..', '..');
export const pkg = embedded_pkg ?? JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf-8'));
export const pkg = embedded_pkg ?? JSON.parse(await fs.readFile(path.join(dir, 'package.json'), 'utf-8'));
export const version: string = pkg.version;
export const release: string | null = embedded_release ?? pkg.__nxapi_release ?? null;
export const git = typeof embedded_git !== 'undefined' ? embedded_git : (() => {
export const git = typeof embedded_git !== 'undefined' ? embedded_git : await (async () => {
try {
fs.statSync(path.join(dir, '.git'));
await fs.stat(path.join(dir, '.git'));
} catch (err) {
return null;
}
const options: child_process.ExecSyncOptions = {cwd: dir};
const revision = child_process.execSync('git rev-parse HEAD', options).toString().trim();
const branch = child_process.execSync('git rev-parse --abbrev-ref HEAD', options).toString().trim();
const changed_files = child_process.execSync('git diff --name-only HEAD', options).toString().trim();
const options: child_process.ExecOptions = {cwd: dir};
const [revision, branch, changed_files] = await Promise.all([
exec('git rev-parse HEAD', options).then(({stdout}) => stdout.toString().trim()),
exec('git rev-parse --abbrev-ref HEAD', options).then(({stdout}) => stdout.toString().trim()),
exec('git diff --name-only HEAD', options).then(({stdout}) => stdout.toString().trim()),
]);
return {
revision,