gpt4 book ai didi

reactjs - 项目目录之外的 node_modules 不会被 Babel 转译 (7)

转载 作者:行者123 更新时间:2023-12-03 14:24:57 25 4
gpt4 key购买 nike

我正在编写一个基于 Symfony4ReactJS 的应用程序,并使用 webpack encore 来构建 bundle 。另外,node_modules 目录位于项目外部,并且是符号链接(symbolic link)的 - 可能这是罪魁祸首,但我需要将 node_modules 保留在项目目录之外。

我遇到的问题与使用一些由 React 组件/HOC 组成的 npm 包(例如 @foes/react-i18n-routing)有关,因此需要对它们进行转译,但是 babel不这样做,结果我收到类似于此的错误:

error  in ./node_modules/@foes/react-i18n-routing/dist/esm/component/withI18nRouting.js

Module parse failed: Unexpected token (6:2)
You may need an appropriate loader to handle this file type.
|
| export default Component => props => (
> <I18nRoutingContext.Consumer>{context => <Component i18nRouting={context} {...props} />}</I18nRoutingContext.Consumer>
| );

据我所知,当我使用 Babel 7 时,我需要将 .babelrc 文件更改为 babel.config.js提供广泛的项目配置,但这还不够,我仍然面临这个问题。

谁能给我指出正确的方向吗?

我尝试了各种配置,但我停止了设置 babelrcRoots 。也许这是关键,但我无法正确设置它。

目录结构如下:

ROOT/
├── node_modules/
└── project/
├── assets/
├── package.json
├── babel.config.js
└── webpack.config.js

这是babel.config.js

module.exports = function(api) {
api.cache(true);

const presets = ['@babel/preset-env', '@babel/preset-react'];
const plugins = [
'@babel/plugin-proposal-class-properties',
'transform-es2015-modules-commonjs',
'babel-plugin-dynamic-import-node'
];

// const babelrcRoots = ['.', '../node_modules/*']; ????

return {
presets,
plugins
};
};


这是webpack.config.js

const Encore = require('@symfony/webpack-encore');
const path = require('path');

Encore
.setOutputPath('public/build/')
.setPublicPath('/build')
.addEntry('app', './assets/js/app.jsx')
.splitEntryChunks()
.enableSingleRuntimeChunk()
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
.enableLessLoader()
.enableReactPreset();


let config = Encore.getWebpackConfig();
config.resolve.alias['App'] = path.resolve(__dirname, 'assets/js');
// config.resolve.modules = [path.resolve('./node_modules'), path.resolve('../node_modules')]; here's the another try with no luck

module.exports = config;

这是package.json

{
"devDependencies": {
"@babel/preset-react": "^7.0.0",
"@symfony/webpack-encore": "^0.27.0",
"babel-plugin-dynamic-import-node": "^2.2.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"core-js": "^3.0.0",
"less-loader": "^4.1.0",
"prop-types": "^15.7.2",
"webpack-notifier": "^1.6.0"
},
"dependencies": {
"@babel/plugin-proposal-class-properties": "^7.4.4",
"@foes/react-i18n-routing": "^0.8.0",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.12.1",
"history": "^4.9.0",
"lodash.flatmap": "^4.5.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.0",
"react-router-named-routes": "0.0.23"
}
}


最佳答案

您可以尝试配置提到的 rootMode:"upward" 设置 here像这样:

babel --root-mode upward src -d lib

或者在 webpack 中

module: {
rules: [{
loader: "babel-loader",
options: {
rootMode: "upward",
}
}]
}

关于reactjs - 项目目录之外的 node_modules 不会被 Babel 转译 (7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56144968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com