web-dev-qa-db-ja.com

requirejs / AMDを使用したWebpack

モジュールの読み込みにrequireJSをまだ使用している既存のプロジェクトの新しいモジュールに取り組んでいます。私は、webpackなどの新しいモジュールに新しいテクノロジーを使用しようとしています(これにより、es6インポートを使用してes6ローダーを使用できます)。 webpackがrequireJS構文と一致しないようです。 「モジュールが見つかりません:エラー:で解決できません」のように表示されます。

問題:WebpackはrequireJS/AMD構文を含むファイルをバンドルしません。
質問:requireJSでWebpackをうまくプレイさせる方法はありますか?

プロジェクトが適切にロードするには、最終出力がAMD形式である必要があります。ありがとう。

9
goldensausage

同じ質問があり、なんとかそれを達成することができました。以下は同じです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;
10
softvar