@@ -9,16 +9,72 @@ import childProcess from 'node:child_process';
99import esbuild from 'esbuild' ;
1010import config from 'polykey/config.js' ;
1111import packageJSON from '../package.json' assert { type : 'json ' } ;
12+ import { createRequire } from 'node:module' ;
1213
1314const projectPath = path . dirname (
1415 path . dirname ( url . fileURLToPath ( import . meta. url ) ) ,
1516) ;
1617
1718const platform = os . platform ( ) ;
1819
20+ const nativeDirnameOverridePlugin = ( nativePackages ) => {
21+ return {
22+ name : 'native-dirname-override' ,
23+ setup ( build ) {
24+ const require = createRequire ( import . meta. url ) ;
25+
26+ for ( const pkgName of nativePackages ) {
27+ let pkgJsonPath ;
28+ try {
29+ pkgJsonPath = require . resolve ( `${ pkgName } /package.json` ) ;
30+ } catch {
31+ console . log ( 'Skipping' , pkgName ) ;
32+ continue ;
33+ }
34+ const pkgRoot = path . dirname ( pkgJsonPath ) ;
35+
36+ // Read package.json and resolve entry point
37+ const pkgJson = JSON . parse ( fs . readFileSync ( pkgJsonPath , 'utf8' ) ) ;
38+ let relativeEntry ;
39+
40+ if ( pkgJson . exports && typeof pkgJson . exports === 'object' ) {
41+ const mainExport = pkgJson . exports [ '.' ] ;
42+ if ( typeof mainExport === 'string' ) {
43+ relativeEntry = mainExport ;
44+ } else if ( mainExport ?. require ) {
45+ relativeEntry = mainExport . require ;
46+ } else if ( mainExport ?. import ) {
47+ relativeEntry = mainExport . import ;
48+ }
49+ }
50+
51+ if ( ! relativeEntry ) {
52+ relativeEntry = 'index.js' ;
53+ }
54+
55+ const resolvedEntry = path . join ( pkgRoot , relativeEntry ) ;
56+
57+ build . onLoad ( { filter : / .* / , namespace : 'file' } , async ( args ) => {
58+ if ( path . resolve ( args . path ) !== path . resolve ( resolvedEntry ) ) return ;
59+
60+ return {
61+ contents : ` module.exports = require('node-gyp-build')(${ JSON . stringify (
62+ pkgRoot ,
63+ ) } ); `,
64+ loader : 'js' ,
65+ } ;
66+ } ) ;
67+ }
68+ } ,
69+ } ;
70+ } ;
71+
1972/* eslint-disable no-console */
2073async function main ( argv = process . argv ) {
2174 argv = argv . slice ( 2 ) ;
75+ const pkgIndex = argv . findIndex ( ( v ) => v == '--pkg' ) ;
76+ if ( pkgIndex >= 0 ) argv . splice ( pkgIndex , 1 ) ;
77+ const isPkg = pkgIndex >= 0 ;
2278 const buildPath = path . join ( projectPath , 'build' ) ;
2379 const distPath = path . join ( projectPath , 'dist' ) ;
2480 const gitPath = process . env . GIT_DIR ?? path . join ( projectPath , '.git' ) ;
@@ -64,9 +120,11 @@ async function main(argv = process.argv) {
64120 path . join ( buildPath , 'build.json' ) ,
65121 JSON . stringify ( buildJSON , null , 2 ) ,
66122 ) ;
123+
67124 // This specifies import paths that is left as an external require
68125 // This is kept to packages that have a native binding
69126 const externalDependencies = Object . keys ( packageJSON . optionalDependencies ) ;
127+ /** @type { import('esbuild').BuildOptions } */
70128 const esbuildOptions = {
71129 // 2 entrypoints, the main script and the worker script
72130 entryPoints : [
@@ -77,26 +135,45 @@ async function main(argv = process.argv) {
77135 bundle : true ,
78136 platform : 'node' ,
79137 outdir : distPath ,
80- external : externalDependencies ,
138+ // external: isPkg ? ['*.node'] : externalDependencies,
139+ external : isPkg ? [ ] : externalDependencies ,
140+ // external: externalDependencies,
81141 treeShaking : true ,
82142 // External source map for debugging
83143 sourcemap : true ,
84144 // Minify and keep the original names
85145 minify : false ,
86146 keepNames : true ,
87147 // Supporting ESM
88- format : 'esm' ,
89- inject : [ path . join ( projectPath , './shims/require-shim.mjs' ) ] ,
90- outExtension : { '.js' : '.mjs' } ,
148+ format : isPkg ? 'cjs' : 'esm' ,
149+ // format: 'esm',
150+ inject : isPkg
151+ ? [ path . join ( projectPath , './shims/import-meta-url-shim.mjs' ) ]
152+ : [ path . join ( projectPath , './shims/require-shim.mjs' ) ] ,
153+ // inject: [path.join(projectPath, './shims/require-shim.mjs')],
154+ // Fix import.meta.url in CJS output
155+ define : isPkg
156+ ? {
157+ 'import.meta.url' : '__import_meta_url' ,
158+ }
159+ : { } ,
160+ outExtension : isPkg ? { '.js' : '.cjs' } : { '.js' : '.mjs' } ,
161+ // outExtension: { '.js': '.mjs' },
162+ plugins : isPkg ? [ nativeDirnameOverridePlugin ( externalDependencies ) ] : [ ] ,
91163 } ;
92164 console . error ( 'Running esbuild:' ) ;
93165 console . error ( esbuildOptions ) ;
94166 await esbuild . build ( esbuildOptions ) ;
167+
95168 // Rename worker script
96169 console . error ( 'Renaming worker script' ) ;
97170 childProcess . execFileSync (
98171 'mv' ,
99- [ 'dist/polykeyWorkerManifest.mjs' , 'dist/polykeyWorkerManifest.js' ] ,
172+ [
173+ `dist/polykeyWorkerManifest.${ isPkg ? 'cjs' : 'mjs' } ` ,
174+ 'dist/polykeyWorkerManifest.js' ,
175+ ] ,
176+ // ['dist/polykeyWorkerManifest.mjs', 'dist/polykeyWorkerManifest.js'],
100177 {
101178 stdio : [ 'inherit' , 'inherit' , 'inherit' ] ,
102179 windowsHide : true ,
@@ -106,7 +183,11 @@ async function main(argv = process.argv) {
106183 ) ;
107184 childProcess . execFileSync (
108185 'mv' ,
109- [ 'dist/polykeyWorkerManifest.mjs.map' , 'dist/polykeyWorkerManifest.js.map' ] ,
186+ [
187+ `dist/polykeyWorkerManifest.${ isPkg ? 'cjs' : 'mjs' } .map` ,
188+ 'dist/polykeyWorkerManifest.js.map' ,
189+ ] ,
190+ // ['dist/polykeyWorkerManifest.mjs.map', 'dist/polykeyWorkerManifest.js.map'],
110191 {
111192 stdio : [ 'inherit' , 'inherit' , 'inherit' ] ,
112193 windowsHide : true ,
0 commit comments