简体   繁体   中英

Can Babel Apply to Selected Files in Webpack Config?

Here is my webpack.config (using webpack 3) below

I have several .js files in entry .

I'd like to find a way to transpile select .js files through babel while excluding others.

I found a way to include all the .js files in the js/ directory through babel.. But how can I exclude the .js files in js/vendor?

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'}
        }
      }
    ]
  },

include can accept a function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM