如何在babel-node中包含一些node_modules包 [英] How to include a few node_modules package in babel-node

查看:2123
本文介绍了如何在babel-node中包含一些node_modules包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图包含@ mycompany/package1和@ mycompany/package2以及使用babel-node的其余代码进行编译.由于package1和package2在ES6中. (还请注意,我没有使用Webpack)

I'm trying to include @mycompany/package1, and @mycompany/package2 to be compiled along with the rest of my code using babel-node. Since package1 and package2 are in ES6. (Also note I'm not using Webpack)

在我的jest配置中,我在jest配置中添加了以下选项,该选项可以正常工作.在测试代​​码时,将正确编译软件包

In my jest config I added the below option into my jest config which works fine. When testing the code will compile the packages correctly

"transformIgnorePatterns": [
  "/node_modules/(?!(@mycompany)/).*/"
],

但是当尝试运行babel-node时出现错误. 在我的babel.config.js中

But when trying to run babel-node I get errors. In my babel.config.js

module.exports = {
  presets: [
    '@babel/preset-flow',
    [
      '@babel/preset-env',
      {
        targets: {
          node: 8
        }
      }
    ]
  ],
  plugins: ['@babel/plugin-proposal-class-properties']
};

我尝试将以下代码添加到babel.config.js中,但它仍然抱怨node_modules/@ mycompany/package1中的ES6错误

I tried adding the below code to my babel.config.js but it still complains about ES6 errors within my node_modules/@mycompany/package1

我尝试include viz软件包,但是babel无法编译我的src文件

I tried to include the viz package but then babel wouldn't compile my src files

include: [path.resolve(__dirname, 'node_modules/@mycompany/package1')]

include: ['/node_modules/((@mycompany)/).*/']

我尝试exclude除@mycompany软件包以外的所有内容,但在我的package1中仍然出现可移植错误

I tried to exclude everything but @mycompany packages but I still get transpile errors in my package1

exclude: [/node_modules\/(?!(@mycompany)\/).*/],

我尝试过使用ignore,但是基于阅读文档,这些选项似乎并不是正确的选择

I tried playing with ignore but those don't seem like they are the right options based on reading the docs

推荐答案

我发现我们可以使用webpack来做到这一点,以帮助将软件包与您的其余代码捆绑在一起.

I found out that we can do this with webpack to help bundle the packages with the rest of your code.

这是我的NodeJS的webpack文件.

This is my webpack file for NodeJS.

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

  1. 添加webpack-node-externals.由于这是一个node.js服务器,因此我们不需要捆绑node_modules.
  2. 确保您要打包/bundle的软件包whitelist,并且还确保要包含要打包在babel-loader
  3. 中的软件包.
  1. Adding webpack-node-externals. Since this is a node.js server we don't need to bundle the node_modules.
  2. 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 your babel-loader

  • nodeExternal告诉webpack知道不捆绑任何node_modules.
  • whitelist表示我们应该捆绑列出的软件包

    • nodeExternal tells webpack know not to bundle ANY node_modules.
    • whitelist is saying that we should bundle the packages we listed

      externals: [ nodeExternals({ whitelist: [/@mycompany\/.*/] }) ]

      externals: [ nodeExternals({ whitelist: [/@mycompany\/.*/] }) ]

      该行表示排除@ mycompany/*软件包以外的所有node_modules

      This line means to exclude all node_modules EXCEPT @mycompany/* packages

      exclude: /node_modules\/(?!(@mycompany)\/).*/,

      这篇关于如何在babel-node中包含一些node_modules包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆