[Pkg-javascript-commits] [node-rollup-plugin-node-resolve] 01/02: New upstream version 3.0.0
Julien Puydt
julien.puydt at laposte.net
Thu Aug 17 14:55:20 UTC 2017
This is an automated email from the git hooks/post-receive script.
jpuydt-guest pushed a commit to branch master
in repository node-rollup-plugin-node-resolve.
commit 30baab3cbce253ae09b54a18fb3b5dcafafbb128
Author: Julien Puydt <julien.puydt at laposte.net>
Date: Fri Jul 7 10:26:54 2017 +0200
New upstream version 3.0.0
---
.eslintrc | 26 ++
.gitignore | 5 +
.travis.yml | 8 +
CHANGELOG.md | 73 ++++
README.md | 95 +++++
appveyor.yml | 30 ++
package.json | 51 +++
rollup.config.js | 11 +
src/empty.js | 1 +
src/index.js | 112 ++++++
test/mocha.opts | 1 +
test/node_modules/.gitkeep | 0
test/node_modules/@scoped/foo/index.js | 1 +
test/node_modules/entries/jsnext-entry.js | 1 +
test/node_modules/entries/main-entry.js | 1 +
test/node_modules/entries/module-entry.js | 1 +
test/node_modules/entries/package.json | 5 +
test/node_modules/events/index.js | 1 +
test/node_modules/events/package.json | 3 +
test/node_modules/isomorphic-object/browser-dep.js | 1 +
.../node_modules/isomorphic-object/browser-test.js | 1 +
test/node_modules/isomorphic-object/browser.js | 5 +
test/node_modules/isomorphic-object/dep.js | 1 +
test/node_modules/isomorphic-object/index.js | 5 +
test/node_modules/isomorphic-object/package.json | 8 +
test/node_modules/isomorphic/browser.js | 1 +
test/node_modules/isomorphic/node.js | 1 +
test/node_modules/isomorphic/package.json | 4 +
test/node_modules/jsnext/entry.js | 1 +
test/node_modules/jsnext/package.json | 3 +
test/node_modules/legacy/entry.js | 1 +
test/node_modules/legacy/package.json | 3 +
test/node_modules/module/entry.js | 1 +
test/node_modules/module/package.json | 3 +
test/node_modules/string/uppercase.js | 3 +
test/node_modules/test/index.js | 1 +
test/node_modules/uses-buffer/index.js | 2 +
test/node_modules/uses-buffer/package.json | 5 +
test/samples/browser-false/main.js | 3 +
test/samples/browser-object/main.js | 2 +
test/samples/browser/main.js | 3 +
test/samples/builtins/main.js | 2 +
test/samples/commonjs/main.js | 3 +
.../custom-resolve-options/js_modules/foo.js | 1 +
test/samples/custom-resolve-options/main.js | 3 +
test/samples/deep-import-non-module/main.js | 3 +
test/samples/extensions/lol.wut | 1 +
test/samples/extensions/main.js | 3 +
test/samples/granular/main.js | 3 +
test/samples/jail/main.js | 3 +
test/samples/jsnext/main.js | 3 +
test/samples/local-index/forty-two/index.js | 1 +
test/samples/local-index/main.js | 3 +
test/samples/module/main.js | 3 +
test/samples/package-index/main.js | 1 +
test/samples/prefer-builtin/main.js | 1 +
test/samples/prefer-jsnext/main.js | 3 +
test/samples/prefer-module/main.js | 3 +
test/samples/scoped/main.js | 3 +
test/samples/symlinked/first/index.js | 2 +
test/samples/symlinked/second/index.js | 3 +
test/samples/symlinked/third/index.js | 3 +
test/samples/trailing-slash/main.js | 3 +
test/samples/unresolved-global/main.js | 1 +
test/samples/unresolved-local/main.js | 1 +
test/test.js | 386 +++++++++++++++++++++
66 files changed, 926 insertions(+)
diff --git a/.eslintrc b/.eslintrc
new file mode 100644
index 0000000..6f95398
--- /dev/null
+++ b/.eslintrc
@@ -0,0 +1,26 @@
+{
+ "rules": {
+ "indent": [ 2, "tab", { "SwitchCase": 1 } ],
+ "quotes": [ 2, "single", { allowTemplateLiterals: true } ],
+ "semi": [ 2, "always" ],
+ "keyword-spacing": [ 2, { "before": true, "after": true } ],
+ "space-before-blocks": [ 2, "always" ],
+ "space-before-function-paren": [ 2, "always" ],
+ "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ],
+ "no-cond-assign": 0,
+ "no-unused-vars": 2,
+ "prefer-const": 2,
+ "no-var": 2
+ },
+ "env": {
+ "es6": true,
+ "browser": true,
+ "mocha": true,
+ "node": true
+ },
+ "extends": "eslint:recommended",
+ "parserOptions": {
+ "ecmaVersion": 6,
+ "sourceType": "module"
+ }
+}
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..08625d3
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+.DS_Store
+node_modules
+dist
+.gobble*
+!test/node_modules
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..81cac26
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,8 @@
+language: node_js
+node_js:
+ - "4"
+ - "6"
+ - "stable"
+env:
+ global:
+ - BUILD_TIMEOUT=10000
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..b328ccc
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,73 @@
+# rollup-plugin-node-resolve changelog
+
+## 3.0.0
+
+* [BREAKING] Remove `options.skip` ([#90](https://github.com/rollup/rollup-plugin-node-resolve/pull/90))
+* Add `modulesOnly` option ([#96](https://github.com/rollup/rollup-plugin-node-resolve/pull/96))
+
+## 2.1.1
+
+* Prevent `jail` from breaking builds on Windows ([#93](https://github.com/rollup/rollup-plugin-node-resolve/issues/93))
+
+## 2.1.0
+
+* Add `jail` option ([#53](https://github.com/rollup/rollup-plugin-node-resolve/pull/53))
+* Add `customResolveOptions` option ([#79](https://github.com/rollup/rollup-plugin-node-resolve/pull/79))
+* Support symlinked packages ([#82](https://github.com/rollup/rollup-plugin-node-resolve/pull/82))
+
+## 2.0.0
+
+* Add support `module` field in package.json as an official alternative to jsnext
+
+## 1.7.3
+
+* Error messages are more descriptive ([#50](https://github.com/rollup/rollup-plugin-node-resolve/issues/50))
+
+## 1.7.2
+
+* Allow entry point paths beginning with ./
+
+## 1.7.1
+
+* Return a `name`
+
+## 1.7.0
+
+* Allow relative IDs to be external ([#32](https://github.com/rollup/rollup-plugin-node-resolve/pull/32))
+
+## 1.6.0
+
+* Skip IDs containing null character
+
+## 1.5.0
+
+* Prefer built-in options, but allow opting out ([#28](https://github.com/rollup/rollup-plugin-node-resolve/pull/28))
+
+## 1.4.0
+
+* Pass `options.extensions` through to `node-resolve`
+
+## 1.3.0
+
+* `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))
+
+## 1.2.1
+
+* Support scoped packages in `skip` option ([#15](https://github.com/rollup/rollup-plugin-node-resolve/issues/15))
+
+## 1.2.0
+
+* Support `browser` field ([#8](https://github.com/rollup/rollup-plugin-node-resolve/issues/8))
+* Get tests to pass on Windows
+
+## 1.1.0
+
+* Use node-resolve to handle various corner cases
+
+## 1.0.0
+
+* Add ES6 build, use Rollup 0.20.0
+
+## 0.1.0
+
+* First release
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..428c51e
--- /dev/null
+++ b/README.md
@@ -0,0 +1,95 @@
+# rollup-plugin-node-resolve
+
+*This plugin used to be called rollup-plugin-npm*
+
+Locate modules using the [Node resolution algorithm](https://nodejs.org/api/modules.html#modules_all_together), for using third party modules in `node_modules`
+
+## Installation
+
+```bash
+npm install --save-dev rollup-plugin-node-resolve
+```
+
+## Usage
+
+```js
+// rollup.config.js
+import { rollup } from 'rollup';
+import resolve from 'rollup-plugin-node-resolve';
+
+export default {
+ entry: 'main.js',
+ dest: 'bundle.js',
+ moduleName: 'MyModule',
+ format: 'iife'
+ plugins: [
+ resolve({
+ // use "module" field for ES6 module if possible
+ module: true, // Default: true
+
+ // use "jsnext:main" if possible
+ // – see https://github.com/rollup/rollup/wiki/jsnext:main
+ jsnext: true, // Default: false
+
+ // 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
+ 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
+ browser: true, // Default: false
+
+ // not all files you want to resolve are .js files
+ extensions: [ '.js', '.json' ], // Default: ['.js']
+
+ // whether to prefer built-in modules (e.g. `fs`, `path`) or
+ // local ones with the same names
+ preferBuiltins: false, // Default: true
+
+ // Lock the module search in this path (like a chroot). Module defined
+ // outside this path will be mark has external
+ jail: '/my/jail/path', // Default: '/'
+
+ // If true, inspect resolved files to check that they are
+ // ES2015 modules
+ modulesOnly: true, // Default: false
+
+ // Any additional options that should be passed through
+ // to node-resolve
+ customResolveOptions: {
+ moduleDirectory: 'js_modules'
+ }
+ })
+ ]
+};
+```
+
+## Using with rollup-plugin-commonjs
+
+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):
+
+```js
+// rollup.config.js
+import { rollup } from 'rollup';
+import resolve from 'rollup-plugin-node-resolve';
+import commonjs from 'rollup-plugin-commonjs';
+
+export default {
+ entry: 'main.js',
+ dest: 'bundle.js',
+ moduleName: 'MyModule',
+ format: 'iife'
+ plugins: [
+ resolve({ jsnext: true, main: true }),
+ commonjs()
+ ]
+};
+```
+
+
+## License
+
+MIT
diff --git a/appveyor.yml b/appveyor.yml
new file mode 100644
index 0000000..5ff3e17
--- /dev/null
+++ b/appveyor.yml
@@ -0,0 +1,30 @@
+# http://www.appveyor.com/docs/appveyor-yml
+
+version: "{build}"
+
+clone_depth: 10
+
+init:
+ - git config --global core.autocrlf false
+
+environment:
+ matrix:
+ # node.js
+ - nodejs_version: stable
+
+install:
+ - ps: Install-Product node $env:nodejs_version
+ - npm install
+
+build: off
+
+test_script:
+ - node --version && npm --version
+ - npm test
+
+matrix:
+ fast_finish: false
+
+# cache:
+# - C:\Users\appveyor\AppData\Roaming\npm-cache -> package.json # npm cache
+# - node_modules -> package.json # local npm modules
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..87a28e6
--- /dev/null
+++ b/package.json
@@ -0,0 +1,51 @@
+{
+ "name": "rollup-plugin-node-resolve",
+ "description": "Bundle third-party dependencies in node_modules",
+ "version": "3.0.0",
+ "devDependencies": {
+ "buble": "^0.15.2",
+ "es5-ext": "^0.10.11",
+ "eslint": "^3.18.0",
+ "mocha": "^3.0.2",
+ "rollup": "^0.41.6",
+ "rollup-plugin-buble": "^0.15.0",
+ "rollup-plugin-commonjs": "^8.0.2",
+ "string-capitalize": "^1.0.1",
+ "vlq": "^0.2.1"
+ },
+ "main": "dist/rollup-plugin-node-resolve.cjs.js",
+ "module": "dist/rollup-plugin-node-resolve.es.js",
+ "jsnext:main": "dist/rollup-plugin-node-resolve.es.js",
+ "scripts": {
+ "build": "rollup -c",
+ "pretest": "npm run build",
+ "test": "mocha",
+ "posttest": "eslint src test/*.js",
+ "prepublish": "npm test",
+ "lint": "eslint src"
+ },
+ "files": [
+ "src",
+ "dist"
+ ],
+ "dependencies": {
+ "browser-resolve": "^1.11.0",
+ "builtin-modules": "^1.1.0",
+ "is-module": "^1.0.0",
+ "resolve": "^1.1.6"
+ },
+ "repository": "rollup/rollup-plugin-node-resolve",
+ "keywords": [
+ "rollup",
+ "rollup-plugin",
+ "es2015",
+ "npm",
+ "modules"
+ ],
+ "author": "Rich Harris <richard.a.harris at gmail.com>",
+ "license": "MIT",
+ "bugs": {
+ "url": "https://github.com/rollup/rollup-plugin-node-resolve/issues"
+ },
+ "homepage": "https://github.com/rollup/rollup-plugin-node-resolve#readme"
+}
diff --git a/rollup.config.js b/rollup.config.js
new file mode 100644
index 0000000..f22bb4b
--- /dev/null
+++ b/rollup.config.js
@@ -0,0 +1,11 @@
+import buble from 'rollup-plugin-buble';
+
+export default {
+ entry: 'src/index.js',
+ plugins: [ buble() ],
+ external: [ 'path', 'fs', 'builtin-modules', 'resolve', 'browser-resolve', 'is-module' ],
+ targets: [
+ { dest: 'dist/rollup-plugin-node-resolve.cjs.js', format: 'cjs' },
+ { dest: 'dist/rollup-plugin-node-resolve.es.js', format: 'es' }
+ ]
+};
diff --git a/src/empty.js b/src/empty.js
new file mode 100644
index 0000000..ff8b4c5
--- /dev/null
+++ b/src/empty.js
@@ -0,0 +1 @@
+export default {};
diff --git a/src/index.js b/src/index.js
new file mode 100644
index 0000000..5fe6d4c
--- /dev/null
+++ b/src/index.js
@@ -0,0 +1,112 @@
+import { dirname, resolve, normalize, sep } from 'path';
+import builtins from 'builtin-modules';
+import _nodeResolve from 'resolve';
+import browserResolve from 'browser-resolve';
+import isModule from 'is-module';
+import fs from 'fs';
+
+const COMMONJS_BROWSER_EMPTY = _nodeResolve.sync( 'browser-resolve/empty.js', __dirname );
+const ES6_BROWSER_EMPTY = resolve( __dirname, '../src/empty.js' );
+const CONSOLE_WARN = ( ...args ) => console.warn( ...args ); // eslint-disable-line no-console
+
+export default function nodeResolve ( options = {} ) {
+ const useModule = options.module !== false;
+ const useMain = options.main !== false;
+ const useJsnext = options.jsnext === true;
+ const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
+ const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
+ const customResolveOptions = options.customResolveOptions || {};
+ const jail = options.jail;
+
+ const onwarn = options.onwarn || CONSOLE_WARN;
+ const resolveId = options.browser ? browserResolve : _nodeResolve;
+
+ if ( options.skip ) {
+ throw new Error( 'options.skip is no longer supported — you should use the main Rollup `externals` option instead' );
+ }
+
+ if ( !useModule && !useMain && !useJsnext ) {
+ throw new Error( `At least one of options.module, options.main or options.jsnext must be true` );
+ }
+
+ return {
+ name: 'node-resolve',
+
+ resolveId ( importee, importer ) {
+ if ( /\0/.test( importee ) ) return null; // ignore IDs with null character, these belong to other plugins
+
+ // disregard entry module
+ if ( !importer ) return null;
+
+ const parts = importee.split( /[\/\\]/ );
+ let id = parts.shift();
+
+ if ( id[0] === '@' && parts.length ) {
+ // scoped packages
+ id += `/${parts.shift()}`;
+ } else if ( id[0] === '.' ) {
+ // an import relative to the parent dir of the importer
+ id = resolve( importer, '..', importee );
+ }
+
+ return new Promise( ( fulfil, reject ) => {
+ let disregardResult = false;
+
+ resolveId(
+ importee,
+ Object.assign({
+ basedir: dirname( importer ),
+ packageFilter ( pkg ) {
+ if ( useModule && pkg[ 'module' ] ) {
+ pkg[ 'main' ] = pkg[ 'module' ];
+ } else if ( useJsnext && pkg[ 'jsnext:main' ] ) {
+ pkg[ 'main' ] = pkg[ 'jsnext:main' ];
+ } else if ( ( useJsnext || useModule ) && !useMain ) {
+ disregardResult = true;
+ }
+ return pkg;
+ },
+ extensions: options.extensions
+ }, customResolveOptions ),
+ ( err, resolved ) => {
+ if ( !disregardResult && !err ) {
+ if ( resolved && fs.existsSync( resolved ) ) {
+ resolved = fs.realpathSync( resolved );
+ }
+
+ if ( resolved === COMMONJS_BROWSER_EMPTY ) {
+ fulfil( ES6_BROWSER_EMPTY );
+ } else if ( ~builtins.indexOf( resolved ) ) {
+ fulfil( null );
+ } else if ( ~builtins.indexOf( importee ) && preferBuiltins ) {
+ if ( !isPreferBuiltinsSet ) {
+ onwarn(
+ `preferring built-in module '${importee}' over local alternative ` +
+ `at '${resolved}', pass 'preferBuiltins: false' to disable this ` +
+ `behavior or 'preferBuiltins: true' to disable this warning`
+ );
+ }
+ fulfil( null );
+ } else if ( jail && resolved.indexOf( normalize( jail.trim( sep ) ) ) !== 0 ) {
+ fulfil( null );
+ }
+ }
+
+ if ( resolved && options.modulesOnly ) {
+ fs.readFile( resolved, 'utf-8', ( err, code ) => {
+ if ( err ) {
+ reject( err );
+ } else {
+ const valid = isModule( code );
+ fulfil( valid ? resolved : null );
+ }
+ });
+ } else {
+ fulfil( resolved );
+ }
+ }
+ );
+ });
+ }
+ };
+}
diff --git a/test/mocha.opts b/test/mocha.opts
new file mode 100644
index 0000000..078771e
--- /dev/null
+++ b/test/mocha.opts
@@ -0,0 +1 @@
+--compilers js:buble/register
diff --git a/test/node_modules/.gitkeep b/test/node_modules/.gitkeep
new file mode 100644
index 0000000..e69de29
diff --git a/test/node_modules/@scoped/foo/index.js b/test/node_modules/@scoped/foo/index.js
new file mode 100644
index 0000000..20fc97b
--- /dev/null
+++ b/test/node_modules/@scoped/foo/index.js
@@ -0,0 +1 @@
+export default 'FOO';
diff --git a/test/node_modules/entries/jsnext-entry.js b/test/node_modules/entries/jsnext-entry.js
new file mode 100644
index 0000000..1f55238
--- /dev/null
+++ b/test/node_modules/entries/jsnext-entry.js
@@ -0,0 +1 @@
+export default 'JSNEXT-ENTRY';
diff --git a/test/node_modules/entries/main-entry.js b/test/node_modules/entries/main-entry.js
new file mode 100644
index 0000000..4af5ca4
--- /dev/null
+++ b/test/node_modules/entries/main-entry.js
@@ -0,0 +1 @@
+export default 'MAIN-ENTRY';
diff --git a/test/node_modules/entries/module-entry.js b/test/node_modules/entries/module-entry.js
new file mode 100644
index 0000000..95c37d9
--- /dev/null
+++ b/test/node_modules/entries/module-entry.js
@@ -0,0 +1 @@
+export default 'MODULE-ENTRY';
diff --git a/test/node_modules/entries/package.json b/test/node_modules/entries/package.json
new file mode 100644
index 0000000..155bbf8
--- /dev/null
+++ b/test/node_modules/entries/package.json
@@ -0,0 +1,5 @@
+{
+ "main": "main-entry.js",
+ "module": "module-entry.js",
+ "jsnext:main": "jsnext-entry.js"
+}
diff --git a/test/node_modules/events/index.js b/test/node_modules/events/index.js
new file mode 100644
index 0000000..e9aca16
--- /dev/null
+++ b/test/node_modules/events/index.js
@@ -0,0 +1 @@
+export default 'not the built-in events module';
diff --git a/test/node_modules/events/package.json b/test/node_modules/events/package.json
new file mode 100644
index 0000000..5e55a1e
--- /dev/null
+++ b/test/node_modules/events/package.json
@@ -0,0 +1,3 @@
+{
+ "main": "./index.js"
+}
diff --git a/test/node_modules/isomorphic-object/browser-dep.js b/test/node_modules/isomorphic-object/browser-dep.js
new file mode 100644
index 0000000..35ba3db
--- /dev/null
+++ b/test/node_modules/isomorphic-object/browser-dep.js
@@ -0,0 +1 @@
+export default 'browser-dep';
diff --git a/test/node_modules/isomorphic-object/browser-test.js b/test/node_modules/isomorphic-object/browser-test.js
new file mode 100644
index 0000000..8efc729
--- /dev/null
+++ b/test/node_modules/isomorphic-object/browser-test.js
@@ -0,0 +1 @@
+export default 43;
diff --git a/test/node_modules/isomorphic-object/browser.js b/test/node_modules/isomorphic-object/browser.js
new file mode 100644
index 0000000..136fdc4
--- /dev/null
+++ b/test/node_modules/isomorphic-object/browser.js
@@ -0,0 +1,5 @@
+import dep from './dep.js';
+import test from 'test';
+
+var env = 'browser';
+export { env, dep, test };
diff --git a/test/node_modules/isomorphic-object/dep.js b/test/node_modules/isomorphic-object/dep.js
new file mode 100644
index 0000000..2089e19
--- /dev/null
+++ b/test/node_modules/isomorphic-object/dep.js
@@ -0,0 +1 @@
+export default 'node-dep';
diff --git a/test/node_modules/isomorphic-object/index.js b/test/node_modules/isomorphic-object/index.js
new file mode 100644
index 0000000..12299ac
--- /dev/null
+++ b/test/node_modules/isomorphic-object/index.js
@@ -0,0 +1,5 @@
+import dep from './dep.js';
+import test from 'test';
+
+var env = 'node';
+export { env, dep, test };
diff --git a/test/node_modules/isomorphic-object/package.json b/test/node_modules/isomorphic-object/package.json
new file mode 100644
index 0000000..c723259
--- /dev/null
+++ b/test/node_modules/isomorphic-object/package.json
@@ -0,0 +1,8 @@
+{
+ "main": "./index.js",
+ "browser": {
+ "./index.js": "./browser.js",
+ "./dep.js": "./browser-dep.js",
+ "test": "./browser-test.js"
+ }
+}
diff --git a/test/node_modules/isomorphic/browser.js b/test/node_modules/isomorphic/browser.js
new file mode 100644
index 0000000..9577136
--- /dev/null
+++ b/test/node_modules/isomorphic/browser.js
@@ -0,0 +1 @@
+export default 'browser';
diff --git a/test/node_modules/isomorphic/node.js b/test/node_modules/isomorphic/node.js
new file mode 100644
index 0000000..502c042
--- /dev/null
+++ b/test/node_modules/isomorphic/node.js
@@ -0,0 +1 @@
+export default 'node';
diff --git a/test/node_modules/isomorphic/package.json b/test/node_modules/isomorphic/package.json
new file mode 100644
index 0000000..f3c0e61
--- /dev/null
+++ b/test/node_modules/isomorphic/package.json
@@ -0,0 +1,4 @@
+{
+ "main": "./node.js",
+ "browser": "./browser.js"
+}
diff --git a/test/node_modules/jsnext/entry.js b/test/node_modules/jsnext/entry.js
new file mode 100644
index 0000000..b8e8806
--- /dev/null
+++ b/test/node_modules/jsnext/entry.js
@@ -0,0 +1 @@
+export default 'JSNEXT';
diff --git a/test/node_modules/jsnext/package.json b/test/node_modules/jsnext/package.json
new file mode 100644
index 0000000..ce8c9fe
--- /dev/null
+++ b/test/node_modules/jsnext/package.json
@@ -0,0 +1,3 @@
+{
+ "jsnext:main": "entry.js"
+}
diff --git a/test/node_modules/legacy/entry.js b/test/node_modules/legacy/entry.js
new file mode 100644
index 0000000..67ad7f6
--- /dev/null
+++ b/test/node_modules/legacy/entry.js
@@ -0,0 +1 @@
+export default 'LEGACY';
diff --git a/test/node_modules/legacy/package.json b/test/node_modules/legacy/package.json
new file mode 100644
index 0000000..6cde76d
--- /dev/null
+++ b/test/node_modules/legacy/package.json
@@ -0,0 +1,3 @@
+{
+ "main": "entry.js"
+}
diff --git a/test/node_modules/module/entry.js b/test/node_modules/module/entry.js
new file mode 100644
index 0000000..6772250
--- /dev/null
+++ b/test/node_modules/module/entry.js
@@ -0,0 +1 @@
+export default 'MODULE';
diff --git a/test/node_modules/module/package.json b/test/node_modules/module/package.json
new file mode 100644
index 0000000..8b4060a
--- /dev/null
+++ b/test/node_modules/module/package.json
@@ -0,0 +1,3 @@
+{
+ "module": "entry.js"
+}
diff --git a/test/node_modules/string/uppercase.js b/test/node_modules/string/uppercase.js
new file mode 100644
index 0000000..05fdfb7
--- /dev/null
+++ b/test/node_modules/string/uppercase.js
@@ -0,0 +1,3 @@
+export default function uppercase ( str ) {
+ return str.toUpperCase();
+}
diff --git a/test/node_modules/test/index.js b/test/node_modules/test/index.js
new file mode 100644
index 0000000..7a4e8a7
--- /dev/null
+++ b/test/node_modules/test/index.js
@@ -0,0 +1 @@
+export default 42;
diff --git a/test/node_modules/uses-buffer/index.js b/test/node_modules/uses-buffer/index.js
new file mode 100644
index 0000000..849dd1e
--- /dev/null
+++ b/test/node_modules/uses-buffer/index.js
@@ -0,0 +1,2 @@
+import Buffer from 'buffer';
+export default Buffer;
diff --git a/test/node_modules/uses-buffer/package.json b/test/node_modules/uses-buffer/package.json
new file mode 100644
index 0000000..0793b7b
--- /dev/null
+++ b/test/node_modules/uses-buffer/package.json
@@ -0,0 +1,5 @@
+{
+ "browser": {
+ "buffer": false
+ }
+}
diff --git a/test/samples/browser-false/main.js b/test/samples/browser-false/main.js
new file mode 100644
index 0000000..4fe7b8e
--- /dev/null
+++ b/test/samples/browser-false/main.js
@@ -0,0 +1,3 @@
+import thing from 'uses-buffer';
+
+assert.deepEqual( thing, {} );
diff --git a/test/samples/browser-object/main.js b/test/samples/browser-object/main.js
new file mode 100644
index 0000000..f1ff980
--- /dev/null
+++ b/test/samples/browser-object/main.js
@@ -0,0 +1,2 @@
+import { env, dep, test } from 'isomorphic-object';
+export { env, dep, test };
diff --git a/test/samples/browser/main.js b/test/samples/browser/main.js
new file mode 100644
index 0000000..043d164
--- /dev/null
+++ b/test/samples/browser/main.js
@@ -0,0 +1,3 @@
+import env from 'isomorphic';
+
+export default env;
diff --git a/test/samples/builtins/main.js b/test/samples/builtins/main.js
new file mode 100644
index 0000000..3c3ed41
--- /dev/null
+++ b/test/samples/builtins/main.js
@@ -0,0 +1,2 @@
+import { sep } from 'path';
+export default sep;
diff --git a/test/samples/commonjs/main.js b/test/samples/commonjs/main.js
new file mode 100644
index 0000000..97d5074
--- /dev/null
+++ b/test/samples/commonjs/main.js
@@ -0,0 +1,3 @@
+import capitalize from 'string-capitalize';
+
+export default capitalize( 'it works!' );
diff --git a/test/samples/custom-resolve-options/js_modules/foo.js b/test/samples/custom-resolve-options/js_modules/foo.js
new file mode 100644
index 0000000..a4012bf
--- /dev/null
+++ b/test/samples/custom-resolve-options/js_modules/foo.js
@@ -0,0 +1 @@
+export default 42;
\ No newline at end of file
diff --git a/test/samples/custom-resolve-options/main.js b/test/samples/custom-resolve-options/main.js
new file mode 100644
index 0000000..805f580
--- /dev/null
+++ b/test/samples/custom-resolve-options/main.js
@@ -0,0 +1,3 @@
+import foo from 'foo';
+
+assert.equal( foo, 42 );
\ No newline at end of file
diff --git a/test/samples/deep-import-non-module/main.js b/test/samples/deep-import-non-module/main.js
new file mode 100644
index 0000000..7ac076d
--- /dev/null
+++ b/test/samples/deep-import-non-module/main.js
@@ -0,0 +1,3 @@
+import deep from 'foo/deep';
+
+console.log( deep );
\ No newline at end of file
diff --git a/test/samples/extensions/lol.wut b/test/samples/extensions/lol.wut
new file mode 100644
index 0000000..96100ae
--- /dev/null
+++ b/test/samples/extensions/lol.wut
@@ -0,0 +1 @@
+export var answer = 42;
diff --git a/test/samples/extensions/main.js b/test/samples/extensions/main.js
new file mode 100644
index 0000000..f0d71fe
--- /dev/null
+++ b/test/samples/extensions/main.js
@@ -0,0 +1,3 @@
+import { answer } from './lol';
+
+assert.equal( answer, 42 );
diff --git a/test/samples/granular/main.js b/test/samples/granular/main.js
new file mode 100644
index 0000000..5fd4a7b
--- /dev/null
+++ b/test/samples/granular/main.js
@@ -0,0 +1,3 @@
+import uppercase from 'string/uppercase.js';
+
+export default uppercase( 'foo' );
diff --git a/test/samples/jail/main.js b/test/samples/jail/main.js
new file mode 100644
index 0000000..5fd4a7b
--- /dev/null
+++ b/test/samples/jail/main.js
@@ -0,0 +1,3 @@
+import uppercase from 'string/uppercase.js';
+
+export default uppercase( 'foo' );
diff --git a/test/samples/jsnext/main.js b/test/samples/jsnext/main.js
new file mode 100644
index 0000000..a1d379a
--- /dev/null
+++ b/test/samples/jsnext/main.js
@@ -0,0 +1,3 @@
+import { encode } from 'vlq';
+
+export default encode( 123 ); // 2H
diff --git a/test/samples/local-index/forty-two/index.js b/test/samples/local-index/forty-two/index.js
new file mode 100644
index 0000000..7a4e8a7
--- /dev/null
+++ b/test/samples/local-index/forty-two/index.js
@@ -0,0 +1 @@
+export default 42;
diff --git a/test/samples/local-index/main.js b/test/samples/local-index/main.js
new file mode 100644
index 0000000..6825e66
--- /dev/null
+++ b/test/samples/local-index/main.js
@@ -0,0 +1,3 @@
+import fortyTwo from './forty-two';
+
+export default fortyTwo;
diff --git a/test/samples/module/main.js b/test/samples/module/main.js
new file mode 100644
index 0000000..01b304c
--- /dev/null
+++ b/test/samples/module/main.js
@@ -0,0 +1,3 @@
+import module from 'module';
+
+export default module; // MODULE
diff --git a/test/samples/package-index/main.js b/test/samples/package-index/main.js
new file mode 100644
index 0000000..5870991
--- /dev/null
+++ b/test/samples/package-index/main.js
@@ -0,0 +1 @@
+import 'es5-ext/object/set-prototype-of';
diff --git a/test/samples/prefer-builtin/main.js b/test/samples/prefer-builtin/main.js
new file mode 100644
index 0000000..c6ab084
--- /dev/null
+++ b/test/samples/prefer-builtin/main.js
@@ -0,0 +1 @@
+import 'events';
diff --git a/test/samples/prefer-jsnext/main.js b/test/samples/prefer-jsnext/main.js
new file mode 100644
index 0000000..c5a945d
--- /dev/null
+++ b/test/samples/prefer-jsnext/main.js
@@ -0,0 +1,3 @@
+import entry from 'entries';
+
+export default entry;
diff --git a/test/samples/prefer-module/main.js b/test/samples/prefer-module/main.js
new file mode 100644
index 0000000..c5a945d
--- /dev/null
+++ b/test/samples/prefer-module/main.js
@@ -0,0 +1,3 @@
+import entry from 'entries';
+
+export default entry;
diff --git a/test/samples/scoped/main.js b/test/samples/scoped/main.js
new file mode 100644
index 0000000..708f7f9
--- /dev/null
+++ b/test/samples/scoped/main.js
@@ -0,0 +1,3 @@
+import foo from '@scoped/foo';
+
+foo();
diff --git a/test/samples/symlinked/first/index.js b/test/samples/symlinked/first/index.js
new file mode 100644
index 0000000..2258e07
--- /dev/null
+++ b/test/samples/symlinked/first/index.js
@@ -0,0 +1,2 @@
+export { default as number1 } from 'second';
+export { default as number2 } from 'third';
diff --git a/test/samples/symlinked/second/index.js b/test/samples/symlinked/second/index.js
new file mode 100644
index 0000000..989df82
--- /dev/null
+++ b/test/samples/symlinked/second/index.js
@@ -0,0 +1,3 @@
+import randomNumber from 'third';
+
+export default randomNumber;
diff --git a/test/samples/symlinked/third/index.js b/test/samples/symlinked/third/index.js
new file mode 100644
index 0000000..f62d293
--- /dev/null
+++ b/test/samples/symlinked/third/index.js
@@ -0,0 +1,3 @@
+const randomNumber = Math.random();
+
+export default randomNumber;
diff --git a/test/samples/trailing-slash/main.js b/test/samples/trailing-slash/main.js
new file mode 100644
index 0000000..2f52b4b
--- /dev/null
+++ b/test/samples/trailing-slash/main.js
@@ -0,0 +1,3 @@
+import capitalize from 'string-capitalize/';
+
+export default capitalize( 'it works!' );
diff --git a/test/samples/unresolved-global/main.js b/test/samples/unresolved-global/main.js
new file mode 100644
index 0000000..02155a9
--- /dev/null
+++ b/test/samples/unresolved-global/main.js
@@ -0,0 +1 @@
+import 'foo';
\ No newline at end of file
diff --git a/test/samples/unresolved-local/main.js b/test/samples/unresolved-local/main.js
new file mode 100644
index 0000000..1e3b070
--- /dev/null
+++ b/test/samples/unresolved-local/main.js
@@ -0,0 +1 @@
+import './foo';
\ No newline at end of file
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..614a687
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,386 @@
+const path = require( 'path' );
+const assert = require( 'assert' );
+const rollup = require( 'rollup' );
+const commonjs = require( 'rollup-plugin-commonjs' );
+const buble = require( 'rollup-plugin-buble' );
+const nodeResolve = require( '..' );
+const fs = require( 'fs' );
+
+process.chdir( __dirname );
+
+function executeBundle ( bundle ) {
+ const generated = bundle.generate({
+ format: 'cjs'
+ });
+
+ const fn = new Function ( 'module', 'exports', 'assert', generated.code );
+ const module = { exports: {} };
+
+ fn( module, module.exports, assert );
+
+ return module;
+}
+
+describe( 'rollup-plugin-node-resolve', function () {
+ it( 'finds a module with jsnext:main', function () {
+ return rollup.rollup({
+ entry: 'samples/jsnext/main.js',
+ plugins: [
+ nodeResolve({ jsnext: true })
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports, '2H' );
+ });
+ });
+
+ it( 'finds and converts a basic CommonJS module', function () {
+ return rollup.rollup({
+ entry: 'samples/commonjs/main.js',
+ plugins: [
+ nodeResolve({ main: true }),
+ commonjs()
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports, 'It works!' );
+ });
+ });
+
+ it( 'handles a trailing slash', function () {
+ return rollup.rollup({
+ entry: 'samples/trailing-slash/main.js',
+ plugins: [
+ nodeResolve({ main: true }),
+ commonjs()
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports, 'It works!' );
+ });
+ });
+
+ it( 'finds a file inside a package directory', function () {
+ return rollup.rollup({
+ entry: 'samples/granular/main.js',
+ plugins: [
+ nodeResolve(),
+ buble()
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports, 'FOO' );
+ });
+ });
+
+ it( 'loads local directories by finding index.js within them', function () {
+ return rollup.rollup({
+ entry: 'samples/local-index/main.js',
+ plugins: [
+ nodeResolve()
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports, 42 );
+ });
+ });
+
+ it( 'loads package directories by finding index.js within them', function () {
+ return rollup.rollup({
+ entry: 'samples/package-index/main.js',
+ plugins: [
+ nodeResolve()
+ ]
+ }).then( function ( bundle ) {
+ const generated = bundle.generate({
+ format: 'cjs'
+ });
+
+ assert.ok( ~generated.code.indexOf( 'setPrototypeOf' ) );
+ });
+ });
+
+ it( 'disregards top-level browser field by default', function () {
+ return rollup.rollup({
+ entry: 'samples/browser/main.js',
+ plugins: [
+ nodeResolve({
+ main: true,
+ browser: false
+ })
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports, 'node' );
+ });
+ });
+
+ it( 'allows use of the top-level browser field', function () {
+ return rollup.rollup({
+ entry: 'samples/browser/main.js',
+ plugins: [
+ nodeResolve({
+ main: true,
+ browser: true
+ })
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports, 'browser' );
+ });
+ });
+
+ it( 'disregards object browser field by default', function () {
+ return rollup.rollup({
+ entry: 'samples/browser-object/main.js',
+ plugins: [
+ nodeResolve({
+ main: true,
+ browser: false
+ })
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports.env, 'node' );
+ assert.equal( module.exports.dep, 'node-dep' );
+ assert.equal( module.exports.test, 42 );
+ });
+ });
+
+ it( 'allows use of the object browser field', function () {
+ return rollup.rollup({
+ entry: 'samples/browser-object/main.js',
+ plugins: [
+ nodeResolve({
+ main: true,
+ browser: true
+ })
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports.env, 'browser' );
+ assert.equal( module.exports.dep, 'browser-dep' );
+ assert.equal( module.exports.test, 43 );
+ });
+ });
+
+ it( 'supports `false` in browser field', function () {
+ return rollup.rollup({
+ entry: 'samples/browser-false/main.js',
+ plugins: [
+ nodeResolve({
+ main: true,
+ browser: true
+ })
+ ]
+ }).then( executeBundle );
+ });
+
+ it( 'preferBuiltins: true allows preferring a builtin to a local module of the same name', () => {
+ return rollup.rollup({
+ entry: 'samples/prefer-builtin/main.js',
+ plugins: [
+ nodeResolve({
+ preferBuiltins: true
+ })
+ ]
+ }).then( bundle => {
+ assert.deepEqual( bundle.imports.sort(), [ 'events' ] );
+ });
+ });
+
+ it( 'preferBuiltins: false allows resolving a local module with the same name as a builtin module', () => {
+ return rollup.rollup({
+ entry: 'samples/prefer-builtin/main.js',
+ plugins: [
+ nodeResolve({
+ preferBuiltins: false
+ })
+ ]
+ }).then( bundle => {
+ assert.deepEqual( bundle.imports.sort(), [] );
+ });
+ });
+
+ it( 'issues a warning when preferring a builtin module without having explicit configuration', () => {
+ let warning = null;
+ return rollup.rollup({
+ entry: 'samples/prefer-builtin/main.js',
+ plugins: [
+ nodeResolve({
+ onwarn ( message ) {
+ if ( ~message.indexOf( 'prefer' ) ) {
+ warning = message;
+ }
+ }
+ })
+ ]
+ }).then( () => {
+ const localPath = path.join(__dirname, 'node_modules/events/index.js');
+ assert.strictEqual(
+ warning,
+ `preferring built-in module 'events' over local alternative ` +
+ `at '${localPath}', pass 'preferBuiltins: false' to disable this behavior ` +
+ `or 'preferBuiltins: true' to disable this warning`
+ );
+ });
+ });
+
+ it( 'supports non-standard extensions', () => {
+ return rollup.rollup({
+ entry: 'samples/extensions/main.js',
+ plugins: [
+ nodeResolve({
+ extensions: [ '.js', '.wut' ]
+ })
+ ]
+ }).then( executeBundle );
+ });
+
+ it( 'ignores IDs with null character', () => {
+ return Promise.resolve( nodeResolve().resolveId( '\0someid', 'test.js' ) ).then( result => {
+ assert.equal( result, null );
+ });
+ });
+
+ it( 'finds a module with module field', () => {
+ return rollup.rollup({
+ entry: 'samples/module/main.js',
+ plugins: [
+ nodeResolve({ preferBuiltins: false })
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports, 'MODULE' );
+ });
+ });
+
+ it( 'prefers module field over jsnext:main and main', () => {
+ return rollup.rollup({
+ entry: 'samples/prefer-module/main.js',
+ plugins: [
+ nodeResolve({ jsnext: true, preferBuiltins: false })
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports, 'MODULE-ENTRY' );
+ });
+ });
+
+ it( 'resolves symlinked packages', () => {
+ createMissingDirectories();
+ linkDirectories();
+
+ return rollup.rollup({
+ entry: 'samples/symlinked/first/index.js',
+ plugins: [
+ nodeResolve()
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports.number1, module.exports.number2 );
+ }).then(() => {
+ unlinkDirectories();
+ }).catch(err => {
+ unlinkDirectories();
+ throw err;
+ });
+
+ function createMissingDirectories () {
+ createDirectory( './samples/symlinked/first/node_modules' );
+ createDirectory( './samples/symlinked/second/node_modules' );
+ createDirectory( './samples/symlinked/third/node_modules' );
+ }
+
+ function createDirectory ( pathToDir ) {
+ if ( !fs.existsSync( pathToDir ) ) {
+ fs.mkdirSync( pathToDir );
+ }
+ }
+
+ function linkDirectories () {
+ fs.symlinkSync('../../second', './samples/symlinked/first/node_modules/second', 'dir');
+ fs.symlinkSync('../../third', './samples/symlinked/first/node_modules/third', 'dir');
+ fs.symlinkSync('../../third', './samples/symlinked/second/node_modules/third', 'dir');
+ }
+
+ function unlinkDirectories () {
+ fs.unlinkSync('./samples/symlinked/first/node_modules/second');
+ fs.unlinkSync('./samples/symlinked/first/node_modules/third');
+ fs.unlinkSync('./samples/symlinked/second/node_modules/third');
+ }
+ });
+
+ it( 'prefers jsnext:main field over main', () => {
+ return rollup.rollup({
+ entry: 'samples/prefer-jsnext/main.js',
+ plugins: [
+ nodeResolve({ jsnext: true, module: false, preferBuiltins: false })
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports, 'JSNEXT-ENTRY' );
+ });
+ });
+
+ it( 'supports ./ in entry filename', () => {
+ return rollup.rollup({
+ entry: './samples/jsnext/main.js',
+ plugins: [
+ nodeResolve({ jsnext: true })
+ ]
+ }).then( executeBundle ).then( module => {
+ assert.equal( module.exports, '2H' );
+ });
+ });
+
+ it( 'throws error if local id is not resolved', () => {
+ const entry = path.join( 'samples', 'unresolved-local', 'main.js' );
+ return rollup.rollup({
+ entry,
+ plugins: [
+ nodeResolve()
+ ]
+ }).then( () => {
+ throw Error( 'test should fail' );
+ }, err => {
+ assert.equal( err.message, `Could not resolve './foo' from ${entry}` );
+ });
+ });
+
+ it( 'mark as external to module outside the jail', () => {
+ return rollup.rollup({
+ entry: 'samples/jail/main.js',
+ plugins: [ nodeResolve({
+ jail: `${__dirname}/samples/`
+ }) ]
+ }).then( (bundle) => {
+ assert.deepEqual(bundle.imports, [ 'string/uppercase.js' ]);
+ });
+ });
+
+ it( 'bundle module defined inside the jail', () => {
+ return rollup.rollup({
+ entry: 'samples/jail/main.js',
+ plugins: [ nodeResolve({
+ jail: `${__dirname}/`
+ }) ]
+ }).then( (bundle) => {
+ assert.deepEqual(bundle.imports, []);
+ });
+ });
+
+ it( 'allows custom options', () => {
+ return rollup.rollup({
+ entry: 'samples/custom-resolve-options/main.js',
+ plugins: [ nodeResolve({
+ customResolveOptions: {
+ moduleDirectory: 'js_modules'
+ }
+ }) ]
+ }).then( bundle => {
+ assert.equal(
+ bundle.modules[0].id,
+ path.resolve( __dirname, 'samples/custom-resolve-options/js_modules/foo.js' )
+ );
+ });
+ });
+
+ it( 'ignores deep-import non-modules', () => {
+ return rollup.rollup({
+ entry: 'samples/deep-import-non-module/main.js',
+ plugins: [ nodeResolve({
+ modulesOnly: true
+ }) ]
+ }).then( bundle => {
+ assert.deepEqual( bundle.imports, [ 'foo/deep' ] );
+ });
+ });
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-rollup-plugin-node-resolve.git
More information about the Pkg-javascript-commits
mailing list