Skip to content

Commit

Permalink
fix(externals): fallback traced files for minor/patch versions (unjs#529
Browse files Browse the repository at this point in the history
)
  • Loading branch information
danielroe authored and WinterYukky committed Nov 1, 2022
1 parent b04d315 commit aad5146
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/rollup/plugins/externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export function externals (opts: NodeExternalsOptions): Plugin {
const isNewer = semver.gt(v2, v1)

// Warn about major version differences
const getMajor = v => v.split('.').filter(s => s !== '0')[0]
const getMajor = (v: string) => v.split('.').filter(s => s !== '0')[0]
if (getMajor(v1) !== getMajor(v2)) {
const warn = `Multiple major versions of package \`${pkgName}\` are being externalized. Picking latest version:\n\n` + [
` ${isNewer ? '-' : '+'} ` + existingPkgDir + '@' + v1,
Expand All @@ -195,13 +195,14 @@ export function externals (opts: NodeExternalsOptions): Plugin {
}
}

// Exclude older version files
if (isNewer) {
ignoreDirs.push(existingPkgDir)
} else {
ignoreDirs.push(pkgDir)
pkgDir = existingPkgDir // Update for tracedPackages
const [newerDir, olderDir] = isNewer ? [pkgDir, existingPkgDir] : [existingPkgDir, pkgDir]
// Try to map traced files from one package to another for minor/patch versions
if (getMajor(v1) === getMajor(v2)) {
tracedFiles = tracedFiles.map(f => f.startsWith(olderDir + '/') ? f.replace(olderDir, newerDir) : f)
}
// Exclude older version files
ignoreDirs.push(olderDir + '/')
pkgDir = newerDir // Update for tracedPackages
}

// Add to traced packages
Expand All @@ -224,9 +225,13 @@ export function externals (opts: NodeExternalsOptions): Plugin {
if (!await isFile(file)) { return }
const src = resolve(opts.traceOptions.base, file)
const { pkgName, subpath } = parseNodeModulePath(file)
const dst = resolve(opts.outDir, `node_modules/${pkgName}/${subpath}`)
const dst = resolve(opts.outDir, `node_modules/${pkgName + subpath}`)
await fsp.mkdir(dirname(dst), { recursive: true })
await fsp.copyFile(src, dst)
try {
await fsp.copyFile(src, dst)
} catch (err) {
consola.warn(`Could not resolve \`${src}\`. Skipping.`)
}
}

// Write traced files
Expand Down

0 comments on commit aad5146

Please sign in to comment.