diff --git a/src/index.js b/src/index.js index a9263c48..755ad1d5 100644 --- a/src/index.js +++ b/src/index.js @@ -55,6 +55,9 @@ function ncc ( license = '', target, production = true, + // webpack defaults to `module` and `main`, but that's + // not really what node.js supports, so we reset it + mainFields = ['main'] } = {} ) { // v8 cache not supported for ES modules @@ -62,14 +65,14 @@ function ncc ( v8cache = false; const cjsDeps = () => ({ - mainFields: ["main"], + mainFields, extensions: SUPPORTED_EXTENSIONS, exportsFields: ["exports"], importsFields: ["imports"], conditionNames: ["require", "node", production ? "production" : "development"] }); const esmDeps = () => ({ - mainFields: ["main"], + mainFields, extensions: SUPPORTED_EXTENSIONS, exportsFields: ["exports"], importsFields: ["imports"], @@ -310,9 +313,7 @@ function ncc ( // 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"], + mainFields, plugins: resolvePlugins }, // https://github.com/vercel/ncc/pull/29#pullrequestreview-177152175 diff --git a/test/unit/browser-mainfield/input.js b/test/unit/browser-mainfield/input.js new file mode 100644 index 00000000..aa495939 --- /dev/null +++ b/test/unit/browser-mainfield/input.js @@ -0,0 +1,2 @@ +import crypto from 'crypto-browser'; +console.log('main file') \ No newline at end of file diff --git a/test/unit/browser-mainfield/node_modules/crypto-browser/browser.js b/test/unit/browser-mainfield/node_modules/crypto-browser/browser.js new file mode 100644 index 00000000..765e19a5 --- /dev/null +++ b/test/unit/browser-mainfield/node_modules/crypto-browser/browser.js @@ -0,0 +1 @@ +console.log('browser file!!') \ No newline at end of file diff --git a/test/unit/browser-mainfield/node_modules/crypto-browser/node.js b/test/unit/browser-mainfield/node_modules/crypto-browser/node.js new file mode 100644 index 00000000..b035d09a --- /dev/null +++ b/test/unit/browser-mainfield/node_modules/crypto-browser/node.js @@ -0,0 +1 @@ +console.log('node file!!') \ No newline at end of file diff --git a/test/unit/browser-mainfield/node_modules/crypto-browser/package.json b/test/unit/browser-mainfield/node_modules/crypto-browser/package.json new file mode 100644 index 00000000..9c151a24 --- /dev/null +++ b/test/unit/browser-mainfield/node_modules/crypto-browser/package.json @@ -0,0 +1,5 @@ +{ + "name": "crypto", + "main": "./node.js", + "browser": "./browser.js" +} \ No newline at end of file diff --git a/test/unit/browser-mainfield/opt.json b/test/unit/browser-mainfield/opt.json new file mode 100644 index 00000000..f5c307f0 --- /dev/null +++ b/test/unit/browser-mainfield/opt.json @@ -0,0 +1,3 @@ +{ + "mainFields": ["browser", "main"] +} \ No newline at end of file diff --git a/test/unit/browser-mainfield/output-coverage.js b/test/unit/browser-mainfield/output-coverage.js new file mode 100644 index 00000000..544f1985 --- /dev/null +++ b/test/unit/browser-mainfield/output-coverage.js @@ -0,0 +1,102 @@ +/******/ (() => { // webpackBootstrap +/******/ var __webpack_modules__ = ({ + +/***/ 402: +/***/ (() => { + +console.log('browser file!!') + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // The module cache +/******/ var __webpack_module_cache__ = {}; +/******/ +/******/ // The require function +/******/ function __nccwpck_require__(moduleId) { +/******/ // Check if module is in cache +/******/ var cachedModule = __webpack_module_cache__[moduleId]; +/******/ if (cachedModule !== undefined) { +/******/ return cachedModule.exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = __webpack_module_cache__[moduleId] = { +/******/ // no module.id needed +/******/ // no module.loaded needed +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ var threw = true; +/******/ try { +/******/ __webpack_modules__[moduleId](module, module.exports, __nccwpck_require__); +/******/ threw = false; +/******/ } finally { +/******/ if(threw) delete __webpack_module_cache__[moduleId]; +/******/ } +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/************************************************************************/ +/******/ /* webpack/runtime/compat get default export */ +/******/ (() => { +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __nccwpck_require__.n = (module) => { +/******/ var getter = module && module.__esModule ? +/******/ () => (module['default']) : +/******/ () => (module); +/******/ __nccwpck_require__.d(getter, { a: getter }); +/******/ return getter; +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/define property getters */ +/******/ (() => { +/******/ // define getter functions for harmony exports +/******/ __nccwpck_require__.d = (exports, definition) => { +/******/ for(var key in definition) { +/******/ if(__nccwpck_require__.o(definition, key) && !__nccwpck_require__.o(exports, key)) { +/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); +/******/ } +/******/ } +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/hasOwnProperty shorthand */ +/******/ (() => { +/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) +/******/ })(); +/******/ +/******/ /* 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__ = {}; +// This entry need to be wrapped in an IIFE because it need to be in strict mode. +(() => { +"use strict"; +__nccwpck_require__.r(__webpack_exports__); +/* harmony import */ var crypto_browser__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(402); +/* harmony import */ var crypto_browser__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(crypto_browser__WEBPACK_IMPORTED_MODULE_0__); + +console.log('main file') +})(); + +module.exports = __webpack_exports__; +/******/ })() +; \ No newline at end of file diff --git a/test/unit/browser-mainfield/output.js b/test/unit/browser-mainfield/output.js new file mode 100644 index 00000000..8ec706d8 --- /dev/null +++ b/test/unit/browser-mainfield/output.js @@ -0,0 +1,102 @@ +/******/ (() => { // webpackBootstrap +/******/ var __webpack_modules__ = ({ + +/***/ 329: +/***/ (() => { + +console.log('browser file!!') + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // The module cache +/******/ var __webpack_module_cache__ = {}; +/******/ +/******/ // The require function +/******/ function __nccwpck_require__(moduleId) { +/******/ // Check if module is in cache +/******/ var cachedModule = __webpack_module_cache__[moduleId]; +/******/ if (cachedModule !== undefined) { +/******/ return cachedModule.exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = __webpack_module_cache__[moduleId] = { +/******/ // no module.id needed +/******/ // no module.loaded needed +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ var threw = true; +/******/ try { +/******/ __webpack_modules__[moduleId](module, module.exports, __nccwpck_require__); +/******/ threw = false; +/******/ } finally { +/******/ if(threw) delete __webpack_module_cache__[moduleId]; +/******/ } +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/************************************************************************/ +/******/ /* webpack/runtime/compat get default export */ +/******/ (() => { +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __nccwpck_require__.n = (module) => { +/******/ var getter = module && module.__esModule ? +/******/ () => (module['default']) : +/******/ () => (module); +/******/ __nccwpck_require__.d(getter, { a: getter }); +/******/ return getter; +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/define property getters */ +/******/ (() => { +/******/ // define getter functions for harmony exports +/******/ __nccwpck_require__.d = (exports, definition) => { +/******/ for(var key in definition) { +/******/ if(__nccwpck_require__.o(definition, key) && !__nccwpck_require__.o(exports, key)) { +/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); +/******/ } +/******/ } +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/hasOwnProperty shorthand */ +/******/ (() => { +/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) +/******/ })(); +/******/ +/******/ /* 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__ = {}; +// This entry need to be wrapped in an IIFE because it need to be in strict mode. +(() => { +"use strict"; +__nccwpck_require__.r(__webpack_exports__); +/* harmony import */ var crypto_browser__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(329); +/* harmony import */ var crypto_browser__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(crypto_browser__WEBPACK_IMPORTED_MODULE_0__); + +console.log('main file') +})(); + +module.exports = __webpack_exports__; +/******/ })() +; \ No newline at end of file