Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rollup/rollup-plugin-node-resolve
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 73b01b1d96d95021af5fb5026e0a9ea946448cb0
Choose a base ref
...
head repository: rollup/rollup-plugin-node-resolve
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 519ab43cb947ac376fcbb2ec80e44c206fdf630c
Choose a head ref

Commits on Feb 22, 2019

  1. Fix pkg.browser mappings issue by specifying a value of false (#183)

    * Fix pkg.browser mappings issue by specifying a value of `false`
    
    * Add tests for #183
    allex authored and lukastaegert committed Feb 22, 2019
    Copy the full SHA
    9ce01d4 View commit details
  2. Update changelog

    lukastaegert committed Feb 22, 2019
    Copy the full SHA
    aad0239 View commit details
  3. fix: regression in browser objects pointing to nested node_mpodules (#…

    …143)
    
    * fix: regression in browser objects pointing to nested node_mpodules
    
    * Update dependencies and simplify tests for Rollup 1
    
    * Add missing test dependency and add explicit warning checks for tests
    keithamus authored and lukastaegert committed Feb 22, 2019
    Copy the full SHA
    1eff8d7 View commit details
  4. Update changelog

    lukastaegert committed Feb 22, 2019
    Copy the full SHA
    2ba2515 View commit details
  5. 4.0.1

    lukastaegert committed Feb 22, 2019
    Copy the full SHA
    f8dfa57 View commit details

Commits on Apr 5, 2019

  1. Copy the full SHA
    ac791de View commit details
  2. Add .d.ts typings file (#189)

    * Add .d.ts typings file
    
    * Add test file to check that types are set correctly
    
    * Move typings test file to root so its not run by mocha
    
    * Automate type tests
    NotWoods authored and lukastaegert committed Apr 5, 2019
    Copy the full SHA
    6d3567e View commit details
  3. Update changelog

    lukastaegert committed Apr 5, 2019
    Copy the full SHA
    213fe39 View commit details
  4. 4.1.0

    lukastaegert committed Apr 5, 2019
    Copy the full SHA
    5fa2cf6 View commit details

Commits on Apr 6, 2019

  1. New mainfields option (#182)

    * feat: add `mainFields` api. Deprecate `browser`, `jsnext`, `module`, `main`
    
    * Fix remaining issues with mainfields
    
    * Add new option to types
    keithamus authored and lukastaegert committed Apr 6, 2019
    Copy the full SHA
    68f80df View commit details
  2. Copy the full SHA
    86905f4 View commit details
  3. Update changelog

    lukastaegert committed Apr 6, 2019
    Copy the full SHA
    c5112f5 View commit details
  4. 4.2.0

    lukastaegert committed Apr 6, 2019
    Copy the full SHA
    2344757 View commit details
  5. fix: respect setting the deprecated fields "module", "main", and "jsn…

    …ext" (#204)
    Nick Woodward authored and lukastaegert committed Apr 6, 2019
    Copy the full SHA
    b6f6eb5 View commit details
  6. Update changelog

    lukastaegert committed Apr 6, 2019
    Copy the full SHA
    ed6a2dc View commit details
  7. 4.2.1

    lukastaegert committed Apr 6, 2019
    Copy the full SHA
    e222b64 View commit details

Commits on Apr 10, 2019

  1. fix mainFields typing (#207)

    nicolashenry authored and lukastaegert committed Apr 10, 2019
    Copy the full SHA
    70f54ed View commit details
  2. Fix TypeScript Typings (rename and export Options interface) (#206)

    * fix(type): allow options to be exported
    
    * fix(type): rename options type name
    
    * add some spaces
    Kocal authored and lukastaegert committed Apr 10, 2019
    Copy the full SHA
    f582f9c View commit details
  3. Extend typings test

    lukastaegert committed Apr 10, 2019
    Copy the full SHA
    3233cec View commit details
  4. Update changelog

    lukastaegert committed Apr 10, 2019
    Copy the full SHA
    5564de2 View commit details
  5. 4.2.2

    lukastaegert committed Apr 10, 2019
    Copy the full SHA
    9736d86 View commit details

Commits on Apr 11, 2019

  1. Fix ordering of jsnext:main when using the jsnext option (#209)

    * Update dependencies
    
    * Test and fix wrong ordering of jsnext:main when using jsnext: true
    lukastaegert authored Apr 11, 2019
    Copy the full SHA
    befca76 View commit details
  2. Update changelog

    lukastaegert committed Apr 11, 2019
    Copy the full SHA
    9490cb6 View commit details
  3. 4.2.3

    lukastaegert committed Apr 11, 2019
    Copy the full SHA
    4ba2f87 View commit details

Commits on May 10, 2019

  1. #214 Add issue templates (#217)

    * add issue templates
    
    * update issue-bug template
    Dmitry Vakhnenko authored and lukastaegert committed May 10, 2019
    Copy the full SHA
    6eb687b View commit details
  2. docs: add note on builtins (#215)

    Fixes #146
    keithamus authored and lukastaegert committed May 10, 2019
    Copy the full SHA
    0596cab View commit details
  3. Update changelog

    lukastaegert committed May 10, 2019
    Copy the full SHA
    952033a View commit details

Commits on May 11, 2019

  1. perf: add cached version of isDir (#218)

    * perf: add cached version of `isDir`
    
    * refactor: DRY up cache functions
    
    * refactor: use Map for cache over Object
    
    * refactor: rename variables for clarity
    keithamus authored and lukastaegert committed May 11, 2019
    Copy the full SHA
    e7eef7f View commit details
  2. Update changelog

    lukastaegert committed May 11, 2019
    Copy the full SHA
    6f551b8 View commit details
  3. 4.2.4

    lukastaegert committed May 11, 2019
    Copy the full SHA
    519ab43 View commit details
Showing with 2,626 additions and 594 deletions.
  1. +48 −0 .github/ISSUE_TEMPLATE/BUG.md
  2. +33 −0 .github/ISSUE_TEMPLATE/DOCS.md
  3. +24 −0 .github/ISSUE_TEMPLATE/FEATURE.md
  4. +28 −0 .github/ISSUE_TEMPLATE/MODIFICATION.md
  5. +13 −0 .github/ISSUE_TEMPLATE/SUPPORT.md
  6. +1 −0 .gitignore
  7. +34 −0 CHANGELOG.md
  8. +38 −7 README.md
  9. +100 −0 index.d.ts
  10. +1,800 −449 package-lock.json
  11. +15 −13 package.json
  12. +88 −50 src/index.js
  13. +2 −0 test/node_modules/browser-entry-points-to-node-module/index.js
  14. +6 −0 test/node_modules/browser-entry-points-to-node-module/package.json
  15. +1 −0 test/node_modules/component-type/index.js
  16. +3 −0 test/node_modules/component-type/package.json
  17. +1 −1 test/node_modules/isomorphic-nested/lib/browser.js
  18. +1 −1 test/node_modules/isomorphic-nested/lib/index.js
  19. +1 −1 test/node_modules/isomorphic-object-main-implicit/browser.js
  20. +1 −1 test/node_modules/isomorphic-object-main-implicit/index.js
  21. +1 −1 test/node_modules/isomorphic-object-main/browser.js
  22. +1 −1 test/node_modules/isomorphic-object-main/index.js
  23. +4 −0 test/node_modules/isomorphic-object-with-false/lib/client/http-tracker.js
  24. +4 −0 test/node_modules/isomorphic-object-with-false/lib/client/udp-tracker.js
  25. +4 −0 test/node_modules/isomorphic-object-with-false/lib/client/websocket-tracker.js
  26. +20 −0 test/node_modules/isomorphic-object-with-false/lib/index.js
  27. +2 −0 test/node_modules/isomorphic-object-with-false/lib/subpath/foo/index.js
  28. +8 −0 test/node_modules/isomorphic-object-with-false/package.json
  29. +1 −1 test/node_modules/isomorphic-object/browser.js
  30. +1 −1 test/node_modules/isomorphic-object/index.js
  31. +1 −0 test/node_modules/jsnext/entry-main.js
  32. +2 −1 test/node_modules/jsnext/package.json
  33. +1 −0 test/node_modules/module/entry-main.js
  34. +1 −0 test/node_modules/module/package.json
  35. +3 −0 test/node_modules/react-consumer/index.js
  36. 0 test/node_modules/react-consumer/node_modules/.gitkeep
  37. +1 −0 test/node_modules/react-consumer/node_modules/react/index.js
  38. +1 −0 test/node_modules/react/index.js
  39. +2 −0 test/samples/browser-entry-points-to-node-module/index.js
  40. +15 −0 test/samples/browser-object-with-false/main.js
  41. +2 −2 test/samples/jsnext/main.js
  42. +3 −0 test/samples/prefer-main/main.js
  43. +4 −0 test/samples/react-app/main.js
  44. +258 −64 test/test.js
  45. +16 −0 tsconfig.json
  46. +32 −0 typings-test.js
48 changes: 48 additions & 0 deletions .github/ISSUE_TEMPLATE/BUG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
name: 🐞 Bug Report
about: Something went awry and you'd like to tell us about it.

---

<!--
⚡️ katchow! We 💛 issues.
Please - do not - remove this template.
Please - do not - skip or remove parts of this template.
Or your issue may be closed.
👉🏽 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
❤️ Rollup? Please consider supporting our collective:
👉 https://opencollective.com/rollup/donate
-->

- Node-Resolve Plugin Version:
- Rollup Version:
- Operating System (or Browser):
- Node Version:

### How Do We Reproduce?

<!--
Issues without minimal reproductions will be closed! Please provide one by:
1. Work to isolate the problem and provide the exact steps in this issue, or
2. Provide a minimal repository link (Read https://git.io/fNzHA for instructions).
These may take more time to triage than the other options.
-->


### Expected Behavior


### Actual Behavior


<!--
Most issues can be expressed or demonstrated through a reproduction repository.
However, the situation may arise where some small code snippets also need to
be provided. In that situation, please add your code below using
Fenced Code Blocks (https://help.github.com/articles/creating-and-highlighting-code-blocks/)
-->

33 changes: 33 additions & 0 deletions .github/ISSUE_TEMPLATE/DOCS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
name: 📚 Documentation
about: Are the docs lacking or missing something? Do they need some new 🔥 hotness? Tell us here.

---

<!--
⚡️ katchow! We 💛 issues.
Please - do not - remove this template.
Please - do not - skip or remove parts of this template.
Or your issue may be closed.
👉🏽 Need help or tech support? Don't open an issue!
Head to https://gitter.im/rollup/rollup or https://stackoverflow.com/questions/tagged/rollupjs
❤️ Rollup? Please consider supporting our collective:
👉 https://opencollective.com/rollup/donate
-->

Documentation Is:

<!-- Please place an x (no spaces!) in all [ ] that apply -->

- [ ] Missing
- [ ] Needed
- [ ] Confusing
- [ ] Not Sure?

### Please Explain in Detail...


### Your Proposal for Changes
24 changes: 24 additions & 0 deletions .github/ISSUE_TEMPLATE/FEATURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
name: ✨ Feature Request
about: Suggest an idea for this project

---

<!--
⚡️ katchow! We 💛 issues.
Please - do not - remove this template.
Please - do not - skip or remove parts of this template.
Or your issue may be closed.
👉🏽 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
❤️ Rollup? Please consider supporting our collective:
👉 https://opencollective.com/rollup/donate
-->

### Feature Use Case


### Feature Proposal
28 changes: 28 additions & 0 deletions .github/ISSUE_TEMPLATE/MODIFICATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: 🔧 Modification Request
about: Would you like something work differently? Have an alternative approach? This is the template for you.

---

<!--
⚡️ katchow! We 💛 issues.
Please - do not - remove this template.
Please - do not - skip or remove parts of this template.
Or your issue may be closed.
👉🏽 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
❤️ Rollup? Please consider supporting our collective:
👉 https://opencollective.com/rollup/donate
-->


### Expected Behavior / Situation


### Actual Behavior / Situation


### Modification Proposal
13 changes: 13 additions & 0 deletions .github/ISSUE_TEMPLATE/SUPPORT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
name: 🆘 Support, Help, and Advice
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.

---

Hey there! If you need help or tech support then this is not the place to
ask. Please head to [the Rollup Gitter](https://gitter.im/rollup/rollup)
instead or post a question to https://stackoverflow.com/questions/tagged/rollupjs.

If you arrived here because you think Rollup's documentation is unclear,
insufficient or wrong, please consider creating an issue for the documentation
instead.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -3,3 +3,4 @@ node_modules
dist
.gobble*
!test/node_modules
!test/node_modules/react-consumer/node_modules
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
# rollup-plugin-node-resolve changelog

## 4.2.4 (2019-05-11)

* Add note on builtins to Readme ([#215](https://github.com/rollup/rollup-plugin-node-resolve/pull/215) by @keithamus)
* Add issue templates ([#217](https://github.com/rollup/rollup-plugin-node-resolve/pull/217) by @mecurc)
* Improve performance by caching `isDir` ([#218](https://github.com/rollup/rollup-plugin-node-resolve/pull/218) by @keithamus)

## 4.2.3 (2019-04-11)

* Fix ordering of jsnext:main when using the jsnext option ([#209](https://github.com/rollup/rollup-plugin-node-resolve/pull/209) by @lukastaegert)

## 4.2.2 (2019-04-10)

* Fix TypeScript typings (rename and export Options interface) ([#206](https://github.com/rollup/rollup-plugin-node-resolve/pull/206) by @Kocal)
* Fix mainfields typing ([#207](https://github.com/rollup/rollup-plugin-node-resolve/pull/207) by @nicolashenry)

## 4.2.1 (2019-04-06)

* Respect setting the deprecated fields "module", "main", and "jsnext" ([#204](https://github.com/rollup/rollup-plugin-node-resolve/pull/204) by @nick-woodward)

## 4.2.0 (2019-04-06)

* Add new mainfields option ([#182](https://github.com/rollup/rollup-plugin-node-resolve/pull/182) by @keithamus)
* Added dedupe option to prevent bundling the same package multiple times ([#201](https://github.com/rollup/rollup-plugin-node-resolve/pull/182) by @sormy)

## 4.1.0 (2019-04-05)

* Add TypeScript typings ([#189](https://github.com/rollup/rollup-plugin-node-resolve/pull/189) by @NotWoods)
* Update dependencies ([#202](https://github.com/rollup/rollup-plugin-node-resolve/pull/202) by @lukastaegert)

## 4.0.1 (2019-02-22)

* Fix issue when external modules are specified in `package.browser` ([#143](https://github.com/rollup/rollup-plugin-node-resolve/pull/143) by @keithamus)
* Fix `package.browser` mapping issue when `false` is specified ([#183](https://github.com/rollup/rollup-plugin-node-resolve/pull/183) by @allex)

## 4.0.0 (2018-12-09)

This release will support rollup@1.0
45 changes: 38 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -20,29 +20,38 @@ export default {
input: 'main.js',
output: {
file: 'bundle.js',
format: 'iife'
format: 'iife',
name: 'MyModule'
},
name: 'MyModule',
plugins: [
resolve({

// the fields to scan in a package.json to determine the entry point
// if this list contains "browser", overrides specified in "pkg.browser"
// will be used
mainFields: ['module', 'main'], // Default: ['module', 'main']

// DEPRECATED: use "mainFields" instead
// use "module" field for ES6 module if possible
module: true, // Default: true

// DEPRECATED: use "mainFields" instead
// use "jsnext:main" if possible
// legacy field pointing to ES6 module in third-party libraries,
// deprecated in favor of "pkg.module":
// - see: https://github.com/rollup/rollup/wiki/pkg.module
jsnext: true, // Default: false

// DEPRECATED: use "mainFields" instead
// use "main" field or index.js, even if it's not an ES6 module
// (needs to be converted from CommonJS to ES6
// (needs to be converted from CommonJS to ES6)
// – see https://github.com/rollup/rollup-plugin-commonjs
main: true, // Default: true

// some package.json files have a `browser` field which
// specifies alternative files to load for people bundling
// for the browser. If that's you, use this option, otherwise
// pkg.browser will be ignored
// some package.json files have a "browser" field which specifies
// alternative files to load for people bundling for the browser. If
// that's you, either use this option or add "browser" to the
// "mainfields" option, otherwise pkg.browser will be ignored
browser: true, // Default: false

// not all files you want to resolve are .js files
@@ -65,6 +74,11 @@ export default {
// ES2015 modules
modulesOnly: true, // Default: false

// Force resolving for these modules to root's node_modules that helps
// to prevent bundling the same package multiple times if package is
// imported from dependencies.
dedupe: [ 'react', 'react-dom' ], // Default: []

// Any additional options that should be passed through
// to node-resolve
customResolveOptions: {
@@ -98,6 +112,23 @@ export default {
};
```

## Resolving Built-Ins (like `fs`)

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.

If you want to silence warnings about builtins, you can add the list of builtins to the `externals` option; like so:

```js
import resolve from 'rollup-plugin-node-resolve';
import builtins from 'builtin-modules'
export default ({
input: ...,
plugins: [resolve()],
externals: builtins,
output: ...
})
```


## License

100 changes: 100 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {Plugin} from 'rollup';
import {AsyncOpts} from 'resolve';

export interface Options {
/**
* the fields to scan in a package.json to determine the entry point
* if this list contains "browser", overrides specified in "pkg.browser"
* will be used
* @default ['module', 'main']
*/
mainFields?: ReadonlyArray<string>;

/**
* @deprecated use "mainFields" instead
* use "module" field for ES6 module if possible
* @default true
*/
module?: boolean;

/**
* @deprecated use "mainFields" instead
* use "jsnext:main" if possible
* legacy field pointing to ES6 module in third-party libraries,
* deprecated in favor of "pkg.module":
* - see: https://github.com/rollup/rollup/wiki/pkg.module
* @default false
*/
jsnext?: boolean;

/**
* @deprecated use "mainFields" instead
* use "main" field or index.js, even if it's not an ES6 module
* (needs to be converted from CommonJS to ES6)
* – see https://github.com/rollup/rollup-plugin-commonjs
* @default true
*/
main?: boolean;

/**
* some package.json files have a "browser" field which specifies
* alternative files to load for people bundling for the browser. If
* that's you, either use this option or add "browser" to the
* "mainfields" option, otherwise pkg.browser will be ignored
* @default false
*/
browser?: boolean;

/**
* not all files you want to resolve are .js files
* @default [ '.mjs', '.js', '.json', '.node' ]
*/
extensions?: ReadonlyArray<string>;

/**
* whether to prefer built-in modules (e.g. `fs`, `path`) or
* local ones with the same names
* @default true
*/
preferBuiltins?: boolean;

/**
* Lock the module search in this path (like a chroot). Module defined
* outside this path will be marked as external
* @default '/'
*/
jail?: string;

/**
* Set to an array of strings and/or regexps to lock the module search
* to modules that match at least one entry. Modules not matching any
* entry will be marked as external
* @default null
*/
only?: ReadonlyArray<string | RegExp> | null;

/**
* If true, inspect resolved files to check that they are
* ES2015 modules
* @default false
*/
modulesOnly?: boolean;

/**
* Force resolving for these modules to root's node_modules that helps
* to prevent bundling the same package multiple times if package is
* imported from dependencies.
*/
dedupe?: string[];

/**
* Any additional options that should be passed through
* to node-resolve
*/
customResolveOptions?: AsyncOpts;
}

/**
* Locate modules using the Node resolution algorithm, for using third party modules in node_modules
*/
export default function nodeResolve(options?: Options): Plugin;
2,249 changes: 1,800 additions & 449 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 15 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "rollup-plugin-node-resolve",
"description": "Bundle third-party dependencies in node_modules",
"version": "4.0.0",
"version": "4.2.4",
"devDependencies": {
"buble": "^0.19.6",
"es5-ext": "^0.10.46",
"eslint": "^5.10.0",
"mocha": "^5.2.0",
"rollup": "^0.67.4",
"rollup-plugin-buble": "^0.19.4",
"rollup-plugin-commonjs": "^9.2.0",
"buble": "^0.19.7",
"es5-ext": "^0.10.49",
"eslint": "^5.16.0",
"mocha": "^6.1.2",
"rollup": "^1.9.3",
"rollup-plugin-buble": "^0.19.6",
"rollup-plugin-commonjs": "^9.3.4",
"string-capitalize": "^1.0.1",
"vlq": "^1.0.0"
"typescript": "^3.4.3"
},
"main": "dist/rollup-plugin-node-resolve.cjs.js",
"module": "dist/rollup-plugin-node-resolve.es.js",
@@ -20,19 +20,21 @@
"build": "rollup -c",
"pretest": "npm run build",
"test": "mocha",
"posttest": "eslint src test/*.js",
"posttest": "tsc && eslint src test/*.js",
"lint": "eslint src",
"prepublishOnly": "npm test",
"prepare": "npm run build"
},
"files": [
"src",
"dist"
"dist",
"index.d.ts"
],
"dependencies": {
"builtin-modules": "^3.0.0",
"@types/resolve": "0.0.8",
"builtin-modules": "^3.1.0",
"is-module": "^1.0.0",
"resolve": "^1.8.1"
"resolve": "^1.10.0"
},
"repository": "rollup/rollup-plugin-node-resolve",
"keywords": [
138 changes: 88 additions & 50 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {dirname, extname, normalize, resolve, sep} from 'path';
import {dirname, extname, join, normalize, resolve, sep} from 'path';
import builtins from 'builtin-modules';
import resolveId from 'resolve';
import isModule from 'is-module';
@@ -9,40 +9,66 @@ const ES6_BROWSER_EMPTY = resolve( __dirname, '../src/empty.js' );
// which deploy both ESM .mjs and CommonJS .js files as ESM.
const DEFAULT_EXTS = [ '.mjs', '.js', '.json', '.node' ];

let readFileCache = {};
const readFileAsync = file => new Promise((fulfil, reject) => fs.readFile(file, (err, contents) => err ? reject(err) : fulfil(contents)));
const statAsync = file => new Promise((fulfil, reject) => fs.stat(file, (err, contents) => err ? reject(err) : fulfil(contents)));
function cachedReadFile (file, cb) {
if (file in readFileCache === false) {
readFileCache[file] = readFileAsync(file).catch(err => {
delete readFileCache[file];
throw err;
const cache = fn => {
const cache = new Map();
const wrapped = (param, done) => {
if (cache.has(param) === false) {
cache.set(param, fn(param).catch(err => {
cache.delete(param);
throw err;
}));
}
return cache.get(param).then(result => done(null, result), done);
};
wrapped.clear = () => cache.clear();
return wrapped;
};
const ignoreENOENT = err => {
if (err.code === 'ENOENT') return false;
throw err;
};
const readFileCached = cache(readFileAsync);
const isDirCached = cache(file => statAsync(file).then(stat => stat.isDirectory(), ignoreENOENT));
const isFileCached = cache(file => statAsync(file).then(stat => stat.isFile(), ignoreENOENT));

function getMainFields (options) {
let mainFields;
if (options.mainFields) {
if ('module' in options || 'main' in options || 'jsnext' in options) {
throw new Error(`node-resolve: do not use deprecated 'module', 'main', 'jsnext' options with 'mainFields'`);
}
mainFields = options.mainFields;
} else {
mainFields = [];
[['module', 'module', true], ['jsnext', 'jsnext:main', false], ['main', 'main', true]].forEach(([option, field, defaultIncluded]) => {
if (option in options) {
// eslint-disable-next-line no-console
console.warn(`node-resolve: setting options.${option} is deprecated, please override options.mainFields instead`);
if (options[option]) {
mainFields.push(field);
}
} else if (defaultIncluded) {
mainFields.push(field);
}
});
}
readFileCache[file].then(contents => cb(null, contents), cb);
}

let isFileCache = {};
function cachedIsFile (file, cb) {
if (file in isFileCache === false) {
isFileCache[file] = statAsync(file)
.then(
stat => stat.isFile(),
err => {
if (err.code == 'ENOENT') return false;
delete isFileCache[file];
throw err;
});
if (options.browser && mainFields.indexOf('browser') === -1) {
return ['browser'].concat(mainFields);
}
isFileCache[file].then(contents => cb(null, contents), cb);
if ( !mainFields.length ) {
throw new Error( `Please ensure at least one 'mainFields' value is specified` );
}
return mainFields;
}

const resolveIdAsync = (file, opts) => new Promise((fulfil, reject) => resolveId(file, opts, (err, contents) => err ? reject(err) : fulfil(contents)));

export default function nodeResolve ( options = {} ) {
const useModule = options.module !== false;
const useMain = options.main !== false;
const useJsnext = options.jsnext === true;
const mainFields = getMainFields(options);
const useBrowserOverrides = mainFields.indexOf('browser') !== -1;
const dedupe = options.dedupe || [];
const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
const customResolveOptions = options.customResolveOptions || {};
@@ -59,10 +85,6 @@ export default function nodeResolve ( options = {} ) {
throw new Error( 'options.skip is no longer supported — you should use the main Rollup `external` option instead' );
}

if ( !useModule && !useMain && !useJsnext ) {
throw new Error( `At least one of options.module, options.main or options.jsnext must be true` );
}

let preserveSymlinks;

return {
@@ -73,16 +95,22 @@ export default function nodeResolve ( options = {} ) {
},

generateBundle () {
isFileCache = {};
readFileCache = {};
readFileCached.clear();
isFileCached.clear();
isDirCached.clear();
},

resolveId ( importee, importer ) {
if ( /\0/.test( importee ) ) return null; // ignore IDs with null character, these belong to other plugins

const basedir = importer ? dirname( importer ) : process.cwd();

if (options.browser && browserMapCache[importer]) {
if (dedupe.indexOf(importee) !== -1) {
importee = join(process.cwd(), 'node_modules', importee);
}

// https://github.com/defunctzombie/package-browser-field-spec
if (useBrowserOverrides && browserMapCache[importer]) {
const resolvedImportee = resolve( basedir, importee );
const browser = browserMapCache[importer];
if (browser[importee] === false || browser[resolvedImportee] === false) {
@@ -93,7 +121,6 @@ export default function nodeResolve ( options = {} ) {
}
}


const parts = importee.split( /[/\\]/ );
let id = parts.shift();

@@ -115,9 +142,12 @@ export default function nodeResolve ( options = {} ) {
basedir,
packageFilter ( pkg, pkgPath ) {
const pkgRoot = dirname( pkgPath );
if (options.browser && typeof pkg[ 'browser' ] === 'object') {
if (useBrowserOverrides && typeof pkg[ 'browser' ] === 'object') {
packageBrowserField = Object.keys(pkg[ 'browser' ]).reduce((browser, key) => {
const resolved = pkg[ 'browser' ][ key ] === false ? false : resolve( pkgRoot, pkg[ 'browser' ][ key ] );
let resolved = pkg[ 'browser' ][ key ];
if (resolved && resolved[0] === '.') {
resolved = resolve( pkgRoot, pkg[ 'browser' ][ key ] );
}
browser[ key ] = resolved;
if ( key[0] === '.' ) {
const absoluteKey = resolve( pkgRoot, key );
@@ -133,19 +163,23 @@ export default function nodeResolve ( options = {} ) {
}, {});
}

if (options.browser && typeof pkg[ 'browser' ] === 'string') {
pkg[ 'main' ] = pkg[ 'browser' ];
} else if ( useModule && pkg[ 'module' ] ) {
pkg[ 'main' ] = pkg[ 'module' ];
} else if ( useJsnext && pkg[ 'jsnext:main' ] ) {
pkg[ 'main' ] = pkg[ 'jsnext:main' ];
} else if ( ( useJsnext || useModule ) && !useMain ) {
let overriddenMain = false;
for ( let i = 0; i < mainFields.length; i++ ) {
const field = mainFields[i];
if ( typeof pkg[ field ] === 'string' ) {
pkg[ 'main' ] = pkg[ field ];
overriddenMain = true;
break;
}
}
if ( overriddenMain === false && mainFields.indexOf( 'main' ) === -1 ) {
disregardResult = true;
}
return pkg;
},
readFile: cachedReadFile,
isFile: cachedIsFile,
readFile: readFileCached,
isFile: isFileCached,
isDirectory: isDirCached,
extensions: extensions
};

@@ -157,16 +191,19 @@ export default function nodeResolve ( options = {} ) {
importee,
Object.assign( resolveOptions, customResolveOptions )
)
.catch(() => false)
.then(resolved => {
if (options.browser && packageBrowserField) {
if (packageBrowserField[ resolved ]) {
if ( resolved && useBrowserOverrides && packageBrowserField ) {
if ( packageBrowserField.hasOwnProperty(resolved) ) {
if (!packageBrowserField[resolved]) {
browserMapCache[resolved] = packageBrowserField;
return ES6_BROWSER_EMPTY;
}
resolved = packageBrowserField[ resolved ];
}
browserMapCache[resolved] = packageBrowserField;
}

if ( !disregardResult && resolved !== false ) {
if ( !disregardResult ) {
if ( !preserveSymlinks && resolved && fs.existsSync( resolved ) ) {
resolved = fs.realpathSync( resolved );
}
@@ -190,9 +227,10 @@ export default function nodeResolve ( options = {} ) {
if ( resolved && options.modulesOnly ) {
return readFileAsync( resolved, 'utf-8').then(code => isModule( code ) ? resolved : null);
} else {
return resolved === false ? null : resolved;
return resolved;
}
});
})
.catch(() => null);
}
};
}
1 change: 1 addition & 0 deletions test/node_modules/component-type/index.js
3 changes: 3 additions & 0 deletions test/node_modules/component-type/package.json
2 changes: 1 addition & 1 deletion test/node_modules/isomorphic-nested/lib/browser.js
2 changes: 1 addition & 1 deletion test/node_modules/isomorphic-nested/lib/index.js
2 changes: 1 addition & 1 deletion test/node_modules/isomorphic-object-main-implicit/index.js
2 changes: 1 addition & 1 deletion test/node_modules/isomorphic-object-main/browser.js
2 changes: 1 addition & 1 deletion test/node_modules/isomorphic-object-main/index.js
20 changes: 20 additions & 0 deletions test/node_modules/isomorphic-object-with-false/lib/index.js
8 changes: 8 additions & 0 deletions test/node_modules/isomorphic-object-with-false/package.json
2 changes: 1 addition & 1 deletion test/node_modules/isomorphic-object/browser.js
2 changes: 1 addition & 1 deletion test/node_modules/isomorphic-object/index.js
1 change: 1 addition & 0 deletions test/node_modules/jsnext/entry-main.js
3 changes: 2 additions & 1 deletion test/node_modules/jsnext/package.json
1 change: 1 addition & 0 deletions test/node_modules/module/entry-main.js
1 change: 1 addition & 0 deletions test/node_modules/module/package.json
3 changes: 3 additions & 0 deletions test/node_modules/react-consumer/index.js
Empty file.
1 change: 1 addition & 0 deletions test/node_modules/react/index.js
2 changes: 2 additions & 0 deletions test/samples/browser-entry-points-to-node-module/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import sample from 'browser-entry-points-to-node-module';
export default sample;
15 changes: 15 additions & 0 deletions test/samples/browser-object-with-false/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Client from 'isomorphic-object-with-false';
import HTTPTracker from 'isomorphic-object-with-false/lib/client/http-tracker';
import ES6_BROWSER_EMPTY from '../../../src/empty';
import HTTPTrackerWithSubPath from 'isomorphic-object-with-false/lib/subpath/foo';

// do some assert

assert.deepEqual(new Client('ws:'), { name: 'websocket-tracker' })
assert.deepEqual(new Client('http:'), { name: 'NULL' })
assert.equal(HTTPTracker, ES6_BROWSER_EMPTY);
assert.equal(HTTPTrackerWithSubPath, ES6_BROWSER_EMPTY);


// expose
export default "ok"
4 changes: 2 additions & 2 deletions test/samples/jsnext/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { encode } from 'vlq';
import value from 'jsnext';

export default encode( 123 ); // 2H
export default value;
3 changes: 3 additions & 0 deletions test/samples/prefer-main/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import entry from 'entries';

export default entry;
4 changes: 4 additions & 0 deletions test/samples/react-app/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from 'react'
import ReactConsumer from 'react-consumer'

export { React, ReactConsumer }
322 changes: 258 additions & 64 deletions test/test.js

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strict": true,
"noEmit": true,
"allowJs": true
},
"files": [
"index.d.ts",
"typings-test.js"
]
}
32 changes: 32 additions & 0 deletions typings-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @ts-check
import resolve from '.';

/** @type {import("rollup").RollupOptions} */
const config = {
input: 'main.js',
output: {
file: 'bundle.js',
format: 'iife',
name: 'MyModule',
},
plugins: [
resolve({
mainFields: ['untranspiled', 'module', 'main'],
module: true,
jsnext: true,
main: true,
browser: true,
extensions: [ '.mjs', '.js', '.jsx', '.json' ],
preferBuiltins: false,
jail: '/my/jail/path',
only: [ 'some_module', /^@some_scope\/.*$/ ],
dedupe: ['lodash'],
modulesOnly: true,
customResolveOptions: {
moduleDirectory: 'js_modules'
}
})
]
};

export default config;