如何仅使用 babel 7 转换 node_modules 文件夹?
How to transpile node_modules folder using just babel 7?
尝试使用 mocha 对 reactjs 应用程序进行单元测试,但在 node_modules
文件夹中使用的 es6 功能 (import/export) 出现错误。有问题的应用程序是使用 babel 转译的,但是由于其中一个反应组件正在使用来自 node_modules
的模块,它的抛出错误 <strong>Syntax Error:Unexpected token export</strong>
。我知道 babel 默认会忽略 node_modules 文件夹,但不确定如何处理。任何帮助,将不胜感激。谢谢。
测试命令:-
"test": "SET NODE_ENV=test&& mocha --require @babel/register --require ignore-styles -r jsdom-global/register \"./src/**/*.test.js\"",
babel.config.js :-
module.exports = function (api) {
const presets = ["react-app"];
const plugins = [
"@babel/plugin-transform-modules-commonjs",
"inline-react-svg"
];
const ignore = [/node_modules/]
api.cache(false);
return {
presets,
plugins,
ignore
}
};
试试这个命令,你需要在命令行中使用 --ignored
选项传递被忽略的目录。
./node_modules/.bin/babel . -d ~/app_compressed/ --ignore node_modules,test,assets,stuff,views,public,test,spec,logs,lib/jasmine_examples,db,routes/api/drafts,**/drafts
此外,请确保使用 babel.config.js
文件而不是 .babelrc
文件。
所以配置文件将如下所示。
module.exports = function (api) {
api.cache(true);
return {
babelrcRoots: [
'.',
'./modules/*'
],
ignore: [/node_modules/],
presets: ["@babel/preset-env"]
};
}
成功了!必须从 Mocha 切换到 Jest 因为我使用的是内部配置的 react-app-rewired使用笑话。需要 config-override.js
的小改动。使用 "transformIgnorePatterns": ["node_modules/(?!MODULES_TO_BE_TRANSPILED)"]
添加了 jest 配置。希望对其他人有帮助。
尝试使用 mocha 对 reactjs 应用程序进行单元测试,但在 node_modules
文件夹中使用的 es6 功能 (import/export) 出现错误。有问题的应用程序是使用 babel 转译的,但是由于其中一个反应组件正在使用来自 node_modules
的模块,它的抛出错误 <strong>Syntax Error:Unexpected token export</strong>
。我知道 babel 默认会忽略 node_modules 文件夹,但不确定如何处理。任何帮助,将不胜感激。谢谢。
测试命令:-
"test": "SET NODE_ENV=test&& mocha --require @babel/register --require ignore-styles -r jsdom-global/register \"./src/**/*.test.js\"",
babel.config.js :-
module.exports = function (api) {
const presets = ["react-app"];
const plugins = [
"@babel/plugin-transform-modules-commonjs",
"inline-react-svg"
];
const ignore = [/node_modules/]
api.cache(false);
return {
presets,
plugins,
ignore
}
};
试试这个命令,你需要在命令行中使用 --ignored
选项传递被忽略的目录。
./node_modules/.bin/babel . -d ~/app_compressed/ --ignore node_modules,test,assets,stuff,views,public,test,spec,logs,lib/jasmine_examples,db,routes/api/drafts,**/drafts
此外,请确保使用 babel.config.js
文件而不是 .babelrc
文件。
所以配置文件将如下所示。
module.exports = function (api) {
api.cache(true);
return {
babelrcRoots: [
'.',
'./modules/*'
],
ignore: [/node_modules/],
presets: ["@babel/preset-env"]
};
}
成功了!必须从 Mocha 切换到 Jest 因为我使用的是内部配置的 react-app-rewired使用笑话。需要 config-override.js
的小改动。使用 "transformIgnorePatterns": ["node_modules/(?!MODULES_TO_BE_TRANSPILED)"]
添加了 jest 配置。希望对其他人有帮助。