Skip to content

Commit

Permalink
feat: Add new export for initializeInpageProvider (#391)
Browse files Browse the repository at this point in the history
* feat: Add subpath export for `initializeInpageProvider.cjs`

Add a subpath export for the `initializeInpageProvider.cjs` file. This
allows projects with legacy build systems to import this file directly,
which can be useful for reducing bundle size (especially for build
systems without tree shaking).

The `metamask-extension` project uses two build systems: one which
obeys export maps, one which ignores them (Webpack and browserify
respectively). The existing `initializeInpageProvider` export map entry
doesn't work for browserify because it's looking for
`initializeInpageProvider.js`. The file extension is wrong.

* Use alternative strategy that works better for ESM builds

Add a new top-level export map entry for `initializeProvider` that maps
correctly onto CJS and ESM builds, and add a fallback JavaScript file
at the root of the repository for build systems that don't support
export maps.

This gives the best of both worlds: build systems that support exports
will behave correctly, and those that don't will get the CJS fallback.
  • Loading branch information
Gudahtt authored Nov 27, 2024
1 parent 8464e2b commit 1c4015d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion constraints.pro
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ gen_enforced_field(WorkspaceCwd, 'exports["./package.json"]', './package.json').

% The list of files included in the package must only include files generated
% during the build step.
gen_enforced_field(WorkspaceCwd, 'files', ['dist', 'stream-provider.js']).
gen_enforced_field(WorkspaceCwd, 'files', ['dist', 'initializeInpageProvider.d.ts', 'initializeInpageProvider.js', 'stream-provider.js']).

% If a dependency is listed under "dependencies", it should not be listed under
% "devDependencies".
Expand Down
5 changes: 5 additions & 0 deletions initializeInpageProvider.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable import/extensions */

// Re-exported for compatibility with build tools that don't support the
// `exports` field in package.json
export * from './dist/initializeInpageProvider.cjs';
5 changes: 5 additions & 0 deletions initializeInpageProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable import/extensions,import/no-unresolved */

// Re-exported for compatibility with build tools that don't support the
// `exports` field in package.json
module.exports = require('./dist/initializeInpageProvider.cjs');
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@
"default": "./dist/initializeInpageProvider.cjs"
}
},
"./initializeInpageProvider": {
"import": {
"types": "./dist/initializeInpageProvider.d.mts",
"default": "./dist/initializeInpageProvider.mjs"
},
"require": {
"types": "./dist/initializeInpageProvider.d.cts",
"default": "./dist/initializeInpageProvider.cjs"
}
},
"./stream-provider": {
"import": {
"types": "./dist/StreamProvider.d.mts",
Expand All @@ -66,6 +76,8 @@
"types": "./dist/index.d.cts",
"files": [
"dist",
"initializeInpageProvider.d.ts",
"initializeInpageProvider.js",
"stream-provider.js"
],
"scripts": {
Expand Down

0 comments on commit 1c4015d

Please sign in to comment.