我正在基于Symfony4
和ReactJS
编写应用程序,并且正在使用Webpack Encore构建捆绑包。此外,node_modules
目录在项目外部并且是符号链接的-可能是主要的罪魁祸首,但是我需要将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"
}
}
答案 0 :(得分:1)
您可以尝试将here中提到的rootMode:"upward"
设置为:
babel --root-mode upward src -d lib
或在webpack中
module: {
rules: [{
loader: "babel-loader",
options: {
rootMode: "upward",
}
}]
}