简体   繁体   English

带有requirejs / AMD的Webpack

[英]Webpack with requirejs/AMD

I'm working on a new module for an existing project that still uses requireJS for module loading. 我正在为现有项目开发一个新模块,该模块仍然使用requireJS进行模块加载。 I'm trying to use new technologies for my new module like webpack (which allows me to use es6 loaders using es6 imports). 我正在尝试将新技术用于我的新模块,例如webpack(允许我使用es6导入器使用es6加载器)。 It seems like webpack can't reconcile with requireJS syntax. 似乎webpack无法与requireJS语法协调。 It will say things like: "Module not found: Error: Can't resolve in ". 它会说:“找不到模块:错误:无法解决”。

Problem : Webpack won't bundle files with requireJS/AMD syntax in them. 问题 :Webpack不会捆绑带有requireJS / AMD语法的文件。
Question : Is there any way to make webpack play nice with requireJS? 问题 :有没有什么方法可以让webpack在requireJS上玩得很好?

My final output must be in AMD format in order for the project to properly load it. 我的最终输出必须是AMD格式才能让项目正确加载它。 Thanks. 谢谢。

I had the same question and I managed to achieve it. 我有同样的问题,我设法实现了它。 Below is the same webpack.config.js file. 下面是相同的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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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