├── .eslintrc ├── .github └── ISSUE_TEMPLATE │ ├── BUG.md │ ├── DOCS.md │ ├── FEATURE.md │ ├── MODIFICATION.md │ └── SUPPORT.md ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── appveyor.yml ├── index.d.ts ├── package-lock.json ├── package.json ├── rollup.config.js ├── src └── index.js ├── test ├── mocha.opts ├── node_modules │ ├── .gitkeep │ ├── @scoped │ │ ├── bar │ │ │ └── index.js │ │ └── foo │ │ │ └── index.js │ ├── browser-entry-points-to-node-module │ │ ├── index.js │ │ └── package.json │ ├── builtins-browser │ │ ├── fs.js │ │ ├── index.js │ │ └── package.json │ ├── component-type │ │ ├── index.js │ │ └── package.json │ ├── dual-cjs-mjs │ │ ├── entry.js │ │ ├── entry.mjs │ │ └── package.json │ ├── entries │ │ ├── jsnext-entry.js │ │ ├── main-entry.js │ │ ├── module-entry.js │ │ └── package.json │ ├── events │ │ ├── index.js │ │ └── package.json │ ├── isomorphic-nested │ │ ├── lib │ │ │ ├── browser-dep.js │ │ │ ├── browser-test.js │ │ │ ├── browser.js │ │ │ ├── dep.js │ │ │ └── index.js │ │ └── package.json │ ├── isomorphic-object-main-implicit │ │ ├── browser.js │ │ ├── index.js │ │ └── package.json │ ├── isomorphic-object-main │ │ ├── browser-dep.js │ │ ├── browser-test.js │ │ ├── browser.js │ │ ├── dep.js │ │ ├── index.js │ │ └── package.json │ ├── isomorphic-object-with-false │ │ ├── lib │ │ │ ├── client │ │ │ │ ├── http-tracker.js │ │ │ │ ├── udp-tracker.js │ │ │ │ └── websocket-tracker.js │ │ │ ├── index.js │ │ │ └── subpath │ │ │ │ └── foo │ │ │ │ └── index.js │ │ └── package.json │ ├── isomorphic-object │ │ ├── browser-dep.js │ │ ├── browser-test.js │ │ ├── browser.js │ │ ├── dep.js │ │ ├── index.js │ │ └── package.json │ ├── isomorphic │ │ ├── browser.js │ │ ├── node.js │ │ └── package.json │ ├── jsnext │ │ ├── entry-main.js │ │ ├── entry.js │ │ └── package.json │ ├── legacy │ │ ├── entry.js │ │ └── package.json │ ├── module-mjs │ │ ├── entry.mjs │ │ └── package.json │ ├── module │ │ ├── entry-main.js │ │ ├── entry.js │ │ └── package.json │ ├── react-consumer │ │ ├── index.js │ │ └── node_modules │ │ │ ├── .gitkeep │ │ │ └── react │ │ │ └── index.js │ ├── react │ │ └── index.js │ ├── side-effects-array │ │ ├── dep1.js │ │ ├── dep2.js │ │ ├── dep3-effect.js │ │ ├── index.js │ │ ├── nested │ │ │ ├── dep4.js │ │ │ └── dep5-effect.js │ │ └── package.json │ ├── side-effects-false │ │ ├── dep1.js │ │ ├── dep2.js │ │ ├── index.js │ │ └── package.json │ ├── side-effects-true │ │ ├── dep1.js │ │ ├── dep2.js │ │ ├── index.js │ │ └── package.json │ ├── simple │ │ └── index.js │ ├── string │ │ └── uppercase.js │ ├── test │ │ └── index.js │ └── uses-buffer │ │ ├── index.js │ │ └── package.json ├── package.json ├── samples │ ├── browser-entry-points-to-node-module │ │ └── index.js │ ├── browser-false │ │ └── main.js │ ├── browser-local │ │ └── main.js │ ├── browser-object-builtin │ │ └── main.js │ ├── browser-object-main │ │ └── main.js │ ├── browser-object-nested │ │ └── main.js │ ├── browser-object-with-false │ │ └── main.js │ ├── browser-object │ │ ├── main-implicit.js │ │ └── main.js │ ├── browser │ │ └── main.js │ ├── builtins │ │ └── main.js │ ├── commonjs │ │ └── main.js │ ├── custom-resolve-options │ │ ├── js_modules │ │ │ └── foo.js │ │ └── main.js │ ├── deep-import-non-module │ │ └── main.js │ ├── dual-cjs-mjs │ │ └── main.js │ ├── dynamic │ │ └── main.js │ ├── extensions │ │ ├── lol.wut │ │ └── main.js │ ├── granular │ │ └── main.js │ ├── jail │ │ └── main.js │ ├── jsnext │ │ └── main.js │ ├── local-index │ │ ├── forty-two │ │ │ └── index.js │ │ └── main.js │ ├── manualchunks │ │ └── main.js │ ├── module-mjs │ │ └── main.js │ ├── module │ │ └── main.js │ ├── only │ │ └── main.js │ ├── package-index │ │ └── main.js │ ├── prefer-builtin │ │ └── main.js │ ├── prefer-jsnext │ │ └── main.js │ ├── prefer-main │ │ └── main.js │ ├── prefer-module │ │ └── main.js │ ├── react-app │ │ └── main.js │ ├── scoped │ │ └── main.js │ ├── side-effects │ │ └── main.js │ ├── symlinked │ │ ├── first │ │ │ └── index.js │ │ ├── second │ │ │ └── index.js │ │ └── third │ │ │ └── index.js │ ├── trailing-slash │ │ └── main.js │ ├── unresolved-global │ │ └── main.js │ └── unresolved-local │ │ └── main.js └── test.js ├── tsconfig.json └── typings-test.js /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "indent": [ 2, "tab", { "SwitchCase": 1 } ], 4 | "quotes": [ 2, "single", { "allowTemplateLiterals": true } ], 5 | "semi": [ 2, "always" ], 6 | "keyword-spacing": [ 2, { "before": true, "after": true } ], 7 | "space-before-blocks": [ 2, "always" ], 8 | "space-before-function-paren": [ 2, "always" ], 9 | "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ], 10 | "no-cond-assign": 0, 11 | "no-unused-vars": 2, 12 | "prefer-const": 2, 13 | "no-var": 2 14 | }, 15 | "env": { 16 | "es6": true, 17 | "browser": true, 18 | "mocha": true, 19 | "node": true 20 | }, 21 | "extends": "eslint:recommended", 22 | "parserOptions": { 23 | "ecmaVersion": 6, 24 | "sourceType": "module" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/BUG.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 🐞 Bug Report 3 | about: Something went awry and you'd like to tell us about it. 4 | 5 | --- 6 | 7 | 20 | 21 | - Node-Resolve Plugin Version: 22 | - Rollup Version: 23 | - Operating System (or Browser): 24 | - Node Version: 25 | 26 | ### How Do We Reproduce? 27 | 28 | 34 | 35 | 36 | ### Expected Behavior 37 | 38 | 39 | ### Actual Behavior 40 | 41 | 42 | 48 | 49 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/DOCS.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 📚 Documentation 3 | about: Are the docs lacking or missing something? Do they need some new 🔥 hotness? Tell us here. 4 | 5 | --- 6 | 7 | 20 | 21 | Documentation Is: 22 | 23 | 24 | 25 | - [ ] Missing 26 | - [ ] Needed 27 | - [ ] Confusing 28 | - [ ] Not Sure? 29 | 30 | ### Please Explain in Detail... 31 | 32 | 33 | ### Your Proposal for Changes -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/FEATURE.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: ✨ Feature Request 3 | about: Suggest an idea for this project 4 | 5 | --- 6 | 7 | 20 | 21 | ### Feature Use Case 22 | 23 | 24 | ### Feature Proposal -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/MODIFICATION.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 🔧 Modification Request 3 | about: Would you like something work differently? Have an alternative approach? This is the template for you. 4 | 5 | --- 6 | 7 | 20 | 21 | 22 | ### Expected Behavior / Situation 23 | 24 | 25 | ### Actual Behavior / Situation 26 | 27 | 28 | ### Modification Proposal -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/SUPPORT.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 🆘 Support, Help, and Advice 3 | about: 👉🏽 Need help or tech support? Please don't open an issue! Head to https://gitter.im/rollup/rollup or https://stackoverflow.com/questions/tagged/rollupjs. 4 | 5 | --- 6 | 7 | Hey there! If you need help or tech support then this is not the place to 8 | ask. Please head to [the Rollup Gitter](https://gitter.im/rollup/rollup) 9 | instead or post a question to https://stackoverflow.com/questions/tagged/rollupjs. 10 | 11 | If you arrived here because you think Rollup's documentation is unclear, 12 | insufficient or wrong, please consider creating an issue for the documentation 13 | instead. 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | dist 4 | .gobble* 5 | !test/node_modules 6 | !test/node_modules/react-consumer/node_modules 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "6" 4 | - "8" 5 | - "10" 6 | - "12" 7 | env: 8 | global: 9 | - BUILD_TIMEOUT=10000 10 | install: npm ci --ignore-scripts 11 | before_install: 12 | - if [[ $TRAVIS_NODE_VERSION -lt 8 ]]; then npm install --global npm@5; fi 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # rollup-plugin-node-resolve changelog 2 | 3 | ## 5.2.1 (unreleased) 4 | 5 | * add missing MIT license file ([#233](https://github.com/rollup/rollup-plugin-node-resolve/pull/233) by @kenjiO) 6 | * Fix incorrect example of config ([#239](https://github.com/rollup/rollup-plugin-node-resolve/pull/240) by @myshov) 7 | * Fix typo in readme ([#240](https://github.com/rollup/rollup-plugin-node-resolve/pull/240) by @LinusU) 8 | 9 | ## 5.2.0 (2019-06-29) 10 | 11 | * dedupe accepts a function ([#225](https://github.com/rollup/rollup-plugin-node-resolve/pull/225) by @manucorporat) 12 | 13 | ## 5.1.1 (2019-06-29) 14 | 15 | * Move Rollup version check to buildStart hook to avoid issues ([#232](https://github.com/rollup/rollup-plugin-node-resolve/pull/232) by @lukastaegert) 16 | 17 | ## 5.1.0 (2019-06-22) 18 | 19 | * Fix path fragment inputs ([#229](https://github.com/rollup/rollup-plugin-node-resolve/pull/229) by @bterlson) 20 | 21 | ## 5.0.4 (2019-06-22) 22 | 23 | * Treat sideEffects array as inclusion list ([#227](https://github.com/rollup/rollup-plugin-node-resolve/pull/227) by @mikeharder) 24 | 25 | ## 5.0.3 (2019-06-16) 26 | 27 | * Make empty.js a virtual module ([#224](https://github.com/rollup/rollup-plugin-node-resolve/pull/224) by @manucorporat) 28 | 29 | ## 5.0.2 (2019-06-13) 30 | 31 | * Support resolve 1.11.1, add built-in test ([#223](https://github.com/rollup/rollup-plugin-node-resolve/pull/223) by @bterlson) 32 | 33 | ## 5.0.1 (2019-05-31) 34 | 35 | * Update to resolve@1.11.0 for better performance ([#220](https://github.com/rollup/rollup-plugin-node-resolve/pull/220) by @keithamus) 36 | 37 | ## 5.0.0 (2019-05-15) 38 | 39 | * Replace bublé with babel, update dependencies ([#216](https://github.com/rollup/rollup-plugin-node-resolve/pull/216) by @mecurc) 40 | * Handle module side-effects ([#219](https://github.com/rollup/rollup-plugin-node-resolve/pull/219) by @lukastaegert) 41 | 42 | ### Breaking Changes 43 | 44 | * Requires at least rollup@1.11.0 to work (v1.12.0 for module side-effects to be respected) 45 | * If used with rollup-plugin-commonjs, it should be at least v10.0.0 46 | 47 | ## 4.2.4 (2019-05-11) 48 | 49 | * Add note on builtins to Readme ([#215](https://github.com/rollup/rollup-plugin-node-resolve/pull/215) by @keithamus) 50 | * Add issue templates ([#217](https://github.com/rollup/rollup-plugin-node-resolve/pull/217) by @mecurc) 51 | * Improve performance by caching `isDir` ([#218](https://github.com/rollup/rollup-plugin-node-resolve/pull/218) by @keithamus) 52 | 53 | ## 4.2.3 (2019-04-11) 54 | 55 | * Fix ordering of jsnext:main when using the jsnext option ([#209](https://github.com/rollup/rollup-plugin-node-resolve/pull/209) by @lukastaegert) 56 | 57 | ## 4.2.2 (2019-04-10) 58 | 59 | * Fix TypeScript typings (rename and export Options interface) ([#206](https://github.com/rollup/rollup-plugin-node-resolve/pull/206) by @Kocal) 60 | * Fix mainfields typing ([#207](https://github.com/rollup/rollup-plugin-node-resolve/pull/207) by @nicolashenry) 61 | 62 | ## 4.2.1 (2019-04-06) 63 | 64 | * Respect setting the deprecated fields "module", "main", and "jsnext" ([#204](https://github.com/rollup/rollup-plugin-node-resolve/pull/204) by @nick-woodward) 65 | 66 | ## 4.2.0 (2019-04-06) 67 | 68 | * Add new mainfields option ([#182](https://github.com/rollup/rollup-plugin-node-resolve/pull/182) by @keithamus) 69 | * Added dedupe option to prevent bundling the same package multiple times ([#201](https://github.com/rollup/rollup-plugin-node-resolve/pull/182) by @sormy) 70 | 71 | ## 4.1.0 (2019-04-05) 72 | 73 | * Add TypeScript typings ([#189](https://github.com/rollup/rollup-plugin-node-resolve/pull/189) by @NotWoods) 74 | * Update dependencies ([#202](https://github.com/rollup/rollup-plugin-node-resolve/pull/202) by @lukastaegert) 75 | 76 | ## 4.0.1 (2019-02-22) 77 | 78 | * Fix issue when external modules are specified in `package.browser` ([#143](https://github.com/rollup/rollup-plugin-node-resolve/pull/143) by @keithamus) 79 | * Fix `package.browser` mapping issue when `false` is specified ([#183](https://github.com/rollup/rollup-plugin-node-resolve/pull/183) by @allex) 80 | 81 | ## 4.0.0 (2018-12-09) 82 | 83 | This release will support rollup@1.0 84 | 85 | ### Features 86 | * Resolve modules used to define manual chunks ([#185](https://github.com/rollup/rollup-plugin-node-resolve/pull/185) by @mcshaman) 87 | * Update dependencies and plugin hook usage ([#187](https://github.com/rollup/rollup-plugin-node-resolve/pull/187) by @lukastaegert) 88 | 89 | ## 3.4.0 (2018-09-04) 90 | 91 | This release now supports `.mjs` files by default 92 | 93 | ### Features 94 | * feat: Support .mjs files by default (https://github.com/rollup/rollup-plugin-node-resolve/pull/151, by @leebyron) 95 | 96 | ## 3.3.0 (2018-03-17) 97 | 98 | This release adds the `only` option 99 | 100 | ### New Features 101 | * feat: add `only` option (#83; @arantes555) 102 | 103 | ### Docs 104 | * docs: correct description of `jail` option (#120; @GeorgeTaveras1231) 105 | 106 | ## 3.2.0 (2018-03-07) 107 | 108 | This release caches reading/statting of files, to improve speed. 109 | 110 | ### Performance Improvements 111 | * perf: cache file stats/reads (#126; @keithamus) 112 | 113 | ## 3.0.4 (unreleased) 114 | 115 | * Update lockfile [#137](https://github.com/rollup/rollup-plugin-node-resolve/issues/137) 116 | * Update rollup dependency [#138](https://github.com/rollup/rollup-plugin-node-resolve/issues/138) 117 | * Enable installation from Github [#142](https://github.com/rollup/rollup-plugin-node-resolve/issues/142) 118 | 119 | ## 3.0.3 120 | 121 | * Fix [#130](https://github.com/rollup/rollup-plugin-node-resolve/issues/130) and [#131](https://github.com/rollup/rollup-plugin-node-resolve/issues/131) 122 | 123 | ## 3.0.2 124 | 125 | * Ensure `pkg.browser` is an object if necessary ([#129](https://github.com/rollup/rollup-plugin-node-resolve/pull/129)) 126 | 127 | ## 3.0.1 128 | 129 | * Remove `browser-resolve` dependency ([#127](https://github.com/rollup/rollup-plugin-node-resolve/pull/127)) 130 | 131 | ## 3.0.0 132 | 133 | * [BREAKING] Remove `options.skip` ([#90](https://github.com/rollup/rollup-plugin-node-resolve/pull/90)) 134 | * Add `modulesOnly` option ([#96](https://github.com/rollup/rollup-plugin-node-resolve/pull/96)) 135 | 136 | ## 2.1.1 137 | 138 | * Prevent `jail` from breaking builds on Windows ([#93](https://github.com/rollup/rollup-plugin-node-resolve/issues/93)) 139 | 140 | ## 2.1.0 141 | 142 | * Add `jail` option ([#53](https://github.com/rollup/rollup-plugin-node-resolve/pull/53)) 143 | * Add `customResolveOptions` option ([#79](https://github.com/rollup/rollup-plugin-node-resolve/pull/79)) 144 | * Support symlinked packages ([#82](https://github.com/rollup/rollup-plugin-node-resolve/pull/82)) 145 | 146 | ## 2.0.0 147 | 148 | * Add support `module` field in package.json as an official alternative to jsnext 149 | 150 | ## 1.7.3 151 | 152 | * Error messages are more descriptive ([#50](https://github.com/rollup/rollup-plugin-node-resolve/issues/50)) 153 | 154 | ## 1.7.2 155 | 156 | * Allow entry point paths beginning with ./ 157 | 158 | ## 1.7.1 159 | 160 | * Return a `name` 161 | 162 | ## 1.7.0 163 | 164 | * Allow relative IDs to be external ([#32](https://github.com/rollup/rollup-plugin-node-resolve/pull/32)) 165 | 166 | ## 1.6.0 167 | 168 | * Skip IDs containing null character 169 | 170 | ## 1.5.0 171 | 172 | * Prefer built-in options, but allow opting out ([#28](https://github.com/rollup/rollup-plugin-node-resolve/pull/28)) 173 | 174 | ## 1.4.0 175 | 176 | * Pass `options.extensions` through to `node-resolve` 177 | 178 | ## 1.3.0 179 | 180 | * `skip: true` skips all packages that don't satisfy the `main` or `jsnext` options ([#16](https://github.com/rollup/rollup-plugin-node-resolve/pull/16)) 181 | 182 | ## 1.2.1 183 | 184 | * Support scoped packages in `skip` option ([#15](https://github.com/rollup/rollup-plugin-node-resolve/issues/15)) 185 | 186 | ## 1.2.0 187 | 188 | * Support `browser` field ([#8](https://github.com/rollup/rollup-plugin-node-resolve/issues/8)) 189 | * Get tests to pass on Windows 190 | 191 | ## 1.1.0 192 | 193 | * Use node-resolve to handle various corner cases 194 | 195 | ## 1.0.0 196 | 197 | * Add ES6 build, use Rollup 0.20.0 198 | 199 | ## 0.1.0 200 | 201 | * First release 202 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 [these people](https://github.com/rollup/rollup-plugin-node-resolve/graphs/contributors) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Moved 2 | 3 | This module has moved and is now available at [@rollup/plugin-node-resolve](https://github.com/rollup/plugins). Please update your dependencies. This repository is no longer maintained. 4 | 5 | 6 | # rollup-plugin-node-resolve 7 | 8 | *This plugin used to be called rollup-plugin-npm* 9 | 10 | Locate modules using the [Node resolution algorithm](https://nodejs.org/api/modules.html#modules_all_together), for using third party modules in `node_modules` 11 | 12 | ## Installation 13 | 14 | ```bash 15 | npm install --save-dev rollup-plugin-node-resolve 16 | ``` 17 | 18 | ## Usage 19 | 20 | ```js 21 | // rollup.config.js 22 | import resolve from 'rollup-plugin-node-resolve'; 23 | 24 | export default { 25 | input: 'main.js', 26 | output: { 27 | file: 'bundle.js', 28 | format: 'iife', 29 | name: 'MyModule' 30 | }, 31 | plugins: [ 32 | resolve({ 33 | 34 | // the fields to scan in a package.json to determine the entry point 35 | // if this list contains "browser", overrides specified in "pkg.browser" 36 | // will be used 37 | mainFields: ['module', 'main'], // Default: ['module', 'main'] 38 | 39 | // DEPRECATED: use "mainFields" instead 40 | // use "module" field for ES6 module if possible 41 | module: true, // Default: true 42 | 43 | // DEPRECATED: use "mainFields" instead 44 | // use "jsnext:main" if possible 45 | // legacy field pointing to ES6 module in third-party libraries, 46 | // deprecated in favor of "pkg.module": 47 | // - see: https://github.com/rollup/rollup/wiki/pkg.module 48 | jsnext: true, // Default: false 49 | 50 | // DEPRECATED: use "mainFields" instead 51 | // use "main" field or index.js, even if it's not an ES6 module 52 | // (needs to be converted from CommonJS to ES6) 53 | // – see https://github.com/rollup/rollup-plugin-commonjs 54 | main: true, // Default: true 55 | 56 | // some package.json files have a "browser" field which specifies 57 | // alternative files to load for people bundling for the browser. If 58 | // that's you, either use this option or add "browser" to the 59 | // "mainfields" option, otherwise pkg.browser will be ignored 60 | browser: true, // Default: false 61 | 62 | // not all files you want to resolve are .js files 63 | extensions: [ '.mjs', '.js', '.jsx', '.json' ], // Default: [ '.mjs', '.js', '.json', '.node' ] 64 | 65 | // whether to prefer built-in modules (e.g. `fs`, `path`) or 66 | // local ones with the same names 67 | preferBuiltins: false, // Default: true 68 | 69 | // Lock the module search in this path (like a chroot). Module defined 70 | // outside this path will be marked as external 71 | jail: '/my/jail/path', // Default: '/' 72 | 73 | // Set to an array of strings and/or regexps to lock the module search 74 | // to modules that match at least one entry. Modules not matching any 75 | // entry will be marked as external 76 | only: [ 'some_module', /^@some_scope\/.*$/ ], // Default: null 77 | 78 | // If true, inspect resolved files to check that they are 79 | // ES2015 modules 80 | modulesOnly: true, // Default: false 81 | 82 | // Force resolving for these modules to root's node_modules that helps 83 | // to prevent bundling the same package multiple times if package is 84 | // imported from dependencies. 85 | dedupe: [ 'react', 'react-dom' ], // Default: [] 86 | 87 | // Any additional options that should be passed through 88 | // to node-resolve 89 | customResolveOptions: { 90 | moduleDirectory: 'js_modules' 91 | } 92 | }) 93 | ] 94 | }; 95 | ``` 96 | 97 | ## Using with rollup-plugin-commonjs 98 | 99 | Since most packages in your node_modules folder are probably legacy CommonJS rather than JavaScript modules, you may need to use [rollup-plugin-commonjs](https://github.com/rollup/rollup-plugin-commonjs): 100 | 101 | ```js 102 | // rollup.config.js 103 | import resolve from 'rollup-plugin-node-resolve'; 104 | import commonjs from 'rollup-plugin-commonjs'; 105 | 106 | export default { 107 | input: 'main.js', 108 | output: { 109 | file: 'bundle.js', 110 | format: 'iife', 111 | name: 'MyModule' 112 | }, 113 | plugins: [ 114 | resolve(), 115 | commonjs() 116 | ] 117 | }; 118 | ``` 119 | 120 | ## Resolving Built-Ins (like `fs`) 121 | 122 | This plugin won't resolve any builtins (e.g. `fs`). If you need to resolve builtins you can install local modules and set `preferBuiltins` to `false`, or install a plugin like [rollup-plugin-node-builtins](https://github.com/calvinmetcalf/rollup-plugin-node-builtins) which provides stubbed versions of these methods. 123 | 124 | If you want to silence warnings about builtins, you can add the list of builtins to the `externals` option; like so: 125 | 126 | ```js 127 | import resolve from 'rollup-plugin-node-resolve'; 128 | import builtins from 'builtin-modules' 129 | export default ({ 130 | input: ..., 131 | plugins: [resolve()], 132 | external: builtins, 133 | output: ... 134 | }) 135 | ``` 136 | 137 | ## Additional Plugin APIs 138 | 139 | In addition to the standard hooks used by Rollup, this plugin exposes additional functionality useful for other plugins. 140 | 141 | ## getPackageInfoForId (moduleId: string) => PackageInfo 142 | 143 | Returns an object with metadata about the package containing the specified module. PackageInfo has the following fields: 144 | 145 | * **packageJson**: The package.json file for the package 146 | * **packageJsonPath**: The path to the package.json file 147 | * **root**: The root directory of the package 148 | * **resolvedMainField**: Which main field was used during resolution (see the mainFields option) 149 | * **browserMappedMain**: Whether the browser map was used to resolve the module's entry point 150 | * **resolvedEntrypoint**: The resolved entry point to the module with respect to the mainFields configuration and browser mappings. 151 | 152 | This object is populated during the `resolve` hook, so plugins should only depend on this information being present in hooks that run after `resolve`. 153 | 154 | 155 | ## Usage from Other Plugins 156 | 157 | `getPackageInfoForId` is exposed as a method on the plugin object along side the other hooks expected of a Rollup plugin. 158 | 159 | ```js 160 | import resolve from 'rollup-plugin-node-resolve'; 161 | const resolve = resolve(); 162 | 163 | export default ({ 164 | input: ..., 165 | plugins: [ 166 | resolve(), 167 | // custom plugin 168 | { 169 | transform(code, id) { 170 | // get package info for this module id 171 | const info = resolve.getPackageInfoForId(id); 172 | 173 | // if it's the buffer shim, return nothing. 174 | if (info.packageJson.name === 'buffer') { 175 | return ''; 176 | } 177 | 178 | return code; 179 | } 180 | } 181 | ], 182 | output: ... 183 | }) 184 | ``` 185 | 186 | If you're writing a standalone plugin, you can get access to the plugin object by pulling it out of the config provided to the `buildStart` hook: 187 | 188 | ```js 189 | 190 | export default function { 191 | let nodeResolvePlugin; 192 | 193 | function getPackageInfoForId(id) { 194 | // user config isn't using this plugin 195 | if (!nodeResolvePlugin) return; 196 | 197 | // user config has an older version without this API 198 | if (!nodeResolvePlugin.getPackageInfoForId) return; 199 | 200 | return nodeResolvePlugin.getPackageInfoForId(id); 201 | } 202 | 203 | return { 204 | buildStart (options) { 205 | nodeResolvePlugin = options.plugins && options.plugins.filter(p => p.name === 'node-resolve')[0]; 206 | }, 207 | transform (code, id) { 208 | const info = getPackageInfoForId(id); 209 | // ... 210 | } 211 | } 212 | } 213 | ``` 214 | 215 | ## License 216 | 217 | MIT 218 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # http://www.appveyor.com/docs/appveyor-yml 2 | 3 | version: "{build}" 4 | 5 | clone_depth: 10 6 | 7 | init: 8 | - git config --global core.autocrlf false 9 | 10 | environment: 11 | matrix: 12 | # node.js 13 | - nodejs_version: stable 14 | 15 | install: 16 | - ps: Install-Product node $env:nodejs_version 17 | - npm install 18 | 19 | build: off 20 | 21 | test_script: 22 | - node --version && npm --version 23 | - npm test 24 | 25 | matrix: 26 | fast_finish: false 27 | 28 | # cache: 29 | # - C:\Users\appveyor\AppData\Roaming\npm-cache -> package.json # npm cache 30 | # - node_modules -> package.json # local npm modules 31 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import {Plugin} from 'rollup'; 2 | import {AsyncOpts} from 'resolve'; 3 | 4 | export interface Options { 5 | /** 6 | * the fields to scan in a package.json to determine the entry point 7 | * if this list contains "browser", overrides specified in "pkg.browser" 8 | * will be used 9 | * @default ['module', 'main'] 10 | */ 11 | mainFields?: ReadonlyArray; 12 | 13 | /** 14 | * @deprecated use "mainFields" instead 15 | * use "module" field for ES6 module if possible 16 | * @default true 17 | */ 18 | module?: boolean; 19 | 20 | /** 21 | * @deprecated use "mainFields" instead 22 | * use "jsnext:main" if possible 23 | * legacy field pointing to ES6 module in third-party libraries, 24 | * deprecated in favor of "pkg.module": 25 | * - see: https://github.com/rollup/rollup/wiki/pkg.module 26 | * @default false 27 | */ 28 | jsnext?: boolean; 29 | 30 | /** 31 | * @deprecated use "mainFields" instead 32 | * use "main" field or index.js, even if it's not an ES6 module 33 | * (needs to be converted from CommonJS to ES6) 34 | * – see https://github.com/rollup/rollup-plugin-commonjs 35 | * @default true 36 | */ 37 | main?: boolean; 38 | 39 | /** 40 | * some package.json files have a "browser" field which specifies 41 | * alternative files to load for people bundling for the browser. If 42 | * that's you, either use this option or add "browser" to the 43 | * "mainfields" option, otherwise pkg.browser will be ignored 44 | * @default false 45 | */ 46 | browser?: boolean; 47 | 48 | /** 49 | * not all files you want to resolve are .js files 50 | * @default [ '.mjs', '.js', '.json', '.node' ] 51 | */ 52 | extensions?: ReadonlyArray; 53 | 54 | /** 55 | * whether to prefer built-in modules (e.g. `fs`, `path`) or 56 | * local ones with the same names 57 | * @default true 58 | */ 59 | preferBuiltins?: boolean; 60 | 61 | /** 62 | * Lock the module search in this path (like a chroot). Module defined 63 | * outside this path will be marked as external 64 | * @default '/' 65 | */ 66 | jail?: string; 67 | 68 | /** 69 | * Set to an array of strings and/or regexps to lock the module search 70 | * to modules that match at least one entry. Modules not matching any 71 | * entry will be marked as external 72 | * @default null 73 | */ 74 | only?: ReadonlyArray | null; 75 | 76 | /** 77 | * If true, inspect resolved files to check that they are 78 | * ES2015 modules 79 | * @default false 80 | */ 81 | modulesOnly?: boolean; 82 | 83 | /** 84 | * Force resolving for these modules to root's node_modules that helps 85 | * to prevent bundling the same package multiple times if package is 86 | * imported from dependencies. 87 | */ 88 | dedupe?: string[] | ((importee: string) => boolean); 89 | 90 | /** 91 | * Any additional options that should be passed through 92 | * to node-resolve 93 | */ 94 | customResolveOptions?: AsyncOpts; 95 | } 96 | 97 | /** 98 | * Locate modules using the Node resolution algorithm, for using third party modules in node_modules 99 | */ 100 | export default function nodeResolve(options?: Options): Plugin; 101 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-plugin-node-resolve", 3 | "description": "Bundle third-party dependencies in node_modules", 4 | "version": "5.2.0", 5 | "devDependencies": { 6 | "@babel/core": "^7.4.5", 7 | "@babel/preset-env": "^7.4.5", 8 | "@babel/register": "^7.4.4", 9 | "es5-ext": "^0.10.50", 10 | "eslint": "^5.16.0", 11 | "mocha": "^6.1.4", 12 | "rollup": "^1.16.1", 13 | "rollup-plugin-babel": "^4.3.2", 14 | "rollup-plugin-commonjs": "^10.0.0", 15 | "rollup-plugin-json": "^4.0.0", 16 | "string-capitalize": "^1.0.1", 17 | "typescript": "^3.5.2" 18 | }, 19 | "main": "dist/rollup-plugin-node-resolve.cjs.js", 20 | "module": "dist/rollup-plugin-node-resolve.es.js", 21 | "jsnext:main": "dist/rollup-plugin-node-resolve.es.js", 22 | "scripts": { 23 | "build": "rollup -c", 24 | "pretest": "npm run build", 25 | "test": "mocha", 26 | "posttest": "tsc && eslint src test/*.js", 27 | "lint": "eslint src", 28 | "prepublishOnly": "npm test", 29 | "prepare": "npm run build" 30 | }, 31 | "files": [ 32 | "src", 33 | "dist", 34 | "index.d.ts" 35 | ], 36 | "dependencies": { 37 | "@types/resolve": "0.0.8", 38 | "builtin-modules": "^3.1.0", 39 | "is-module": "^1.0.0", 40 | "resolve": "^1.11.1", 41 | "rollup-pluginutils": "^2.8.1" 42 | }, 43 | "peerDependencies": { 44 | "rollup": ">=1.11.0" 45 | }, 46 | "repository": "rollup/rollup-plugin-node-resolve", 47 | "keywords": [ 48 | "rollup", 49 | "rollup-plugin", 50 | "es2015", 51 | "npm", 52 | "modules" 53 | ], 54 | "author": "Rich Harris ", 55 | "license": "MIT", 56 | "bugs": { 57 | "url": "https://github.com/rollup/rollup-plugin-node-resolve/issues" 58 | }, 59 | "homepage": "https://github.com/rollup/rollup-plugin-node-resolve#readme" 60 | } 61 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import babel from 'rollup-plugin-babel'; 2 | import json from 'rollup-plugin-json'; 3 | 4 | export default { 5 | input: 'src/index.js', 6 | plugins: [ 7 | json(), 8 | babel({ 9 | presets: [['@babel/preset-env', { 10 | targets: { 11 | node: 6 12 | } 13 | }] 14 | ] 15 | }) 16 | ], 17 | external: [ 'path', 'fs', 'builtin-modules', 'resolve', 'browser-resolve', 'is-module', 'rollup-pluginutils' ], 18 | output: [ 19 | { file: 'dist/rollup-plugin-node-resolve.cjs.js', format: 'cjs' }, 20 | { file: 'dist/rollup-plugin-node-resolve.es.js', format: 'es' } 21 | ] 22 | }; 23 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import {dirname, extname, join, normalize, resolve, sep} from 'path'; 2 | import builtinList from 'builtin-modules'; 3 | import resolveId from 'resolve'; 4 | import isModule from 'is-module'; 5 | import fs, { realpathSync } from 'fs'; 6 | import {createFilter} from 'rollup-pluginutils'; 7 | import {peerDependencies} from '../package.json'; 8 | 9 | const builtins = new Set(builtinList); 10 | 11 | const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js'; 12 | // It is important that .mjs occur before .js so that Rollup will interpret npm modules 13 | // which deploy both ESM .mjs and CommonJS .js files as ESM. 14 | const DEFAULT_EXTS = [ '.mjs', '.js', '.json', '.node' ]; 15 | 16 | const existsAsync = file => new Promise(fulfil => fs.exists(file, fulfil)); 17 | 18 | const readFileAsync = file => new Promise((fulfil, reject) => fs.readFile(file, (err, contents) => err ? reject(err) : fulfil(contents))); 19 | 20 | const realpathAsync = file => new Promise((fulfil, reject) => fs.realpath(file, (err, contents) => err ? reject(err) : fulfil(contents))); 21 | 22 | const statAsync = file => new Promise((fulfil, reject) => fs.stat(file, (err, contents) => err ? reject(err) : fulfil(contents))); 23 | 24 | const cache = fn => { 25 | const cache = new Map(); 26 | const wrapped = (param, done) => { 27 | if (cache.has(param) === false) { 28 | cache.set(param, fn(param).catch(err => { 29 | cache.delete(param); 30 | throw err; 31 | })); 32 | } 33 | return cache.get(param).then(result => done(null, result), done); 34 | }; 35 | wrapped.clear = () => cache.clear(); 36 | return wrapped; 37 | }; 38 | 39 | const ignoreENOENT = err => { 40 | if (err.code === 'ENOENT') return false; 41 | throw err; 42 | }; 43 | 44 | const readFileCached = cache(readFileAsync); 45 | 46 | const isDirCached = cache(file => statAsync(file).then(stat => stat.isDirectory(), ignoreENOENT)); 47 | 48 | const isFileCached = cache(file => statAsync(file).then(stat => stat.isFile(), ignoreENOENT)); 49 | 50 | function getMainFields (options) { 51 | let mainFields; 52 | if (options.mainFields) { 53 | if ('module' in options || 'main' in options || 'jsnext' in options) { 54 | throw new Error(`node-resolve: do not use deprecated 'module', 'main', 'jsnext' options with 'mainFields'`); 55 | } 56 | mainFields = options.mainFields; 57 | } else { 58 | mainFields = []; 59 | [['module', 'module', true], ['jsnext', 'jsnext:main', false], ['main', 'main', true]].forEach(([option, field, defaultIncluded]) => { 60 | if (option in options) { 61 | // eslint-disable-next-line no-console 62 | console.warn(`node-resolve: setting options.${option} is deprecated, please override options.mainFields instead`); 63 | if (options[option]) { 64 | mainFields.push(field); 65 | } 66 | } else if (defaultIncluded) { 67 | mainFields.push(field); 68 | } 69 | }); 70 | } 71 | if (options.browser && mainFields.indexOf('browser') === -1) { 72 | return ['browser'].concat(mainFields); 73 | } 74 | if ( !mainFields.length ) { 75 | throw new Error( `Please ensure at least one 'mainFields' value is specified` ); 76 | } 77 | return mainFields; 78 | } 79 | 80 | const alwaysNull = () => null; 81 | 82 | const resolveIdAsync = (file, opts) => new Promise((fulfil, reject) => resolveId(file, opts, (err, contents) => err ? reject(err) : fulfil(contents))); 83 | 84 | // Resolve module specifiers in order. Promise resolves to the first 85 | // module that resolves successfully, or the error that resulted from 86 | // the last attempted module resolution. 87 | function resolveImportSpecifiers (importSpecifierList, resolveOptions) { 88 | let p = Promise.resolve(); 89 | for (let i = 0; i < importSpecifierList.length; i++) { 90 | p = p.then(v => { 91 | // if we've already resolved to something, just return it. 92 | if (v) return v; 93 | 94 | return resolveIdAsync(importSpecifierList[i], resolveOptions); 95 | }); 96 | 97 | if (i < importSpecifierList.length - 1) { 98 | // swallow MODULE_NOT_FOUND errors from all but the last resolution 99 | p = p.catch(err => { 100 | if (err.code !== 'MODULE_NOT_FOUND') { 101 | throw err; 102 | } 103 | }); 104 | } 105 | } 106 | 107 | return p; 108 | } 109 | 110 | export default function nodeResolve ( options = {} ) { 111 | const mainFields = getMainFields(options); 112 | const useBrowserOverrides = mainFields.indexOf('browser') !== -1; 113 | const dedupe = options.dedupe || []; 114 | const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false; 115 | const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true; 116 | const customResolveOptions = options.customResolveOptions || {}; 117 | const jail = options.jail; 118 | const only = Array.isArray(options.only) 119 | ? options.only.map(o => o instanceof RegExp 120 | ? o 121 | : new RegExp('^' + String(o).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&') + '$') 122 | ) 123 | : null; 124 | const browserMapCache = new Map(); 125 | 126 | if ( options.skip ) { 127 | throw new Error( 'options.skip is no longer supported — you should use the main Rollup `external` option instead' ); 128 | } 129 | 130 | const extensions = options.extensions || DEFAULT_EXTS; 131 | const packageInfoCache = new Map(); 132 | const idToPackageInfo = new Map(); 133 | 134 | const shouldDedupe = typeof dedupe === 'function' 135 | ? dedupe 136 | : importee => dedupe.includes(importee); 137 | 138 | function getCachedPackageInfo (pkg, pkgPath) { 139 | if (packageInfoCache.has(pkgPath)) { 140 | return packageInfoCache.get(pkgPath); 141 | } 142 | 143 | // browserify/resolve doesn't realpath paths returned in its packageFilter callback 144 | if (!preserveSymlinks) { 145 | pkgPath = realpathSync(pkgPath); 146 | } 147 | 148 | const pkgRoot = dirname( pkgPath ); 149 | 150 | const packageInfo = { 151 | // copy as we are about to munge the `main` field of `pkg`. 152 | packageJson: Object.assign({}, pkg), 153 | 154 | // path to package.json file 155 | packageJsonPath: pkgPath, 156 | 157 | // directory containing the package.json 158 | root: pkgRoot, 159 | 160 | // which main field was used during resolution of this module (main, module, or browser) 161 | resolvedMainField: 'main', 162 | 163 | // whether the browser map was used to resolve the entry point to this module 164 | browserMappedMain: false, 165 | 166 | // the entry point of the module with respect to the selected main field and any 167 | // relevant browser mappings. 168 | resolvedEntryPoint: '' 169 | }; 170 | 171 | let overriddenMain = false; 172 | for ( let i = 0; i < mainFields.length; i++ ) { 173 | const field = mainFields[i]; 174 | if ( typeof pkg[ field ] === 'string' ) { 175 | pkg[ 'main' ] = pkg[ field ]; 176 | packageInfo.resolvedMainField = field; 177 | overriddenMain = true; 178 | break; 179 | } 180 | } 181 | 182 | const internalPackageInfo = { 183 | cachedPkg: pkg, 184 | hasModuleSideEffects: alwaysNull, 185 | hasPackageEntry: overriddenMain !== false || mainFields.indexOf( 'main' ) !== -1, 186 | packageBrowserField: useBrowserOverrides && typeof pkg[ 'browser' ] === 'object' && 187 | Object.keys(pkg[ 'browser' ]).reduce((browser, key) => { 188 | let resolved = pkg[ 'browser' ][ key ]; 189 | if (resolved && resolved[0] === '.') { 190 | resolved = resolve( pkgRoot, resolved ); 191 | } 192 | browser[ key ] = resolved; 193 | if ( key[0] === '.' ) { 194 | const absoluteKey = resolve( pkgRoot, key ); 195 | browser[ absoluteKey ] = resolved; 196 | if ( !extname(key) ) { 197 | extensions.reduce( ( browser, ext ) => { 198 | browser[ absoluteKey + ext ] = browser[ key ]; 199 | return browser; 200 | }, browser ); 201 | } 202 | } 203 | return browser; 204 | }, {}), 205 | packageInfo 206 | }; 207 | 208 | const browserMap = internalPackageInfo.packageBrowserField; 209 | if (useBrowserOverrides && typeof pkg['browser'] === 'object' && browserMap.hasOwnProperty(pkg.main)) { 210 | packageInfo.resolvedEntryPoint = browserMap[pkg.main]; 211 | packageInfo.browserMappedMain = true; 212 | } else { 213 | // index.node is technically a valid default entrypoint as well... 214 | packageInfo.resolvedEntryPoint = resolve(pkgRoot, pkg.main || 'index.js'); 215 | packageInfo.browserMappedMain = false; 216 | } 217 | 218 | const packageSideEffects = pkg['sideEffects']; 219 | if (typeof packageSideEffects === 'boolean') { 220 | internalPackageInfo.hasModuleSideEffects = () => packageSideEffects; 221 | } else if (Array.isArray(packageSideEffects)) { 222 | internalPackageInfo.hasModuleSideEffects = createFilter(packageSideEffects, null, {resolve: pkgRoot}); 223 | } 224 | 225 | packageInfoCache.set(pkgPath, internalPackageInfo); 226 | return internalPackageInfo; 227 | } 228 | 229 | let preserveSymlinks; 230 | 231 | return { 232 | name: 'node-resolve', 233 | 234 | buildStart ( options ) { 235 | preserveSymlinks = options.preserveSymlinks; 236 | const [major, minor] = this.meta.rollupVersion.split('.').map(Number); 237 | const minVersion = peerDependencies.rollup.slice(2); 238 | const [minMajor, minMinor] = minVersion.split('.').map(Number); 239 | if (major < minMajor || (major === minMajor && minor < minMinor)) { 240 | this.error( 241 | `Insufficient Rollup version: "rollup-plugin-node-resolve" requires at least rollup@${minVersion} but found rollup@${ 242 | this.meta.rollupVersion 243 | }.` 244 | ); 245 | } 246 | }, 247 | 248 | generateBundle () { 249 | readFileCached.clear(); 250 | isFileCached.clear(); 251 | isDirCached.clear(); 252 | }, 253 | 254 | resolveId ( importee, importer ) { 255 | if (importee === ES6_BROWSER_EMPTY) { 256 | return importee; 257 | } 258 | 259 | if ( /\0/.test( importee ) ) return null; // ignore IDs with null character, these belong to other plugins 260 | 261 | const basedir = importer ? dirname( importer ) : process.cwd(); 262 | 263 | if (shouldDedupe(importee)) { 264 | importee = join(process.cwd(), 'node_modules', importee); 265 | } 266 | 267 | // https://github.com/defunctzombie/package-browser-field-spec 268 | const browser = browserMapCache.get(importer); 269 | if (useBrowserOverrides && browser) { 270 | const resolvedImportee = resolve( basedir, importee ); 271 | if (browser[importee] === false || browser[resolvedImportee] === false) { 272 | return ES6_BROWSER_EMPTY; 273 | } 274 | const browserImportee = browser[importee] || browser[resolvedImportee] || browser[resolvedImportee + '.js'] || browser[resolvedImportee + '.json']; 275 | if (browserImportee) { 276 | importee = browserImportee; 277 | } 278 | } 279 | 280 | const parts = importee.split( /[/\\]/ ); 281 | let id = parts.shift(); 282 | 283 | if ( id[0] === '@' && parts.length > 0 ) { 284 | // scoped packages 285 | id += `/${parts.shift()}`; 286 | } else if ( id[0] === '.' ) { 287 | // an import relative to the parent dir of the importer 288 | id = resolve( basedir, importee ); 289 | } 290 | 291 | if (only && !only.some(pattern => pattern.test(id))) return null; 292 | 293 | let hasModuleSideEffects = alwaysNull; 294 | let hasPackageEntry = true; 295 | let packageBrowserField = false; 296 | let packageInfo = undefined; 297 | 298 | const resolveOptions = { 299 | basedir, 300 | packageFilter ( pkg, pkgPath ) { 301 | let cachedPkg; 302 | ({packageInfo, cachedPkg, hasModuleSideEffects, hasPackageEntry, packageBrowserField} = 303 | getCachedPackageInfo(pkg, pkgPath)); 304 | 305 | return cachedPkg; 306 | }, 307 | readFile: readFileCached, 308 | isFile: isFileCached, 309 | isDirectory: isDirCached, 310 | extensions: extensions 311 | }; 312 | 313 | if (preserveSymlinks !== undefined) { 314 | resolveOptions.preserveSymlinks = preserveSymlinks; 315 | } 316 | 317 | const importSpecifierList = []; 318 | 319 | if (importer === undefined && !importee[0].match(/^\.?\.?\//)) { 320 | // For module graph roots (i.e. when importer is undefined), we 321 | // need to handle 'path fragments` like `foo/bar` that are commonly 322 | // found in rollup config files. If importee doesn't look like a 323 | // relative or absolute path, we make it relative and attempt to 324 | // resolve it. If we don't find anything, we try resolving it as we 325 | // got it. 326 | importSpecifierList.push('./' + importee); 327 | } 328 | 329 | const importeeIsBuiltin = builtins.has(importee); 330 | 331 | if (importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet)) { 332 | // The `resolve` library will not resolve packages with the same 333 | // name as a node built-in module. If we're resolving something 334 | // that's a builtin, and we don't prefer to find built-ins, we 335 | // first try to look up a local module with that name. If we don't 336 | // find anything, we resolve the builtin which just returns back 337 | // the built-in's name. 338 | importSpecifierList.push(importee + '/'); 339 | } 340 | 341 | importSpecifierList.push(importee); 342 | return resolveImportSpecifiers( 343 | importSpecifierList, 344 | Object.assign(resolveOptions, customResolveOptions) 345 | ) 346 | .then(resolved => { 347 | if ( resolved && packageBrowserField ) { 348 | if ( Object.prototype.hasOwnProperty.call(packageBrowserField,resolved) ) { 349 | if (!packageBrowserField[resolved]) { 350 | browserMapCache.set(resolved, packageBrowserField); 351 | return ES6_BROWSER_EMPTY; 352 | } 353 | resolved = packageBrowserField[ resolved ]; 354 | } 355 | browserMapCache.set(resolved, packageBrowserField); 356 | } 357 | 358 | if ( hasPackageEntry && !preserveSymlinks && resolved ) { 359 | return existsAsync( resolved ) 360 | .then(exists => exists ? realpathAsync( resolved ) : resolved); 361 | } 362 | return resolved; 363 | }) 364 | .then(resolved => { 365 | idToPackageInfo.set(resolved, packageInfo); 366 | 367 | if ( hasPackageEntry ) { 368 | if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) { 369 | return null; 370 | } else if (importeeIsBuiltin && preferBuiltins) { 371 | if ( !isPreferBuiltinsSet ) { 372 | this.warn( 373 | `preferring built-in module '${importee}' over local alternative ` + 374 | `at '${resolved}', pass 'preferBuiltins: false' to disable this ` + 375 | `behavior or 'preferBuiltins: true' to disable this warning` 376 | ); 377 | } 378 | return null; 379 | } else if ( jail && resolved.indexOf( normalize( jail.trim( sep ) ) ) !== 0 ) { 380 | return null; 381 | } 382 | } 383 | 384 | if ( resolved && options.modulesOnly ) { 385 | return readFileAsync( resolved, 'utf-8') 386 | .then(code => isModule( code ) ? {id: resolved, moduleSideEffects: hasModuleSideEffects(resolved)} : null); 387 | } else { 388 | return {id: resolved, moduleSideEffects: hasModuleSideEffects(resolved)}; 389 | } 390 | }) 391 | .catch(() => null); 392 | }, 393 | 394 | load ( importee ) { 395 | if ( importee === ES6_BROWSER_EMPTY ) { 396 | return 'export default {};'; 397 | } 398 | return null; 399 | }, 400 | 401 | getPackageInfoForId (id) { 402 | return idToPackageInfo.get(id); 403 | } 404 | }; 405 | } 406 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --require @babel/register 2 | -------------------------------------------------------------------------------- /test/node_modules/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rollup/rollup-plugin-node-resolve/daa51892f9b19dac46dffbdfe986d768543b6a2f/test/node_modules/.gitkeep -------------------------------------------------------------------------------- /test/node_modules/@scoped/bar/index.js: -------------------------------------------------------------------------------- 1 | export default 'BAR'; 2 | -------------------------------------------------------------------------------- /test/node_modules/@scoped/foo/index.js: -------------------------------------------------------------------------------- 1 | export default 'FOO'; 2 | -------------------------------------------------------------------------------- /test/node_modules/browser-entry-points-to-node-module/index.js: -------------------------------------------------------------------------------- 1 | import dep from 'type'; 2 | export default dep; 3 | -------------------------------------------------------------------------------- /test/node_modules/browser-entry-points-to-node-module/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "index.js", 3 | "browser": { 4 | "type": "component-type" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/node_modules/builtins-browser/fs.js: -------------------------------------------------------------------------------- 1 | export default 'browser-fs'; 2 | -------------------------------------------------------------------------------- /test/node_modules/builtins-browser/index.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | export default fs; 3 | -------------------------------------------------------------------------------- /test/node_modules/builtins-browser/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./index.js", 3 | "browser": { 4 | "fs": "./fs.js" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/node_modules/component-type/index.js: -------------------------------------------------------------------------------- 1 | export default 'component-type'; 2 | -------------------------------------------------------------------------------- /test/node_modules/component-type/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "index.js" 3 | } 4 | -------------------------------------------------------------------------------- /test/node_modules/dual-cjs-mjs/entry.js: -------------------------------------------------------------------------------- 1 | module.exports = 'DUAL-CJS'; 2 | -------------------------------------------------------------------------------- /test/node_modules/dual-cjs-mjs/entry.mjs: -------------------------------------------------------------------------------- 1 | export default 'DUAL-MJS'; 2 | -------------------------------------------------------------------------------- /test/node_modules/dual-cjs-mjs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "entry" 3 | } -------------------------------------------------------------------------------- /test/node_modules/entries/jsnext-entry.js: -------------------------------------------------------------------------------- 1 | export default 'JSNEXT-ENTRY'; 2 | -------------------------------------------------------------------------------- /test/node_modules/entries/main-entry.js: -------------------------------------------------------------------------------- 1 | export default 'MAIN-ENTRY'; 2 | -------------------------------------------------------------------------------- /test/node_modules/entries/module-entry.js: -------------------------------------------------------------------------------- 1 | export default 'MODULE-ENTRY'; 2 | -------------------------------------------------------------------------------- /test/node_modules/entries/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "main-entry.js", 3 | "module": "module-entry.js", 4 | "jsnext:main": "jsnext-entry.js" 5 | } 6 | -------------------------------------------------------------------------------- /test/node_modules/events/index.js: -------------------------------------------------------------------------------- 1 | export default 'not the built-in events module'; 2 | -------------------------------------------------------------------------------- /test/node_modules/events/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./index.js" 3 | } 4 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-nested/lib/browser-dep.js: -------------------------------------------------------------------------------- 1 | export default 'browser-dep'; 2 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-nested/lib/browser-test.js: -------------------------------------------------------------------------------- 1 | export default 43; 2 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-nested/lib/browser.js: -------------------------------------------------------------------------------- 1 | import dep from './dep.js'; 2 | import test from './test'; 3 | 4 | const env = 'browser'; 5 | export { env, dep, test }; 6 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-nested/lib/dep.js: -------------------------------------------------------------------------------- 1 | export default 'node-dep'; 2 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-nested/lib/index.js: -------------------------------------------------------------------------------- 1 | import dep from './dep.js'; 2 | import test from 'test'; 3 | 4 | const env = 'node'; 5 | export { env, dep, test }; 6 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-nested/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./lib/index.js", 3 | "browser": { 4 | "./lib/index.js": "./lib/browser.js", 5 | "./lib/dep.js": "./lib/browser-dep.js", 6 | "./lib/test": "./lib/browser-test.js" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-main-implicit/browser.js: -------------------------------------------------------------------------------- 1 | const env = 'browser'; 2 | export { env }; 3 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-main-implicit/index.js: -------------------------------------------------------------------------------- 1 | const env = 'node'; 2 | export { env }; 3 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-main-implicit/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "browser": { 3 | "./index.js": "./browser.js" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-main/browser-dep.js: -------------------------------------------------------------------------------- 1 | export default 'browser-dep'; 2 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-main/browser-test.js: -------------------------------------------------------------------------------- 1 | export default 43; 2 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-main/browser.js: -------------------------------------------------------------------------------- 1 | import dep from './dep.js'; 2 | import test from 'test'; 3 | 4 | const env = 'browser'; 5 | export { env, dep, test }; 6 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-main/dep.js: -------------------------------------------------------------------------------- 1 | export default 'node-dep'; 2 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-main/index.js: -------------------------------------------------------------------------------- 1 | import dep from './dep.js'; 2 | import test from 'test'; 3 | 4 | const env = 'node'; 5 | export { env, dep, test }; 6 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-main/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "index.js", 3 | "browser": { 4 | "./index.js": "./browser.js", 5 | "./dep.js": "./browser-dep.js", 6 | "test": "./browser-test.js" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-with-false/lib/client/http-tracker.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = function(client, announceUrl) { 3 | this.name = 'http-tracker' 4 | } 5 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-with-false/lib/client/udp-tracker.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = function(client, announceUrl) { 3 | this.name = 'udp-tracker' 4 | } 5 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-with-false/lib/client/websocket-tracker.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = function(client, announceUrl) { 3 | this.name = 'websocket-tracker' 4 | } 5 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-with-false/lib/index.js: -------------------------------------------------------------------------------- 1 | // sample code inspired by npm: bittorrent-tracker-client 2 | 3 | var HTTPTracker = require('./client/http-tracker') // empty object in browser 4 | var UDPTracker = require('./client/udp-tracker') // empty object in browser 5 | var WebSocketTracker = require('./client/websocket-tracker') 6 | 7 | function Client(protocol, announceUrl) { 8 | var self = this; 9 | if ((protocol === 'http:' || protocol === 'https:') && 10 | typeof HTTPTracker === 'function') { 11 | return new HTTPTracker(self, announceUrl) 12 | } else if (protocol === 'udp:' && typeof UDPTracker === 'function') { 13 | return new UDPTracker(self, announceUrl) 14 | } else if ((protocol === 'ws:' || protocol === 'wss:')) { 15 | return new WebSocketTracker(self, announceUrl) 16 | } 17 | this.name = 'NULL'; 18 | } 19 | 20 | module.exports = Client 21 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-with-false/lib/subpath/foo/index.js: -------------------------------------------------------------------------------- 1 | var HTTPTracker = require('../../client/http-tracker') // empty object in browser 2 | module.exports = HTTPTracker 3 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object-with-false/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./lib/index.js", 3 | "browser": { 4 | "./lib/common.js": "./lib/common-browser.js", 5 | "./lib/client/http-tracker.js": false, 6 | "./lib/client/udp-tracker.js": false 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object/browser-dep.js: -------------------------------------------------------------------------------- 1 | export default 'browser-dep'; 2 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object/browser-test.js: -------------------------------------------------------------------------------- 1 | export default 43; 2 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object/browser.js: -------------------------------------------------------------------------------- 1 | import dep from './dep.js'; 2 | import test from 'test'; 3 | 4 | const env = 'browser'; 5 | export { env, dep, test }; 6 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object/dep.js: -------------------------------------------------------------------------------- 1 | export default 'node-dep'; 2 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object/index.js: -------------------------------------------------------------------------------- 1 | import dep from './dep.js'; 2 | import test from 'test'; 3 | 4 | const env = 'node'; 5 | export { env, dep, test }; 6 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic-object/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./index.js", 3 | "browser": { 4 | "./index.js": "./browser.js", 5 | "./dep.js": "./browser-dep.js", 6 | "test": "./browser-test.js" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic/browser.js: -------------------------------------------------------------------------------- 1 | export default 'browser'; 2 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic/node.js: -------------------------------------------------------------------------------- 1 | export default 'node'; 2 | -------------------------------------------------------------------------------- /test/node_modules/isomorphic/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./node.js", 3 | "browser": "./browser.js" 4 | } 5 | -------------------------------------------------------------------------------- /test/node_modules/jsnext/entry-main.js: -------------------------------------------------------------------------------- 1 | export default 'MAIN'; 2 | -------------------------------------------------------------------------------- /test/node_modules/jsnext/entry.js: -------------------------------------------------------------------------------- 1 | export default 'JSNEXT'; 2 | -------------------------------------------------------------------------------- /test/node_modules/jsnext/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "jsnext:main": "entry.js", 3 | "main": "entry-main.js" 4 | } 5 | -------------------------------------------------------------------------------- /test/node_modules/legacy/entry.js: -------------------------------------------------------------------------------- 1 | export default 'LEGACY'; 2 | -------------------------------------------------------------------------------- /test/node_modules/legacy/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "entry.js" 3 | } 4 | -------------------------------------------------------------------------------- /test/node_modules/module-mjs/entry.mjs: -------------------------------------------------------------------------------- 1 | export default 'MODULE-MJS'; 2 | -------------------------------------------------------------------------------- /test/node_modules/module-mjs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "entry" 3 | } -------------------------------------------------------------------------------- /test/node_modules/module/entry-main.js: -------------------------------------------------------------------------------- 1 | export default 'MAIN'; 2 | -------------------------------------------------------------------------------- /test/node_modules/module/entry.js: -------------------------------------------------------------------------------- 1 | export default 'MODULE'; 2 | -------------------------------------------------------------------------------- /test/node_modules/module/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "entry-main.js", 3 | "module": "entry.js" 4 | } 5 | -------------------------------------------------------------------------------- /test/node_modules/react-consumer/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | export default 'react-consumer:' + React 4 | -------------------------------------------------------------------------------- /test/node_modules/react-consumer/node_modules/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rollup/rollup-plugin-node-resolve/daa51892f9b19dac46dffbdfe986d768543b6a2f/test/node_modules/react-consumer/node_modules/.gitkeep -------------------------------------------------------------------------------- /test/node_modules/react-consumer/node_modules/react/index.js: -------------------------------------------------------------------------------- 1 | export default 'react:child' -------------------------------------------------------------------------------- /test/node_modules/react/index.js: -------------------------------------------------------------------------------- 1 | export default 'react:root' 2 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-array/dep1.js: -------------------------------------------------------------------------------- 1 | global.sideEffects = global.sideEffects || []; 2 | global.sideEffects.push('array-dep1'); 3 | 4 | export const value = 'dep1'; 5 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-array/dep2.js: -------------------------------------------------------------------------------- 1 | global.sideEffects = global.sideEffects || []; 2 | global.sideEffects.push('array-dep2'); 3 | 4 | export const value = 'dep2'; 5 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-array/dep3-effect.js: -------------------------------------------------------------------------------- 1 | global.sideEffects = global.sideEffects || []; 2 | global.sideEffects.push('array-dep3'); 3 | 4 | export const value = 'dep3'; 5 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-array/index.js: -------------------------------------------------------------------------------- 1 | global.sideEffects = global.sideEffects || []; 2 | global.sideEffects.push('array-index'); 3 | 4 | export {value as value1} from './dep1.js'; 5 | export {value as value2} from './dep2.js'; 6 | export {value as value3} from './dep3-effect.js'; 7 | export {value as value4} from './nested/dep4'; 8 | export {value as value5} from './nested/dep5-effect'; 9 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-array/nested/dep4.js: -------------------------------------------------------------------------------- 1 | global.sideEffects = global.sideEffects || []; 2 | global.sideEffects.push('array-dep4'); 3 | 4 | export const value = 'dep3'; 5 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-array/nested/dep5-effect.js: -------------------------------------------------------------------------------- 1 | global.sideEffects = global.sideEffects || []; 2 | global.sideEffects.push('array-dep5'); 3 | 4 | export const value = 'dep3'; 5 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-array/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./index.js", 3 | "sideEffects": [ 4 | "./index.js", 5 | "./dep1.js", 6 | "**/*-effect.js" 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-false/dep1.js: -------------------------------------------------------------------------------- 1 | global.sideEffects = global.sideEffects || []; 2 | global.sideEffects.push('false-dep1'); 3 | 4 | export const value = 'dep1'; 5 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-false/dep2.js: -------------------------------------------------------------------------------- 1 | global.sideEffects = global.sideEffects || []; 2 | global.sideEffects.push('false-dep2'); 3 | 4 | export const value = 'dep2'; 5 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-false/index.js: -------------------------------------------------------------------------------- 1 | global.sideEffects = global.sideEffects || []; 2 | global.sideEffects.push('false-index'); 3 | 4 | export {value as value1} from './dep1.js'; 5 | export {value as value2} from './dep2.js'; 6 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-false/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./index.js", 3 | "sideEffects": false 4 | } 5 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-true/dep1.js: -------------------------------------------------------------------------------- 1 | global.sideEffects = global.sideEffects || []; 2 | global.sideEffects.push('true-dep1'); 3 | 4 | export const value = 'dep1'; 5 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-true/dep2.js: -------------------------------------------------------------------------------- 1 | global.sideEffects = global.sideEffects || []; 2 | global.sideEffects.push('true-dep2'); 3 | 4 | export const value = 'dep2'; 5 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-true/index.js: -------------------------------------------------------------------------------- 1 | global.sideEffects = global.sideEffects || []; 2 | global.sideEffects.push('true-index'); 3 | 4 | export {value as value1} from './dep1.js'; 5 | export {value as value2} from './dep2.js'; 6 | -------------------------------------------------------------------------------- /test/node_modules/side-effects-true/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./index.js", 3 | "sideEffects": true 4 | } 5 | -------------------------------------------------------------------------------- /test/node_modules/simple/index.js: -------------------------------------------------------------------------------- 1 | export default 42; 2 | -------------------------------------------------------------------------------- /test/node_modules/string/uppercase.js: -------------------------------------------------------------------------------- 1 | export default function uppercase ( str ) { 2 | return str.toUpperCase(); 3 | } 4 | -------------------------------------------------------------------------------- /test/node_modules/test/index.js: -------------------------------------------------------------------------------- 1 | export default 42; 2 | -------------------------------------------------------------------------------- /test/node_modules/uses-buffer/index.js: -------------------------------------------------------------------------------- 1 | import Buffer from 'buffer'; 2 | export default Buffer; 3 | -------------------------------------------------------------------------------- /test/node_modules/uses-buffer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "browser": { 3 | "buffer": false 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "test", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "test.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "browser": { 13 | "dummy-module": "component-type" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /test/samples/browser-entry-points-to-node-module/index.js: -------------------------------------------------------------------------------- 1 | import sample from 'browser-entry-points-to-node-module'; 2 | export default sample; 3 | -------------------------------------------------------------------------------- /test/samples/browser-false/main.js: -------------------------------------------------------------------------------- 1 | import thing from 'uses-buffer'; 2 | 3 | assert.deepEqual( thing, {} ); 4 | -------------------------------------------------------------------------------- /test/samples/browser-local/main.js: -------------------------------------------------------------------------------- 1 | // test browser mapped imports from the main entrypoint 2 | import s from 'dummy-module'; 3 | 4 | export default s; -------------------------------------------------------------------------------- /test/samples/browser-object-builtin/main.js: -------------------------------------------------------------------------------- 1 | import main from 'builtins-browser'; 2 | export default main; 3 | -------------------------------------------------------------------------------- /test/samples/browser-object-main/main.js: -------------------------------------------------------------------------------- 1 | import { env, dep, test } from 'isomorphic-object-main'; 2 | export { env, dep, test }; 3 | -------------------------------------------------------------------------------- /test/samples/browser-object-nested/main.js: -------------------------------------------------------------------------------- 1 | import { env, dep, test } from 'isomorphic-nested'; 2 | export { env, dep, test }; 3 | -------------------------------------------------------------------------------- /test/samples/browser-object-with-false/main.js: -------------------------------------------------------------------------------- 1 | import Client from 'isomorphic-object-with-false'; 2 | import HTTPTracker from 'isomorphic-object-with-false/lib/client/http-tracker'; 3 | import ES6_BROWSER_EMPTY from '\0node-resolve:empty.js'; 4 | import HTTPTrackerWithSubPath from 'isomorphic-object-with-false/lib/subpath/foo'; 5 | 6 | // do some assert 7 | 8 | assert.deepEqual(new Client('ws:'), { name: 'websocket-tracker' }) 9 | assert.deepEqual(new Client('http:'), { name: 'NULL' }) 10 | assert.equal(HTTPTracker, ES6_BROWSER_EMPTY); 11 | assert.equal(HTTPTrackerWithSubPath, ES6_BROWSER_EMPTY); 12 | 13 | 14 | // expose 15 | export default "ok" 16 | -------------------------------------------------------------------------------- /test/samples/browser-object/main-implicit.js: -------------------------------------------------------------------------------- 1 | import { env } from 'isomorphic-object-main-implicit'; 2 | export { env }; 3 | -------------------------------------------------------------------------------- /test/samples/browser-object/main.js: -------------------------------------------------------------------------------- 1 | import { env, dep, test } from 'isomorphic-object'; 2 | export { env, dep, test }; 3 | -------------------------------------------------------------------------------- /test/samples/browser/main.js: -------------------------------------------------------------------------------- 1 | import env from 'isomorphic'; 2 | 3 | export default env; 4 | -------------------------------------------------------------------------------- /test/samples/builtins/main.js: -------------------------------------------------------------------------------- 1 | import { sep } from 'path'; 2 | export default sep; 3 | -------------------------------------------------------------------------------- /test/samples/commonjs/main.js: -------------------------------------------------------------------------------- 1 | import capitalize from 'string-capitalize'; 2 | 3 | export default capitalize( 'it works!' ); 4 | -------------------------------------------------------------------------------- /test/samples/custom-resolve-options/js_modules/foo.js: -------------------------------------------------------------------------------- 1 | export default 42; -------------------------------------------------------------------------------- /test/samples/custom-resolve-options/main.js: -------------------------------------------------------------------------------- 1 | import foo from 'foo'; 2 | 3 | assert.equal( foo, 42 ); -------------------------------------------------------------------------------- /test/samples/deep-import-non-module/main.js: -------------------------------------------------------------------------------- 1 | import deep from 'foo/deep'; 2 | 3 | console.log( deep ); -------------------------------------------------------------------------------- /test/samples/dual-cjs-mjs/main.js: -------------------------------------------------------------------------------- 1 | import module from 'dual-cjs-mjs'; 2 | 3 | export default module; // MODULE 4 | -------------------------------------------------------------------------------- /test/samples/dynamic/main.js: -------------------------------------------------------------------------------- 1 | export default import('test'); 2 | -------------------------------------------------------------------------------- /test/samples/extensions/lol.wut: -------------------------------------------------------------------------------- 1 | export var answer = 42; 2 | -------------------------------------------------------------------------------- /test/samples/extensions/main.js: -------------------------------------------------------------------------------- 1 | import { answer } from './lol'; 2 | 3 | assert.equal( answer, 42 ); 4 | -------------------------------------------------------------------------------- /test/samples/granular/main.js: -------------------------------------------------------------------------------- 1 | import uppercase from 'string/uppercase.js'; 2 | 3 | export default uppercase( 'foo' ); 4 | -------------------------------------------------------------------------------- /test/samples/jail/main.js: -------------------------------------------------------------------------------- 1 | import uppercase from 'string/uppercase.js'; 2 | 3 | export default uppercase( 'foo' ); 4 | -------------------------------------------------------------------------------- /test/samples/jsnext/main.js: -------------------------------------------------------------------------------- 1 | import value from 'jsnext'; 2 | 3 | export default value; 4 | -------------------------------------------------------------------------------- /test/samples/local-index/forty-two/index.js: -------------------------------------------------------------------------------- 1 | export default 42; 2 | -------------------------------------------------------------------------------- /test/samples/local-index/main.js: -------------------------------------------------------------------------------- 1 | import fortyTwo from './forty-two'; 2 | 3 | export default fortyTwo; 4 | -------------------------------------------------------------------------------- /test/samples/manualchunks/main.js: -------------------------------------------------------------------------------- 1 | import simple from 'simple'; 2 | 3 | console.log(simple); -------------------------------------------------------------------------------- /test/samples/module-mjs/main.js: -------------------------------------------------------------------------------- 1 | import module from 'module-mjs'; 2 | 3 | export default module; // MODULE 4 | -------------------------------------------------------------------------------- /test/samples/module/main.js: -------------------------------------------------------------------------------- 1 | import module from 'module'; 2 | 3 | export default module; // MODULE 4 | -------------------------------------------------------------------------------- /test/samples/only/main.js: -------------------------------------------------------------------------------- 1 | import foo from '@scoped/foo'; 2 | import bar from '@scoped/bar'; 3 | import test from 'test'; 4 | 5 | console.log( foo ); 6 | console.log( bar ); 7 | console.log( test ); 8 | -------------------------------------------------------------------------------- /test/samples/package-index/main.js: -------------------------------------------------------------------------------- 1 | import 'es5-ext/object/set-prototype-of'; 2 | -------------------------------------------------------------------------------- /test/samples/prefer-builtin/main.js: -------------------------------------------------------------------------------- 1 | import 'events'; 2 | -------------------------------------------------------------------------------- /test/samples/prefer-jsnext/main.js: -------------------------------------------------------------------------------- 1 | import entry from 'entries'; 2 | 3 | export default entry; 4 | -------------------------------------------------------------------------------- /test/samples/prefer-main/main.js: -------------------------------------------------------------------------------- 1 | import entry from 'entries'; 2 | 3 | export default entry; 4 | -------------------------------------------------------------------------------- /test/samples/prefer-module/main.js: -------------------------------------------------------------------------------- 1 | import entry from 'entries'; 2 | 3 | export default entry; 4 | -------------------------------------------------------------------------------- /test/samples/react-app/main.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import ReactConsumer from 'react-consumer' 3 | 4 | export { React, ReactConsumer } 5 | -------------------------------------------------------------------------------- /test/samples/scoped/main.js: -------------------------------------------------------------------------------- 1 | import foo from '@scoped/foo'; 2 | 3 | foo(); 4 | -------------------------------------------------------------------------------- /test/samples/side-effects/main.js: -------------------------------------------------------------------------------- 1 | export {value1 as falseValue} from 'side-effects-false'; 2 | export {value1 as trueValue} from 'side-effects-true'; 3 | import 'side-effects-array'; 4 | -------------------------------------------------------------------------------- /test/samples/symlinked/first/index.js: -------------------------------------------------------------------------------- 1 | export { default as number1 } from 'second'; 2 | export { default as number2 } from 'third'; 3 | -------------------------------------------------------------------------------- /test/samples/symlinked/second/index.js: -------------------------------------------------------------------------------- 1 | import randomNumber from 'third'; 2 | 3 | export default randomNumber; 4 | -------------------------------------------------------------------------------- /test/samples/symlinked/third/index.js: -------------------------------------------------------------------------------- 1 | const randomNumber = Math.random(); 2 | 3 | export default randomNumber; 4 | -------------------------------------------------------------------------------- /test/samples/trailing-slash/main.js: -------------------------------------------------------------------------------- 1 | import capitalize from 'string-capitalize/'; 2 | 3 | export default capitalize( 'it works!' ); 4 | -------------------------------------------------------------------------------- /test/samples/unresolved-global/main.js: -------------------------------------------------------------------------------- 1 | import 'foo'; -------------------------------------------------------------------------------- /test/samples/unresolved-local/main.js: -------------------------------------------------------------------------------- 1 | import './foo'; -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | const path = require( 'path' ); 2 | const assert = require( 'assert' ); 3 | const rollup = require( 'rollup' ); 4 | const commonjs = require( 'rollup-plugin-commonjs' ); 5 | const babel = require( 'rollup-plugin-babel' ); 6 | const nodeResolve = require( '..' ); 7 | const fs = require( 'fs' ); 8 | 9 | process.chdir( __dirname ); 10 | 11 | function expectWarnings (warnings) { 12 | let warningIndex = 0; 13 | return warning => { 14 | if (warningIndex >= warnings.length) { 15 | throw new Error(`Unexpected warning: "${warning.message}"`); 16 | } else { 17 | const expectedWarning = warnings[warningIndex]; 18 | for (const key of Object.keys(expectedWarning)) { 19 | assert.strictEqual(warning[key], expectedWarning[key]); 20 | } 21 | } 22 | warningIndex++; 23 | }; 24 | } 25 | 26 | const expectNoWarnings = expectWarnings([]); 27 | 28 | function executeBundle ( bundle ) { 29 | return bundle.generate({ 30 | format: 'cjs' 31 | }).then( generated => { 32 | const fn = new Function ( 'module', 'exports', 'assert', 'require', generated.output[0].code ); 33 | const module = { exports: {} }; 34 | 35 | try { 36 | fn(module, module.exports, assert, require); 37 | } catch (error) { 38 | // eslint-disable-next-line no-console 39 | console.log(generated.output[0].code); 40 | throw error; 41 | } 42 | 43 | return module; 44 | }); 45 | } 46 | 47 | function getBundleImports ( bundle) { 48 | return bundle.imports ? Promise.resolve(bundle.imports) : bundle.generate({format: 'esm'}) 49 | .then(generated => generated.output[0].imports); 50 | } 51 | 52 | describe( 'rollup-plugin-node-resolve', function () { 53 | it( 'finds a module with jsnext:main', function () { 54 | return rollup.rollup({ 55 | input: 'samples/jsnext/main.js', 56 | onwarn: expectNoWarnings, 57 | plugins: [ 58 | nodeResolve({ mainFields: ['jsnext:main', 'module', 'main'] }) 59 | ] 60 | }).then( executeBundle ).then( module => { 61 | assert.equal( module.exports, 'JSNEXT' ); 62 | }); 63 | }); 64 | 65 | it( 'DEPRECATED: options.jsnext still works with correct priority', function () { 66 | return rollup.rollup({ 67 | input: 'samples/jsnext/main.js', 68 | plugins: [ 69 | nodeResolve({ jsnext: true, main: true }) 70 | ] 71 | }).then( executeBundle ).then( module => { 72 | assert.equal( module.exports, 'JSNEXT' ); 73 | }); 74 | }); 75 | 76 | it( 'DEPRECATED: options.module still works with correct priority', function () { 77 | return rollup.rollup({ 78 | input: 'samples/module/main.js', 79 | plugins: [ 80 | nodeResolve({ module: true, main: true, preferBuiltins: false }) 81 | ] 82 | }).then( executeBundle ).then( module => { 83 | assert.equal( module.exports, 'MODULE' ); 84 | }); 85 | }); 86 | 87 | it( 'finds and converts a basic CommonJS module', function () { 88 | return rollup.rollup({ 89 | input: 'samples/commonjs/main.js', 90 | onwarn: expectNoWarnings, 91 | plugins: [ 92 | nodeResolve({ mainFields: ['main'] }), 93 | commonjs() 94 | ] 95 | }).then( executeBundle ).then( module => { 96 | assert.equal( module.exports, 'It works!' ); 97 | }); 98 | }); 99 | 100 | it( 'handles a trailing slash', function () { 101 | return rollup.rollup({ 102 | input: 'samples/trailing-slash/main.js', 103 | onwarn: expectNoWarnings, 104 | plugins: [ 105 | nodeResolve({ mainFields: ['main'] }), 106 | commonjs() 107 | ] 108 | }).then( executeBundle ).then( module => { 109 | assert.equal( module.exports, 'It works!' ); 110 | }); 111 | }); 112 | 113 | it( 'finds a file inside a package directory', function () { 114 | return rollup.rollup({ 115 | input: 'samples/granular/main.js', 116 | onwarn: expectNoWarnings, 117 | plugins: [ 118 | nodeResolve(), 119 | babel({ 120 | presets: [ 121 | [ 122 | '@babel/preset-env', 123 | { 124 | targets: { 125 | node: 6 126 | } 127 | } 128 | ] 129 | ] 130 | }) 131 | ] 132 | }).then( executeBundle ).then( module => { 133 | assert.equal( module.exports, 'FOO' ); 134 | }); 135 | }); 136 | 137 | it( 'loads local directories by finding index.js within them', function () { 138 | return rollup.rollup({ 139 | input: 'samples/local-index/main.js', 140 | onwarn: expectNoWarnings, 141 | plugins: [ 142 | nodeResolve() 143 | ] 144 | }).then( executeBundle ).then( module => { 145 | assert.equal( module.exports, 42 ); 146 | }); 147 | }); 148 | 149 | it( 'loads package directories by finding index.js within them', function () { 150 | return rollup.rollup({ 151 | input: 'samples/package-index/main.js', 152 | onwarn: expectNoWarnings, 153 | plugins: [ 154 | nodeResolve() 155 | ] 156 | }).then( function ( bundle ) { 157 | return bundle.generate({ 158 | format: 'cjs' 159 | }); 160 | }).then( generated => { 161 | assert.ok( ~generated.output[0].code.indexOf( 'setPrototypeOf' ) ); 162 | }); 163 | }); 164 | 165 | it( 'disregards top-level browser field', function () { 166 | return rollup.rollup({ 167 | input: 'samples/browser/main.js', 168 | onwarn: expectNoWarnings, 169 | plugins: [ 170 | nodeResolve() 171 | ] 172 | }).then( executeBundle ).then( module => { 173 | assert.equal( module.exports, 'node' ); 174 | }); 175 | }); 176 | 177 | it( 'allows use of the top-level browser field', function () { 178 | return rollup.rollup({ 179 | input: 'samples/browser/main.js', 180 | onwarn: expectNoWarnings, 181 | plugins: [ 182 | nodeResolve({ 183 | mainFields: [ 'browser', 'main' ] 184 | }) 185 | ] 186 | }).then( executeBundle ).then( module => { 187 | assert.equal( module.exports, 'browser' ); 188 | }); 189 | }); 190 | 191 | it( 'disregards object browser field', function () { 192 | return rollup.rollup({ 193 | input: 'samples/browser-object/main.js', 194 | onwarn: expectNoWarnings, 195 | plugins: [ 196 | nodeResolve() 197 | ] 198 | }).then( executeBundle ).then( module => { 199 | assert.equal( module.exports.env, 'node' ); 200 | assert.equal( module.exports.dep, 'node-dep' ); 201 | assert.equal( module.exports.test, 42 ); 202 | }); 203 | }); 204 | 205 | it( 'allows use of the object browser field', function () { 206 | return rollup.rollup({ 207 | input: 'samples/browser-object/main.js', 208 | onwarn: expectNoWarnings, 209 | plugins: [ 210 | nodeResolve({ 211 | mainFields: [ 'browser', 'main' ] 212 | }) 213 | ] 214 | }).then( executeBundle ).then( module => { 215 | assert.equal( module.exports.env, 'browser' ); 216 | assert.equal( module.exports.dep, 'browser-dep' ); 217 | assert.equal( module.exports.test, 43 ); 218 | }); 219 | }); 220 | 221 | it( 'allows use of object browser field, resolving `main`', function () { 222 | return rollup.rollup({ 223 | input: 'samples/browser-object-main/main.js', 224 | onwarn: expectNoWarnings, 225 | plugins: [ 226 | nodeResolve({ 227 | mainFields: [ 'browser', 'main' ] 228 | }) 229 | ] 230 | }).then( executeBundle ).then( module => { 231 | assert.equal( module.exports.env, 'browser' ); 232 | assert.equal( module.exports.dep, 'browser-dep' ); 233 | assert.equal( module.exports.test, 43 ); 234 | }); 235 | }); 236 | 237 | it( 'options.browser = true still works', function () { 238 | return rollup.rollup({ 239 | input: 'samples/browser-object-main/main.js', 240 | plugins: [ 241 | nodeResolve({ 242 | browser: true 243 | }) 244 | ] 245 | }).then( executeBundle ).then( module => { 246 | assert.equal( module.exports.env, 'browser' ); 247 | assert.equal( module.exports.dep, 'browser-dep' ); 248 | assert.equal( module.exports.test, 43 ); 249 | }); 250 | }); 251 | 252 | it( 'allows use of object browser field, resolving implicit `main`', function () { 253 | return rollup.rollup({ 254 | input: 'samples/browser-object/main-implicit.js', 255 | onwarn: expectNoWarnings, 256 | plugins: [ 257 | nodeResolve({ 258 | mainFields: [ 'browser', 'main' ] 259 | }) 260 | ] 261 | }).then( executeBundle ).then( module => { 262 | assert.equal( module.exports.env, 'browser' ); 263 | }); 264 | }); 265 | 266 | it( 'allows use of object browser field, resolving replaced builtins', function () { 267 | return rollup.rollup({ 268 | input: 'samples/browser-object-builtin/main.js', 269 | onwarn: expectNoWarnings, 270 | plugins: [ 271 | nodeResolve({ 272 | mainFields: [ 'browser', 'main' ] 273 | }) 274 | ] 275 | }).then( executeBundle ).then( module => { 276 | assert.equal( module.exports, 'browser-fs' ); 277 | }); 278 | }); 279 | 280 | it( 'allows use of object browser field, resolving nested directories', function () { 281 | return rollup.rollup({ 282 | input: 'samples/browser-object-nested/main.js', 283 | onwarn: expectNoWarnings, 284 | plugins: [ 285 | nodeResolve({ 286 | mainFields: [ 'browser', 'main' ] 287 | }) 288 | ] 289 | }).then( executeBundle ).then( module => { 290 | assert.equal( module.exports.env, 'browser' ); 291 | assert.equal( module.exports.dep, 'browser-dep' ); 292 | assert.equal( module.exports.test, 43 ); 293 | }); 294 | }); 295 | 296 | it( 'allows use of object browser field, resolving `main`', function () { 297 | return rollup.rollup({ 298 | input: 'samples/browser-object-main/main.js', 299 | onwarn: expectNoWarnings, 300 | plugins: [ 301 | nodeResolve({ 302 | mainFields: [ 'browser', 'main' ] 303 | }) 304 | ] 305 | }).then( executeBundle ).then( module => { 306 | assert.equal( module.exports.env, 'browser' ); 307 | assert.equal( module.exports.dep, 'browser-dep' ); 308 | assert.equal( module.exports.test, 43 ); 309 | }); 310 | }); 311 | 312 | it( 'allows use of object browser field, resolving implicit `main`', function () { 313 | return rollup.rollup({ 314 | input: 'samples/browser-object/main-implicit.js', 315 | onwarn: expectNoWarnings, 316 | plugins: [ 317 | nodeResolve({ 318 | mainFields: [ 'browser', 'main' ] 319 | }) 320 | ] 321 | }).then( executeBundle ).then( module => { 322 | assert.equal( module.exports.env, 'browser' ); 323 | }); 324 | }); 325 | 326 | it( 'allows use of object browser field, resolving replaced builtins', function () { 327 | return rollup.rollup({ 328 | input: 'samples/browser-object-builtin/main.js', 329 | onwarn: expectNoWarnings, 330 | plugins: [ 331 | nodeResolve({ 332 | mainFields: [ 'browser', 'main' ] 333 | }) 334 | ] 335 | }).then( executeBundle ).then( module => { 336 | assert.equal( module.exports, 'browser-fs' ); 337 | }); 338 | }); 339 | 340 | it('respects local browser field', function () { 341 | return rollup.rollup({ 342 | input: 'samples/browser-local/main.js', 343 | onwarn: expectNoWarnings, 344 | plugins: [ 345 | nodeResolve({ 346 | mainFields: ['browser', 'main'] 347 | }) 348 | ] 349 | }).then(executeBundle).then(module => { 350 | assert.equal(module.exports, 'component-type'); 351 | }); 352 | }); 353 | 354 | it( 'warns when importing builtins', function () { 355 | return rollup.rollup({ 356 | input: 'samples/builtins/main.js', 357 | onwarn: expectWarnings([{ 358 | code: 'UNRESOLVED_IMPORT', 359 | source: 'path' 360 | }]), 361 | plugins: [ 362 | nodeResolve({ 363 | mainFields: ['browser', 'main'], 364 | preferBuiltins: true 365 | }) 366 | ] 367 | }).then(executeBundle).then(module => { 368 | assert.equal(module.exports, require('path').sep); 369 | }); 370 | }); 371 | 372 | it( 'allows use of object browser field, resolving nested directories', function () { 373 | return rollup.rollup({ 374 | input: 'samples/browser-object-nested/main.js', 375 | onwarn: expectNoWarnings, 376 | plugins: [ 377 | nodeResolve({ 378 | mainFields: [ 'browser', 'main' ] 379 | }) 380 | ] 381 | }).then( executeBundle ).then( module => { 382 | assert.equal( module.exports.env, 'browser' ); 383 | assert.equal( module.exports.dep, 'browser-dep' ); 384 | assert.equal( module.exports.test, 43 ); 385 | }); 386 | }); 387 | 388 | it( 'allows use of object browser field, resolving `main`', function () { 389 | return rollup.rollup({ 390 | input: 'samples/browser-object-main/main.js', 391 | onwarn: expectNoWarnings, 392 | plugins: [ 393 | nodeResolve({ 394 | mainFields: [ 'browser', 'main' ] 395 | }) 396 | ] 397 | }).then( executeBundle ).then( module => { 398 | assert.equal( module.exports.env, 'browser' ); 399 | assert.equal( module.exports.dep, 'browser-dep' ); 400 | assert.equal( module.exports.test, 43 ); 401 | }); 402 | }); 403 | 404 | it( 'allows use of object browser field, resolving implicit `main`', function () { 405 | return rollup.rollup({ 406 | input: 'samples/browser-object/main-implicit.js', 407 | onwarn: expectNoWarnings, 408 | plugins: [ 409 | nodeResolve({ 410 | mainFields: [ 'browser', 'main' ] 411 | }) 412 | ] 413 | }).then( executeBundle ).then( module => { 414 | assert.equal( module.exports.env, 'browser' ); 415 | }); 416 | }); 417 | 418 | it( 'allows use of object browser field, resolving replaced builtins', function () { 419 | return rollup.rollup({ 420 | input: 'samples/browser-object-builtin/main.js', 421 | onwarn: expectNoWarnings, 422 | plugins: [ 423 | nodeResolve({ 424 | mainFields: [ 'browser', 'main' ] 425 | }) 426 | ] 427 | }).then( executeBundle ).then( module => { 428 | assert.equal( module.exports, 'browser-fs' ); 429 | }); 430 | }); 431 | 432 | it( 'allows use of object browser field, resolving nested directories', function () { 433 | return rollup.rollup({ 434 | input: 'samples/browser-object-nested/main.js', 435 | onwarn: expectNoWarnings, 436 | plugins: [ 437 | nodeResolve({ 438 | mainFields: [ 'browser', 'main' ] 439 | }) 440 | ] 441 | }).then( executeBundle ).then( module => { 442 | assert.equal( module.exports.env, 'browser' ); 443 | assert.equal( module.exports.dep, 'browser-dep' ); 444 | assert.equal( module.exports.test, 43 ); 445 | }); 446 | }); 447 | 448 | it( 'allows use of object browser field, resolving to nested node_modules', function () { 449 | return rollup.rollup({ 450 | input: 'samples/browser-entry-points-to-node-module/index.js', 451 | onwarn: expectNoWarnings, 452 | plugins: [ 453 | nodeResolve({ 454 | main: true, 455 | browser: true 456 | }) 457 | ] 458 | }).then( executeBundle ).then( module => { 459 | assert.equal( module.exports, 'component-type' ); 460 | }); 461 | }); 462 | 463 | it( 'supports `false` in browser field', function () { 464 | return rollup.rollup({ 465 | input: 'samples/browser-false/main.js', 466 | onwarn: expectNoWarnings, 467 | plugins: [ 468 | nodeResolve({ 469 | mainFields: [ 'browser', 'main' ] 470 | }) 471 | ] 472 | }).then( executeBundle ); 473 | }); 474 | 475 | it( 'preferBuiltins: true allows preferring a builtin to a local module of the same name', () => { 476 | return rollup.rollup({ 477 | input: 'samples/prefer-builtin/main.js', 478 | onwarn: expectWarnings([{ 479 | code:'UNRESOLVED_IMPORT', 480 | source:'events' 481 | }]), 482 | plugins: [ 483 | nodeResolve({ 484 | preferBuiltins: true 485 | }) 486 | ] 487 | }).then(getBundleImports) 488 | .then(imports => assert.deepEqual(imports, ['events'])); 489 | }); 490 | 491 | it( 'preferBuiltins: false allows resolving a local module with the same name as a builtin module', () => { 492 | return rollup.rollup({ 493 | input: 'samples/prefer-builtin/main.js', 494 | onwarn: expectWarnings([{ 495 | code:'EMPTY_BUNDLE' 496 | }]), 497 | plugins: [ 498 | nodeResolve({ 499 | preferBuiltins: false 500 | }) 501 | ] 502 | }).then(getBundleImports) 503 | .then(imports => assert.deepEqual(imports, [])); 504 | }); 505 | 506 | it( 'issues a warning when preferring a builtin module without having explicit configuration', () => { 507 | let warning = null; 508 | return rollup.rollup({ 509 | input: 'samples/prefer-builtin/main.js', 510 | onwarn ({message}) { 511 | if ( ~message.indexOf( 'preferring' ) ) { 512 | warning = message; 513 | } 514 | }, 515 | plugins: [nodeResolve()] 516 | }).then( () => { 517 | const localPath = path.join(__dirname, 'node_modules/events/index.js'); 518 | assert.strictEqual( 519 | warning, 520 | `preferring built-in module 'events' over local alternative ` + 521 | `at '${localPath}', pass 'preferBuiltins: false' to disable this behavior ` + 522 | `or 'preferBuiltins: true' to disable this warning` 523 | ); 524 | }); 525 | }); 526 | 527 | it( 'supports non-standard extensions', () => { 528 | return rollup.rollup({ 529 | input: 'samples/extensions/main.js', 530 | onwarn: expectNoWarnings, 531 | plugins: [ 532 | nodeResolve({ 533 | extensions: [ '.js', '.wut' ] 534 | }) 535 | ] 536 | }).then( executeBundle ); 537 | }); 538 | 539 | it( 'ignores IDs with null character', () => { 540 | return Promise.resolve( nodeResolve().resolveId( '\0someid', 'test.js' ) ).then( result => { 541 | assert.equal( result, null ); 542 | }); 543 | }); 544 | 545 | it( 'finds a module with module field', () => { 546 | return rollup.rollup({ 547 | input: 'samples/module/main.js', 548 | onwarn: expectNoWarnings, 549 | plugins: [ 550 | nodeResolve({ preferBuiltins: false }) 551 | ] 552 | }).then( executeBundle ).then( module => { 553 | assert.equal( module.exports, 'MODULE' ); 554 | }); 555 | }); 556 | 557 | it( 'respects order if given module,jsnext:main,main', () => { 558 | return rollup.rollup({ 559 | input: 'samples/prefer-module/main.js', 560 | onwarn: expectNoWarnings, 561 | plugins: [ 562 | nodeResolve({ mainFields: [ 'module', 'jsnext:main', 'main' ], preferBuiltins: false }) 563 | ] 564 | }).then( executeBundle ).then( module => { 565 | assert.equal( module.exports, 'MODULE-ENTRY' ); 566 | }); 567 | }); 568 | 569 | it('finds and uses an .mjs module', function () { 570 | return rollup.rollup({ 571 | input: 'samples/module-mjs/main.js', 572 | onwarn: expectNoWarnings, 573 | plugins: [ 574 | nodeResolve({ preferBuiltins: false }) 575 | ] 576 | }).then( executeBundle ).then( module => { 577 | assert.equal( module.exports, 'MODULE-MJS' ); 578 | }); 579 | }); 580 | 581 | it('finds and uses a dual-distributed .js & .mjs module', function () { 582 | return rollup.rollup({ 583 | input: 'samples/dual-cjs-mjs/main.js', 584 | onwarn: expectNoWarnings, 585 | plugins: [ 586 | nodeResolve({ preferBuiltins: false }) 587 | ] 588 | }).then( executeBundle ).then( module => { 589 | assert.equal( module.exports, 'DUAL-MJS' ); 590 | }); 591 | }); 592 | 593 | it( 'keeps the order of [browser, module, jsnext, main] with all enabled', function () { 594 | return rollup.rollup({ 595 | input: 'samples/browser/main.js', 596 | plugins: [ 597 | nodeResolve({ main: true, browser: true, jsnext: true, module: true }) 598 | ] 599 | }).then( executeBundle ).then( module => { 600 | assert.equal( module.exports, 'browser' ); 601 | }); 602 | }); 603 | 604 | it( 'should support disabling "module" field resolution', function () { 605 | return rollup.rollup({ 606 | input: 'samples/prefer-main/main.js', 607 | plugins: [ 608 | nodeResolve({ module: false }) 609 | ] 610 | }).then( executeBundle ).then( module => { 611 | assert.equal( module.exports, 'MAIN-ENTRY' ); 612 | }); 613 | }); 614 | 615 | it( 'should support disabling "main" field resolution', function () { 616 | return rollup.rollup({ 617 | input: 'samples/prefer-module/main.js', 618 | plugins: [ 619 | nodeResolve({ main: false }) 620 | ] 621 | }).then( executeBundle ).then( module => { 622 | assert.equal( module.exports, 'MODULE-ENTRY' ); 623 | }); 624 | }); 625 | 626 | it( 'should support enabling "jsnext" field resolution', function () { 627 | return rollup.rollup({ 628 | input: 'samples/prefer-module/main.js', 629 | plugins: [ 630 | nodeResolve({ main: false, module: false, jsnext: true }) 631 | ] 632 | }).then( executeBundle ).then( module => { 633 | assert.equal( module.exports, 'JSNEXT-ENTRY' ); 634 | }); 635 | }); 636 | 637 | describe( 'symlinks', () => { 638 | function createMissingDirectories () { 639 | createDirectory( './samples/symlinked/first/node_modules' ); 640 | createDirectory( './samples/symlinked/second/node_modules' ); 641 | createDirectory( './samples/symlinked/third/node_modules' ); 642 | } 643 | 644 | function createDirectory ( pathToDir ) { 645 | if ( !fs.existsSync( pathToDir ) ) { 646 | fs.mkdirSync( pathToDir ); 647 | } 648 | } 649 | 650 | function linkDirectories () { 651 | fs.symlinkSync('../../second', './samples/symlinked/first/node_modules/second', 'dir'); 652 | fs.symlinkSync('../../third', './samples/symlinked/first/node_modules/third', 'dir'); 653 | fs.symlinkSync('../../third', './samples/symlinked/second/node_modules/third', 'dir'); 654 | } 655 | 656 | function unlinkDirectories () { 657 | fs.unlinkSync('./samples/symlinked/first/node_modules/second'); 658 | fs.unlinkSync('./samples/symlinked/first/node_modules/third'); 659 | fs.unlinkSync('./samples/symlinked/second/node_modules/third'); 660 | } 661 | 662 | beforeEach( () => { 663 | createMissingDirectories(); 664 | linkDirectories(); 665 | }); 666 | 667 | afterEach( () => { 668 | unlinkDirectories(); 669 | }); 670 | 671 | it( 'resolves symlinked packages', () => { 672 | return rollup.rollup({ 673 | input: 'samples/symlinked/first/index.js', 674 | onwarn: expectNoWarnings, 675 | plugins: [ 676 | nodeResolve() 677 | ] 678 | }).then( executeBundle ).then( module => { 679 | assert.equal( module.exports.number1, module.exports.number2 ); 680 | }); 681 | }); 682 | 683 | it( 'preserves symlinks if `preserveSymlinks` is true', () => { 684 | return rollup.rollup({ 685 | input: 'samples/symlinked/first/index.js', 686 | onwarn: expectNoWarnings, 687 | plugins: [ 688 | nodeResolve() 689 | ], 690 | preserveSymlinks: true 691 | }).then( executeBundle ).then( module => { 692 | assert.notEqual( module.exports.number1, module.exports.number2 ); 693 | }); 694 | }); 695 | }); 696 | 697 | it( 'respects order if given jsnext:main, main', () => { 698 | return rollup.rollup({ 699 | input: 'samples/prefer-jsnext/main.js', 700 | onwarn: expectNoWarnings, 701 | plugins: [ 702 | nodeResolve({ mainFields: ['jsnext:main', 'main'], preferBuiltins: false }) 703 | ] 704 | }).then( executeBundle ).then( module => { 705 | assert.equal( module.exports, 'JSNEXT-ENTRY' ); 706 | }); 707 | }); 708 | 709 | it( 'supports ./ in entry filename', () => { 710 | return rollup.rollup({ 711 | input: './samples/jsnext/main.js', 712 | onwarn: expectNoWarnings, 713 | plugins: [ 714 | nodeResolve({}) 715 | ] 716 | }).then( executeBundle ).then( module => { 717 | assert.equal( module.exports, 'MAIN' ); 718 | }); 719 | }); 720 | 721 | it( 'throws error if local id is not resolved', () => { 722 | const input = path.join( 'samples', 'unresolved-local', 'main.js' ); 723 | return rollup.rollup({ 724 | input, 725 | onwarn: expectNoWarnings, 726 | plugins: [ 727 | nodeResolve() 728 | ] 729 | }).then( () => { 730 | throw Error( 'test should fail' ); 731 | }, err => { 732 | assert.equal( err.message, `Could not resolve './foo' from ${input}` ); 733 | }); 734 | }); 735 | 736 | it( 'mark as external to module outside the jail', () => { 737 | return rollup.rollup({ 738 | input: 'samples/jail/main.js', 739 | onwarn: expectWarnings([{ 740 | code:'UNRESOLVED_IMPORT', 741 | source:'string/uppercase.js' 742 | }]), 743 | plugins: [ nodeResolve({ 744 | jail: `${__dirname}/samples/` 745 | }) ] 746 | }).then(getBundleImports) 747 | .then(imports => assert.deepEqual(imports, ['string/uppercase.js'])); 748 | }); 749 | 750 | it( 'bundle module defined inside the jail', () => { 751 | return rollup.rollup({ 752 | input: 'samples/jail/main.js', 753 | onwarn: expectNoWarnings, 754 | plugins: [ nodeResolve({ 755 | jail: `${__dirname}/` 756 | }) ] 757 | }).then(getBundleImports) 758 | .then(imports => assert.deepEqual(imports, [])); 759 | }); 760 | 761 | it( '"only" option allows to specify the only packages to resolve', () => { 762 | return rollup.rollup({ 763 | input: 'samples/only/main.js', 764 | onwarn: expectWarnings([{ 765 | code:'UNRESOLVED_IMPORT', 766 | source:'@scoped/foo' 767 | }, { 768 | code:'UNRESOLVED_IMPORT', 769 | source:'@scoped/bar' 770 | }]), 771 | plugins: [ 772 | nodeResolve({ 773 | only: [ 'test' ] 774 | }) 775 | ] 776 | }).then(getBundleImports) 777 | .then(imports => assert.deepEqual(imports, ['@scoped/foo', '@scoped/bar'])); 778 | }); 779 | 780 | it( '"only" option works with a regex', () => { 781 | return rollup.rollup({ 782 | input: 'samples/only/main.js', 783 | onwarn: expectWarnings([{ 784 | code:'UNRESOLVED_IMPORT', 785 | source:'test' 786 | }]), 787 | plugins: [ 788 | nodeResolve({ 789 | only: [ /^@scoped\/.*$/ ] 790 | }) 791 | ] 792 | }).then(getBundleImports) 793 | .then(imports => assert.deepEqual(imports, ['test'])); 794 | }); 795 | 796 | it( 'allows custom options', () => { 797 | return rollup.rollup({ 798 | input: 'samples/custom-resolve-options/main.js', 799 | onwarn: expectNoWarnings, 800 | plugins: [ nodeResolve({ 801 | customResolveOptions: { 802 | moduleDirectory: 'js_modules' 803 | } 804 | }) ] 805 | }).then( bundle => { 806 | assert.equal( 807 | bundle.cache.modules[0].id, 808 | path.resolve( __dirname, 'samples/custom-resolve-options/js_modules/foo.js' ) 809 | ); 810 | }); 811 | }); 812 | 813 | it( 'ignores deep-import non-modules', () => { 814 | return rollup.rollup({ 815 | input: 'samples/deep-import-non-module/main.js', 816 | onwarn: expectWarnings([{ 817 | code:'UNRESOLVED_IMPORT', 818 | source:'foo/deep' 819 | }]), 820 | plugins: [ nodeResolve({ 821 | modulesOnly: true 822 | }) ] 823 | }).then(getBundleImports) 824 | .then(imports => assert.deepEqual(imports, ['foo/deep'])); 825 | }); 826 | 827 | it( 'generates manual chunks', () => { 828 | const chunkName = 'mychunk'; 829 | return rollup.rollup({ 830 | input: 'samples/manualchunks/main.js', 831 | onwarn: expectNoWarnings, 832 | manualChunks: { 833 | [ chunkName ]: [ 'simple' ] 834 | }, 835 | plugins: [ nodeResolve() ] 836 | }).then( bundle => 837 | bundle.generate({ 838 | format: 'esm', 839 | chunkFileNames: '[name]', 840 | })).then( generated => { 841 | assert.ok(generated.output.find(({fileName}) => fileName === chunkName)); 842 | }); 843 | }); 844 | 845 | it('resolves dynamic imports', () => { 846 | return rollup.rollup({ 847 | input: 'samples/dynamic/main.js', 848 | onwarn: expectNoWarnings, 849 | inlineDynamicImports: true, 850 | plugins: [ nodeResolve() ] 851 | }).then(executeBundle) 852 | .then(({exports}) => exports.then(result => assert.equal(result.default, 42))); 853 | }); 854 | 855 | it( 'pkg.browser with mapping to prevent bundle by specifying a value of false', () => { 856 | return rollup.rollup({ 857 | input: 'samples/browser-object-with-false/main.js', 858 | plugins: [ 859 | nodeResolve({ browser: true }), 860 | commonjs() 861 | ] 862 | }).then( executeBundle ).then( module => { 863 | assert.equal( module.exports, 'ok' ); 864 | }); 865 | }); 866 | 867 | it( 'single module version is bundle if dedupe is set', () => { 868 | return rollup.rollup({ 869 | input: 'samples/react-app/main.js', 870 | plugins: [ 871 | nodeResolve({ 872 | dedupe: [ 'react' ] 873 | }) 874 | ] 875 | }).then( executeBundle ).then( module => { 876 | assert.deepEqual(module.exports, { 877 | React: 'react:root', 878 | ReactConsumer: 'react-consumer:react:root' 879 | }); 880 | }); 881 | }); 882 | 883 | it( 'single module version is bundle if dedupe is set as a function', () => { 884 | return rollup.rollup({ 885 | input: 'samples/react-app/main.js', 886 | plugins: [ 887 | nodeResolve({ 888 | dedupe: (dep) => dep === 'react' 889 | }) 890 | ] 891 | }).then( executeBundle ).then( module => { 892 | assert.deepEqual(module.exports, { 893 | React: 'react:root', 894 | ReactConsumer: 'react-consumer:react:root' 895 | }); 896 | }); 897 | }); 898 | 899 | it( 'multiple module versions are bundled if dedupe is not set', () => { 900 | return rollup.rollup({ 901 | input: 'samples/react-app/main.js', 902 | plugins: [ 903 | nodeResolve() 904 | ] 905 | }).then( executeBundle ).then( module => { 906 | assert.deepEqual(module.exports, { 907 | React: 'react:root', 908 | ReactConsumer: 'react-consumer:react:child' 909 | }); 910 | }); 911 | }); 912 | 913 | 914 | it('handles package side-effects', () => 915 | rollup.rollup({ 916 | input: 'samples/side-effects/main.js', 917 | plugins: [nodeResolve()] 918 | }).then(executeBundle).then(() => { 919 | assert.deepStrictEqual(global.sideEffects, [ 920 | 'false-dep1', 921 | 'true-dep1', 922 | 'true-dep2', 923 | 'true-index', 924 | 'array-dep1', 925 | 'array-dep3', 926 | 'array-dep5', 927 | 'array-index' 928 | ]); 929 | delete global.sideEffects; 930 | }) 931 | ); 932 | 933 | describe('getPackageInfoForId', () => { 934 | it('populates info for main', () => { 935 | const resolve = nodeResolve({ 936 | mainFields: ['main'] 937 | }); 938 | 939 | let entriesInfo; 940 | 941 | return rollup.rollup({ 942 | input: 'samples/prefer-main/main.js', 943 | plugins: [ 944 | resolve, 945 | { 946 | transform (code, id) { 947 | if (!id.match(/main-entry.js$/)) return; 948 | entriesInfo = resolve.getPackageInfoForId(id); 949 | return code; 950 | } 951 | } 952 | ] 953 | }).then(() => { 954 | const entriesPkgJsonPath = path.resolve(__dirname, './node_modules/entries/package.json'); 955 | const root = path.dirname(entriesPkgJsonPath); 956 | assert.deepStrictEqual(entriesInfo, { 957 | browserMappedMain: false, 958 | resolvedMainField: 'main', 959 | packageJson: require(entriesPkgJsonPath), 960 | packageJsonPath: entriesPkgJsonPath, 961 | root, 962 | resolvedEntryPoint: path.resolve(root, './main-entry.js') 963 | }); 964 | }); 965 | }); 966 | 967 | it('populates info for module', () => { 968 | const resolve = nodeResolve({ 969 | mainFields: ['module'] 970 | }); 971 | 972 | let entriesInfo; 973 | 974 | return rollup.rollup({ 975 | input: 'samples/prefer-main/main.js', 976 | plugins: [ 977 | resolve, 978 | { 979 | transform (code, id) { 980 | if (!id.match(/module-entry.js$/)) return; 981 | entriesInfo = resolve.getPackageInfoForId(id); 982 | return code; 983 | } 984 | } 985 | ] 986 | }).then(() => { 987 | const entriesPkgJsonPath = path.resolve(__dirname, './node_modules/entries/package.json'); 988 | const root = path.dirname(entriesPkgJsonPath); 989 | 990 | assert.deepStrictEqual(entriesInfo, { 991 | browserMappedMain: false, 992 | resolvedMainField: 'module', 993 | packageJson: require(entriesPkgJsonPath), 994 | packageJsonPath: entriesPkgJsonPath, 995 | root, 996 | resolvedEntryPoint: path.resolve(root, './module-entry.js') 997 | }); 998 | }); 999 | }); 1000 | 1001 | it('populates info for browser', () => { 1002 | const resolve = nodeResolve({ 1003 | mainFields: ['browser'] 1004 | }); 1005 | 1006 | const entriesInfoMap = new Map(); 1007 | 1008 | return rollup.rollup({ 1009 | input: 'samples/browser-object/main.js', 1010 | plugins: [ 1011 | resolve, 1012 | { 1013 | transform (code, id) { 1014 | if (!id.match(/isomorphic-object/)) return; 1015 | entriesInfoMap.set(id, resolve.getPackageInfoForId(id)); 1016 | return code; 1017 | } 1018 | } 1019 | ] 1020 | }).then(() => { 1021 | const entriesPkgJsonPath = path.resolve(__dirname, './node_modules/isomorphic-object/package.json'); 1022 | const root = path.dirname(entriesPkgJsonPath); 1023 | const expectedPkgJson = require(entriesPkgJsonPath); 1024 | 1025 | for (const entriesInfo of entriesInfoMap.values()) { 1026 | assert.deepStrictEqual(entriesInfo, { 1027 | browserMappedMain: true, 1028 | resolvedMainField: 'main', 1029 | packageJson: expectedPkgJson, 1030 | packageJsonPath: entriesPkgJsonPath, 1031 | root, 1032 | resolvedEntryPoint: path.resolve(root, './browser.js') 1033 | }); 1034 | } 1035 | }); 1036 | }); 1037 | 1038 | }); 1039 | }); 1040 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "lib": [ 4 | "es6" 5 | ], 6 | "noImplicitAny": true, 7 | "noImplicitThis": true, 8 | "strict": true, 9 | "noEmit": true, 10 | "allowJs": true 11 | }, 12 | "files": [ 13 | "index.d.ts", 14 | "typings-test.js" 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /typings-test.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | import resolve from '.'; 3 | 4 | /** @type {import("rollup").RollupOptions} */ 5 | const config = { 6 | input: 'main.js', 7 | output: { 8 | file: 'bundle.js', 9 | format: 'iife', 10 | name: 'MyModule', 11 | }, 12 | plugins: [ 13 | resolve({ 14 | mainFields: ['untranspiled', 'module', 'main'], 15 | module: true, 16 | jsnext: true, 17 | main: true, 18 | browser: true, 19 | extensions: [ '.mjs', '.js', '.jsx', '.json' ], 20 | preferBuiltins: false, 21 | jail: '/my/jail/path', 22 | only: [ 'some_module', /^@some_scope\/.*$/ ], 23 | dedupe: ['lodash'], 24 | modulesOnly: true, 25 | customResolveOptions: { 26 | moduleDirectory: 'js_modules' 27 | } 28 | }) 29 | ] 30 | }; 31 | 32 | export default config; 33 | --------------------------------------------------------------------------------