Closed astralarya closed 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:
output.js
module.exports = require(
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.
Here is a minimal example (ZIP):
package.json
webpack.config.js
index.js
To build:
If you check the resulting
output.js
, search for the line withmodule.exports = require(
, which looks like:Expected output:
It appears that webpack is incorrectly using the value instead of the key in the externals object to generate the require statement.