Skip to content

Commit

Permalink
fix: fix the issue of CSS files under node_modules not being found, u…
Browse files Browse the repository at this point in the history
…sing the built-in resolve #79 #90
  • Loading branch information
coder-layne committed Aug 24, 2024
1 parent 79f67a3 commit 823f81d
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default function VitePluginLibAssets(options: Options = {}): Plugin {
regExp,
publicUrl = '',
} = options
const pluginName = 'vite-plugin-lib-assets'
const publicDir = publicUrl.endsWith('/') ? publicUrl : `${publicUrl}/`
let isLibBuild = false
let isBuildWatch = false
Expand Down Expand Up @@ -209,7 +210,7 @@ export default function VitePluginLibAssets(options: Options = {}): Plugin {
}

return {
name: 'vite-plugin-lib-assets',
name: pluginName,
apply: 'build',
enforce: 'pre',
configResolved(config) {
Expand All @@ -230,15 +231,29 @@ export default function VitePluginLibAssets(options: Options = {}): Plugin {
}
}
},
async resolveId(source, importer = '') {
async resolveId(source, importer = '', opts) {
if (!isLibBuild)
return null

const importerDir = importer.endsWith(path.sep)
? importer
: path.dirname(importer)
// Full path of the imported file
const id = path.resolve(importerDir, source)
// skip resolves triggered by plugin self
if (opts.custom?.[pluginName]?.fromResolveId)
return null

let id: string
if (path.isAbsolute(source)) {
id = source
}
else if (source.startsWith('.')) {
id = path.resolve(path.dirname(importer), source)
}
else {
const custom = { ...opts.custom, [pluginName]: { fromResolveId: true } }
const resolved = await this.resolve(source, importer, { ...opts, custom })
if (resolved === null)
return null
// Full path of the imported file
id = resolved.id
}

if (cssLangFilter(id)) {
const assetsFromCss = await extractFromFile(this, id)
Expand Down

0 comments on commit 823f81d

Please sign in to comment.