@@ -16,9 +16,69 @@ const projectPath = path.dirname(
1616
1717const platform = os . platform ( ) ;
1818
19+ /**
20+ * This plugin intercepts loading of certain imports and replaces their contents.
21+ * It overrides the import paths for native imports for rocksdb, fd-lock and sodium-native
22+ */
23+ const nativeNodeModulesPlugin = {
24+ name : 'native-node-modules' ,
25+ setup ( build ) {
26+ build . onLoad ( { filter : / \. * / , namespace : 'file' } , ( args ) => {
27+ const filename = path . basename ( args . path ) ;
28+ if ( filename === 'rocksdb.js' ) {
29+ return {
30+ contents : `
31+ import path from 'node:path';
32+ import url from 'node:url';
33+ import nodeGypBuild from 'node-gyp-build';
34+ const projectPath = path.join(path.dirname(url.fileURLToPath(import.meta.url)), '..', 'node_modules', '@matrixai', 'db');
35+ const rocksdb = nodeGypBuild(projectPath);
36+ export default rocksdb;
37+ //# sourceMappingURL=rocksdb.js.map
38+ ` ,
39+ loader : 'js' ,
40+ } ;
41+ }
42+ if ( args . path . endsWith ( 'fd-lock/index.js' ) ) {
43+ return {
44+ contents : `
45+ const path = require('path');
46+ const binding = require('node-gyp-build')(path.join(__dirname, '..', 'node_modules', 'fd-lock'))
47+
48+ lock.unlock = unlock
49+ module.exports = lock
50+
51+ function lock (fd) {
52+ return !!binding.fd_lock(fd)
53+ }
54+
55+ function unlock (fd) {
56+ return !!binding.fd_unlock(fd)
57+ }
58+ ` ,
59+ loader : 'js' ,
60+ } ;
61+ }
62+ if ( args . path . endsWith ( 'sodium-native/index.js' ) ) {
63+ return {
64+ contents : `
65+ const path = require('path');
66+ module.exports = require('node-gyp-build')(path.join(__dirname, '..', 'node_modules', 'sodium-native'))
67+ ` ,
68+ loader : 'js' ,
69+ } ;
70+ }
71+ return ;
72+ } ) ;
73+ } ,
74+ } ;
75+
1976/* eslint-disable no-console */
2077async function main ( argv = process . argv ) {
2178 argv = argv . slice ( 2 ) ;
79+ const pkgIndex = argv . findIndex ( ( v ) => v === '--pkg' ) ;
80+ if ( pkgIndex >= 0 ) argv . splice ( pkgIndex , 1 ) ;
81+ const isPkg = pkgIndex >= 0 ;
2282 const buildPath = path . join ( projectPath , 'build' ) ;
2383 const distPath = path . join ( projectPath , 'dist' ) ;
2484 const gitPath = process . env . GIT_DIR ?? path . join ( projectPath , '.git' ) ;
@@ -64,9 +124,29 @@ async function main(argv = process.argv) {
64124 path . join ( buildPath , 'build.json' ) ,
65125 JSON . stringify ( buildJSON , null , 2 ) ,
66126 ) ;
127+
67128 // This specifies import paths that is left as an external require
68129 // This is kept to packages that have a native binding
69- const externalDependencies = Object . keys ( packageJSON . optionalDependencies ) ;
130+ /** @type { import('esbuild').BuildOptions } */
131+ const isPkgSwitchedOptions = isPkg
132+ ? {
133+ external : [ ] ,
134+ format : 'cjs' ,
135+ inject : [ path . join ( projectPath , './shims/import-meta-url-shim.mjs' ) ] ,
136+ // Fix import.meta.url in CJS output
137+ define : {
138+ 'import.meta.url' : '__import_meta_url' ,
139+ } ,
140+ outExtension : { '.js' : '.cjs' } ,
141+ }
142+ : {
143+ // External: externalDependencies,
144+ format : 'esm' ,
145+ inject : [ path . join ( projectPath , './shims/require-shim.mjs' ) ] ,
146+ outExtension : { '.js' : '.mjs' } ,
147+ } ;
148+
149+ /** @type { import('esbuild').BuildOptions } */
70150 const esbuildOptions = {
71151 // 2 entrypoints, the main script and the worker script
72152 entryPoints : [
@@ -77,17 +157,14 @@ async function main(argv = process.argv) {
77157 bundle : true ,
78158 platform : 'node' ,
79159 outdir : distPath ,
80- external : externalDependencies ,
81160 treeShaking : true ,
82161 // External source map for debugging
83162 sourcemap : true ,
84163 // Minify and keep the original names
85164 minify : false ,
86165 keepNames : true ,
87- // Supporting ESM
88- format : 'esm' ,
89- inject : [ path . join ( projectPath , './shims/require-shim.mjs' ) ] ,
90- outExtension : { '.js' : '.mjs' } ,
166+ plugins : [ nativeNodeModulesPlugin ] ,
167+ ...isPkgSwitchedOptions ,
91168 } ;
92169 console . error ( 'Running esbuild:' ) ;
93170 console . error ( esbuildOptions ) ;
@@ -96,7 +173,11 @@ async function main(argv = process.argv) {
96173 console . error ( 'Renaming worker script' ) ;
97174 childProcess . execFileSync (
98175 'mv' ,
99- [ 'dist/polykeyWorkerManifest.mjs' , 'dist/polykeyWorkerManifest.js' ] ,
176+ [
177+ `dist/polykeyWorkerManifest.${ isPkg ? 'cjs' : 'mjs' } ` ,
178+ 'dist/polykeyWorkerManifest.js' ,
179+ ] ,
180+ // ['dist/polykeyWorkerManifest.mjs', 'dist/polykeyWorkerManifest.js'],
100181 {
101182 stdio : [ 'inherit' , 'inherit' , 'inherit' ] ,
102183 windowsHide : true ,
@@ -106,7 +187,10 @@ async function main(argv = process.argv) {
106187 ) ;
107188 childProcess . execFileSync (
108189 'mv' ,
109- [ 'dist/polykeyWorkerManifest.mjs.map' , 'dist/polykeyWorkerManifest.js.map' ] ,
190+ [
191+ `dist/polykeyWorkerManifest.${ isPkg ? 'cjs' : 'mjs' } .map` ,
192+ 'dist/polykeyWorkerManifest.js.map' ,
193+ ] ,
110194 {
111195 stdio : [ 'inherit' , 'inherit' , 'inherit' ] ,
112196 windowsHide : true ,
0 commit comments