首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack :要求/AMD

Webpack :要求/AMD
EN

Stack Overflow用户
提问于 2017-05-29 22:13:38
回答 1查看 19.7K关注 0票数 11

我正在为一个仍然使用requireJS加载模块的现有项目开发一个新模块。我正在尝试为我的新模块使用新技术,比如webpack (它允许我使用es6加载器使用es6导入)。webpack似乎无法与requireJS语法协调一致。它会说:“模块未找到:错误:无法解决”。

Problem:Webpack不会将文件捆绑在其中使用所需的in /AMD语法。

问题:有没有办法让webpack和requireJS打得好?

我的最后输出必须是AMD格式,这样项目才能正确加载它。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-06-18 19:18:32

我有同样的问题,我设法做到了。下面是同一个webpack.config.js文件。

代码语言:javascript
复制
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;
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44250873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档