Skip to content

Commit eb8dc04

Browse files
authored
feat: Make export paths compatible with Browserify (#533)
Ref: #461 The MetaMask extension is built for production using Browserify. Due to its age, Browserify ignores the `exports` field of `package.json`. Instead, when it encounters an import like `@metamask/streams/browser`, it will search the root directory of that package in `node_modules` for a file like `browser.js`. This PR adds such "mirror files" to all of our `@metamask/*` packages with export paths. For prior art, see: MetaMask/providers#351 In addition, we consolidate Vite's "assets" into the root output directory in `kernel-browser-runtime`. This turned out not be a necessary change, but I like it nevertheless. (The single file in that directory isn't an "asset", it's just a `.js` file.)
1 parent 1559111 commit eb8dc04

11 files changed

Lines changed: 39 additions & 7 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint-disable import-x/unambiguous */
2+
// Re-exported for compatibility with Browserify.
3+
module.exports = require('./dist/default-cluster.json');

packages/kernel-browser-runtime/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"main": "./dist/index.cjs",
3737
"types": "./dist/index.d.cts",
3838
"files": [
39-
"dist/"
39+
"dist/",
40+
"default-cluster.js"
4041
],
4142
"scripts": {
4243
"build": "yarn build:ts && yarn build:vite",

packages/kernel-browser-runtime/vite.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export default defineConfig(({ mode }) => {
4949
base: './',
5050

5151
build: {
52+
assetsDir: './',
5253
emptyOutDir: true,
5354
outDir: buildDir,
5455
// Disable Vite's module preload, which may cause SES-dependent code to run before lockdown.
@@ -119,8 +120,8 @@ export default defineConfig(({ mode }) => {
119120
generateBundle(_, bundle) {
120121
const extraneousAssets = Object.values(bundle).filter(
121122
(assetOrChunk) =>
122-
assetOrChunk.fileName.startsWith('assets/sqlite3-') &&
123-
!assetOrChunk.fileName.includes('opfs-async-proxy'),
123+
assetOrChunk.fileName.startsWith('sqlite3-') &&
124+
!assetOrChunk.fileName.includes('sqlite3-opfs-async-proxy'),
124125
);
125126

126127
if (extraneousAssets.length !== 2) {

packages/kernel-store/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"module": "./dist/index.mjs",
5656
"types": "./dist/index.d.cts",
5757
"files": [
58-
"dist/"
58+
"dist/",
59+
"sqlite/"
5960
],
6061
"scripts": {
6162
"build": "ts-bridge --project tsconfig.build.json --clean",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint-disable import-x/unambiguous */
2+
// Re-exported for compatibility with Browserify.
3+
module.exports = require('../dist/sqlite/nodejs.cjs');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint-disable import-x/unambiguous */
2+
// Re-exported for compatibility with Browserify.
3+
module.exports = require('../dist/sqlite/wasm.cjs');

packages/ocap-kernel/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"module": "./dist/index.mjs",
4646
"types": "./dist/index.d.cts",
4747
"files": [
48-
"dist/"
48+
"dist/",
49+
"rpc.js"
4950
],
5051
"scripts": {
5152
"build": "ts-bridge --project tsconfig.build.json --clean",

packages/ocap-kernel/rpc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint-disable import-x/unambiguous */
2+
// Re-exported for compatibility with Browserify.
3+
module.exports = require('./dist/rpc/index.cjs');

packages/streams/browser.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint-disable import-x/unambiguous */
2+
// Re-exported for compatibility with Browserify.
3+
module.exports = require('./dist/browser/index.cjs');

packages/streams/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"module": "./dist/index.mjs",
4646
"types": "./dist/index.d.cts",
4747
"files": [
48-
"dist/"
48+
"dist/",
49+
"browser.js"
4950
],
5051
"scripts": {
5152
"build": "ts-bridge --project tsconfig.build.json --clean",

0 commit comments

Comments
 (0)