Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ERR_PACKAGE_PATH_NOT_EXPORTED when react 18 is installed in project #242

Merged
merged 7 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Transpiler should keep names of files, even if special chars with a simple setup and import correctly 1`] = `
"'use strict';

var jsxRuntime = require('react/cjs/react-jsx-runtime.production.min');
var jsxRuntime = require('/full/path/to/react/cjs/react-jsx-runtime.production.min.js');
require('source-map-support/register');
var path = require('path');

Expand Down Expand Up @@ -36,7 +36,7 @@ exports[`Transpiler should keep names of files, even if special chars with a sim
exports[`Transpiler should transpile CommonJS files with a simple setup and import correctly 1`] = `
"'use strict';

var jsxRuntime = require('react/cjs/react-jsx-runtime.production.min');
var jsxRuntime = require('/full/path/to/react/cjs/react-jsx-runtime.production.min.js');
require('source-map-support/register');

/* eslint-disable no-undef */
Expand Down Expand Up @@ -67,7 +67,7 @@ exports[`Transpiler should transpile CommonJS files with a simple setup and impo
exports[`Transpiler should transpile ES5 files with a simple setup and import correctly 1`] = `
"'use strict';

var jsxRuntime = require('react/cjs/react-jsx-runtime.production.min');
var jsxRuntime = require('/full/path/to/react/cjs/react-jsx-runtime.production.min.js');
require('source-map-support/register');
var path = require('path');

Expand Down Expand Up @@ -102,7 +102,7 @@ exports[`Transpiler should transpile ES5 files with a simple setup and import co
exports[`Transpiler should transpile ES6 files with a simple setup and import correctly 1`] = `
"'use strict';

var jsxRuntime = require('react/cjs/react-jsx-runtime.production.min');
var jsxRuntime = require('/full/path/to/react/cjs/react-jsx-runtime.production.min.js');
require('source-map-support/register');
var path = require('path');

Expand Down
25 changes: 17 additions & 8 deletions src/transpiler/__tests__/transpiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ describe('Transpiler', () => {

test('and import correctly', async () => {
const content = await readFile(commonjs_testFile, 'utf8');
expect(switchToUnixLinebreaks(content)).toMatchSnapshot();
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(content))).toMatchSnapshot();
const mapContent = await readFile(commonjs_testFileMap, 'utf8');
expect(switchToUnixLinebreaks(mapContent)).toMatchSnapshot();
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(mapContent))).toMatchSnapshot();
expect(await import(commonjs_testFile)).toBeDefined();
});

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

test('and import correctly', async () => {
const content = await readFile(es5_testFile, 'utf8')
expect(switchToUnixLinebreaks(content)).toMatchSnapshot();
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(content))).toMatchSnapshot();
const mapContent = await readFile(es5_testFileMap, 'utf8');
expect(switchToUnixLinebreaks(mapContent)).toMatchSnapshot();
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(mapContent))).toMatchSnapshot();
expect(await import(es5_testFile)).toBeDefined();
});

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

test('and import correctly', async () => {
const content = await readFile(es6_testFile, 'utf8')
expect(switchToUnixLinebreaks(content)).toMatchSnapshot();
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(content))).toMatchSnapshot();
const mapContent = await readFile(es6_testFileMap, 'utf8');
expect(switchToUnixLinebreaks(mapContent)).toMatchSnapshot();
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(mapContent))).toMatchSnapshot();
expect(await import(es6_testFile)).toBeDefined();
});

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

test('and import correctly', async () => {
const content = await readFile(special_testFile, 'utf8');
expect(switchToUnixLinebreaks(content)).toMatchSnapshot();
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(content))).toMatchSnapshot();
const mapContent = await readFile(special_testFileMap, 'utf8');
expect(switchToUnixLinebreaks(mapContent)).toMatchSnapshot();
expect(stripAbsolutePathToReactLib(switchToUnixLinebreaks(mapContent))).toMatchSnapshot();
expect(await import(special_testFile)).toBeDefined();
});

Expand All @@ -112,4 +112,13 @@ describe('Transpiler', () => {
*/
function switchToUnixLinebreaks(str: String) {
return str.replace(/\\r/g, "")
}

/*
The transpiler embeds the absolute path to the react library.
We need to replace this in snapshots with something that will be stable across developer environments.
*/
function stripAbsolutePathToReactLib(str: String) {
const reactPath = require.resolve('react/cjs/react-jsx-runtime.production.min')
return str.replace(reactPath, "/full/path/to/react/cjs/react-jsx-runtime.production.min.js")
}
2 changes: 1 addition & 1 deletion src/transpiler/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function transpileFiles(directory: string, outputDir: string, optio
dir: outputDir,
exports: "auto",
paths: {
'react/jsx-runtime': 'react/cjs/react-jsx-runtime.production.min',
'react/jsx-runtime': require.resolve('react/cjs/react-jsx-runtime.production.min'),
},
sanitizeFileName: false,
})
Expand Down
Loading