Issue 1:
Bug report

What is the current behavior?

library target: umd cause "webpack_exports is not defined" error

If the current behavior is a bug, please provide the steps to reproduce.

https://github.com/buhichan/webpack_exports-is-undefined/tree/master

What is the expected behavior?

it should not generate main.js with reference error. This error does not happen with webpack 5.22.

In https://github.com/webpack/webpack/blob/master/lib/javascript/JavascriptModulesPlugin.js, the code is like

renderMain(...){
...
if(inlineModule){
...
generate var __webpack_exports__ = {};
...
}else{
...
}
if(someConfig){
generate return __webpack_exports__;
}
...
}

Maybe generate return __webpack_exports__ should be put into if(inlineModule){ ... }

Other relevant information:

webpack version: 5.34

Node.js version: 14

Operating System: Macos

Additional tools: webpack-dev-server: 4.0.0-beta.2

Issue 2:
I would like to move the discussion that started in uuidjs/uuid#462 (comment) and rollup/rollup#3514 (comment) into this project.

TL;DR: We do believe that, apart from support for pkg.exports.browser that is being added in #10953, we may need to support an additional key in the pkg.exports object.

Let's illustrate this with an example taken from uuid. Before pkg.exports we used to specify the following in package.json:

"main": "./dist/index.js",
"module": "./dist/esm-node/index.js",
"browser": {
"./dist/md5.js": "./dist/md5-browser.js",
"./dist/rng.js": "./dist/rng-browser.js",
"./dist/sha1.js": "./dist/sha1-browser.js",
"./dist/esm-node/index.js": "./dist/esm-browser/index.js"
},

The goal was the following:

Node.js native should pick main

Bundlers targeting Node.js should pick module

Bundlers targeting the browser would pick "module": "./dist/esm-node/index.js", but then use the override from browser: "./dist/esm-node/index.js": "./dist/esm-browser/index.js"

(Before we added the esm build, bundlers targeting the browser would have used the CommonJS build from main, hence the additional browser-overrides)

Now that Node.js has added and webpack is about to add pkg.exports support the above can no longer be achieved for webpack:

"main": "./dist/index.js",
"exports": {
"./package.json": "./package.json",
".": {
"browser": "./dist/esm-browser/index.js",
"require": "./dist/index.js",
"import": "./wrapper.mjs"
}
},
"module": "./dist/esm-node/index.js",
"browser": {
"./dist/md5.js": "./dist/md5-browser.js",
"./dist/rng.js": "./dist/rng-browser.js",
"./dist/sha1.js": "./dist/sha1-browser.js",
"./dist/esm-node/index.js": "./dist/esm-browser/index.js"
},

Since Node.js potentially suffers from the dual package hazard uuid provides a ./wrapper.mjs which wraps the CJS build and exports it as ESM for pkg.exports.import.

While the browser use case is covered by pkg.exports.browser for webpack, things no longer work as expected when bundling for Node.js

While it would technically work to just fall back to pkg.exports.import and pkg.exports.require that is at least not an option for ESM-only bundlers like rollup, see rollup/rollup#3514.

Hence the need for one additional pkg.exports key that allows specifying a "pure esm" build to be used for Node.js bundling.

For more details see the discussion in uuidjs/uuid#462 (comment) onwards.

/cc @sokra @lukastaegert @guybedford @jkrems @evilebottnawi