[英]How to include a few node_modules package in babel-node
I'm trying to include @mycompany/package1, and @mycompany/package2 to be compiled along with the rest of my code using babel-node
. 我试图包括@ mycompany / package1和@ mycompany / package2,以及使用
babel-node
进行其余代码的编译。 Since package1 and package2 are in ES6. 由于package1和package2在ES6中。 (Also note I'm not using Webpack)
(还请注意,我没有使用Webpack)
In my jest
config I added the below option into my jest config which works fine. 在我的
jest
配置中,我将以下选项添加到了我的jest配置中,效果很好。 When testing the code will compile the packages correctly 测试代码时,将正确编译软件包
"transformIgnorePatterns": [
"/node_modules/(?!(@mycompany)/).*/"
],
But when trying to run babel-node I get errors. 但是,当尝试运行babel-node时,我得到了错误。 In my babel.config.js
在我的babel.config.js中
module.exports = {
presets: [
'@babel/preset-flow',
[
'@babel/preset-env',
{
targets: {
node: 8
}
}
]
],
plugins: ['@babel/plugin-proposal-class-properties']
};
I tried adding the below code to my babel.config.js but it still complains about ES6 errors within my node_modules/@mycompany/package1 我尝试将以下代码添加到babel.config.js中,但仍然抱怨node_modules / @ mycompany / package1中的ES6错误
I tried to include
the viz package but then babel wouldn't compile my src files 我尝试
include
viz软件包,但babel不会编译我的src文件
include: [path.resolve(__dirname, 'node_modules/@mycompany/package1')]
include: ['/node_modules/((@mycompany)/).*/']
I tried to exclude
everything but @mycompany packages but I still get transpile errors in my package1 我尝试
exclude
@mycompany软件包以外的所有内容,但在package1中仍然出现可移植错误
exclude: [/node_modules\\/(?!(@mycompany)\\/).*/],
I tried playing with ignore but those don't seem like they are the right options based on reading the docs 我尝试过玩无视,但基于阅读文档,这些似乎不是正确的选择
I found out that we can do this with webpack
to help bundle the packages with the rest of your code. 我发现我们可以使用
webpack
来做到这一点,以帮助将软件包与您的其余代码捆绑在一起。
This is my webpack file for NodeJS. 这是我的NodeJS的webpack文件。
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const webpack = require('webpack');
const spawn = require('child_process').spawn;
const nodeEnv = process.env.NODE_ENV;
const isProduction = nodeEnv === 'production';
const compiler = webpack({
entry: ['@babel/polyfill', './src/server.js'],
output: {
path: path.resolve(__dirname, 'lib'),
filename: 'server.bundle.js',
libraryTarget: 'commonjs2'
},
externals: [
nodeExternals({
whitelist: [/@mycompany\/.*/]
})
],
plugins: plugins,
target: 'node',
mode: 'development',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules\/(?!(@mycompany)\/).*/,
use: {
loader: 'babel-loader',
options: {
configFile: './babel.config.js'
}
}
}
]
}
});
if (isProduction) {
compiler.run((err, stats) => {
if (err) {
console.error(err);
return;
}
console.log(
stats.toString({
colors: true
})
);
});
} else {
let serverControl;
compiler.watch(
{
aggregateTimeout: 300,
poll: 1000
},
(err, stats) => {
if (serverControl) {
serverControl.kill();
}
if (err) {
console.error(err);
return;
}
console.log(
stats.toString({
colors: true
})
);
// change app.js to the relative path to the bundle created by webpack, if necessary
serverControl = spawn('node', [
path.resolve(__dirname, 'lib/server.bundle.js')
]);
serverControl.stdout.on('data', data => console.log(data.toString()));
serverControl.stderr.on('data', data => console.error(data.toString()));
}
);
}
Note the most important part is
注意最重要的部分是
- Adding
webpack-node-externals
.添加
webpack-node-externals
。 Since this is anode.js
server we don't need to bundle the node_modules.由于这是一个
node.js
服务器,因此我们不需要捆绑node_modules。- Make sure you
whitelist
your package that you need to be compiled/bundled and also make sure you have your packages included to be compiled in yourbabel-loader
确保将需要编译/捆绑的软件包列入
whitelist
,并确保包含要打包在babel-loader
软件包
nodeExternal
tells webpack
know not to bundle ANY node_modules. nodeExternal
告诉webpack
知道不捆绑任何node_modules。 whitelist
is saying that we should bundle the packages we listed whitelist
是说我们应该捆绑我们列出的软件包
externals: [ nodeExternals({ whitelist: [/@mycompany\\/.*/] }) ]
This line means to exclude all node_modules EXCEPT @mycompany/* packages 该行表示排除@ mycompany / *包以外的所有node_modules
exclude: /node_modules\\/(?!(@mycompany)\\/).*/,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.