@@ -9,70 +9,70 @@ 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' ;
1312
1413const projectPath = path . dirname (
1514 path . dirname ( url . fileURLToPath ( import . meta. url ) ) ,
1615) ;
1716
1817const platform = os . platform ( ) ;
1918
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- } ) ;
19+ const nativeNodeModulesPlugin = {
20+ name : 'native-node-modules' ,
21+ setup ( build ) {
22+ build . onLoad ( { filter : / \. * / , namespace : 'file' } , ( args ) => {
23+ const filename = path . basename ( args . path ) ;
24+ if ( filename === 'rocksdb.js' ) {
25+ return {
26+ contents : `
27+ import path from 'node:path';
28+ import url from 'node:url';
29+ import nodeGypBuild from 'node-gyp-build';
30+ const projectPath = path.join(path.dirname(url.fileURLToPath(import.meta.url)), '..', 'node_modules', '@matrixai', 'db');
31+ const rocksdb = nodeGypBuild(projectPath);
32+ export default rocksdb;
33+ //# sourceMappingURL=rocksdb.js.map
34+ ` ,
35+ loader : 'js' ,
36+ } ;
6737 }
68- } ,
69- } ;
38+ if ( args . path . endsWith ( 'fd-lock/index.js' ) ) {
39+ return {
40+ contents : `
41+ const path = require('path');
42+ const binding = require('node-gyp-build')(path.join(__dirname, '..', 'node_modules', 'fd-lock'))
43+
44+ lock.unlock = unlock
45+ module.exports = lock
46+
47+ function lock (fd) {
48+ return !!binding.fd_lock(fd)
49+ }
50+
51+ function unlock (fd) {
52+ return !!binding.fd_unlock(fd)
53+ }
54+ ` ,
55+ loader : 'js' ,
56+ } ;
57+ }
58+ if ( args . path . endsWith ( 'sodium-native/index.js' ) ) {
59+ return {
60+ contents : `
61+ const path = require('path');
62+ module.exports = require('node-gyp-build')(path.join(__dirname, '..', 'node_modules', 'sodium-native'))
63+ ` ,
64+ loader : 'js' ,
65+ } ;
66+ }
67+ return ;
68+ } ) ;
69+ } ,
7070} ;
7171
7272/* eslint-disable no-console */
7373async function main ( argv = process . argv ) {
7474 argv = argv . slice ( 2 ) ;
75- const pkgIndex = argv . findIndex ( ( v ) => v == '--pkg' ) ;
75+ const pkgIndex = argv . findIndex ( ( v ) => v === '--pkg' ) ;
7676 if ( pkgIndex >= 0 ) argv . splice ( pkgIndex , 1 ) ;
7777 const isPkg = pkgIndex >= 0 ;
7878 const buildPath = path . join ( projectPath , 'build' ) ;
@@ -123,7 +123,25 @@ async function main(argv = process.argv) {
123123
124124 // This specifies import paths that is left as an external require
125125 // This is kept to packages that have a native binding
126- const externalDependencies = Object . keys ( packageJSON . optionalDependencies ) ;
126+ /** @type { import('esbuild').BuildOptions } */
127+ const extraOptions = isPkg
128+ ? {
129+ external : [ ] ,
130+ format : 'cjs' ,
131+ inject : [ path . join ( projectPath , './shims/import-meta-url-shim.mjs' ) ] ,
132+ // Fix import.meta.url in CJS output
133+ define : {
134+ 'import.meta.url' : '__import_meta_url' ,
135+ } ,
136+ outExtension : { '.js' : '.cjs' } ,
137+ }
138+ : {
139+ // External: externalDependencies,
140+ format : 'esm' ,
141+ inject : [ path . join ( projectPath , './shims/require-shim.mjs' ) ] ,
142+ outExtension : { '.js' : '.mjs' } ,
143+ } ;
144+
127145 /** @type { import('esbuild').BuildOptions } */
128146 const esbuildOptions = {
129147 // 2 entrypoints, the main script and the worker script
@@ -135,36 +153,18 @@ async function main(argv = process.argv) {
135153 bundle : true ,
136154 platform : 'node' ,
137155 outdir : distPath ,
138- // external: isPkg ? ['*.node'] : externalDependencies,
139- external : isPkg ? [ ] : externalDependencies ,
140- // external: externalDependencies,
141- treeShaking : true ,
156+ treeShaking : false ,
142157 // External source map for debugging
143158 sourcemap : true ,
144159 // Minify and keep the original names
145160 minify : false ,
146161 keepNames : true ,
147- // Supporting ESM
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 ) ] : [ ] ,
162+ plugins : [ nativeNodeModulesPlugin ] ,
163+ ...extraOptions ,
163164 } ;
164165 console . error ( 'Running esbuild:' ) ;
165166 console . error ( esbuildOptions ) ;
166167 await esbuild . build ( esbuildOptions ) ;
167-
168168 // Rename worker script
169169 console . error ( 'Renaming worker script' ) ;
170170 childProcess . execFileSync (
@@ -187,7 +187,6 @@ async function main(argv = process.argv) {
187187 `dist/polykeyWorkerManifest.${ isPkg ? 'cjs' : 'mjs' } .map` ,
188188 'dist/polykeyWorkerManifest.js.map' ,
189189 ] ,
190- // ['dist/polykeyWorkerManifest.mjs.map', 'dist/polykeyWorkerManifest.js.map'],
191190 {
192191 stdio : [ 'inherit' , 'inherit' , 'inherit' ] ,
193192 windowsHide : true ,
0 commit comments