Skip to content

Commit

Permalink
build: attempt to provide both ESM and CJS builds
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaddock committed Dec 24, 2024
1 parent a742e3b commit d6f761b
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 19 deletions.
31 changes: 28 additions & 3 deletions packages/electron-chrome-web-store/esbuild.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 10 additions & 1 deletion packages/electron-chrome-web-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions packages/electron-chrome-web-store/src/browser/api.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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'

Expand Down
2 changes: 2 additions & 0 deletions packages/electron-chrome-web-store/src/browser/deps.td.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare module 'adm-zip'
declare module 'debug'
2 changes: 1 addition & 1 deletion packages/electron-chrome-web-store/src/browser/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
15 changes: 8 additions & 7 deletions packages/electron-chrome-web-store/src/browser/installer.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
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'
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')
Expand Down
3 changes: 2 additions & 1 deletion packages/electron-chrome-web-store/src/browser/loader.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
7 changes: 5 additions & 2 deletions packages/electron-chrome-web-store/src/browser/updater.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -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<void> {
export async function updateExtensions(
session: Electron.Session = electronSession.defaultSession,
): Promise<void> {
const updates = await checkForUpdates(session)
if (updates.length > 0) {
await installUpdates(session, updates)
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-chrome-web-store/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"compilerOptions": {
"moduleResolution": "node",
"outDir": "dist",
"outDir": "dist/types",
"declaration": true,
"emitDeclarationOnly": true
},
Expand Down

0 comments on commit d6f761b

Please sign in to comment.