-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Add support for CJS files #12700
Comments
This breaks a few different packages for me. I need to go in and directly modify the This issue was referenced back in 2020, but was closed as completed for some reason: #8754 |
For anyone who needs CJS support, add const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier');
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
module.exports = function override(config, webpackEnv) {
console.log('overriding webpack config...');
const isEnvDevelopment = webpackEnv === 'development';
const isEnvProduction = webpackEnv === 'production';
const loaders = config.module.rules[1].oneOf;
loaders.splice(loaders.length - 1, 0, {
test: /\.(js|mjs|cjs)$/,
exclude: /@babel(?:\/|\\{1,2})runtime/,
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
configFile: false,
compact: false,
presets: [
[
require.resolve('babel-preset-react-app/dependencies'),
{ helpers: true },
],
],
cacheDirectory: true,
// See #6846 for context on why cacheCompression is disabled
cacheCompression: false,
// @remove-on-eject-begin
cacheIdentifier: getCacheIdentifier(
isEnvProduction
? 'production'
: isEnvDevelopment && 'development',
[
'babel-plugin-named-asset-import',
'babel-preset-react-app',
'react-dev-utils',
'react-scripts',
]
),
// @remove-on-eject-end
// Babel sourcemaps are needed for debugging into node_modules
// code. Without the options below, debuggers like VSCode
// show incorrect code and set breakpoints on the wrong lines.
sourceMaps: shouldUseSourceMap,
inputSourceMap: shouldUseSourceMap,
},
});
return config;
}; Then replace your the
Fixed it for me. |
I would recommend using https://github.com/dilanx/craco/ instead, it is better maintained and provide more flexibility. |
I can confirm the issue and here's the work around to prove it, would be good to have CRA updated to include CJS. When I change webpack.config.js (adding cjs to the mix) within the node_modules of my CRA app I no longer receive undefined error when loading my dependency which uses .cjs.
|
I adapted @nyan-left's workaround for use with // craco.config.ts
export default {
webpack: {
configure: config => ({
...config,
module: {
...config.module,
rules: config.module.rules.map(rule => {
if (rule.oneOf instanceof Array) {
// eslint-disable-next-line no-param-reassign
rule.oneOf[rule.oneOf.length - 1].exclude = [
/\.(js|mjs|jsx|cjs|ts|tsx)$/,
/\.html$/,
/\.json$/,
];
}
return rule;
}),
},
}),
},
}; However, I still think this is a bug in |
Hi. I spent couple of hours with solving nothing expanatory error message. I was trying to use my package with dependency on Following workaround above, my project works, but we have like 20 projects depended on our custom package and all of them are built with create-react-app. |
Describe the bug
If a dependency contains a
.cjs
file, then said file is not processed by babel due to the default webpack config, meaning it will not be loaded.Also, some
cjs
file are not excluded when needed: #11889Did you try recovering your dependencies?
Tried but did not help
Which terms did you search for in User Guide?
CJS, ESM, commonjs
Environment
Steps to reproduce
Use any npm package that contains
*.cjs
files.Expected behavior
The
.cjs
files are correctly loaded and the functions, variable, classes, defined in them are available.Actual behavior
The
.cjs
files notloaded and the functions, variable, classes, defined in them areundefined
.Reproducible demo
I don't believe this is necessary as there at least 5 PRs opened to fix this issue:
Related issues
The text was updated successfully, but these errors were encountered: