1.0.20 • Published 2 years ago

amd-webpack-plugin v1.0.20

Weekly downloads
20
License
MIT
Repository
github
Last release
2 years ago

AMD webpack plugin

Features

This plugin is used to enhance the AMD packaging mode of webpack:

  1. Make SplitChunks to be AMD modules.
  2. Inject SplitChunks AMD module names to entry chunk dependencies automatically.
  3. Make webpack replace the root external(global variable) correctly.
  4. The dynamic imports capability of webpack will be preserved.
  5. Can Change the define wrapper name.

Installation

npm i amd-webpack-plugin --save-dev

or

yarn add amd-webpack-plugin --dev

Usage

webpack.config.js

const path = require('path')
const AmdWebpackPlugin = require('amd-webpack-plugin')

// webpack config
module.exports = {
  mode: 'development',
  devtool: false,
  entry: {
    'entry1': path.join(__dirname, 'src/entry1.js'),
    'entry2': path.join(__dirname, 'src/entry2.js')
  },
  module: {
    rules: [
      {
        test: /\.jsx?|tsx?$/,
        loader: ['babel-loader']
      }
    ]
  },
  externals: {
    // this will be replaced as global variable
    'jquery': { root: '$' },
    // this will be replaced as AMD dependency
    'three': 'three',
    'd3': { amd: 'd3' }
  },
  output: {
    filename: '[name].js',
    libraryTarget: 'amd'
  },
  plugins: [
    new AmdWebpackPlugin()
    // The following options are used by default
    // new AmdWebpackPlugin({
    //   wrapper: 'define',
    //   hashedModuleIds: {
    //      hashFunction: 'md4',
    //      hashDigest: 'base64',
    //      hashDigestLength: 16
    //   }
    // })

    // in webpack mode 'production'
    // it will set chunkIds = 'named' automatically

    // you can set hashedModuleIds = false to ignore
  ],
  optimization: {
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        default: false,
        vendors: false,
        // choose other node_modules to be vendor.js
        vendor: {
          name: 'vendor',
          chunks: 'all',
          minChunks: 1,
          test: /[\\/]node_modules[\\/]/,
          priority: 10
        }
      }
    }
  }
}

src/entry1.js

import jquery from 'jquery'
import three from 'three'
import lodash from 'lodash'

src/entry2.js

import jquery from 'jquery'
import d3 from 'd3'
import lodash from 'lodash'

// dynamic imports will use webpack module engine
// this chunk will not convert to AMD Module
import(
  /* webpackChunkName: "asyc-import-data" */
  './data'
).then(data => {
  console.log(data)
});

dist/async-import-data

(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["asyc-import-data"],{
  ...
}]);

dist/entry1.js

define(["three","vendor"], function(__WEBPACK_EXTERNAL_MODULE_three__) {
  ...
})

dist/entry2.js

define(["d3","vendor"], function(__WEBPACK_EXTERNAL_MODULE_d3__) {
  ...
})

dist/vendor.js

 define(function() { return (window["webpackJsonp"] = window["webpackJsonp"] || []).push([["vendor"],{
   ...
 }])})

You can get the full demo

License

MIT

1.0.20

2 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago