@rollup/plugin-alias
Advanced tools
Comparing version 3.1.5 to 3.1.6
# @rollup/plugin-alias ChangeLog | ||
## v3.1.6 | ||
_2021-10-19_ | ||
### Updates | ||
- refactor: avoid resolving customResolver every time handling resolveId (#1000) | ||
## v3.1.5 | ||
@@ -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 customResolverFromOptions = resolveCustomResolver(customResolver); | ||
if (Array.isArray(entries)) { | ||
return entries; | ||
return entries.map((entry) => { | ||
return { | ||
find: entry.find, | ||
replacement: entry.replacement, | ||
customResolver: resolveCustomResolver(entry.customResolver) || customResolverFromOptions | ||
}; | ||
}); | ||
} | ||
return Object.entries(entries).map(([key, value]) => { | ||
return { find: key, replacement: value }; | ||
return { find: key, replacement: value, customResolver: customResolverFromOptions }; | ||
}); | ||
} | ||
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([...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) { | ||
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, {}); | ||
const updatedId = importee.replace(matchedEntry.find, matchedEntry.replacement); | ||
if (matchedEntry.customResolver) { | ||
return matchedEntry.customResolver.call(this, updatedId, importer, {}); | ||
} | ||
return this.resolve(updatedId, importer, { skipSelf: true }).then((resolved) => { | ||
let finalResult = resolved; | ||
if (!finalResult) { | ||
finalResult = { id: updatedId }; | ||
} | ||
return finalResult; | ||
}); | ||
return this.resolve(updatedId, importer, { skipSelf: true }).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 customResolverFromOptions = resolveCustomResolver(customResolver); | ||
if (Array.isArray(entries)) { | ||
return entries; | ||
return entries.map((entry) => { | ||
return { | ||
find: entry.find, | ||
replacement: entry.replacement, | ||
customResolver: resolveCustomResolver(entry.customResolver) || customResolverFromOptions | ||
}; | ||
}); | ||
} | ||
return Object.entries(entries).map(([key, value]) => { | ||
return { find: key, replacement: value }; | ||
return { find: key, replacement: value, customResolver: customResolverFromOptions }; | ||
}); | ||
} | ||
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([...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) { | ||
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, {}); | ||
const updatedId = importee.replace(matchedEntry.find, matchedEntry.replacement); | ||
if (matchedEntry.customResolver) { | ||
return matchedEntry.customResolver.call(this, updatedId, importer, {}); | ||
} | ||
return this.resolve(updatedId, importer, { skipSelf: true }).then((resolved) => { | ||
let finalResult = resolved; | ||
if (!finalResult) { | ||
finalResult = { id: updatedId }; | ||
} | ||
return finalResult; | ||
}); | ||
return this.resolve(updatedId, importer, { skipSelf: true }).then((resolved) => resolved || { id: updatedId }); | ||
} | ||
@@ -89,0 +79,0 @@ }; |
{ | ||
"name": "@rollup/plugin-alias", | ||
"version": "3.1.5", | ||
"version": "3.1.6", | ||
"publishConfig": { | ||
@@ -5,0 +5,0 @@ "access": "public" |
@@ -16,2 +16,8 @@ import { Plugin, PluginHooks } from 'rollup'; | ||
export interface ResolvedAlias { | ||
find: string | RegExp; | ||
replacement: string; | ||
customResolver: 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
17039
188