1
+ import path from 'path'
1
2
import { Plugin } from 'esbuild'
2
3
3
4
// Copied from https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
@@ -7,20 +8,34 @@ export const nativeNodeModulesPlugin = (): Plugin => {
7
8
setup ( build ) {
8
9
// If a ".node" file is imported within a module in the "file" namespace, resolve
9
10
// it to an absolute path and put it into the "node-file" virtual namespace.
10
- build . onResolve ( { filter : / \. n o d e $ / , namespace : 'file' } , ( args ) => ( {
11
- path : require . resolve ( args . path , { paths : [ args . resolveDir ] } ) ,
12
- namespace : 'node-file' ,
13
- } ) )
11
+ build . onResolve ( { filter : / \. n o d e $ / , namespace : 'file' } , ( args ) => {
12
+ const resolvedId = require . resolve ( args . path , {
13
+ paths : [ args . resolveDir ] ,
14
+ } )
15
+ if ( resolvedId . endsWith ( '.node' ) ) {
16
+ return {
17
+ path : resolvedId ,
18
+ namespace : 'node-file' ,
19
+ }
20
+ }
21
+ return {
22
+ path : resolvedId ,
23
+ }
24
+ } )
14
25
15
26
// Files in the "node-file" virtual namespace call "require()" on the
16
27
// path from esbuild of the ".node" file in the output directory.
17
- build . onLoad ( { filter : / .* / , namespace : 'node-file' } , ( args ) => ( {
18
- contents : `
19
- import path from ${ JSON . stringify ( args . path ) }
20
- try { module.exports = require(path) }
21
- catch {}
22
- ` ,
23
- } ) )
28
+ build . onLoad ( { filter : / .* / , namespace : 'node-file' } , ( args ) => {
29
+ console . log ( args . path )
30
+ return {
31
+ contents : `
32
+ import path from ${ JSON . stringify ( args . path ) }
33
+ try { module.exports = require(path) }
34
+ catch {}
35
+ ` ,
36
+ resolveDir : path . dirname ( args . path ) ,
37
+ }
38
+ } )
24
39
25
40
// If a ".node" file is imported within a module in the "node-file" namespace, put
26
41
// it in the "file" namespace where esbuild's default loading behavior will handle
0 commit comments