Skip to content

Commit

Permalink
feat(compiler): Allow native .node files #2560
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksrandall authored and MichaelDeBoey committed Jul 18, 2022
1 parent b2ef490 commit 45016f1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/remix-dev/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { serverAssetsManifestPlugin } from "./compiler/plugins/serverAssetsManif
import { serverBareModulesPlugin } from "./compiler/plugins/serverBareModulesPlugin";
import { serverEntryModulePlugin } from "./compiler/plugins/serverEntryModulePlugin";
import { serverRouteModulesPlugin } from "./compiler/plugins/serverRouteModulesPlugin";
import { serverNativeNodeModulesPlugin } from "./compiler/plugins/serverNativeNodeModules";
import { writeFileSafe } from "./compiler/utils/fs";
import { urlImportsPlugin } from "./compiler/plugins/urlImportsPlugin";

Expand Down Expand Up @@ -423,6 +424,7 @@ function createServerBuild(
serverAssetsManifestPlugin(assetsManifestPromiseRef),
serverBareModulesPlugin(config, options.onWarning),
yarnPnpPlugin(),
serverNativeNodeModulesPlugin,
];

if (config.serverPlatform !== "node") {
Expand Down
1 change: 1 addition & 0 deletions packages/remix-dev/compiler/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const loaders: { [ext: string]: esbuild.Loader } = {
".mdx": "jsx",
".mp3": "file",
".mp4": "file",
".node": "file",
".ogg": "file",
".otf": "file",
".png": "file",
Expand Down
32 changes: 32 additions & 0 deletions packages/remix-dev/compiler/plugins/serverNativeNodeModules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Plugin } from "esbuild";

// See https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
export const serverNativeNodeModulesPlugin: Plugin = {
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',
}))
},
}

0 comments on commit 45016f1

Please sign in to comment.