Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions plugin/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
const webpack = require('webpack');

module.exports = class WasiExtPlugin {
constructor() {
this.fallbacks = {
process: require.resolve('../lib/process'),
fs: require.resolve('../lib/fs'),
path: require.resolve('path-browserify'),
};

}

apply(compiler) {
const providePlugin = new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
process: [require.resolve('../lib/process'), 'default']
process: [require.resolve('../lib/process'), 'default'],
});

// Add a NormalModuleReplacementPlugin to handle `node:` imports
const normalModuleReplacements = [
['node:fs', 'fs'],
['node:process', 'process'],
['node:path', 'path'],
].map(([from, to]) => new webpack.NormalModuleReplacementPlugin(
new RegExp(`^${from}$`),
to
));

compiler.options.resolve.fallback = {
...compiler.options.resolve.fallback,
...this.fallbacks,
};

compiler.options.plugins.push(providePlugin);

compiler.options.plugins = compiler.options.plugins || [];
compiler.options.plugins.push(providePlugin, ...normalModuleReplacements);
}
};
};
2 changes: 1 addition & 1 deletion tests/test-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
import { setupExt } from "@spinframework/wasi-ext";
import process from "process"
import process from "node:process"

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