2017-05-29 76 views
3

我正在为仍然使用requireJS进行模块加载的现有项目开发新模块。我正在尝试将新技术用于像webpack这样的新模块(它允许我使用使用es6导入的es6加载器)。似乎webpack无法与requireJS语法协调一致。它会这样说:“未找到模块:错误:无法解析”。带有requirejs/AMD的Webpack

问题:Webpack不会将包含requireJS/AMD语法的文件捆绑在其中。
问题:有没有什么办法让webpack和requireJS一起玩呢?

我的最终输出必须是AMD格式才能正确加载项目。谢谢。

+0

你可以看看一些webpack的babel加载器。我有类似的问题,你通常可以在模块系统之间使用babel –

+0

即时通讯在我的webpack配置中使用babel loader – goldensausage

回答

4

我有同样的问题,我设法实现它。以下是相同的webpack.config.js文件。

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

let basePath = path.join(__dirname, '/'); 

let config = { 
    // Entry, file to be bundled 
    entry: { 
    'main': basePath + '/src/main.js', 
    }, 
    devtool: 'source-map', 
    output: { 
    // Output directory 
    path: basePath + '/dist/', 
    library: '[name]', 
    // [hash:6] with add a SHA based on file changes if the env is build 
    filename: env === EnvEnum.BUILD ? '[name]-[hash:6].min.js' : '[name].min.js', 
    libraryTarget: 'amd', 
    umdNamedDefine: true 
    }, 
    module: { 
    rules: [{ 
     test: /(\.js)$/, 
     exclude: /(node_modules|bower_components)/, 
     use: { 
     // babel-loader to convert ES6 code to ES5 + amdCleaning requirejs code into simple JS code, taking care of modules to load as desired 
     loader: 'babel-loader', 
     options: { 
      presets: ['es2015'], 
      plugins: [] 
     } 
     } 
    }, { test: /jQuery/, loader: 'expose-loader?$' }, 
    { test: /application/, loader: 'expose-loader?application' }, 
    { test: /base64/, loader: 'exports-loader?Base64' } 
    ] 
    }, 
    resolve: { 
    alias: { 
     'jQuery': 'bower_components/jquery/dist/jquery.min', 
     'application': 'main', 
     'base64': 'vendor/base64' 
    }, 
    modules: [ 
     // Files path which will be referenced while bundling 
     'src/**/*.js', 
     'src/bower_components', 
     path.resolve('./src') 
    ], 
    extensions: ['.js'] // File types 
    }, 
    plugins: [ 

    ] 
}; 

module.exports = config; 
相关问题