Webpack with requirejs/AMD

后端 未结 1 871
甜味超标
甜味超标 2021-02-04 11:43

I\'m working on a new module for an existing project that still uses requireJS for module loading. I\'m trying to use new technologies for my new module like webpack (which all

1条回答
  •  旧时难觅i
    2021-02-04 11:56

    I had the same question and I managed to achieve it. Below is the same webpack.config.js file.

    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;
    

    0 讨论(0)
提交回复
热议问题