webpack can't find module if file named jsx

前端 未结 7 955
既然无缘
既然无缘 2020-11-28 06:43

As I write webpack.config.js like this

module.exports = {
  entry: \'./index.jsx\',
  output: {
    filename: \'bundle.js\'
  },
  module: {
    loaders: [{         


        
7条回答
  •  野性不改
    2020-11-28 07:02

    Adding to the above answer,

    The resolve property is where you have to add all the file types you are using in your application.

    Suppose you want to use .jsx or .es6 files; you can happily include them here and webpack will resolve them:

    resolve: {
      extensions: ["", ".js", ".jsx", ".es6"]
    }
    

    If you add the extensions in the resolve property, you can remove them from the import statement:

    import Hello from './hello'; // Extensions removed
    import World from './world';
    

    Other scenarios like if you want coffee script to be detected you should configure your test property as well like:

    // webpack.config.js
    module.exports = {
      entry: './main.js',
      output: {
        filename: 'bundle.js'       
      },
      module: {
        loaders: [
          { test: /\.coffee$/, loader: 'coffee-loader' },
          {
            test: /\.js$/,
            loader: 'babel-loader',
            query: {
              presets: ['es2015', 'react']
            }
          }
        ]
      },
      resolve: {
        // you can now require('file') instead of require('file.coffee')
        extensions: ['', '.js', '.json', '.coffee'] 
      }
    };
    

提交回复
热议问题