From d6f761b873a7ca6ea2dd058ea90a2567c39e1053 Mon Sep 17 00:00:00 2001 From: Samuel Maddock Date: Mon, 23 Dec 2024 23:57:01 -0500 Subject: [PATCH] build: attempt to provide both ESM and CJS builds --- .../esbuild.config.js | 31 +++++++++++++++++-- .../electron-chrome-web-store/package.json | 11 ++++++- .../src/browser/api.ts | 7 +++-- .../src/browser/deps.td.ts | 2 ++ .../src/browser/index.ts | 2 +- .../src/browser/installer.ts | 15 ++++----- .../src/browser/loader.ts | 3 +- .../src/browser/updater.ts | 7 +++-- .../electron-chrome-web-store/tsconfig.json | 2 +- 9 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 packages/electron-chrome-web-store/src/browser/deps.td.ts diff --git a/packages/electron-chrome-web-store/esbuild.config.js b/packages/electron-chrome-web-store/esbuild.config.js index 4f88cad7..d8c66159 100644 --- a/packages/electron-chrome-web-store/esbuild.config.js +++ b/packages/electron-chrome-web-store/esbuild.config.js @@ -3,20 +3,45 @@ const { createConfig, build } = require('../../build/esbuild/esbuild.config.base console.log(`building ${packageJson.name}`) +const external = [ + 'node:crypto', + 'node:fs', + 'node:os', + 'node:path', + 'node:stream', + 'node:stream/promises', + 'electron', + 'debug', + 'adm-zip', + 'pbf', +] + +const esmOnlyModules = ['pbf'] + const browserConfig = createConfig({ entryPoints: ['src/browser/index.ts'], - outfile: 'dist/browser/index.js', + outfile: 'dist/cjs/browser/index.js', platform: 'node', - external: ['electron'], + external: external.filter((module) => !esmOnlyModules.includes(module)), +}) + +const browserESMConfig = createConfig({ + entryPoints: ['src/browser/index.ts'], + outfile: 'dist/esm/browser/index.mjs', + platform: 'neutral', + external, + format: 'esm', }) build(browserConfig) +build(browserESMConfig) const preloadConfig = createConfig({ entryPoints: ['src/renderer/web-store-preload.ts'], outfile: 'dist/renderer/web-store-preload.js', platform: 'browser', - external: ['electron'], + external, + sourcemap: false, }) build(preloadConfig) diff --git a/packages/electron-chrome-web-store/package.json b/packages/electron-chrome-web-store/package.json index d001d3ec..0f01ccad 100644 --- a/packages/electron-chrome-web-store/package.json +++ b/packages/electron-chrome-web-store/package.json @@ -2,7 +2,16 @@ "name": "electron-chrome-web-store", "version": "0.6.1", "description": "Install and update Chrome extensions from the Chrome Web Store for Electron", - "main": "dist/browser/index.js", + "main": "./dist/cjs/browser/index.js", + "module": "./dist/esm/browser/index.mjs", + "exports": { + ".": { + "import": "./dist/esm/browser/index.mjs", + "require": "./dist/cjs/browser/index.js", + "types": "./dist/types/browser/index.d.ts" + } + }, + "types": "./dist/types/browser/index.d.ts", "scripts": { "build": "yarn clean && tsc && node esbuild.config.js", "clean": "node ../../scripts/clean.js", diff --git a/packages/electron-chrome-web-store/src/browser/api.ts b/packages/electron-chrome-web-store/src/browser/api.ts index 60574451..933ee0a8 100644 --- a/packages/electron-chrome-web-store/src/browser/api.ts +++ b/packages/electron-chrome-web-store/src/browser/api.ts @@ -1,5 +1,6 @@ -import * as fs from 'fs' -import * as path from 'path' +import * as fs from 'node:fs' +import * as path from 'node:path' +import debug from 'debug' import { app, ipcMain } from 'electron' import { @@ -10,7 +11,7 @@ import { } from '../common/constants' import { installExtension } from './installer' -const d = require('debug')('electron-chrome-web-store:api') +const d = debug('electron-chrome-web-store:api') const WEBSTORE_URL = 'https://chromewebstore.google.com' diff --git a/packages/electron-chrome-web-store/src/browser/deps.td.ts b/packages/electron-chrome-web-store/src/browser/deps.td.ts new file mode 100644 index 00000000..cef3f104 --- /dev/null +++ b/packages/electron-chrome-web-store/src/browser/deps.td.ts @@ -0,0 +1,2 @@ +declare module 'adm-zip' +declare module 'debug' diff --git a/packages/electron-chrome-web-store/src/browser/index.ts b/packages/electron-chrome-web-store/src/browser/index.ts index b11e5669..f1778217 100644 --- a/packages/electron-chrome-web-store/src/browser/index.ts +++ b/packages/electron-chrome-web-store/src/browser/index.ts @@ -1,5 +1,5 @@ import { app, session as electronSession } from 'electron' -import * as path from 'path' +import * as path from 'node:path' import { registerWebStoreApi } from './api' import { loadAllExtensions } from './loader' diff --git a/packages/electron-chrome-web-store/src/browser/installer.ts b/packages/electron-chrome-web-store/src/browser/installer.ts index 9ff65260..f6a93358 100644 --- a/packages/electron-chrome-web-store/src/browser/installer.ts +++ b/packages/electron-chrome-web-store/src/browser/installer.ts @@ -1,10 +1,12 @@ -import * as fs from 'fs' -import * as os from 'os' -import * as path from 'path' -import { Readable } from 'stream' -import { pipeline } from 'stream/promises' +import * as fs from 'node:fs' +import * as os from 'node:os' +import * as path from 'node:path' +import { Readable } from 'node:stream' +import { pipeline } from 'node:stream/promises' import { session as electronSession } from 'electron' +import AdmZip from 'adm-zip' +import debug from 'debug' import Pbf from 'pbf' import { readCrxFileHeader, readSignedData } from './crx3' @@ -12,8 +14,7 @@ import { convertHexadecimalToIDAlphabet, generateId } from './id' import { fetch, getChromeVersion, getDefaultExtensionsPath } from './utils' import { findExtensionInstall } from './loader' -const AdmZip = require('adm-zip') -const d = require('debug')('electron-chrome-web-store:installer') +const d = debug('electron-chrome-web-store:installer') function getExtensionCrxURL(extensionId: ExtensionId) { const url = new URL('https://clients2.google.com/service/update2/crx') diff --git a/packages/electron-chrome-web-store/src/browser/loader.ts b/packages/electron-chrome-web-store/src/browser/loader.ts index 29063a60..71cc1982 100644 --- a/packages/electron-chrome-web-store/src/browser/loader.ts +++ b/packages/electron-chrome-web-store/src/browser/loader.ts @@ -1,10 +1,11 @@ import * as fs from 'node:fs' import * as path from 'node:path' +import debug from 'debug' import { generateId } from './id' import { compareVersions } from './utils' -const d = require('debug')('electron-chrome-web-store:loader') +const d = debug('electron-chrome-web-store:loader') type ExtensionPathBaseInfo = { manifest: chrome.runtime.Manifest; path: string } type ExtensionPathInfo = diff --git a/packages/electron-chrome-web-store/src/browser/updater.ts b/packages/electron-chrome-web-store/src/browser/updater.ts index b80a1255..a4fce354 100644 --- a/packages/electron-chrome-web-store/src/browser/updater.ts +++ b/packages/electron-chrome-web-store/src/browser/updater.ts @@ -1,11 +1,12 @@ import * as fs from 'node:fs' import * as path from 'node:path' +import debug from 'debug' import { app, powerMonitor, session as electronSession } from 'electron' import { compareVersions, fetch, getChromeVersion } from './utils' import { downloadExtensionFromURL } from './installer' -const d = require('debug')('electron-chrome-web-store:updater') +const d = debug('electron-chrome-web-store:updater') interface OmahaResponseBody { response: { @@ -271,7 +272,9 @@ async function installUpdates(session: Electron.Session, updates: ExtensionUpdat /** * Check session's loaded extensions for updates and install any if available. */ -export async function updateExtensions(session: Electron.Session = electronSession.defaultSession): Promise { +export async function updateExtensions( + session: Electron.Session = electronSession.defaultSession, +): Promise { const updates = await checkForUpdates(session) if (updates.length > 0) { await installUpdates(session, updates) diff --git a/packages/electron-chrome-web-store/tsconfig.json b/packages/electron-chrome-web-store/tsconfig.json index d829bc85..01eddcb6 100644 --- a/packages/electron-chrome-web-store/tsconfig.json +++ b/packages/electron-chrome-web-store/tsconfig.json @@ -3,7 +3,7 @@ "compilerOptions": { "moduleResolution": "node", - "outDir": "dist", + "outDir": "dist/types", "declaration": true, "emitDeclarationOnly": true },