webpack / docs

[OLD] documentation for webpack
http://webpack.github.io/docs/
1.46k stars 127 forks source link

commonjs2 library with externals uses value instead of key for require #74

Closed astralarya closed 8 years ago

astralarya commented 8 years ago

Here is a minimal example (ZIP):

package.json

{
  "name": "webpack-externals-error",
  "version": "1.0.0",
  "description": "Minimal example demonstrating webpack externals error when exporting commonjs2 library",
  "main": "output.js",
  "scripts": {
    "prepublish": "webpack"
  },
  "author": "Mara Kim",
  "license": "ISC",
  "devDependencies": {
    "react": "^15.2.0",
    "webpack": "^1.13.1"
  }
}

webpack.config.js

module.exports = {
    entry: __dirname + '/index.js',
    output: {
        filename: 'output.js',
        path: __dirname,
        library: "Test",
        libraryTarget: "commonjs2",
    },
    externals: {
        react: "React",
    },
};

index.js

"use strict";

let React = require('react');

console.log(React);

To build:

npm install

If you check the resulting output.js, search for the line with module.exports = require(, which looks like:

    module.exports = require("React");

Expected output:

    module.exports = require("react");

It appears that webpack is incorrectly using the value instead of the key in the externals object to generate the require statement.