【问题标题】:Using Webpack's Coffee-Loader without explicitly stating ".coffee" file extension在不明确声明“.coffee”文件扩展名的情况下使用 Webpack 的 Coffee-Loader
【发布时间】:2017-08-25 05:10:26
【问题描述】:

前言

我目前正在将我们的构建过程从 Browserify 切换到 Webpack。由于该项目使用了大量的咖啡脚本,我有许多导入语句,例如:

require('./coffee-file-without-extension') # note the lack of .coffee
require('./legacy-js-file-without-extension') # note the lack of .js

问题

Browserify 可以很好地处理缺少文件扩展名的情况。 Webpack 似乎有这个错误的问题:

找不到模块:错误:无法解析“/Users/jusopi/Dev/Workspaces/nx/nx-ui/src”中的“./wptest-req”

我为此设置了一个超级简单的测试项目,其中包含以下文件:

wptest.coffee

require('./wptest-req')

wptest-req.coffee

module.exports = {}

webpack.config.js

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

module.exports = {

  entry: {
    main: './src/wptest.coffee'
  },

  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist')
  },

  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common' // Specify the common bundle's name.
   })
  ],

  module: {
    rules: [
        {
            test: /\.coffee$/,
            use: [
                {
                    loader: 'coffee-loader',
                    options: { sourceMap: true }
                }
            ]   
        }
    ]
  }
};

最终目标

希望如果可能的话,我不必检查应用程序中的每个文件并将.coffee附加到咖啡文件的所有要求语句中。

【问题讨论】:

    标签: build webpack coffeescript


    【解决方案1】:

    虽然此解决方案并非特定于咖啡装载机,但它确实解决了我的问题。我需要在我的配置中添加一个resolve 对象:

    const path = require('path');
    const webpack = require('webpack')
    
    module.exports = {
    
        entry: {
            main: './src/main.coffee'
            // other: './src/index2.js'
        },
    
        output: {
            filename: '[name].js',
            path: path.resolve(__dirname, 'dist')
        },
    
        plugins: [
            new webpack.optimize.CommonsChunkPlugin({
              name: 'common' // Specify the common bundle's name.
            })
        ],
    
        module: {
            rules: [
                {
                    test: /\.coffee$/,
                    use: [
                        {
                            loader: 'coffee-loader',
                            options: { sourceMap: true }
                        }
                    ]
                }
            ]
        },
    
        resolve: {
            extensions: [ '.coffee', '.js' ]
        }
    };
    

    src - https://github.com/webpack-contrib/coffee-loader/issues/36

    【讨论】:

      猜你喜欢
      • 2019-08-31
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 2016-09-27
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多