@rollup/plugin-node-resolve
Advanced tools
Comparing version 7.0.0 to 7.1.0
# @rollup/plugin-node-resolve ChangeLog | ||
## v7.1.0 | ||
_2020-02-01_ | ||
### Updates | ||
- refactor: clean codebase and fix external warnings (#155) | ||
## v7.0.0 | ||
@@ -4,0 +12,0 @@ |
@@ -1,96 +0,143 @@ | ||
import { dirname, resolve, normalize, sep, extname } from 'path'; | ||
import fs, { realpathSync } from 'fs'; | ||
import { dirname, resolve, extname, normalize, sep } from 'path'; | ||
import builtinList from 'builtin-modules'; | ||
import resolveId from 'resolve'; | ||
import isModule from 'is-module'; | ||
import fs, { realpathSync } from 'fs'; | ||
import { promisify } from 'util'; | ||
import { createFilter } from '@rollup/pluginutils'; | ||
import resolveModule from 'resolve'; | ||
function _slicedToArray(arr, i) { | ||
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); | ||
} | ||
function _arrayWithHoles(arr) { | ||
if (Array.isArray(arr)) return arr; | ||
} | ||
function _iterableToArrayLimit(arr, i) { | ||
if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
var info = gen[key](arg); | ||
var value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
Promise.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
try { | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new Promise(function (resolve, reject) { | ||
var gen = fn.apply(self, args); | ||
if (i && _arr.length === i) break; | ||
} | ||
} catch (err) { | ||
_d = true; | ||
_e = err; | ||
} finally { | ||
try { | ||
if (!_n && _i["return"] != null) _i["return"](); | ||
} finally { | ||
if (_d) throw _e; | ||
} | ||
} | ||
function _next(value) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
return _arr; | ||
} | ||
function _throw(err) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
function _nonIterableRest() { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance"); | ||
_next(undefined); | ||
}); | ||
}; | ||
} | ||
var peerDependencies = { | ||
rollup: "^1.20.0" | ||
const exists = promisify(fs.exists); | ||
const readFile = promisify(fs.readFile); | ||
const realpath = promisify(fs.realpath); | ||
const stat = promisify(fs.stat); | ||
const onError = error => { | ||
if (error.code === 'ENOENT') { | ||
return false; | ||
} | ||
throw error; | ||
}; | ||
const builtins = new Set(builtinList); | ||
const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js'; // It is important that .mjs occur before .js so that Rollup will interpret npm modules | ||
// which deploy both ESM .mjs and CommonJS .js files as ESM. | ||
const makeCache = fn => { | ||
const cache = new Map(); | ||
const DEFAULT_EXTS = ['.mjs', '.js', '.json', '.node']; | ||
const wrapped = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref = _asyncToGenerator(function* (param, done) { | ||
if (cache.has(param) === false) { | ||
cache.set(param, fn(param).catch(err => { | ||
cache.delete(param); | ||
throw err; | ||
})); | ||
} | ||
const existsAsync = file => new Promise(fulfil => fs.exists(file, fulfil)); | ||
try { | ||
const result = cache.get(param); | ||
const value = yield result; | ||
return done(null, value); | ||
} catch (error) { | ||
return done(error); | ||
} | ||
}); | ||
const readFileAsync = file => new Promise((fulfil, reject) => fs.readFile(file, (err, contents) => err ? reject(err) : fulfil(contents))); | ||
return function wrapped(_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
const realpathAsync = file => new Promise((fulfil, reject) => fs.realpath(file, (err, contents) => err ? reject(err) : fulfil(contents))); | ||
wrapped.clear = () => cache.clear(); | ||
const statAsync = file => new Promise((fulfil, reject) => fs.stat(file, (err, contents) => err ? reject(err) : fulfil(contents))); | ||
return wrapped; | ||
}; | ||
const cache = fn => { | ||
const cache = new Map(); | ||
const isDirCached = makeCache( | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref2 = _asyncToGenerator(function* (file) { | ||
try { | ||
const stats = yield stat(file); | ||
return stats.isDirectory(); | ||
} catch (error) { | ||
return onError(error); | ||
} | ||
}); | ||
const wrapped = (param, done) => { | ||
if (cache.has(param) === false) { | ||
cache.set(param, fn(param).catch(err => { | ||
cache.delete(param); | ||
throw err; | ||
})); | ||
return function (_x3) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}()); | ||
const isFileCached = makeCache( | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref3 = _asyncToGenerator(function* (file) { | ||
try { | ||
const stats = yield stat(file); | ||
return stats.isFile(); | ||
} catch (error) { | ||
return onError(error); | ||
} | ||
}); | ||
return cache.get(param).then(result => done(null, result), done); | ||
return function (_x4) { | ||
return _ref3.apply(this, arguments); | ||
}; | ||
}()); | ||
const readCachedFile = makeCache(readFile); | ||
wrapped.clear = () => cache.clear(); | ||
const resolveId = promisify(resolveModule); // returns the imported package name for bare module imports | ||
return wrapped; | ||
}; | ||
function getPackageName(id) { | ||
if (id.startsWith('.') || id.startsWith('/')) { | ||
return null; | ||
} | ||
const ignoreENOENT = err => { | ||
if (err.code === 'ENOENT') return false; | ||
throw err; | ||
}; | ||
const split = id.split('/'); // @my-scope/my-package/foo.js -> @my-scope/my-package | ||
// @my-scope/my-package -> @my-scope/my-package | ||
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)); | ||
if (split[0][0] === '@') { | ||
return `${split[0]}/${split[1]}`; | ||
} // my-package/foo.js -> my-package | ||
// my-package -> my-package | ||
return split[0]; | ||
} | ||
function getMainFields(options) { | ||
@@ -100,21 +147,5 @@ 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); | ||
} | ||
}); | ||
mainFields = ['main', 'module']; | ||
} | ||
@@ -127,3 +158,3 @@ | ||
if (!mainFields.length) { | ||
throw new Error(`Please ensure at least one 'mainFields' value is specified`); | ||
throw new Error('Please ensure at least one `mainFields` value is specified'); | ||
} | ||
@@ -133,167 +164,191 @@ | ||
} | ||
function getPackageInfo(options) { | ||
const cache = options.cache, | ||
extensions = options.extensions, | ||
pkg = options.pkg, | ||
mainFields = options.mainFields, | ||
preserveSymlinks = options.preserveSymlinks, | ||
useBrowserOverrides = options.useBrowserOverrides; | ||
let pkgPath = options.pkgPath; | ||
const alwaysNull = () => null; | ||
if (cache.has(pkgPath)) { | ||
return cache.get(pkgPath); | ||
} // browserify/resolve doesn't realpath paths returned in its packageFilter callback | ||
const resolveIdAsync = (file, opts) => new Promise((fulfil, reject) => resolveId(file, opts, (err, contents) => err ? reject(err) : fulfil(contents))); // Resolve module specifiers in order. Promise resolves to the first | ||
// module that resolves successfully, or the error that resulted from | ||
// the last attempted module resolution. | ||
if (!preserveSymlinks) { | ||
pkgPath = realpathSync(pkgPath); | ||
} | ||
function resolveImportSpecifiers(importSpecifierList, resolveOptions) { | ||
let p = Promise.resolve(); | ||
const pkgRoot = dirname(pkgPath); | ||
const packageInfo = { | ||
// copy as we are about to munge the `main` field of `pkg`. | ||
packageJson: Object.assign({}, pkg), | ||
// path to package.json file | ||
packageJsonPath: pkgPath, | ||
// directory containing the package.json | ||
root: pkgRoot, | ||
// which main field was used during resolution of this module (main, module, or browser) | ||
resolvedMainField: 'main', | ||
// whether the browser map was used to resolve the entry point to this module | ||
browserMappedMain: false, | ||
// the entry point of the module with respect to the selected main field and any | ||
// relevant browser mappings. | ||
resolvedEntryPoint: '' | ||
}; | ||
let overriddenMain = false; | ||
for (let i = 0; i < importSpecifierList.length; i++) { | ||
p = p.then(v => { | ||
// if we've already resolved to something, just return it. | ||
if (v) return v; | ||
return resolveIdAsync(importSpecifierList[i], resolveOptions); | ||
}); | ||
for (let i = 0; i < mainFields.length; i++) { | ||
const field = mainFields[i]; | ||
if (i < importSpecifierList.length - 1) { | ||
// swallow MODULE_NOT_FOUND errors from all but the last resolution | ||
p = p.catch(err => { | ||
if (err.code !== 'MODULE_NOT_FOUND') { | ||
throw err; | ||
} | ||
}); | ||
if (typeof pkg[field] === 'string') { | ||
pkg.main = pkg[field]; | ||
packageInfo.resolvedMainField = field; | ||
overriddenMain = true; | ||
break; | ||
} | ||
} | ||
return p; | ||
} // returns the imported package name for bare module imports | ||
const internalPackageInfo = { | ||
cachedPkg: pkg, | ||
hasModuleSideEffects: () => null, | ||
hasPackageEntry: overriddenMain !== false || mainFields.indexOf('main') !== -1, | ||
packageBrowserField: useBrowserOverrides && typeof pkg.browser === 'object' && Object.keys(pkg.browser).reduce((browser, key) => { | ||
let resolved = pkg.browser[key]; | ||
if (resolved && resolved[0] === '.') { | ||
resolved = resolve(pkgRoot, resolved); | ||
} | ||
/* eslint-disable no-param-reassign */ | ||
function getPackageName(id) { | ||
if (id.startsWith('.') || id.startsWith('/')) { | ||
return null; | ||
} | ||
const split = id.split('/'); // @my-scope/my-package/foo.js -> @my-scope/my-package | ||
// @my-scope/my-package -> @my-scope/my-package | ||
browser[key] = resolved; | ||
if (split[0][0] === '@') { | ||
return `${split[0]}/${split[1]}`; | ||
} // my-package/foo.js -> my-package | ||
// my-package -> my-package | ||
if (key[0] === '.') { | ||
const absoluteKey = resolve(pkgRoot, key); | ||
browser[absoluteKey] = resolved; | ||
if (!extname(key)) { | ||
extensions.reduce((subBrowser, ext) => { | ||
subBrowser[absoluteKey + ext] = subBrowser[key]; | ||
return subBrowser; | ||
}, browser); | ||
} | ||
} | ||
return split[0]; | ||
} | ||
return browser; | ||
}, {}), | ||
packageInfo | ||
}; | ||
const browserMap = internalPackageInfo.packageBrowserField; | ||
function nodeResolve(options = {}) { | ||
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 || {}; | ||
const rootDir = options.rootDir || process.cwd(); | ||
const jail = options.jail; | ||
const only = Array.isArray(options.only) ? options.only.map(o => o instanceof RegExp ? o : new RegExp(`^${String(o).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&')}$`)) : null; | ||
const browserMapCache = new Map(); | ||
let preserveSymlinks; | ||
if (options.skip) { | ||
throw new Error('options.skip is no longer supported — you should use the main Rollup `external` option instead'); | ||
if (useBrowserOverrides && typeof pkg.browser === 'object' && // eslint-disable-next-line no-prototype-builtins | ||
browserMap.hasOwnProperty(pkg.main)) { | ||
packageInfo.resolvedEntryPoint = browserMap[pkg.main]; | ||
packageInfo.browserMappedMain = true; | ||
} else { | ||
// index.node is technically a valid default entrypoint as well... | ||
packageInfo.resolvedEntryPoint = resolve(pkgRoot, pkg.main || 'index.js'); | ||
packageInfo.browserMappedMain = false; | ||
} | ||
const extensions = options.extensions || DEFAULT_EXTS; | ||
const packageInfoCache = new Map(); | ||
const idToPackageInfo = new Map(); | ||
const shouldDedupe = typeof dedupe === 'function' ? dedupe : importee => dedupe.includes(importee) || dedupe.includes(getPackageName(importee)); | ||
const packageSideEffects = pkg.sideEffects; | ||
function getCachedPackageInfo(pkg, pkgPath) { | ||
if (packageInfoCache.has(pkgPath)) { | ||
return packageInfoCache.get(pkgPath); | ||
} // browserify/resolve doesn't realpath paths returned in its packageFilter callback | ||
if (typeof packageSideEffects === 'boolean') { | ||
internalPackageInfo.hasModuleSideEffects = () => packageSideEffects; | ||
} else if (Array.isArray(packageSideEffects)) { | ||
internalPackageInfo.hasModuleSideEffects = createFilter(packageSideEffects, null, { | ||
resolve: pkgRoot | ||
}); | ||
} | ||
cache.set(pkgPath, internalPackageInfo); | ||
return internalPackageInfo; | ||
} | ||
function normalizeInput(input) { | ||
if (Array.isArray(input)) { | ||
return input; | ||
} else if (typeof input === 'object') { | ||
return Object.values(input); | ||
} // otherwise it's a string | ||
if (!preserveSymlinks) { | ||
pkgPath = realpathSync(pkgPath); | ||
} | ||
const pkgRoot = dirname(pkgPath); | ||
const packageInfo = { | ||
// copy as we are about to munge the `main` field of `pkg`. | ||
packageJson: Object.assign({}, pkg), | ||
// path to package.json file | ||
packageJsonPath: pkgPath, | ||
// directory containing the package.json | ||
root: pkgRoot, | ||
// which main field was used during resolution of this module (main, module, or browser) | ||
resolvedMainField: 'main', | ||
// whether the browser map was used to resolve the entry point to this module | ||
browserMappedMain: false, | ||
// the entry point of the module with respect to the selected main field and any | ||
// relevant browser mappings. | ||
resolvedEntryPoint: '' | ||
}; | ||
let overriddenMain = false; | ||
return input; | ||
} // Resolve module specifiers in order. Promise resolves to the first module that resolves | ||
// successfully, or the error that resulted from the last attempted module resolution. | ||
for (let i = 0; i < mainFields.length; i++) { | ||
const field = mainFields[i]; | ||
function resolveImportSpecifiers(importSpecifierList, resolveOptions) { | ||
let promise = Promise.resolve(); | ||
if (typeof pkg[field] === 'string') { | ||
pkg.main = pkg[field]; | ||
packageInfo.resolvedMainField = field; | ||
overriddenMain = true; | ||
break; | ||
for (let i = 0; i < importSpecifierList.length; i++) { | ||
promise = promise.then(value => { | ||
// if we've already resolved to something, just return it. | ||
if (value) { | ||
return value; | ||
} | ||
} | ||
const internalPackageInfo = { | ||
cachedPkg: pkg, | ||
hasModuleSideEffects: alwaysNull, | ||
hasPackageEntry: overriddenMain !== false || mainFields.indexOf('main') !== -1, | ||
packageBrowserField: useBrowserOverrides && typeof pkg.browser === 'object' && Object.keys(pkg.browser).reduce((browser, key) => { | ||
let resolved = pkg.browser[key]; | ||
return resolveId(importSpecifierList[i], resolveOptions); | ||
}); | ||
if (resolved && resolved[0] === '.') { | ||
resolved = resolve(pkgRoot, resolved); | ||
if (i < importSpecifierList.length - 1) { | ||
// swallow MODULE_NOT_FOUND errors from all but the last resolution | ||
promise = promise.catch(error => { | ||
if (error.code !== 'MODULE_NOT_FOUND') { | ||
throw error; | ||
} | ||
}); | ||
} | ||
} | ||
browser[key] = resolved; | ||
return promise; | ||
} | ||
if (key[0] === '.') { | ||
const absoluteKey = resolve(pkgRoot, key); | ||
browser[absoluteKey] = resolved; | ||
const builtins = new Set(builtinList); | ||
const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js'; | ||
if (!extname(key)) { | ||
extensions.reduce((browser, ext) => { | ||
browser[absoluteKey + ext] = browser[key]; | ||
return browser; | ||
}, browser); | ||
} | ||
} | ||
const nullFn = () => null; | ||
return browser; | ||
}, {}), | ||
packageInfo | ||
}; | ||
const browserMap = internalPackageInfo.packageBrowserField; | ||
const defaults = { | ||
customResolveOptions: {}, | ||
dedupe: [], | ||
// It's important that .mjs is listed before .js so that Rollup will interpret npm modules | ||
// which deploy both ESM .mjs and CommonJS .js files as ESM. | ||
extensions: ['.mjs', '.js', '.json', '.node'], | ||
resolveOnly: [] | ||
}; | ||
function nodeResolve(opts = {}) { | ||
const options = Object.assign({}, defaults, opts); | ||
const customResolveOptions = options.customResolveOptions, | ||
extensions = options.extensions, | ||
jail = options.jail; | ||
const warnings = []; | ||
const packageInfoCache = new Map(); | ||
const idToPackageInfo = new Map(); | ||
const mainFields = getMainFields(options); | ||
const useBrowserOverrides = mainFields.indexOf('browser') !== -1; | ||
const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false; | ||
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true; | ||
const rootDir = options.rootDir || process.cwd(); | ||
let dedupe = options.dedupe; | ||
let rollupOptions; | ||
if (useBrowserOverrides && typeof pkg.browser === 'object' && // eslint-disable-next-line no-prototype-builtins | ||
browserMap.hasOwnProperty(pkg.main)) { | ||
packageInfo.resolvedEntryPoint = browserMap[pkg.main]; | ||
packageInfo.browserMappedMain = true; | ||
} else { | ||
// index.node is technically a valid default entrypoint as well... | ||
packageInfo.resolvedEntryPoint = resolve(pkgRoot, pkg.main || 'index.js'); | ||
packageInfo.browserMappedMain = false; | ||
} | ||
if (options.only) { | ||
warnings.push('node-resolve: The `only` options is deprecated, please use `resolveOnly`'); | ||
options.resolveOnly = options.only; | ||
} | ||
const packageSideEffects = pkg.sideEffects; | ||
if (typeof dedupe !== 'function') { | ||
dedupe = importee => options.dedupe.includes(importee) || options.dedupe.includes(getPackageName(importee)); | ||
} | ||
if (typeof packageSideEffects === 'boolean') { | ||
internalPackageInfo.hasModuleSideEffects = () => packageSideEffects; | ||
} else if (Array.isArray(packageSideEffects)) { | ||
internalPackageInfo.hasModuleSideEffects = createFilter(packageSideEffects, null, { | ||
resolve: pkgRoot | ||
}); | ||
const resolveOnly = options.resolveOnly.map(pattern => { | ||
if (pattern instanceof RegExp) { | ||
return pattern; | ||
} | ||
packageInfoCache.set(pkgPath, internalPackageInfo); | ||
return internalPackageInfo; | ||
} | ||
const normalized = pattern.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&'); | ||
return new RegExp(`^${normalized}$`); | ||
}); | ||
const browserMapCache = new Map(); | ||
let preserveSymlinks; | ||
return { | ||
@@ -303,23 +358,32 @@ name: 'node-resolve', | ||
buildStart(options) { | ||
preserveSymlinks = options.preserveSymlinks; | ||
rollupOptions = options; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
const _this$meta$rollupVers = this.meta.rollupVersion.split('.').map(Number), | ||
_this$meta$rollupVers2 = _slicedToArray(_this$meta$rollupVers, 2), | ||
major = _this$meta$rollupVers2[0], | ||
minor = _this$meta$rollupVers2[1]; | ||
try { | ||
for (var _iterator = warnings[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
const warning = _step.value; | ||
this.warn(warning); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
const minVersion = peerDependencies.rollup.slice(2); | ||
const _minVersion$split$map = minVersion.split('.').map(Number), | ||
_minVersion$split$map2 = _slicedToArray(_minVersion$split$map, 2), | ||
minMajor = _minVersion$split$map2[0], | ||
minMinor = _minVersion$split$map2[1]; | ||
if (major < minMajor || major === minMajor && minor < minMinor) { | ||
this.error(`Insufficient Rollup version: "rollup-plugin-node-resolve" requires at least rollup@${minVersion} but found rollup@${this.meta.rollupVersion}.`); | ||
} | ||
preserveSymlinks = options.preserveSymlinks; | ||
}, | ||
generateBundle() { | ||
readFileCached.clear(); | ||
readCachedFile.clear(); | ||
isFileCached.clear(); | ||
@@ -330,141 +394,174 @@ isDirCached.clear(); | ||
resolveId(importee, importer) { | ||
if (importee === ES6_BROWSER_EMPTY) { | ||
return importee; | ||
} // ignore IDs with null character, these belong to other plugins | ||
var _this = this; | ||
return _asyncToGenerator(function* () { | ||
if (importee === ES6_BROWSER_EMPTY) { | ||
return importee; | ||
} // ignore IDs with null character, these belong to other plugins | ||
if (/\0/.test(importee)) return null; | ||
const basedir = !importer || shouldDedupe(importee) ? rootDir : dirname(importer); // https://github.com/defunctzombie/package-browser-field-spec | ||
const browser = browserMapCache.get(importer); | ||
if (/\0/.test(importee)) return null; | ||
const basedir = !importer || dedupe(importee) ? rootDir : dirname(importer); // https://github.com/defunctzombie/package-browser-field-spec | ||
if (useBrowserOverrides && browser) { | ||
const resolvedImportee = resolve(basedir, importee); | ||
const browser = browserMapCache.get(importer); | ||
if (browser[importee] === false || browser[resolvedImportee] === false) { | ||
return ES6_BROWSER_EMPTY; | ||
if (useBrowserOverrides && browser) { | ||
const resolvedImportee = resolve(basedir, importee); | ||
if (browser[importee] === false || browser[resolvedImportee] === false) { | ||
return ES6_BROWSER_EMPTY; | ||
} | ||
const browserImportee = browser[importee] || browser[resolvedImportee] || browser[`${resolvedImportee}.js`] || browser[`${resolvedImportee}.json`]; | ||
if (browserImportee) { | ||
importee = browserImportee; | ||
} | ||
} | ||
const browserImportee = browser[importee] || browser[resolvedImportee] || browser[`${resolvedImportee}.js`] || browser[`${resolvedImportee}.json`]; | ||
const parts = importee.split(/[/\\]/); | ||
let id = parts.shift(); | ||
if (browserImportee) { | ||
importee = browserImportee; | ||
if (id[0] === '@' && parts.length > 0) { | ||
// scoped packages | ||
id += `/${parts.shift()}`; | ||
} else if (id[0] === '.') { | ||
// an import relative to the parent dir of the importer | ||
id = resolve(basedir, importee); | ||
} | ||
} | ||
const parts = importee.split(/[/\\]/); | ||
let id = parts.shift(); | ||
const input = normalizeInput(rollupOptions.input); | ||
if (id[0] === '@' && parts.length > 0) { | ||
// scoped packages | ||
id += `/${parts.shift()}`; | ||
} else if (id[0] === '.') { | ||
// an import relative to the parent dir of the importer | ||
id = resolve(basedir, importee); | ||
} | ||
if (resolveOnly.length && !resolveOnly.some(pattern => pattern.test(id))) { | ||
if (input.includes(id)) { | ||
return null; | ||
} | ||
if (only && !only.some(pattern => pattern.test(id))) return null; | ||
let hasModuleSideEffects = alwaysNull; | ||
let hasPackageEntry = true; | ||
let packageBrowserField = false; | ||
let packageInfo; | ||
const resolveOptions = { | ||
basedir, | ||
return false; | ||
} | ||
packageFilter(pkg, pkgPath) { | ||
let cachedPkg; | ||
let hasModuleSideEffects = nullFn; | ||
let hasPackageEntry = true; | ||
let packageBrowserField = false; | ||
let packageInfo; | ||
var _getCachedPackageInfo = getCachedPackageInfo(pkg, pkgPath); | ||
const filter = (pkg, pkgPath) => { | ||
const info = getPackageInfo({ | ||
cache: packageInfoCache, | ||
extensions, | ||
pkg, | ||
pkgPath, | ||
mainFields, | ||
preserveSymlinks, | ||
useBrowserOverrides | ||
}); | ||
packageInfo = info.packageInfo; | ||
hasModuleSideEffects = info.hasModuleSideEffects; | ||
hasPackageEntry = info.hasPackageEntry; | ||
packageBrowserField = info.packageBrowserField; | ||
return info.cachedPkg; | ||
}; | ||
packageInfo = _getCachedPackageInfo.packageInfo; | ||
cachedPkg = _getCachedPackageInfo.cachedPkg; | ||
hasModuleSideEffects = _getCachedPackageInfo.hasModuleSideEffects; | ||
hasPackageEntry = _getCachedPackageInfo.hasPackageEntry; | ||
packageBrowserField = _getCachedPackageInfo.packageBrowserField; | ||
return cachedPkg; | ||
}, | ||
let resolveOptions = { | ||
basedir, | ||
packageFilter: filter, | ||
readFile: readCachedFile, | ||
isFile: isFileCached, | ||
isDirectory: isDirCached, | ||
extensions | ||
}; | ||
readFile: readFileCached, | ||
isFile: isFileCached, | ||
isDirectory: isDirCached, | ||
extensions | ||
}; | ||
if (preserveSymlinks !== undefined) { | ||
resolveOptions.preserveSymlinks = preserveSymlinks; | ||
} | ||
if (preserveSymlinks !== undefined) { | ||
resolveOptions.preserveSymlinks = preserveSymlinks; | ||
} | ||
const importSpecifierList = []; | ||
const importSpecifierList = []; | ||
if (importer === undefined && !importee[0].match(/^\.?\.?\//)) { | ||
// For module graph roots (i.e. when importer is undefined), we | ||
// need to handle 'path fragments` like `foo/bar` that are commonly | ||
// found in rollup config files. If importee doesn't look like a | ||
// relative or absolute path, we make it relative and attempt to | ||
// resolve it. If we don't find anything, we try resolving it as we | ||
// got it. | ||
importSpecifierList.push(`./${importee}`); | ||
} | ||
if (importer === undefined && !importee[0].match(/^\.?\.?\//)) { | ||
// For module graph roots (i.e. when importer is undefined), we | ||
// need to handle 'path fragments` like `foo/bar` that are commonly | ||
// found in rollup config files. If importee doesn't look like a | ||
// relative or absolute path, we make it relative and attempt to | ||
// resolve it. If we don't find anything, we try resolving it as we | ||
// got it. | ||
importSpecifierList.push(`./${importee}`); | ||
} | ||
const importeeIsBuiltin = builtins.has(importee); | ||
const importeeIsBuiltin = builtins.has(importee); | ||
if (importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet)) { | ||
// The `resolve` library will not resolve packages with the same | ||
// name as a node built-in module. If we're resolving something | ||
// that's a builtin, and we don't prefer to find built-ins, we | ||
// first try to look up a local module with that name. If we don't | ||
// find anything, we resolve the builtin which just returns back | ||
// the built-in's name. | ||
importSpecifierList.push(`${importee}/`); | ||
} | ||
if (importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet)) { | ||
// The `resolve` library will not resolve packages with the same | ||
// name as a node built-in module. If we're resolving something | ||
// that's a builtin, and we don't prefer to find built-ins, we | ||
// first try to look up a local module with that name. If we don't | ||
// find anything, we resolve the builtin which just returns back | ||
// the built-in's name. | ||
importSpecifierList.push(`${importee}/`); | ||
} | ||
importSpecifierList.push(importee); | ||
resolveOptions = Object.assign(resolveOptions, customResolveOptions); | ||
importSpecifierList.push(importee); | ||
return resolveImportSpecifiers(importSpecifierList, Object.assign(resolveOptions, customResolveOptions)).then(resolved => { | ||
if (resolved && packageBrowserField) { | ||
if (Object.prototype.hasOwnProperty.call(packageBrowserField, resolved)) { | ||
if (!packageBrowserField[resolved]) { | ||
browserMapCache.set(resolved, packageBrowserField); | ||
return ES6_BROWSER_EMPTY; | ||
try { | ||
let resolved = yield resolveImportSpecifiers(importSpecifierList, resolveOptions); | ||
if (resolved && packageBrowserField) { | ||
if (Object.prototype.hasOwnProperty.call(packageBrowserField, resolved)) { | ||
if (!packageBrowserField[resolved]) { | ||
browserMapCache.set(resolved, packageBrowserField); | ||
return ES6_BROWSER_EMPTY; | ||
} | ||
resolved = packageBrowserField[resolved]; | ||
} | ||
resolved = packageBrowserField[resolved]; | ||
browserMapCache.set(resolved, packageBrowserField); | ||
} | ||
browserMapCache.set(resolved, packageBrowserField); | ||
} | ||
if (hasPackageEntry && !preserveSymlinks && resolved) { | ||
const fileExists = yield exists(resolved); | ||
if (hasPackageEntry && !preserveSymlinks && resolved) { | ||
return existsAsync(resolved).then(exists => exists ? realpathAsync(resolved) : resolved); | ||
} | ||
if (fileExists) { | ||
resolved = yield realpath(resolved); | ||
} | ||
} | ||
return resolved; | ||
}).then(resolved => { | ||
idToPackageInfo.set(resolved, packageInfo); | ||
idToPackageInfo.set(resolved, packageInfo); | ||
if (hasPackageEntry) { | ||
if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) { | ||
return null; | ||
} else if (importeeIsBuiltin && preferBuiltins) { | ||
if (!isPreferBuiltinsSet) { | ||
this.warn(`preferring built-in module '${importee}' over local alternative ` + `at '${resolved}', pass 'preferBuiltins: false' to disable this ` + `behavior or 'preferBuiltins: true' to disable this warning`); | ||
if (hasPackageEntry) { | ||
if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) { | ||
return null; | ||
} else if (importeeIsBuiltin && preferBuiltins) { | ||
if (!isPreferBuiltinsSet) { | ||
_this.warn(`preferring built-in module '${importee}' over local alternative at '${resolved}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`); | ||
} | ||
return null; | ||
} else if (jail && resolved.indexOf(normalize(jail.trim(sep))) !== 0) { | ||
return null; | ||
} | ||
} | ||
if (resolved && options.modulesOnly) { | ||
const code = yield readFile(resolved, 'utf-8'); | ||
if (isModule(code)) { | ||
return { | ||
id: resolved, | ||
moduleSideEffects: hasModuleSideEffects(resolved) | ||
}; | ||
} | ||
return null; | ||
} else if (jail && resolved.indexOf(normalize(jail.trim(sep))) !== 0) { | ||
return null; | ||
} | ||
} | ||
if (resolved && options.modulesOnly) { | ||
return readFileAsync(resolved).then(code => isModule(code) ? { | ||
const result = { | ||
id: resolved, | ||
moduleSideEffects: hasModuleSideEffects(resolved) | ||
} : null); | ||
}; | ||
return result; | ||
} catch (error) { | ||
return null; | ||
} | ||
return { | ||
id: resolved, | ||
moduleSideEffects: hasModuleSideEffects(resolved) | ||
}; | ||
}).catch(() => null); | ||
})(); | ||
}, | ||
@@ -471,0 +568,0 @@ |
@@ -6,97 +6,144 @@ 'use strict'; | ||
var path = require('path'); | ||
var builtinList = _interopDefault(require('builtin-modules')); | ||
var isModule = _interopDefault(require('is-module')); | ||
var fs = require('fs'); | ||
var fs__default = _interopDefault(fs); | ||
var builtinList = _interopDefault(require('builtin-modules')); | ||
var resolveId = _interopDefault(require('resolve')); | ||
var isModule = _interopDefault(require('is-module')); | ||
var util = require('util'); | ||
var pluginutils = require('@rollup/pluginutils'); | ||
var resolveModule = _interopDefault(require('resolve')); | ||
function _slicedToArray(arr, i) { | ||
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); | ||
} | ||
function _arrayWithHoles(arr) { | ||
if (Array.isArray(arr)) return arr; | ||
} | ||
function _iterableToArrayLimit(arr, i) { | ||
if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
var info = gen[key](arg); | ||
var value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
Promise.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
try { | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new Promise(function (resolve, reject) { | ||
var gen = fn.apply(self, args); | ||
if (i && _arr.length === i) break; | ||
} | ||
} catch (err) { | ||
_d = true; | ||
_e = err; | ||
} finally { | ||
try { | ||
if (!_n && _i["return"] != null) _i["return"](); | ||
} finally { | ||
if (_d) throw _e; | ||
} | ||
} | ||
function _next(value) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
return _arr; | ||
} | ||
function _throw(err) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
function _nonIterableRest() { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance"); | ||
_next(undefined); | ||
}); | ||
}; | ||
} | ||
var peerDependencies = { | ||
rollup: "^1.20.0" | ||
const exists = util.promisify(fs__default.exists); | ||
const readFile = util.promisify(fs__default.readFile); | ||
const realpath = util.promisify(fs__default.realpath); | ||
const stat = util.promisify(fs__default.stat); | ||
const onError = error => { | ||
if (error.code === 'ENOENT') { | ||
return false; | ||
} | ||
throw error; | ||
}; | ||
const builtins = new Set(builtinList); | ||
const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js'; // It is important that .mjs occur before .js so that Rollup will interpret npm modules | ||
// which deploy both ESM .mjs and CommonJS .js files as ESM. | ||
const makeCache = fn => { | ||
const cache = new Map(); | ||
const DEFAULT_EXTS = ['.mjs', '.js', '.json', '.node']; | ||
const wrapped = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref = _asyncToGenerator(function* (param, done) { | ||
if (cache.has(param) === false) { | ||
cache.set(param, fn(param).catch(err => { | ||
cache.delete(param); | ||
throw err; | ||
})); | ||
} | ||
const existsAsync = file => new Promise(fulfil => fs__default.exists(file, fulfil)); | ||
try { | ||
const result = cache.get(param); | ||
const value = yield result; | ||
return done(null, value); | ||
} catch (error) { | ||
return done(error); | ||
} | ||
}); | ||
const readFileAsync = file => new Promise((fulfil, reject) => fs__default.readFile(file, (err, contents) => err ? reject(err) : fulfil(contents))); | ||
return function wrapped(_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
const realpathAsync = file => new Promise((fulfil, reject) => fs__default.realpath(file, (err, contents) => err ? reject(err) : fulfil(contents))); | ||
wrapped.clear = () => cache.clear(); | ||
const statAsync = file => new Promise((fulfil, reject) => fs__default.stat(file, (err, contents) => err ? reject(err) : fulfil(contents))); | ||
return wrapped; | ||
}; | ||
const cache = fn => { | ||
const cache = new Map(); | ||
const isDirCached = makeCache( | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref2 = _asyncToGenerator(function* (file) { | ||
try { | ||
const stats = yield stat(file); | ||
return stats.isDirectory(); | ||
} catch (error) { | ||
return onError(error); | ||
} | ||
}); | ||
const wrapped = (param, done) => { | ||
if (cache.has(param) === false) { | ||
cache.set(param, fn(param).catch(err => { | ||
cache.delete(param); | ||
throw err; | ||
})); | ||
return function (_x3) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}()); | ||
const isFileCached = makeCache( | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref3 = _asyncToGenerator(function* (file) { | ||
try { | ||
const stats = yield stat(file); | ||
return stats.isFile(); | ||
} catch (error) { | ||
return onError(error); | ||
} | ||
}); | ||
return cache.get(param).then(result => done(null, result), done); | ||
return function (_x4) { | ||
return _ref3.apply(this, arguments); | ||
}; | ||
}()); | ||
const readCachedFile = makeCache(readFile); | ||
wrapped.clear = () => cache.clear(); | ||
const resolveId = util.promisify(resolveModule); // returns the imported package name for bare module imports | ||
return wrapped; | ||
}; | ||
function getPackageName(id) { | ||
if (id.startsWith('.') || id.startsWith('/')) { | ||
return null; | ||
} | ||
const ignoreENOENT = err => { | ||
if (err.code === 'ENOENT') return false; | ||
throw err; | ||
}; | ||
const split = id.split('/'); // @my-scope/my-package/foo.js -> @my-scope/my-package | ||
// @my-scope/my-package -> @my-scope/my-package | ||
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)); | ||
if (split[0][0] === '@') { | ||
return `${split[0]}/${split[1]}`; | ||
} // my-package/foo.js -> my-package | ||
// my-package -> my-package | ||
return split[0]; | ||
} | ||
function getMainFields(options) { | ||
@@ -106,21 +153,5 @@ 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); | ||
} | ||
}); | ||
mainFields = ['main', 'module']; | ||
} | ||
@@ -133,3 +164,3 @@ | ||
if (!mainFields.length) { | ||
throw new Error(`Please ensure at least one 'mainFields' value is specified`); | ||
throw new Error('Please ensure at least one `mainFields` value is specified'); | ||
} | ||
@@ -139,167 +170,191 @@ | ||
} | ||
function getPackageInfo(options) { | ||
const cache = options.cache, | ||
extensions = options.extensions, | ||
pkg = options.pkg, | ||
mainFields = options.mainFields, | ||
preserveSymlinks = options.preserveSymlinks, | ||
useBrowserOverrides = options.useBrowserOverrides; | ||
let pkgPath = options.pkgPath; | ||
const alwaysNull = () => null; | ||
if (cache.has(pkgPath)) { | ||
return cache.get(pkgPath); | ||
} // browserify/resolve doesn't realpath paths returned in its packageFilter callback | ||
const resolveIdAsync = (file, opts) => new Promise((fulfil, reject) => resolveId(file, opts, (err, contents) => err ? reject(err) : fulfil(contents))); // Resolve module specifiers in order. Promise resolves to the first | ||
// module that resolves successfully, or the error that resulted from | ||
// the last attempted module resolution. | ||
if (!preserveSymlinks) { | ||
pkgPath = fs.realpathSync(pkgPath); | ||
} | ||
function resolveImportSpecifiers(importSpecifierList, resolveOptions) { | ||
let p = Promise.resolve(); | ||
const pkgRoot = path.dirname(pkgPath); | ||
const packageInfo = { | ||
// copy as we are about to munge the `main` field of `pkg`. | ||
packageJson: Object.assign({}, pkg), | ||
// path to package.json file | ||
packageJsonPath: pkgPath, | ||
// directory containing the package.json | ||
root: pkgRoot, | ||
// which main field was used during resolution of this module (main, module, or browser) | ||
resolvedMainField: 'main', | ||
// whether the browser map was used to resolve the entry point to this module | ||
browserMappedMain: false, | ||
// the entry point of the module with respect to the selected main field and any | ||
// relevant browser mappings. | ||
resolvedEntryPoint: '' | ||
}; | ||
let overriddenMain = false; | ||
for (let i = 0; i < importSpecifierList.length; i++) { | ||
p = p.then(v => { | ||
// if we've already resolved to something, just return it. | ||
if (v) return v; | ||
return resolveIdAsync(importSpecifierList[i], resolveOptions); | ||
}); | ||
for (let i = 0; i < mainFields.length; i++) { | ||
const field = mainFields[i]; | ||
if (i < importSpecifierList.length - 1) { | ||
// swallow MODULE_NOT_FOUND errors from all but the last resolution | ||
p = p.catch(err => { | ||
if (err.code !== 'MODULE_NOT_FOUND') { | ||
throw err; | ||
} | ||
}); | ||
if (typeof pkg[field] === 'string') { | ||
pkg.main = pkg[field]; | ||
packageInfo.resolvedMainField = field; | ||
overriddenMain = true; | ||
break; | ||
} | ||
} | ||
return p; | ||
} // returns the imported package name for bare module imports | ||
const internalPackageInfo = { | ||
cachedPkg: pkg, | ||
hasModuleSideEffects: () => null, | ||
hasPackageEntry: overriddenMain !== false || mainFields.indexOf('main') !== -1, | ||
packageBrowserField: useBrowserOverrides && typeof pkg.browser === 'object' && Object.keys(pkg.browser).reduce((browser, key) => { | ||
let resolved = pkg.browser[key]; | ||
if (resolved && resolved[0] === '.') { | ||
resolved = path.resolve(pkgRoot, resolved); | ||
} | ||
/* eslint-disable no-param-reassign */ | ||
function getPackageName(id) { | ||
if (id.startsWith('.') || id.startsWith('/')) { | ||
return null; | ||
} | ||
const split = id.split('/'); // @my-scope/my-package/foo.js -> @my-scope/my-package | ||
// @my-scope/my-package -> @my-scope/my-package | ||
browser[key] = resolved; | ||
if (split[0][0] === '@') { | ||
return `${split[0]}/${split[1]}`; | ||
} // my-package/foo.js -> my-package | ||
// my-package -> my-package | ||
if (key[0] === '.') { | ||
const absoluteKey = path.resolve(pkgRoot, key); | ||
browser[absoluteKey] = resolved; | ||
if (!path.extname(key)) { | ||
extensions.reduce((subBrowser, ext) => { | ||
subBrowser[absoluteKey + ext] = subBrowser[key]; | ||
return subBrowser; | ||
}, browser); | ||
} | ||
} | ||
return split[0]; | ||
} | ||
return browser; | ||
}, {}), | ||
packageInfo | ||
}; | ||
const browserMap = internalPackageInfo.packageBrowserField; | ||
function nodeResolve(options = {}) { | ||
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 || {}; | ||
const rootDir = options.rootDir || process.cwd(); | ||
const jail = options.jail; | ||
const only = Array.isArray(options.only) ? options.only.map(o => o instanceof RegExp ? o : new RegExp(`^${String(o).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&')}$`)) : null; | ||
const browserMapCache = new Map(); | ||
let preserveSymlinks; | ||
if (options.skip) { | ||
throw new Error('options.skip is no longer supported — you should use the main Rollup `external` option instead'); | ||
if (useBrowserOverrides && typeof pkg.browser === 'object' && // eslint-disable-next-line no-prototype-builtins | ||
browserMap.hasOwnProperty(pkg.main)) { | ||
packageInfo.resolvedEntryPoint = browserMap[pkg.main]; | ||
packageInfo.browserMappedMain = true; | ||
} else { | ||
// index.node is technically a valid default entrypoint as well... | ||
packageInfo.resolvedEntryPoint = path.resolve(pkgRoot, pkg.main || 'index.js'); | ||
packageInfo.browserMappedMain = false; | ||
} | ||
const extensions = options.extensions || DEFAULT_EXTS; | ||
const packageInfoCache = new Map(); | ||
const idToPackageInfo = new Map(); | ||
const shouldDedupe = typeof dedupe === 'function' ? dedupe : importee => dedupe.includes(importee) || dedupe.includes(getPackageName(importee)); | ||
const packageSideEffects = pkg.sideEffects; | ||
function getCachedPackageInfo(pkg, pkgPath) { | ||
if (packageInfoCache.has(pkgPath)) { | ||
return packageInfoCache.get(pkgPath); | ||
} // browserify/resolve doesn't realpath paths returned in its packageFilter callback | ||
if (typeof packageSideEffects === 'boolean') { | ||
internalPackageInfo.hasModuleSideEffects = () => packageSideEffects; | ||
} else if (Array.isArray(packageSideEffects)) { | ||
internalPackageInfo.hasModuleSideEffects = pluginutils.createFilter(packageSideEffects, null, { | ||
resolve: pkgRoot | ||
}); | ||
} | ||
cache.set(pkgPath, internalPackageInfo); | ||
return internalPackageInfo; | ||
} | ||
function normalizeInput(input) { | ||
if (Array.isArray(input)) { | ||
return input; | ||
} else if (typeof input === 'object') { | ||
return Object.values(input); | ||
} // otherwise it's a string | ||
if (!preserveSymlinks) { | ||
pkgPath = fs.realpathSync(pkgPath); | ||
} | ||
const pkgRoot = path.dirname(pkgPath); | ||
const packageInfo = { | ||
// copy as we are about to munge the `main` field of `pkg`. | ||
packageJson: Object.assign({}, pkg), | ||
// path to package.json file | ||
packageJsonPath: pkgPath, | ||
// directory containing the package.json | ||
root: pkgRoot, | ||
// which main field was used during resolution of this module (main, module, or browser) | ||
resolvedMainField: 'main', | ||
// whether the browser map was used to resolve the entry point to this module | ||
browserMappedMain: false, | ||
// the entry point of the module with respect to the selected main field and any | ||
// relevant browser mappings. | ||
resolvedEntryPoint: '' | ||
}; | ||
let overriddenMain = false; | ||
return input; | ||
} // Resolve module specifiers in order. Promise resolves to the first module that resolves | ||
// successfully, or the error that resulted from the last attempted module resolution. | ||
for (let i = 0; i < mainFields.length; i++) { | ||
const field = mainFields[i]; | ||
function resolveImportSpecifiers(importSpecifierList, resolveOptions) { | ||
let promise = Promise.resolve(); | ||
if (typeof pkg[field] === 'string') { | ||
pkg.main = pkg[field]; | ||
packageInfo.resolvedMainField = field; | ||
overriddenMain = true; | ||
break; | ||
for (let i = 0; i < importSpecifierList.length; i++) { | ||
promise = promise.then(value => { | ||
// if we've already resolved to something, just return it. | ||
if (value) { | ||
return value; | ||
} | ||
} | ||
const internalPackageInfo = { | ||
cachedPkg: pkg, | ||
hasModuleSideEffects: alwaysNull, | ||
hasPackageEntry: overriddenMain !== false || mainFields.indexOf('main') !== -1, | ||
packageBrowserField: useBrowserOverrides && typeof pkg.browser === 'object' && Object.keys(pkg.browser).reduce((browser, key) => { | ||
let resolved = pkg.browser[key]; | ||
return resolveId(importSpecifierList[i], resolveOptions); | ||
}); | ||
if (resolved && resolved[0] === '.') { | ||
resolved = path.resolve(pkgRoot, resolved); | ||
if (i < importSpecifierList.length - 1) { | ||
// swallow MODULE_NOT_FOUND errors from all but the last resolution | ||
promise = promise.catch(error => { | ||
if (error.code !== 'MODULE_NOT_FOUND') { | ||
throw error; | ||
} | ||
}); | ||
} | ||
} | ||
browser[key] = resolved; | ||
return promise; | ||
} | ||
if (key[0] === '.') { | ||
const absoluteKey = path.resolve(pkgRoot, key); | ||
browser[absoluteKey] = resolved; | ||
const builtins = new Set(builtinList); | ||
const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js'; | ||
if (!path.extname(key)) { | ||
extensions.reduce((browser, ext) => { | ||
browser[absoluteKey + ext] = browser[key]; | ||
return browser; | ||
}, browser); | ||
} | ||
} | ||
const nullFn = () => null; | ||
return browser; | ||
}, {}), | ||
packageInfo | ||
}; | ||
const browserMap = internalPackageInfo.packageBrowserField; | ||
const defaults = { | ||
customResolveOptions: {}, | ||
dedupe: [], | ||
// It's important that .mjs is listed before .js so that Rollup will interpret npm modules | ||
// which deploy both ESM .mjs and CommonJS .js files as ESM. | ||
extensions: ['.mjs', '.js', '.json', '.node'], | ||
resolveOnly: [] | ||
}; | ||
function nodeResolve(opts = {}) { | ||
const options = Object.assign({}, defaults, opts); | ||
const customResolveOptions = options.customResolveOptions, | ||
extensions = options.extensions, | ||
jail = options.jail; | ||
const warnings = []; | ||
const packageInfoCache = new Map(); | ||
const idToPackageInfo = new Map(); | ||
const mainFields = getMainFields(options); | ||
const useBrowserOverrides = mainFields.indexOf('browser') !== -1; | ||
const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false; | ||
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true; | ||
const rootDir = options.rootDir || process.cwd(); | ||
let dedupe = options.dedupe; | ||
let rollupOptions; | ||
if (useBrowserOverrides && typeof pkg.browser === 'object' && // eslint-disable-next-line no-prototype-builtins | ||
browserMap.hasOwnProperty(pkg.main)) { | ||
packageInfo.resolvedEntryPoint = browserMap[pkg.main]; | ||
packageInfo.browserMappedMain = true; | ||
} else { | ||
// index.node is technically a valid default entrypoint as well... | ||
packageInfo.resolvedEntryPoint = path.resolve(pkgRoot, pkg.main || 'index.js'); | ||
packageInfo.browserMappedMain = false; | ||
} | ||
if (options.only) { | ||
warnings.push('node-resolve: The `only` options is deprecated, please use `resolveOnly`'); | ||
options.resolveOnly = options.only; | ||
} | ||
const packageSideEffects = pkg.sideEffects; | ||
if (typeof dedupe !== 'function') { | ||
dedupe = importee => options.dedupe.includes(importee) || options.dedupe.includes(getPackageName(importee)); | ||
} | ||
if (typeof packageSideEffects === 'boolean') { | ||
internalPackageInfo.hasModuleSideEffects = () => packageSideEffects; | ||
} else if (Array.isArray(packageSideEffects)) { | ||
internalPackageInfo.hasModuleSideEffects = pluginutils.createFilter(packageSideEffects, null, { | ||
resolve: pkgRoot | ||
}); | ||
const resolveOnly = options.resolveOnly.map(pattern => { | ||
if (pattern instanceof RegExp) { | ||
return pattern; | ||
} | ||
packageInfoCache.set(pkgPath, internalPackageInfo); | ||
return internalPackageInfo; | ||
} | ||
const normalized = pattern.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&'); | ||
return new RegExp(`^${normalized}$`); | ||
}); | ||
const browserMapCache = new Map(); | ||
let preserveSymlinks; | ||
return { | ||
@@ -309,23 +364,32 @@ name: 'node-resolve', | ||
buildStart(options) { | ||
preserveSymlinks = options.preserveSymlinks; | ||
rollupOptions = options; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
const _this$meta$rollupVers = this.meta.rollupVersion.split('.').map(Number), | ||
_this$meta$rollupVers2 = _slicedToArray(_this$meta$rollupVers, 2), | ||
major = _this$meta$rollupVers2[0], | ||
minor = _this$meta$rollupVers2[1]; | ||
try { | ||
for (var _iterator = warnings[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
const warning = _step.value; | ||
this.warn(warning); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
const minVersion = peerDependencies.rollup.slice(2); | ||
const _minVersion$split$map = minVersion.split('.').map(Number), | ||
_minVersion$split$map2 = _slicedToArray(_minVersion$split$map, 2), | ||
minMajor = _minVersion$split$map2[0], | ||
minMinor = _minVersion$split$map2[1]; | ||
if (major < minMajor || major === minMajor && minor < minMinor) { | ||
this.error(`Insufficient Rollup version: "rollup-plugin-node-resolve" requires at least rollup@${minVersion} but found rollup@${this.meta.rollupVersion}.`); | ||
} | ||
preserveSymlinks = options.preserveSymlinks; | ||
}, | ||
generateBundle() { | ||
readFileCached.clear(); | ||
readCachedFile.clear(); | ||
isFileCached.clear(); | ||
@@ -336,141 +400,174 @@ isDirCached.clear(); | ||
resolveId(importee, importer) { | ||
if (importee === ES6_BROWSER_EMPTY) { | ||
return importee; | ||
} // ignore IDs with null character, these belong to other plugins | ||
var _this = this; | ||
return _asyncToGenerator(function* () { | ||
if (importee === ES6_BROWSER_EMPTY) { | ||
return importee; | ||
} // ignore IDs with null character, these belong to other plugins | ||
if (/\0/.test(importee)) return null; | ||
const basedir = !importer || shouldDedupe(importee) ? rootDir : path.dirname(importer); // https://github.com/defunctzombie/package-browser-field-spec | ||
const browser = browserMapCache.get(importer); | ||
if (/\0/.test(importee)) return null; | ||
const basedir = !importer || dedupe(importee) ? rootDir : path.dirname(importer); // https://github.com/defunctzombie/package-browser-field-spec | ||
if (useBrowserOverrides && browser) { | ||
const resolvedImportee = path.resolve(basedir, importee); | ||
const browser = browserMapCache.get(importer); | ||
if (browser[importee] === false || browser[resolvedImportee] === false) { | ||
return ES6_BROWSER_EMPTY; | ||
if (useBrowserOverrides && browser) { | ||
const resolvedImportee = path.resolve(basedir, importee); | ||
if (browser[importee] === false || browser[resolvedImportee] === false) { | ||
return ES6_BROWSER_EMPTY; | ||
} | ||
const browserImportee = browser[importee] || browser[resolvedImportee] || browser[`${resolvedImportee}.js`] || browser[`${resolvedImportee}.json`]; | ||
if (browserImportee) { | ||
importee = browserImportee; | ||
} | ||
} | ||
const browserImportee = browser[importee] || browser[resolvedImportee] || browser[`${resolvedImportee}.js`] || browser[`${resolvedImportee}.json`]; | ||
const parts = importee.split(/[/\\]/); | ||
let id = parts.shift(); | ||
if (browserImportee) { | ||
importee = browserImportee; | ||
if (id[0] === '@' && parts.length > 0) { | ||
// scoped packages | ||
id += `/${parts.shift()}`; | ||
} else if (id[0] === '.') { | ||
// an import relative to the parent dir of the importer | ||
id = path.resolve(basedir, importee); | ||
} | ||
} | ||
const parts = importee.split(/[/\\]/); | ||
let id = parts.shift(); | ||
const input = normalizeInput(rollupOptions.input); | ||
if (id[0] === '@' && parts.length > 0) { | ||
// scoped packages | ||
id += `/${parts.shift()}`; | ||
} else if (id[0] === '.') { | ||
// an import relative to the parent dir of the importer | ||
id = path.resolve(basedir, importee); | ||
} | ||
if (resolveOnly.length && !resolveOnly.some(pattern => pattern.test(id))) { | ||
if (input.includes(id)) { | ||
return null; | ||
} | ||
if (only && !only.some(pattern => pattern.test(id))) return null; | ||
let hasModuleSideEffects = alwaysNull; | ||
let hasPackageEntry = true; | ||
let packageBrowserField = false; | ||
let packageInfo; | ||
const resolveOptions = { | ||
basedir, | ||
return false; | ||
} | ||
packageFilter(pkg, pkgPath) { | ||
let cachedPkg; | ||
let hasModuleSideEffects = nullFn; | ||
let hasPackageEntry = true; | ||
let packageBrowserField = false; | ||
let packageInfo; | ||
var _getCachedPackageInfo = getCachedPackageInfo(pkg, pkgPath); | ||
const filter = (pkg, pkgPath) => { | ||
const info = getPackageInfo({ | ||
cache: packageInfoCache, | ||
extensions, | ||
pkg, | ||
pkgPath, | ||
mainFields, | ||
preserveSymlinks, | ||
useBrowserOverrides | ||
}); | ||
packageInfo = info.packageInfo; | ||
hasModuleSideEffects = info.hasModuleSideEffects; | ||
hasPackageEntry = info.hasPackageEntry; | ||
packageBrowserField = info.packageBrowserField; | ||
return info.cachedPkg; | ||
}; | ||
packageInfo = _getCachedPackageInfo.packageInfo; | ||
cachedPkg = _getCachedPackageInfo.cachedPkg; | ||
hasModuleSideEffects = _getCachedPackageInfo.hasModuleSideEffects; | ||
hasPackageEntry = _getCachedPackageInfo.hasPackageEntry; | ||
packageBrowserField = _getCachedPackageInfo.packageBrowserField; | ||
return cachedPkg; | ||
}, | ||
let resolveOptions = { | ||
basedir, | ||
packageFilter: filter, | ||
readFile: readCachedFile, | ||
isFile: isFileCached, | ||
isDirectory: isDirCached, | ||
extensions | ||
}; | ||
readFile: readFileCached, | ||
isFile: isFileCached, | ||
isDirectory: isDirCached, | ||
extensions | ||
}; | ||
if (preserveSymlinks !== undefined) { | ||
resolveOptions.preserveSymlinks = preserveSymlinks; | ||
} | ||
if (preserveSymlinks !== undefined) { | ||
resolveOptions.preserveSymlinks = preserveSymlinks; | ||
} | ||
const importSpecifierList = []; | ||
const importSpecifierList = []; | ||
if (importer === undefined && !importee[0].match(/^\.?\.?\//)) { | ||
// For module graph roots (i.e. when importer is undefined), we | ||
// need to handle 'path fragments` like `foo/bar` that are commonly | ||
// found in rollup config files. If importee doesn't look like a | ||
// relative or absolute path, we make it relative and attempt to | ||
// resolve it. If we don't find anything, we try resolving it as we | ||
// got it. | ||
importSpecifierList.push(`./${importee}`); | ||
} | ||
if (importer === undefined && !importee[0].match(/^\.?\.?\//)) { | ||
// For module graph roots (i.e. when importer is undefined), we | ||
// need to handle 'path fragments` like `foo/bar` that are commonly | ||
// found in rollup config files. If importee doesn't look like a | ||
// relative or absolute path, we make it relative and attempt to | ||
// resolve it. If we don't find anything, we try resolving it as we | ||
// got it. | ||
importSpecifierList.push(`./${importee}`); | ||
} | ||
const importeeIsBuiltin = builtins.has(importee); | ||
const importeeIsBuiltin = builtins.has(importee); | ||
if (importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet)) { | ||
// The `resolve` library will not resolve packages with the same | ||
// name as a node built-in module. If we're resolving something | ||
// that's a builtin, and we don't prefer to find built-ins, we | ||
// first try to look up a local module with that name. If we don't | ||
// find anything, we resolve the builtin which just returns back | ||
// the built-in's name. | ||
importSpecifierList.push(`${importee}/`); | ||
} | ||
if (importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet)) { | ||
// The `resolve` library will not resolve packages with the same | ||
// name as a node built-in module. If we're resolving something | ||
// that's a builtin, and we don't prefer to find built-ins, we | ||
// first try to look up a local module with that name. If we don't | ||
// find anything, we resolve the builtin which just returns back | ||
// the built-in's name. | ||
importSpecifierList.push(`${importee}/`); | ||
} | ||
importSpecifierList.push(importee); | ||
resolveOptions = Object.assign(resolveOptions, customResolveOptions); | ||
importSpecifierList.push(importee); | ||
return resolveImportSpecifiers(importSpecifierList, Object.assign(resolveOptions, customResolveOptions)).then(resolved => { | ||
if (resolved && packageBrowserField) { | ||
if (Object.prototype.hasOwnProperty.call(packageBrowserField, resolved)) { | ||
if (!packageBrowserField[resolved]) { | ||
browserMapCache.set(resolved, packageBrowserField); | ||
return ES6_BROWSER_EMPTY; | ||
try { | ||
let resolved = yield resolveImportSpecifiers(importSpecifierList, resolveOptions); | ||
if (resolved && packageBrowserField) { | ||
if (Object.prototype.hasOwnProperty.call(packageBrowserField, resolved)) { | ||
if (!packageBrowserField[resolved]) { | ||
browserMapCache.set(resolved, packageBrowserField); | ||
return ES6_BROWSER_EMPTY; | ||
} | ||
resolved = packageBrowserField[resolved]; | ||
} | ||
resolved = packageBrowserField[resolved]; | ||
browserMapCache.set(resolved, packageBrowserField); | ||
} | ||
browserMapCache.set(resolved, packageBrowserField); | ||
} | ||
if (hasPackageEntry && !preserveSymlinks && resolved) { | ||
const fileExists = yield exists(resolved); | ||
if (hasPackageEntry && !preserveSymlinks && resolved) { | ||
return existsAsync(resolved).then(exists => exists ? realpathAsync(resolved) : resolved); | ||
} | ||
if (fileExists) { | ||
resolved = yield realpath(resolved); | ||
} | ||
} | ||
return resolved; | ||
}).then(resolved => { | ||
idToPackageInfo.set(resolved, packageInfo); | ||
idToPackageInfo.set(resolved, packageInfo); | ||
if (hasPackageEntry) { | ||
if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) { | ||
return null; | ||
} else if (importeeIsBuiltin && preferBuiltins) { | ||
if (!isPreferBuiltinsSet) { | ||
this.warn(`preferring built-in module '${importee}' over local alternative ` + `at '${resolved}', pass 'preferBuiltins: false' to disable this ` + `behavior or 'preferBuiltins: true' to disable this warning`); | ||
if (hasPackageEntry) { | ||
if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) { | ||
return null; | ||
} else if (importeeIsBuiltin && preferBuiltins) { | ||
if (!isPreferBuiltinsSet) { | ||
_this.warn(`preferring built-in module '${importee}' over local alternative at '${resolved}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`); | ||
} | ||
return null; | ||
} else if (jail && resolved.indexOf(path.normalize(jail.trim(path.sep))) !== 0) { | ||
return null; | ||
} | ||
} | ||
if (resolved && options.modulesOnly) { | ||
const code = yield readFile(resolved, 'utf-8'); | ||
if (isModule(code)) { | ||
return { | ||
id: resolved, | ||
moduleSideEffects: hasModuleSideEffects(resolved) | ||
}; | ||
} | ||
return null; | ||
} else if (jail && resolved.indexOf(path.normalize(jail.trim(path.sep))) !== 0) { | ||
return null; | ||
} | ||
} | ||
if (resolved && options.modulesOnly) { | ||
return readFileAsync(resolved).then(code => isModule(code) ? { | ||
const result = { | ||
id: resolved, | ||
moduleSideEffects: hasModuleSideEffects(resolved) | ||
} : null); | ||
}; | ||
return result; | ||
} catch (error) { | ||
return null; | ||
} | ||
return { | ||
id: resolved, | ||
moduleSideEffects: hasModuleSideEffects(resolved) | ||
}; | ||
}).catch(() => null); | ||
})(); | ||
}, | ||
@@ -477,0 +574,0 @@ |
{ | ||
"name": "@rollup/plugin-node-resolve", | ||
"version": "7.0.0", | ||
"version": "7.1.0", | ||
"publishConfig": { | ||
@@ -51,16 +51,16 @@ "access": "public" | ||
"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", | ||
@@ -67,0 +67,0 @@ "string-capitalize": "^1.0.1" |
118
README.md
@@ -47,34 +47,46 @@ [npm]: https://img.shields.io/npm/v/@rollup/plugin-node-resolve | ||
### `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'; | ||
``` | ||
@@ -86,11 +98,4 @@ ### `extensions` | ||
Resolve extensions other than .js in the order specified. | ||
Specifies the extensions of files that the plugin will operate on. | ||
### `preferBuiltins` | ||
Type: `Boolean`<br> | ||
Default: `true` | ||
Whether to prefer built-in modules (e.g. `fs`, `path`) or local ones with the same names | ||
### `jail` | ||
@@ -101,55 +106,38 @@ | ||
Lock the module search in this path (like a chroot). Modules defined outside this path will be marked as external. | ||
Locks the module search within specified path (e.g. chroot). Modules defined outside this path will be marked as external. | ||
### `only` | ||
### `mainFields` | ||
Type: `Array[...String|RegExp]`<br> | ||
Default: `null` | ||
Type: `Array[...String]`<br> | ||
Default: `['module', 'main']`<br> | ||
Valid values: `['browser', 'jsnext', 'module', 'main']` | ||
Example: `only: ['some_module', /^@some_scope\/.*$/]` | ||
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. | ||
### `modulesOnly` | ||
### `only` | ||
Type: `Boolean`<br> | ||
Default: `false` | ||
DEPRECATED: use "resolveOnly" instead | ||
If true, inspect resolved files to check that they are ES2015 modules. | ||
### `preferBuiltins` | ||
### `dedupe` | ||
Type: `Boolean`<br> | ||
Default: `true` | ||
Type: `Array[...String]`<br> | ||
Default: `[]` | ||
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. | ||
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. | ||
### `modulesOnly` | ||
``` | ||
dedupe: [ 'my-package', '@namespace/my-package' ] | ||
``` | ||
Type: `Boolean`<br> | ||
Default: `false` | ||
This will deduplicate bare imports such as: | ||
If `true`, inspect resolved files to assert that they are ES2015 modules. | ||
```js | ||
import 'my-package'; | ||
import '@namespace/my-package'; | ||
``` | ||
### `resolveOnly` | ||
And it will deduplicate deep imports such as: | ||
```js | ||
import 'my-package/foo.js'; | ||
import '@namespace/my-package/bar.js'; | ||
``` | ||
### `customResolveOptions` | ||
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\/.*$/]` | ||
@@ -161,3 +149,3 @@ ### `rootDir` | ||
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. | ||
@@ -191,3 +179,3 @@ ``` | ||
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. | ||
@@ -194,0 +182,0 @@ If you want to silence warnings about builtins, you can add the list of builtins to the `externals` option; like so: |
@@ -6,46 +6,25 @@ import { Plugin } from 'rollup'; | ||
/** | ||
* 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 | ||
* 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`. | ||
* @default false | ||
*/ | ||
jsnext?: boolean; | ||
browser?: 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 | ||
* An `Object` that specifies additional options that should be passed through to `node-resolve`. | ||
*/ | ||
main?: boolean; | ||
customResolveOptions?: AsyncOpts; | ||
/** | ||
* 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 | ||
* 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. | ||
*/ | ||
browser?: boolean; | ||
dedupe?: string[] | ((importee: string) => boolean); | ||
/** | ||
* not all files you want to resolve are .js files | ||
* Specifies the extensions of files that the plugin will operate on. | ||
* @default [ '.mjs', '.js', '.json', '.node' ] | ||
@@ -56,11 +35,4 @@ */ | ||
/** | ||
* 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 | ||
* Locks the module search within specified path (e.g. chroot). Modules defined | ||
* outside this path will be marked as external. | ||
* @default '/' | ||
@@ -71,12 +43,10 @@ */ | ||
/** | ||
* 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 | ||
* Specifies the properties to scan within a `package.json`, used to determine the | ||
* bundle entry point. | ||
* @default ['module', 'main'] | ||
*/ | ||
only?: ReadonlyArray<string | RegExp> | null; | ||
mainFields?: ReadonlyArray<string>; | ||
/** | ||
* If true, inspect resolved files to check that they are | ||
* ES2015 modules | ||
* If `true`, inspect resolved files to assert that they are ES2015 modules. | ||
* @default false | ||
@@ -87,18 +57,24 @@ */ | ||
/** | ||
* 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. | ||
* @deprecated use "resolveOnly" instead | ||
* @default null | ||
*/ | ||
dedupe?: string[] | ((importee: string) => boolean); | ||
only?: ReadonlyArray<string | RegExp> | null; | ||
/** | ||
* Any additional options that should be passed through | ||
* to node-resolve | ||
* 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. | ||
* @default true | ||
*/ | ||
customResolveOptions?: AsyncOpts; | ||
preferBuiltins?: boolean; | ||
/** | ||
* 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. | ||
* An `Array` which instructs the plugin to limit module resolution to those whose | ||
* names match patterns in the array. | ||
* @default [] | ||
*/ | ||
resolveOnly?: ReadonlyArray<string | RegExp> | null; | ||
/** | ||
* Specifies the root directory from which to resolve modules. Typically used when | ||
* resolving entry-point imports, and when resolving deduplicated modules. | ||
* @default process.cwd() | ||
@@ -105,0 +81,0 @@ */ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54267
1025
195
1
Updated@rollup/pluginutils@^3.0.6
Updatedresolve@^1.14.2