-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
php-wasm/node: Ship as ESM and CJS (#1585)
The `@php-wasm/node` package used to be CommonJS only. This created tricky dependency identity problems in ESM applications, e.g. in #1577 the main script imports the ESM version of `@php-wasm/universal`, but `@php-wasm/node` imports the CJS version of it. This PR ensures `@php-wasm/node` has an ESM version available. This is first step towards solving #1583 Closes #1579 Closes #1577 ## Testing instructions Run `npm run build`, copy the built packages from `dist/packages/php-wasm` to node_modules in another directory, create a test.js script with the following contents: ``` import { PHP } from '@php-wasm/universal'; import { loadNodeRuntime, useHostFilesystem } from '@php-wasm/node'; const php = new PHP(await loadNodeRuntime('8.0')); console.log("Hey"); console.log(await php.run({ code: 'echo "Hello, World!";' })); ``` Then create a package.json file with `{"type":"module"}` and confirm that running test.js works. Then create test.cjs file as follows: ```js const { PHP } = require('@php-wasm/universal'); const { loadNodeRuntime } = require('@php-wasm/node'); async function main() { const runtimeId = await loadNodeRuntime('8.0'); console.log({runtimeId}) const php = new PHP(runtimeId); console.log(php); } main(); ``` And confirm this one works, too.
- Loading branch information
Showing
4 changed files
with
111 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import esbuild from 'esbuild'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
try { | ||
fs.mkdirSync('dist/packages/php-wasm/node', { recursive: true }); | ||
} catch (e) { | ||
// Ignore | ||
} | ||
|
||
async function build() { | ||
await esbuild.build({ | ||
entryPoints: [ | ||
'packages/php-wasm/node/src/index.ts', | ||
'packages/php-wasm/node/src/noop.ts', | ||
], | ||
supported: { | ||
'dynamic-import': false, | ||
}, | ||
outExtension: { '.js': '.cjs' }, | ||
outdir: 'dist/packages/php-wasm/node', | ||
platform: 'node', | ||
assetNames: '[name]', | ||
chunkNames: '[name]', | ||
logOverride: { | ||
'commonjs-variable-in-esm': 'silent', | ||
}, | ||
format: 'cjs', | ||
bundle: true, | ||
tsconfig: 'packages/php-wasm/node/tsconfig.json', | ||
external: ['@php-wasm/*', '@wp-playground/*', 'ws'], | ||
loader: { | ||
'.php': 'text', | ||
'.ini': 'file', | ||
'.wasm': 'file', | ||
}, | ||
}); | ||
|
||
const nodeModules = new RegExp(/^(?:.*[\\/])?node_modules(?:[\\/].*)?$/); | ||
await esbuild.build({ | ||
entryPoints: [ | ||
'packages/php-wasm/node/src/index.ts', | ||
'packages/php-wasm/node/src/noop.ts', | ||
], | ||
banner: { | ||
js: "import { createRequire as topLevelCreateRequire } from 'module';\n const require = topLevelCreateRequire(import.meta.url);", | ||
}, | ||
plugins: [ | ||
{ | ||
name: 'Support __dirname in ESM', | ||
setup(build) { | ||
build.onLoad({ filter: /.*/ }, ({ path: filePath }) => { | ||
if (!filePath.match(nodeModules)) { | ||
let contents = fs.readFileSync(filePath, 'utf8'); | ||
const loader = path.extname(filePath).substring(1); | ||
const dirname = path.dirname(filePath); | ||
contents = contents | ||
.replaceAll('__dirname', `"${dirname}"`) | ||
.replaceAll('__filename', `"${filePath}"`); | ||
return { | ||
contents, | ||
loader, | ||
}; | ||
} | ||
}); | ||
}, | ||
}, | ||
], | ||
outdir: 'dist/packages/php-wasm/node', | ||
platform: 'node', | ||
assetNames: '[name]', | ||
chunkNames: '[name]', | ||
logOverride: { | ||
'commonjs-variable-in-esm': 'silent', | ||
}, | ||
packages: 'external', | ||
bundle: true, | ||
tsconfig: 'packages/php-wasm/node/tsconfig.json', | ||
external: ['@php-wasm/*', '@wp-playground/*', 'ws', 'fs', 'path'], | ||
supported: { | ||
'dynamic-import': true, | ||
'top-level-await': true, | ||
}, | ||
format: 'esm', | ||
loader: { | ||
'.php': 'text', | ||
'.ini': 'file', | ||
'.wasm': 'file', | ||
}, | ||
}); | ||
|
||
fs.copyFileSync( | ||
'packages/php-wasm/node/README.md', | ||
'dist/packages/php-wasm/node/README.md' | ||
); | ||
} | ||
build(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters