Skip to content

Commit

Permalink
feat: support native .node files, closes #303
Browse files Browse the repository at this point in the history
skip-release
  • Loading branch information
egoist committed Nov 25, 2021
1 parent 325b254 commit 8cadbb1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/esbuild/index.ts
Expand Up @@ -19,6 +19,7 @@ import { getBabel, truthy } from '../utils'
import { PrettyError } from '../errors'
import { transform } from 'sucrase'
import { swcPlugin } from './swc'
import { nativeNodeModulesPlugin } from './native-node-module'

const getOutputExtensionMap = (
pkgTypeField: string | undefined,
Expand Down Expand Up @@ -106,6 +107,7 @@ export async function runEsbuild(
tsconfigResolvePaths: options.tsconfigResolvePaths,
}),
options.tsconfigDecoratorMetadata && swcPlugin(),
nativeNodeModulesPlugin(),
postcssPlugin({ css }),
sveltePlugin({ css }),
...(options.esbuildPlugins || []),
Expand Down
43 changes: 43 additions & 0 deletions src/esbuild/native-node-module.ts
@@ -0,0 +1,43 @@
import { Plugin } from 'esbuild'

// Copied from https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
export const nativeNodeModulesPlugin = (): Plugin => {
return {
name: 'native-node-modules',
setup(build) {
// If a ".node" file is imported within a module in the "file" namespace, resolve
// it to an absolute path and put it into the "node-file" virtual namespace.
build.onResolve({ filter: /\.node$/, namespace: 'file' }, (args) => ({
path: require.resolve(args.path, { paths: [args.resolveDir] }),
namespace: 'node-file',
}))

// Files in the "node-file" virtual namespace call "require()" on the
// path from esbuild of the ".node" file in the output directory.
build.onLoad({ filter: /.*/, namespace: 'node-file' }, (args) => ({
contents: `
import path from ${JSON.stringify(args.path)}
try { module.exports = require(path) }
catch {}
`,
}))

// If a ".node" file is imported within a module in the "node-file" namespace, put
// it in the "file" namespace where esbuild's default loading behavior will handle
// it. It is already an absolute path since we resolved it to one above.
build.onResolve(
{ filter: /\.node$/, namespace: 'node-file' },
(args) => ({
path: args.path,
namespace: 'file',
})
)

// Tell esbuild's default loading behavior to use the "file" loader for
// these ".node" files.
const opts = build.initialOptions
opts.loader = opts.loader || {}
opts.loader['.node'] = 'file'
},
}
}

1 comment on commit 8cadbb1

@vercel
Copy link

@vercel vercel bot commented on 8cadbb1 Nov 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.