Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic library option with multi entry points

Tags:

webpack

I find an example of webpack library option with multiple entry points and UMD

Here is the webpack.config.js in the example:

var path = require("path");
module.exports = {
    entry: {
        alpha: "./alpha",
        beta: "./beta"
    },
    output: {
        path: path.join(__dirname, "js"),
        filename: "MyLibrary.[name].js",
        library: ["MyLibrary", "[name]"],
        libraryTarget: "umd"
    }
}

My question is how to config filename & library dynamically. What I want is:

  • filename for entry alpha to be a.js
  • filename for entry beta to be b.js
  • library for entry alpha to be Alpha
  • library for entry beta to be Beta.

So I wonder if I can config these options through a function like this:

var path = require("path");
module.exports = {
    entry: {
        alpha: "./alpha",
        beta: "./beta"
    },
    output: {
        path: path.join(__dirname, "js"),
        filename: function(entryKey, entryValue) {
            if (entryKey === 'alpha') return 'a.js';
            if (entryKey === 'beta') return 'b.js';
        },
        library: function(entryKey, entryValue) {
            if (entryKey === 'alpha') return 'Alpha';
            if (entryKey === 'beta') return 'Beta';
        },
        libraryTarget: "umd"
    }
}
like image 571
zyy7259 Avatar asked Dec 20 '15 05:12

zyy7259


2 Answers

From webpack 3.1.0, you can export multi configurations from webpack.config.js

So you can try the following:

  module.exports = [
    {
      entry: "./alpha",
      output: {
          path: path.join(__dirname, "js"),
          filename: "a.js",
          library: "Alpha",
          libraryTarget: "umd"
      }
    },
    {
      entry: "./beta",
      output: {
          path: path.join(__dirname, "js"),
          filename: "b.js",
          library: "Beta",
          libraryTarget: "umd"
      }
    },

]

Related docs: Exporting multiple configurations

like image 126
loveky Avatar answered Nov 24 '22 07:11

loveky


You could set the name like so

var path = require("path");
module.exports = {
    entry: {
        Alpha: "./alpha",
        Beta: "./beta"
    },
    output: {
        path: path.join(__dirname, "js"),
        filename: '[name].js',
        library: '[name]',
        libraryTarget: "umd"
    }
}
like image 42
Gabe M Avatar answered Nov 24 '22 07:11

Gabe M