diff --git a/plugin/index.js b/plugin/index.js index 22c8166..8875164 100644 --- a/plugin/index.js +++ b/plugin/index.js @@ -1,4 +1,5 @@ const webpack = require('webpack'); + module.exports = class WasiExtPlugin { constructor() { this.fallbacks = { @@ -6,19 +7,32 @@ module.exports = class WasiExtPlugin { 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); } -}; \ No newline at end of file +}; diff --git a/tests/test-app/src/index.ts b/tests/test-app/src/index.ts index 496c23f..9efc557 100644 --- a/tests/test-app/src/index.ts +++ b/tests/test-app/src/index.ts @@ -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) => {