Skip to content
This repository was archived by the owner on Jul 29, 2025. It is now read-only.

Commit 6b948cb

Browse files
authored
fix: ERR_PACKAGE_PATH_NOT_EXPORTED when react 18 is installed in project (#242)
1 parent dcb04fe commit 6b948cb

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/transpiler/__tests__/__snapshots__/transpiler.spec.tsx.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`Transpiler should keep names of files, even if special chars with a simple setup and import correctly 1`] = `
44
"'use strict';
55
6-
var jsxRuntime = require('react/cjs/react-jsx-runtime.production.min');
6+
var jsxRuntime = require('/full/path/to/react/cjs/react-jsx-runtime.production.min.js');
77
require('source-map-support/register');
88
var path = require('path');
99
@@ -36,7 +36,7 @@ exports[`Transpiler should keep names of files, even if special chars with a sim
3636
exports[`Transpiler should transpile CommonJS files with a simple setup and import correctly 1`] = `
3737
"'use strict';
3838
39-
var jsxRuntime = require('react/cjs/react-jsx-runtime.production.min');
39+
var jsxRuntime = require('/full/path/to/react/cjs/react-jsx-runtime.production.min.js');
4040
require('source-map-support/register');
4141
4242
/* eslint-disable no-undef */
@@ -67,7 +67,7 @@ exports[`Transpiler should transpile CommonJS files with a simple setup and impo
6767
exports[`Transpiler should transpile ES5 files with a simple setup and import correctly 1`] = `
6868
"'use strict';
6969
70-
var jsxRuntime = require('react/cjs/react-jsx-runtime.production.min');
70+
var jsxRuntime = require('/full/path/to/react/cjs/react-jsx-runtime.production.min.js');
7171
require('source-map-support/register');
7272
var path = require('path');
7373
@@ -102,7 +102,7 @@ exports[`Transpiler should transpile ES5 files with a simple setup and import co
102102
exports[`Transpiler should transpile ES6 files with a simple setup and import correctly 1`] = `
103103
"'use strict';
104104
105-
var jsxRuntime = require('react/cjs/react-jsx-runtime.production.min');
105+
var jsxRuntime = require('/full/path/to/react/cjs/react-jsx-runtime.production.min.js');
106106
require('source-map-support/register');
107107
var path = require('path');
108108

src/transpiler/__tests__/transpiler.spec.tsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ describe('Transpiler', () => {
3232

3333
test('and import correctly', async () => {
3434
const content = await readFile(commonjs_testFile, 'utf8');
35-
expect(switchToUnixLinebreaks(content)).toMatchSnapshot();
35+
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(content))).toMatchSnapshot();
3636
const mapContent = await readFile(commonjs_testFileMap, 'utf8');
37-
expect(switchToUnixLinebreaks(mapContent)).toMatchSnapshot();
37+
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(mapContent))).toMatchSnapshot();
3838
expect(await import(commonjs_testFile)).toBeDefined();
3939
});
4040

@@ -52,9 +52,9 @@ describe('Transpiler', () => {
5252

5353
test('and import correctly', async () => {
5454
const content = await readFile(es5_testFile, 'utf8')
55-
expect(switchToUnixLinebreaks(content)).toMatchSnapshot();
55+
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(content))).toMatchSnapshot();
5656
const mapContent = await readFile(es5_testFileMap, 'utf8');
57-
expect(switchToUnixLinebreaks(mapContent)).toMatchSnapshot();
57+
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(mapContent))).toMatchSnapshot();
5858
expect(await import(es5_testFile)).toBeDefined();
5959
});
6060

@@ -72,9 +72,9 @@ describe('Transpiler', () => {
7272

7373
test('and import correctly', async () => {
7474
const content = await readFile(es6_testFile, 'utf8')
75-
expect(switchToUnixLinebreaks(content)).toMatchSnapshot();
75+
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(content))).toMatchSnapshot();
7676
const mapContent = await readFile(es6_testFileMap, 'utf8');
77-
expect(switchToUnixLinebreaks(mapContent)).toMatchSnapshot();
77+
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(mapContent))).toMatchSnapshot();
7878
expect(await import(es6_testFile)).toBeDefined();
7979
});
8080

@@ -92,9 +92,9 @@ describe('Transpiler', () => {
9292

9393
test('and import correctly', async () => {
9494
const content = await readFile(special_testFile, 'utf8');
95-
expect(switchToUnixLinebreaks(content)).toMatchSnapshot();
95+
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(content))).toMatchSnapshot();
9696
const mapContent = await readFile(special_testFileMap, 'utf8');
97-
expect(switchToUnixLinebreaks(mapContent)).toMatchSnapshot();
97+
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(mapContent))).toMatchSnapshot();
9898
expect(await import(special_testFile)).toBeDefined();
9999
});
100100

@@ -110,6 +110,15 @@ describe('Transpiler', () => {
110110
It is a helper required for snapshot testing on windows. It can't be solved by editor configuration and the end line setting because snapshots are generated not created in the editor.
111111
We need to remove `\r` from files transpiled on windows before we can match them with the snapshot generated on unix
112112
*/
113-
function switchToUnixLinebreaks(str: String) {
113+
function switchToUnixLinebreaks(str: string) {
114114
return str.replace(/\\r/g, "")
115+
}
116+
117+
/*
118+
The transpiler embeds the absolute path to the react library.
119+
We need to replace this in snapshots with something that will be stable across developer environments.
120+
*/
121+
function stripAbsolutePathToReactLib(str: string) {
122+
const reactPath = require.resolve('react/cjs/react-jsx-runtime.production.min').replace(/\\/g, '/')
123+
return str.replace(reactPath, "/full/path/to/react/cjs/react-jsx-runtime.production.min.js")
115124
}

src/transpiler/transpiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function transpileFiles(directory: string, outputDir: string, optio
5252
dir: outputDir,
5353
exports: "auto",
5454
paths: {
55-
'react/jsx-runtime': 'react/cjs/react-jsx-runtime.production.min',
55+
'react/jsx-runtime': require.resolve('react/cjs/react-jsx-runtime.production.min').replace(/\\/g, '/'),
5656
},
5757
sanitizeFileName: false,
5858
})

0 commit comments

Comments
 (0)