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(bundling): detect esm vs cjs type for .js files #14060

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion e2e/react/src/react.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,26 @@ describe('React Applications', () => {
it('should be able to use JS and JSX', async () => {
const appName = uniq('app');
const libName = uniq('lib');
const plainJsLib = uniq('jslib');

runCLI(
`generate @nrwl/react:app ${appName} --bundler=webpack --no-interactive --js`
);
runCLI(
`generate @nrwl/react:lib ${libName} --no-interactive --js --unit-test-runner=none`
);
// Make sure plain JS libs can be imported as well.
// There was an issue previously: https://github.com/nrwl/nx/issues/10990
runCLI(
`generate @nrwl/js:lib ${plainJsLib} --js --unit-test-runner=none --bundler=none --compiler=tsc --no-interactive`
);

const mainPath = `apps/${appName}/src/main.js`;
updateFile(
mainPath,
`import '@${proj}/${libName}';\n${readFile(mainPath)}`
`import '@${proj}/${libName}';\nimport '@${proj}/${plainJsLib}';\n${readFile(
mainPath
)}`
);

await testGeneratedApp(appName, {
Expand Down
7 changes: 7 additions & 0 deletions packages/webpack/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ export function getBaseWebpackPartial(
fullySpecified: false,
},
},
// There's an issue when using buildable libs and .js files (instead of .ts files),
// where the wrong type is used (commonjs vs esm) resulting in export-imports throwing errors.
// See: https://github.com/nrwl/nx/issues/10990
{
test: /\.js$/,
type: 'javascript/auto',
},
createLoaderFromCompiler(options, internalOptions),
].filter(Boolean),
},
Expand Down