@rollup/plugin-alias
Advanced tools
Comparing version 3.1.8 to 3.1.9
# @rollup/plugin-alias ChangeLog | ||
## v3.1.9 | ||
_2021-12-31_ | ||
### Updates | ||
- refactor: avoid resolving customResolver every time handling resolveId (#1024) | ||
## v3.1.8 | ||
@@ -4,0 +12,0 @@ |
@@ -1,2 +0,1 @@ | ||
const noop = () => null; | ||
function matches(pattern, importee) { | ||
@@ -12,33 +11,32 @@ if (pattern instanceof RegExp) { | ||
} | ||
const importeeStartsWithKey = importee.indexOf(pattern) === 0; | ||
const importeeHasSlashAfterKey = importee.substring(pattern.length)[0] === '/'; | ||
return importeeStartsWithKey && importeeHasSlashAfterKey; | ||
// eslint-disable-next-line prefer-template | ||
return importee.startsWith(pattern + '/'); | ||
} | ||
function normalizeId(id) { | ||
return id; | ||
} | ||
function getEntries({ entries }) { | ||
function getEntries({ entries, customResolver }) { | ||
if (!entries) { | ||
return []; | ||
} | ||
const resolverFunctionFromOptions = resolveCustomResolver(customResolver); | ||
if (Array.isArray(entries)) { | ||
return entries; | ||
return entries.map((entry) => { | ||
return { | ||
find: entry.find, | ||
replacement: entry.replacement, | ||
resolverFunction: resolveCustomResolver(entry.customResolver) || resolverFunctionFromOptions | ||
}; | ||
}); | ||
} | ||
return Object.entries(entries).map(([key, value]) => { | ||
return { find: key, replacement: value }; | ||
return { find: key, replacement: value, resolverFunction: resolverFunctionFromOptions }; | ||
}); | ||
} | ||
function getCustomResolver({ customResolver }, options) { | ||
if (typeof customResolver === 'function') { | ||
return customResolver; | ||
function resolveCustomResolver(customResolver) { | ||
if (customResolver) { | ||
if (typeof customResolver === 'function') { | ||
return customResolver; | ||
} | ||
if (typeof customResolver.resolveId === 'function') { | ||
return customResolver.resolveId; | ||
} | ||
} | ||
if (customResolver && typeof customResolver.resolveId === 'function') { | ||
return customResolver.resolveId; | ||
} | ||
if (typeof options.customResolver === 'function') { | ||
return options.customResolver; | ||
} | ||
if (options.customResolver && typeof options.customResolver.resolveId === 'function') { | ||
return options.customResolver.resolveId; | ||
} | ||
return null; | ||
@@ -51,3 +49,3 @@ } | ||
name: 'alias', | ||
resolveId: noop | ||
resolveId: () => null | ||
}; | ||
@@ -57,30 +55,22 @@ } | ||
name: 'alias', | ||
buildStart(inputOptions) { | ||
return Promise.all([...entries, options].map(({ customResolver }) => customResolver && | ||
async buildStart(inputOptions) { | ||
await Promise.all([...(Array.isArray(options.entries) ? options.entries : []), options].map(({ customResolver }) => customResolver && | ||
typeof customResolver === 'object' && | ||
typeof customResolver.buildStart === 'function' && | ||
customResolver.buildStart.call(this, inputOptions))).then(() => { | ||
// enforce void return value | ||
}); | ||
customResolver.buildStart.call(this, inputOptions))); | ||
}, | ||
resolveId(importee, importer, resolveOptions) { | ||
const importeeId = normalizeId(importee); | ||
const importerId = normalizeId(importer); | ||
if (!importer) { | ||
return null; | ||
} | ||
// First match is supposed to be the correct one | ||
const matchedEntry = entries.find((entry) => matches(entry.find, importeeId)); | ||
if (!matchedEntry || !importerId) { | ||
const matchedEntry = entries.find((entry) => matches(entry.find, importee)); | ||
if (!matchedEntry) { | ||
return null; | ||
} | ||
const updatedId = normalizeId(importeeId.replace(matchedEntry.find, matchedEntry.replacement)); | ||
const customResolver = getCustomResolver(matchedEntry, options); | ||
if (customResolver) { | ||
return customResolver.call(this, updatedId, importerId, resolveOptions); | ||
const updatedId = importee.replace(matchedEntry.find, matchedEntry.replacement); | ||
if (matchedEntry.resolverFunction) { | ||
return matchedEntry.resolverFunction.call(this, updatedId, importer, resolveOptions); | ||
} | ||
return this.resolve(updatedId, importer, Object.assign({ skipSelf: true }, resolveOptions)).then((resolved) => { | ||
let finalResult = resolved; | ||
if (!finalResult) { | ||
finalResult = { id: updatedId }; | ||
} | ||
return finalResult; | ||
}); | ||
return this.resolve(updatedId, importer, Object.assign({ skipSelf: true }, resolveOptions)).then((resolved) => resolved || { id: updatedId }); | ||
} | ||
@@ -87,0 +77,0 @@ }; |
'use strict'; | ||
const noop = () => null; | ||
function matches(pattern, importee) { | ||
@@ -14,33 +13,32 @@ if (pattern instanceof RegExp) { | ||
} | ||
const importeeStartsWithKey = importee.indexOf(pattern) === 0; | ||
const importeeHasSlashAfterKey = importee.substring(pattern.length)[0] === '/'; | ||
return importeeStartsWithKey && importeeHasSlashAfterKey; | ||
// eslint-disable-next-line prefer-template | ||
return importee.startsWith(pattern + '/'); | ||
} | ||
function normalizeId(id) { | ||
return id; | ||
} | ||
function getEntries({ entries }) { | ||
function getEntries({ entries, customResolver }) { | ||
if (!entries) { | ||
return []; | ||
} | ||
const resolverFunctionFromOptions = resolveCustomResolver(customResolver); | ||
if (Array.isArray(entries)) { | ||
return entries; | ||
return entries.map((entry) => { | ||
return { | ||
find: entry.find, | ||
replacement: entry.replacement, | ||
resolverFunction: resolveCustomResolver(entry.customResolver) || resolverFunctionFromOptions | ||
}; | ||
}); | ||
} | ||
return Object.entries(entries).map(([key, value]) => { | ||
return { find: key, replacement: value }; | ||
return { find: key, replacement: value, resolverFunction: resolverFunctionFromOptions }; | ||
}); | ||
} | ||
function getCustomResolver({ customResolver }, options) { | ||
if (typeof customResolver === 'function') { | ||
return customResolver; | ||
function resolveCustomResolver(customResolver) { | ||
if (customResolver) { | ||
if (typeof customResolver === 'function') { | ||
return customResolver; | ||
} | ||
if (typeof customResolver.resolveId === 'function') { | ||
return customResolver.resolveId; | ||
} | ||
} | ||
if (customResolver && typeof customResolver.resolveId === 'function') { | ||
return customResolver.resolveId; | ||
} | ||
if (typeof options.customResolver === 'function') { | ||
return options.customResolver; | ||
} | ||
if (options.customResolver && typeof options.customResolver.resolveId === 'function') { | ||
return options.customResolver.resolveId; | ||
} | ||
return null; | ||
@@ -53,3 +51,3 @@ } | ||
name: 'alias', | ||
resolveId: noop | ||
resolveId: () => null | ||
}; | ||
@@ -59,30 +57,22 @@ } | ||
name: 'alias', | ||
buildStart(inputOptions) { | ||
return Promise.all([...entries, options].map(({ customResolver }) => customResolver && | ||
async buildStart(inputOptions) { | ||
await Promise.all([...(Array.isArray(options.entries) ? options.entries : []), options].map(({ customResolver }) => customResolver && | ||
typeof customResolver === 'object' && | ||
typeof customResolver.buildStart === 'function' && | ||
customResolver.buildStart.call(this, inputOptions))).then(() => { | ||
// enforce void return value | ||
}); | ||
customResolver.buildStart.call(this, inputOptions))); | ||
}, | ||
resolveId(importee, importer, resolveOptions) { | ||
const importeeId = normalizeId(importee); | ||
const importerId = normalizeId(importer); | ||
if (!importer) { | ||
return null; | ||
} | ||
// First match is supposed to be the correct one | ||
const matchedEntry = entries.find((entry) => matches(entry.find, importeeId)); | ||
if (!matchedEntry || !importerId) { | ||
const matchedEntry = entries.find((entry) => matches(entry.find, importee)); | ||
if (!matchedEntry) { | ||
return null; | ||
} | ||
const updatedId = normalizeId(importeeId.replace(matchedEntry.find, matchedEntry.replacement)); | ||
const customResolver = getCustomResolver(matchedEntry, options); | ||
if (customResolver) { | ||
return customResolver.call(this, updatedId, importerId, resolveOptions); | ||
const updatedId = importee.replace(matchedEntry.find, matchedEntry.replacement); | ||
if (matchedEntry.resolverFunction) { | ||
return matchedEntry.resolverFunction.call(this, updatedId, importer, resolveOptions); | ||
} | ||
return this.resolve(updatedId, importer, Object.assign({ skipSelf: true }, resolveOptions)).then((resolved) => { | ||
let finalResult = resolved; | ||
if (!finalResult) { | ||
finalResult = { id: updatedId }; | ||
} | ||
return finalResult; | ||
}); | ||
return this.resolve(updatedId, importer, Object.assign({ skipSelf: true }, resolveOptions)).then((resolved) => resolved || { id: updatedId }); | ||
} | ||
@@ -89,0 +79,0 @@ }; |
{ | ||
"name": "@rollup/plugin-alias", | ||
"version": "3.1.8", | ||
"version": "3.1.9", | ||
"publishConfig": { | ||
@@ -5,0 +5,0 @@ "access": "public" |
@@ -16,2 +16,8 @@ import { Plugin, PluginHooks } from 'rollup'; | ||
export interface ResolvedAlias { | ||
find: string | RegExp; | ||
replacement: string; | ||
resolverFunction: ResolverFunction | null; | ||
} | ||
export interface RollupAliasOptions { | ||
@@ -18,0 +24,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
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
17587
188