【问题标题】:Can Babel Apply to Selected Files in Webpack Config?Babel 可以应用到 Webpack Config 中的选定文件吗?
【发布时间】:2020-11-25 19:43:06
【问题描述】:

下面是我的 webpack.config(使用 webpack 3)

我在entry 中有几个.js 文件。

我想找到一种方法,通过 babel 转译 select .js 文件,同时排除其他文件。

我找到了一种方法,通过 babel 将 js/ 目录下的所有 .js 文件包含在内。但是如何排除 js/vendor 中的 .js 文件?

const path = require('path');
const webpack = require('webpack');

module.exports = {
  context: __dirname,

  entry: {
    dataviz : '../js/entry-dataviz.js',
    template : '../js/entry-viz-template.js',
    abc : '../js/entry-viz-abc.js',
    sample: '../js/sample.js',
    vendor: [
      '../js/vendor/bootstrap.min.js',
      '../js/vendor/history.js',
      '../js/vendor/history.adapter.jquery.js'
    ]
  },

  output: {
    filename: '[name]-[chunkhash].js'
  },

  module: {
    rules: [
      {
        test: /\.js$/,
        include: [
          path.resolve(__dirname, '../js'), <-- including everything in js/directory
        ],
        use: {
          loader: 'babel-loader',
          options: { presets: 'es2015'}
        }
      }
    ]
  },

【问题讨论】:

    标签: javascript webpack babeljs babel-loader


    【解决方案1】:

    include可以接受一个函数:

    include: function (modulePath) {
      // inside `js` folder but not `js/vendor`
      return true;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-07
      • 2018-04-29
      • 2016-02-02
      • 2012-08-18
      • 2021-06-11
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 2014-02-13
      相关资源
      最近更新 更多