Skip to content

Commit

Permalink
fix: ERR_PACKAGE_PATH_NOT_EXPORTED when react 18 is installed in pr…
Browse files Browse the repository at this point in the history
…oject (#242)
  • Loading branch information
Julusian committed Sep 11, 2024
1 parent dcb04fe commit 6b948cb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
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
27 changes: 18 additions & 9 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 @@ -110,6 +110,15 @@ describe('Transpiler', () => {
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.
We need to remove `\r` from files transpiled on windows before we can match them with the snapshot generated on unix
*/
function switchToUnixLinebreaks(str: String) {
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').replace(/\\/g, '/')
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').replace(/\\/g, '/'),
},
sanitizeFileName: false,
})
Expand Down

0 comments on commit 6b948cb

Please sign in to comment.