2017-08-24 16 views
1

前言

我目前的开关我们构建过程在从Browserify到的WebPack。由于该项目使用咖啡脚本了很多,我有很多import语句,如:使用的WebPack的咖啡装载机但没有明确指出“.coffee”文件扩展名

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

问题

Browserify处理没有文件扩展名就好了。的WebPack似乎已经按照该错误问题:

Module not found: Error: Can't resolve './wptest-req' in '/Users/jusopi/Dev/Workspaces/nx/nx-ui/src'

我建立一个超级简单的测试项目为这个,我有以下文件:

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所有如果可能的话,要求提供咖啡文件的声明。

回答

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 -

相关问题