Webpack与requirejs/AMD结合使用

11

我正在为一个使用requireJS进行模块加载的现有项目编写新的模块。 我试图在我的新模块中使用像webpack这样的新技术(它使我可以使用es6 import来加载es6加载程序)。 看起来webpack无法与requireJS语法协调一致。 它会显示类似于:"Module not found: Error: Can't resolve in "的内容。

问题:Webpack无法打包具有requireJS / AMD语法的文件。
询问:是否有办法让webpack与requireJS兼容?

为了使项目正确加载,我的最终输出必须以AMD格式呈现。 谢谢。


1
你可以看一下webpack的一些babel加载器。我曾经遇到过类似的问题,通常你可以使用babel在模块系统之间进行转译。 - Andres M
1
我已经在我的Webpack配置中使用了Babel加载器。 - goldensausage
1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
15

我曾经有同样的问题并且成功解决了。下面是相同的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;

使用Webpack Encore可以实现这个吗? - crmpicco

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,