node_modules 项目目录外不被 Babel 转译 (7)
node_modules outside of the project directory are not transpiled by Babel (7)
我正在编写一个基于 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"
}
}
您可以尝试配置 rootMode:"upward"
提到的 here 设置,如下所示:
babel --root-mode upward src -d lib
或者在 webpack 中
module: {
rules: [{
loader: "babel-loader",
options: {
rootMode: "upward",
}
}]
}
我正在编写一个基于 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"
}
}
您可以尝试配置 rootMode:"upward"
提到的 here 设置,如下所示:
babel --root-mode upward src -d lib
或者在 webpack 中
module: {
rules: [{
loader: "babel-loader",
options: {
rootMode: "upward",
}
}]
}