Skip to content

Commit 55e2186

Browse files
authored
Merge pull request #419 from jeffijoe/fix/esm-again-fr
fix(esm): provide separate CJS and ESM versions of load-module-native
2 parents 3ea1051 + 084e5ce commit 55e2186

7 files changed

Lines changed: 33 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# v13.0.3
2+
3+
- Fix CJS environments (e.g. Jest + esbuild) failing to `require()` the `.mjs` native loader by providing separate CJS and ESM versions of `load-module-native` ([#418](https://github.com/jeffijoe/awilix/issues/418), [#419](https://github.com/jeffijoe/awilix/pull/419))
4+
15
# v13.0.2
26

37
- Fix ESM interop by renaming `load-module-native.js` to `load-module-native.mjs` so Node.js treats it as ESM regardless of the package `type` field ([#416](https://github.com/jeffijoe/awilix/issues/416), [#417](https://github.com/jeffijoe/awilix/pull/417))

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "awilix",
3-
"version": "13.0.2",
3+
"version": "13.0.3-alpha.0",
44
"description": "Extremely powerful dependency injection container.",
55
"main": "lib/awilix.js",
66
"module": "lib/awilix.module.mjs",
@@ -48,7 +48,7 @@
4848
"cover": "npm run test -- --coverage",
4949
"publish:pre": "npm run lint && npm run build && npm run cover",
5050
"publish:post": "npm publish && git push --follow-tags",
51-
"release:prerelease": "npm run publish:pre && npm version prerelease --preid alpha && npm run publish:post",
51+
"release:prerelease": "npm run publish:pre && npm version prerelease --preid alpha && npm publish --tag alpha && git push --follow-tags",
5252
"release:patch": "npm run publish:pre && npm version patch && npm run publish:post",
5353
"release:minor": "npm run publish:pre && npm version minor && npm run publish:post",
5454
"release:major": "npm run publish:pre && npm version major && npm run publish:post"
@@ -148,6 +148,7 @@
148148
"__tests__",
149149
"lib",
150150
"src/load-module-native.mjs",
151+
"src/load-module-native.js",
151152
"src/awilix.ts"
152153
],
153154
"moduleFileExtensions": [

rollup.config.mjs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,7 @@ export default [
2525
// Build 1: ES modules for Node.
2626
{
2727
input: 'src/awilix.ts',
28-
external: [
29-
'fast-glob',
30-
'path',
31-
'url',
32-
'util',
33-
'./load-module-native.mjs',
34-
],
28+
external: ['fast-glob', 'path', 'url', 'util'],
3529
treeshake: { moduleSideEffects: 'no-external' },
3630
onwarn,
3731
output: [
@@ -41,9 +35,23 @@ export default [
4135
},
4236
],
4337
plugins: [
44-
// Copy the native module loader
38+
// The source imports ./load-module-native.js (the CJS version) so that
39+
// tsc emits require("./load-module-native.js") in the CJS build.
40+
// For this ESM bundle we remap it to the .mjs version.
41+
// Returning a relative id with external: true tells rollup to preserve
42+
// the path as-is in the output (see https://rollupjs.org/plugin-development/#resolveid).
43+
{
44+
name: 'rewrite-native-loader',
45+
resolveId(source) {
46+
if (source === './load-module-native.js') {
47+
return { id: './load-module-native.mjs', external: true }
48+
}
49+
return null
50+
},
51+
},
52+
// Copy the native module loaders and their type declarations to lib/
4553
copy({
46-
targets: [{ src: 'src/load-module-native.mjs', dest: 'lib' }],
54+
targets: [{ src: 'src/load-module-native.*', dest: 'lib' }],
4755
}),
4856
typescript(tsOpts),
4957
],

src/container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import { type InjectionModeType, InjectionMode } from './injection-mode'
88
import { type LifetimeType, Lifetime, isLifetimeLonger } from './lifetime'
99
import { type GlobWithOptions, listModules } from './list-modules'
10-
import { importModule } from './load-module-native.mjs'
10+
import { importModule } from './load-module-native.js'
1111
import {
1212
type LoadModulesOptions,
1313
type LoadModulesResult,

src/load-module-native.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function importModule(path: string): Promise<any>

src/load-module-native.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// This is kept in a separate .js file to prevent TypeScript re-writing the import() statement to a require() statement
2+
// This is the CJS version; the .mjs version is used by the ESM build.
3+
module.exports.importModule = function importModule(path) {
4+
return import(path)
5+
}

0 commit comments

Comments
 (0)