11import { session as electronSession } from 'electron'
22import { EventEmitter } from 'node:events'
33import path from 'node:path'
4- import { promises as fs } from 'node:fs'
4+ import { existsSync } from 'node:fs'
5+ import { createRequire } from 'node:module'
56
67import { BrowserActionAPI } from './api/browser-action'
78import { TabsAPI } from './api/tabs'
@@ -27,6 +28,31 @@ function checkVersion() {
2728 }
2829}
2930
31+ function resolvePreloadPath ( modulePath ?: string ) {
32+ // Attempt to resolve preload path from module exports
33+ try {
34+ return createRequire ( __dirname ) . resolve ( 'electron-chrome-extensions/preload' )
35+ } catch ( error ) {
36+ if ( process . env . NODE_ENV !== 'production' ) {
37+ console . error ( error )
38+ }
39+ }
40+
41+ const preloadFilename = 'chrome-extension-api.preload.js'
42+
43+ // Deprecated: use modulePath if provided
44+ if ( modulePath ) {
45+ process . emitWarning (
46+ 'electron-chrome-extensions: "modulePath" is deprecated and will be removed in future versions.' ,
47+ { type : 'DeprecationWarning' } ,
48+ )
49+ return path . join ( modulePath , 'dist' , preloadFilename )
50+ }
51+
52+ // Fallback to preload relative to entrypoint directory
53+ return path . join ( __dirname , preloadFilename )
54+ }
55+
3056export interface ChromeExtensionOptions extends ChromeExtensionImpl {
3157 /**
3258 * License used to distribute electron-chrome-extensions.
@@ -44,6 +70,8 @@ export interface ChromeExtensionOptions extends ChromeExtensionImpl {
4470 /**
4571 * Path to electron-chrome-extensions module files. Might be needed if
4672 * JavaScript bundlers like Webpack are used in your build process.
73+ *
74+ * @deprecated See "Packaging the preload script" in the readme.
4775 */
4876 modulePath ?: string
4977}
@@ -60,7 +88,6 @@ export class ElectronChromeExtensions extends EventEmitter {
6088 }
6189
6290 private ctx : ExtensionContext
63- private modulePath : string
6491
6592 private api : {
6693 browserAction : BrowserActionAPI
@@ -78,7 +105,7 @@ export class ElectronChromeExtensions extends EventEmitter {
78105 constructor ( opts : ChromeExtensionOptions ) {
79106 super ( )
80107
81- const { license, session = electronSession . defaultSession , modulePath , ...impl } = opts || { }
108+ const { license, session = electronSession . defaultSession , ...impl } = opts || { }
82109
83110 checkVersion ( )
84111 checkLicense ( license )
@@ -99,8 +126,6 @@ export class ElectronChromeExtensions extends EventEmitter {
99126 store,
100127 }
101128
102- this . modulePath = modulePath || path . join ( __dirname , '../..' )
103-
104129 this . api = {
105130 browserAction : new BrowserActionAPI ( this . ctx ) ,
106131 contextMenus : new ContextMenusAPI ( this . ctx ) ,
@@ -115,7 +140,7 @@ export class ElectronChromeExtensions extends EventEmitter {
115140 }
116141
117142 this . listenForExtensions ( )
118- this . prependPreload ( )
143+ this . prependPreload ( opts . modulePath )
119144
120145 // Register crx:// protocol in default session for convenience
121146 if ( this . ctx . session !== electronSession . defaultSession ) {
@@ -129,10 +154,10 @@ export class ElectronChromeExtensions extends EventEmitter {
129154 } )
130155 }
131156
132- private async prependPreload ( ) {
157+ private async prependPreload ( modulePath ?: string ) {
133158 const { session } = this . ctx
134159
135- const preloadPath = path . join ( this . modulePath , 'dist/preload.js' )
160+ const preloadPath = resolvePreloadPath ( modulePath )
136161
137162 if ( 'registerPreloadScript' in session ) {
138163 session . registerPreloadScript ( {
@@ -150,15 +175,12 @@ export class ElectronChromeExtensions extends EventEmitter {
150175 session . setPreloads ( [ ...session . getPreloads ( ) , preloadPath ] )
151176 }
152177
153- let preloadExists = false
154- try {
155- const stat = await fs . stat ( preloadPath )
156- preloadExists = stat . isFile ( )
157- } catch { }
158-
159- if ( ! preloadExists ) {
178+ if ( ! existsSync ( preloadPath ) ) {
160179 console . error (
161- `Unable to access electron-chrome-extensions preload file (${ preloadPath } ). Consider configuring the 'modulePath' constructor option.` ,
180+ new Error (
181+ `electron-chrome-extensions: Preload file not found at "${ preloadPath } ". ` +
182+ 'See "Packaging the preload script" in the readme.' ,
183+ ) ,
162184 )
163185 }
164186 }
0 commit comments