diff --git a/README.md b/README.md index 51d307c..75878b0 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,9 @@ export default { input: 'main.js', output: { file: 'bundle.js', - format: 'iife' + format: 'iife', + name: 'MyModule' }, - name: 'MyModule', plugins: [ resolve({ // use "module" field for ES6 module if possible @@ -35,7 +35,7 @@ export default { 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 + // (needs to be converted from CommonJS to ES6) // – see https://github.com/rollup/rollup-plugin-commonjs main: true, // Default: true diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..b1d3faa --- /dev/null +++ b/index.d.ts @@ -0,0 +1,73 @@ +import { Plugin } from 'rollup'; +import { AsyncOpts } from 'resolve'; + +interface RollupNodeResolveOptions { + /** + * use "module" field for ES6 module if possible + * @default true + */ + module?: boolean; + /** + * 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; + /** + * 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, use this 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; + /** + * 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 | null; + /** + * If true, inspect resolved files to check that they are + * ES2015 modules + * @default false + */ + modulesOnly?: boolean; + /** + * 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?: RollupNodeResolveOptions): Plugin; diff --git a/package-lock.json b/package-lock.json index bcd3e0e..7b116d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -36,6 +36,14 @@ "integrity": "sha512-rx29MMkRdVmzunmiA4lzBYJNnXsW/PhG4kMBy2ATsYaDjGGR75dCFEVVROKpNwlVdcUX3xxlghKQOeDPBJobng==", "dev": true }, + "@types/resolve": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/@types/resolve/-/resolve-0.0.8.tgz", + "integrity": "sha512-auApPaJf3NPfe18hSoJkp8EbZzer2ISk7o8mCC3M9he/a04+gbMF97NkpD2S8riMGvm4BMRI59/SZQSaLTKpsQ==", + "requires": { + "@types/node": "*" + } + }, "acorn": { "version": "6.1.1", "resolved": "https://registry.npmjs.org/acorn/-/acorn-6.1.1.tgz", @@ -2702,6 +2710,12 @@ "prelude-ls": "~1.1.2" } }, + "typescript": { + "version": "3.4.1", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.1.tgz", + "integrity": "sha512-3NSMb2VzDQm8oBTLH6Nj55VVtUEpe/rgkIzMir0qVoLyjDZlnMBva0U6vDiV3IH+sl/Yu6oP5QwsAQtHPmDd2Q==", + "dev": true + }, "unicode-canonical-property-names-ecmascript": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz", diff --git a/package.json b/package.json index 4141b3d..3e64a9c 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "rollup-plugin-buble": "^0.19.6", "rollup-plugin-commonjs": "^9.3.4", "string-capitalize": "^1.0.1", + "typescript": "^3.4.1", "vlq": "^1.0.0" }, "main": "dist/rollup-plugin-node-resolve.cjs.js", @@ -20,16 +21,18 @@ "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": { + "@types/resolve": "0.0.8", "builtin-modules": "^3.0.0", "is-module": "^1.0.0", "resolve": "^1.10.0" diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..842f6de --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strict": true, + "noEmit": true, + "allowJs": true + }, + "files": [ + "index.d.ts", + "typings-test.js" + ] +} diff --git a/typings-test.js b/typings-test.js new file mode 100644 index 0000000..2175718 --- /dev/null +++ b/typings-test.js @@ -0,0 +1,30 @@ +// @ts-check +import resolve from '.'; + +/** @type {import("rollup").RollupOptions} */ +const config = { + input: 'main.js', + output: { + file: 'bundle.js', + format: 'iife', + name: 'MyModule', + }, + plugins: [ + resolve({ + module: true, + jsnext: true, + main: true, + browser: true, + extensions: [ '.mjs', '.js', '.jsx', '.json' ], + preferBuiltins: false, + jail: '/my/jail/path', + only: [ 'some_module', /^@some_scope\/.*$/ ], + modulesOnly: true, + customResolveOptions: { + moduleDirectory: 'js_modules' + } + }) + ] +}; + +export default config;