Skip to content

Commit

Permalink
"exports" conditions semantics (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Mar 18, 2021
1 parent b2b21c5 commit f3fd47b
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,24 @@ function ncc (
transpileOnly = false,
license = '',
target,
production = true,
} = {}
) {
const cjsDeps = () => ({
mainFields: ["main"],
extensions: SUPPORTED_EXTENSIONS,
exportsFields: ["exports"],
importsFields: ["imports"],
conditionNames: ["require", "node", production ? "production" : "development"]
});
const esmDeps = () => ({
mainFields: ["main"],
extensions: SUPPORTED_EXTENSIONS,
exportsFields: ["exports"],
importsFields: ["imports"],
conditionNames: ["import", "node", production ? "production": "development"]
});

process.env.__NCC_OPTS = JSON.stringify({
quiet
});
Expand Down Expand Up @@ -221,6 +237,22 @@ function ncc (
},
resolve: {
extensions: SUPPORTED_EXTENSIONS,
exportsFields: ["exports"],
importsFields: ["imports"],
byDependency: {
wasm: esmDeps(),
esm: esmDeps(),
url: { preferRelative: true },
worker: { ...esmDeps(), preferRelative: true },
commonjs: cjsDeps(),
amd: cjsDeps(),
// for backward-compat: loadModule
loader: cjsDeps(),
// for backward-compat: Custom Dependency
unknown: cjsDeps(),
// for backward-compat: getResolve without dependencyType
undefined: cjsDeps()
},
// webpack defaults to `module` and `main`, but that's
// not really what node.js supports, so we reset it
mainFields: ["main"],
Expand Down
2 changes: 2 additions & 0 deletions test/unit/exports-nomodule/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { x } from 'x';
console.log(x);
1 change: 1 addition & 0 deletions test/unit/exports-nomodule/no.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export var y = 'y';
1 change: 1 addition & 0 deletions test/unit/exports-nomodule/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export var x = 'x';
34 changes: 34 additions & 0 deletions test/unit/exports-nomodule/output-coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ // The require scope
/******/ var __nccwpck_require__ = {};
/******/
/************************************************************************/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __nccwpck_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";/************************************************************************/
var __webpack_exports__ = {};
// ESM COMPAT FLAG
__nccwpck_require__.r(__webpack_exports__);

;// CONCATENATED MODULE: ./test/unit/exports-nomodule/node.js
var x = 'x';

;// CONCATENATED MODULE: ./test/unit/exports-nomodule/input.js

console.log(x);

module.exports = __webpack_exports__;
/******/ })()
;
34 changes: 34 additions & 0 deletions test/unit/exports-nomodule/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ // The require scope
/******/ var __nccwpck_require__ = {};
/******/
/************************************************************************/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __nccwpck_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";/************************************************************************/
var __webpack_exports__ = {};
// ESM COMPAT FLAG
__nccwpck_require__.r(__webpack_exports__);

;// CONCATENATED MODULE: ./test/unit/exports-nomodule/node.js
var x = 'x';

;// CONCATENATED MODULE: ./test/unit/exports-nomodule/input.js

console.log(x);

module.exports = __webpack_exports__;
/******/ })()
;
12 changes: 12 additions & 0 deletions test/unit/exports-nomodule/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "x",
"type": "module",
"exports": {
".": {
"module": "./no.js",
"default": {
"node": "./node.js"
}
}
}
}
2 changes: 2 additions & 0 deletions test/unit/exports-wildcard/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { y } from 'y/no';
console.log(y);
1 change: 1 addition & 0 deletions test/unit/exports-wildcard/no.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export var x = 'x';
1 change: 1 addition & 0 deletions test/unit/exports-wildcard/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export var y = 'y';
34 changes: 34 additions & 0 deletions test/unit/exports-wildcard/output-coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ // The require scope
/******/ var __nccwpck_require__ = {};
/******/
/************************************************************************/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __nccwpck_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";/************************************************************************/
var __webpack_exports__ = {};
// ESM COMPAT FLAG
__nccwpck_require__.r(__webpack_exports__);

;// CONCATENATED MODULE: ./test/unit/exports-wildcard/node.js
var y = 'y';

;// CONCATENATED MODULE: ./test/unit/exports-wildcard/input.js

console.log(y);

module.exports = __webpack_exports__;
/******/ })()
;
34 changes: 34 additions & 0 deletions test/unit/exports-wildcard/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ // The require scope
/******/ var __nccwpck_require__ = {};
/******/
/************************************************************************/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __nccwpck_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";/************************************************************************/
var __webpack_exports__ = {};
// ESM COMPAT FLAG
__nccwpck_require__.r(__webpack_exports__);

;// CONCATENATED MODULE: ./test/unit/exports-wildcard/node.js
var y = 'y';

;// CONCATENATED MODULE: ./test/unit/exports-wildcard/input.js

console.log(y);

module.exports = __webpack_exports__;
/******/ })()
;
12 changes: 12 additions & 0 deletions test/unit/exports-wildcard/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "y",
"type": "module",
"exports": {
"./*": {
"module": "./*.js",
"default": {
"node": "./*de.js"
}
}
}
}

0 comments on commit f3fd47b

Please sign in to comment.