Skip to content

Commit ed83a4a

Browse files
committed
handle node: prefix for path,fs,process
Signed-off-by: karthik2804 <[email protected]>
1 parent 329815f commit ed83a4a

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

plugin/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
const webpack = require('webpack');
2+
23
module.exports = class WasiExtPlugin {
34
constructor() {
45
this.fallbacks = {
56
process: require.resolve('../lib/process'),
67
fs: require.resolve('../lib/fs'),
78
path: require.resolve('path-browserify'),
89
};
10+
911
}
1012

1113
apply(compiler) {
1214
const providePlugin = new webpack.ProvidePlugin({
1315
Buffer: ['buffer', 'Buffer'],
14-
process: [require.resolve('../lib/process'), 'default']
16+
process: [require.resolve('../lib/process'), 'default'],
1517
});
1618

19+
// Add a NormalModuleReplacementPlugin to handle `node:` imports
20+
const normalModuleReplacements = [
21+
['node:fs', 'fs'],
22+
['node:process', 'process'],
23+
['node:path', 'path'],
24+
].map(([from, to]) => new webpack.NormalModuleReplacementPlugin(
25+
new RegExp(`^${from}$`),
26+
to
27+
));
28+
1729
compiler.options.resolve.fallback = {
1830
...compiler.options.resolve.fallback,
1931
...this.fallbacks,
2032
};
2133

22-
compiler.options.plugins.push(providePlugin);
34+
35+
compiler.options.plugins = compiler.options.plugins || [];
36+
compiler.options.plugins.push(providePlugin, ...normalModuleReplacements);
2337
}
24-
};
38+
};

tests/test-app/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from "fs";
22
import { setupExt } from "@spinframework/wasi-ext";
3-
import process from "process"
3+
import process from "node:process"
44

55
//@ts-ignore
66
addEventListener('fetch', (event: FetchEvent) => {

0 commit comments

Comments
 (0)