Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(node resolve): clean codebase and fix external warnings #155

Merged
merged 20 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 53 additions & 65 deletions packages/node-resolve/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,118 +44,106 @@ Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#comma

## Options

### `mainFields`
### `browser`

Type: `Array[...String]`<br>
Default: `['module', 'main']`<br>
Valid values: `['browser', 'jsnext', 'module', 'main']`
Type: `Boolean`<br>
Default: `false`

Specifies the properties to scan within a `package.json`, used to determine the bundle entry point. The order of property names is significant, as the first-found property is used as the resolved entry point. If the array contains `'browser'`, key/values specified in the `package.json` `browser` property will be used.
If `true`, instructs the plugin to use the `"browser"` property in `package.json` files to specify alternative files to load for bundling. This is useful when bundling for a browser environment. Alternatively, a value of `'browser'` can be added to the `mainFields` option. If `false`, any `"browser"` properties in package files will be ignored. This option takes precedence over `mainFields`.

### `module`
### `customResolveOptions`

DEPRECATED: use "mainFields" instead
Type: `Boolean`<br>
Default: `null`

Use `pkg.module` field for ES6 module if possible. This option takes precedence over both "jsnext" and "main" in the list if such are present.
An `Object` that specifies additional options that should be passed through to `node-resolve`.

### `jsnext`
```
customResolveOptions: {
moduleDirectory: 'js_modules'
}
```

DEPRECATED: use "mainFields" instead
### `dedupe`

Use `pkg['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. This option takes precedence over "main" in the list if such is present.
Type: `Array[...String]`<br>
Default: `[]`

### `main`
An `Array` of modules names, which instructs the plugin to force resolving for the specified modules to the root `node_modules`. Helps to prevent bundling the same package multiple times if package is imported from dependencies.

DEPRECATED: use "mainFields" instead
```js
dedupe: ['my-package', '@namespace/my-package'];
```

Use `pkg.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.
This will deduplicate bare imports such as:

### `browser`
```js
import 'my-package';
import '@namespace/my-package';
```

Type: `Boolean`<br>
Default: `false`
And it will deduplicate deep imports such as:

If `true`, instructs the plugin to use the `"browser"` property in `package.json` files to specify alternative files to load for bundling. This is useful when bundling for a browser environment. Alternatively, a value of `'browser'` can be added to the `mainFields` option. If `false`, any `"browser"` properties in package files will be ignored. This option takes precedence over `mainFields`.
```js
import 'my-package/foo.js';
import '@namespace/my-package/bar.js';
```

### `extensions`

Type: `Array[...String]`<br>
Default: `['.mjs', '.js', '.json', '.node']`

Resolve extensions other than .js in the order specified.

### `preferBuiltins`

Type: `Boolean`<br>
Default: `true`

Whether to prefer built-in modules (e.g. `fs`, `path`) or local ones with the same names
Specifies the extensions of files that the plugin will operate on.

### `jail`

Type: `String`<br>
Default: `'/'`

Lock the module search in this path (like a chroot). Modules defined outside this path will be marked as external.

### `only`

Type: `Array[...String|RegExp]`<br>
Default: `null`
Locks the module search within specified path (e.g. chroot). Modules defined outside this path will be marked as external.

Example: `only: ['some_module', /^@some_scope\/.*$/]`
### `mainFields`

### `modulesOnly`
Type: `Array[...String]`<br>
Default: `['module', 'main']`<br>
Valid values: `['browser', 'jsnext', 'module', 'main']`

Type: `Boolean`<br>
Default: `false`
Specifies the properties to scan within a `package.json`, used to determine the bundle entry point. The order of property names is significant, as the first-found property is used as the resolved entry point. If the array contains `'browser'`, key/values specified in the `package.json` `browser` property will be used.

If true, inspect resolved files to check that they are ES2015 modules.
### `only`

### `dedupe`
DEPRECATED: use "resolveOnly" instead

Type: `Array[...String]`<br>
Default: `[]`
### `preferBuiltins`

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.
Type: `Boolean`<br>
Default: `true`

```
dedupe: [ 'my-package', '@namespace/my-package' ]
```
If `true`, the plugin will prefer built-in modules (e.g. `fs`, `path`). If `false`, the plugin will look for locally installed modules of the same name.

This will deduplicate bare imports such as:
### `modulesOnly`

```js
import 'my-package';
import '@namespace/my-package';
```
Type: `Boolean`<br>
Default: `false`

And it will deduplicate deep imports such as:
If `true`, inspect resolved files to assert that they are ES2015 modules.

```js
import 'my-package/foo.js';
import '@namespace/my-package/bar.js';
```

### `customResolveOptions`
### `resolveOnly`

Type: `Boolean`<br>
Type: `Array[...String|RegExp]`<br>
Default: `null`

Any additional options that should be passed through to node-resolve.
An `Array` which instructs the plugin to limit module resolution to those whose names match patterns in the array. _Note: Modules not matching any patterns will be marked as external._

```
customResolveOptions: {
moduleDirectory: 'js_modules'
}
```
Example: `resolveOnly: ['batman', /^@batcave\/.*$/]`

### `rootDir`

Type: `String`<br>
Default: `process.cwd()`

Root directory to resolve modules from. Used when resolving entrypoint imports, and when resolving deduplicated modules. Useful when executing rollup in a package of a monorepository.
Specifies the root directory from which to resolve modules. Typically used when resolving entry-point imports, and when resolving deduplicated modules. Useful when executing rollup in a package of a mono-repository.

```
// Set the root directory to be the parent folder
Expand Down Expand Up @@ -184,7 +172,7 @@ 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.
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-polyfills](https://github.com/ionic-team/rollup-plugin-node-polyfills) 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:

Expand Down
18 changes: 9 additions & 9 deletions packages/node-resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@
"rollup": "^1.20.0"
},
"dependencies": {
"@rollup/pluginutils": "^3.0.0",
"@rollup/pluginutils": "^3.0.6",
"@types/resolve": "0.0.8",
"builtin-modules": "^3.1.0",
"is-module": "^1.0.0",
"resolve": "^1.11.1"
"resolve": "^1.14.2"
},
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"@rollup/plugin-json": "^4.0.0",
"es5-ext": "^0.10.50",
"rollup": "^1.20.0",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-commonjs": "^10.0.0",
"@babel/core": "^7.8.3",
"@babel/preset-env": "^7.8.3",
"@rollup/plugin-json": "^4.0.1",
"es5-ext": "^0.10.53",
"rollup": "^1.29.0",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.1.0",
"source-map": "^0.7.3",
"string-capitalize": "^1.0.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/node-resolve/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default {
]
})
],
external: Object.keys(pkg.dependencies).concat(['path', 'fs', 'os']),
external: Object.keys(pkg.dependencies).concat(['fs', 'path', 'os', 'util']),
output: [
{ file: pkg.main, format: 'cjs' },
{ file: pkg.module, format: 'es' }
Expand Down
55 changes: 55 additions & 0 deletions packages/node-resolve/src/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { readFile, stat } from './fs';

const onError = (error) => {
if (error.code === 'ENOENT') {
return false;
}
throw error;
};

const makeCache = (fn) => {
const cache = new Map();
const wrapped = async (param, done) => {
if (cache.has(param) === false) {
cache.set(
param,
fn(param).catch((err) => {
cache.delete(param);
throw err;
})
);
}

try {
const result = cache.get(param);
const value = await result;
return done(null, value);
} catch (error) {
return done(error);
}
};

wrapped.clear = () => cache.clear();

return wrapped;
};

export const isDirCached = makeCache(async (file) => {
try {
const stats = await stat(file);
return stats.isDirectory();
} catch (error) {
return onError(error);
}
});

export const isFileCached = makeCache(async (file) => {
try {
const stats = await stat(file);
return stats.isFile();
} catch (error) {
return onError(error);
}
});

export const readCachedFile = makeCache(readFile);
9 changes: 9 additions & 0 deletions packages/node-resolve/src/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import fs from 'fs';

import { promisify } from 'util';

export const exists = promisify(fs.exists);
export const readFile = promisify(fs.readFile);
export const realpath = promisify(fs.realpath);
export { realpathSync } from 'fs';
export const stat = promisify(fs.stat);
Loading