This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt): support
~
/~~
/@
/@@
aliases within layers (#19986)
- Loading branch information
Showing
6 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { createUnplugin } from 'unplugin' | ||
import type { NuxtConfigLayer } from 'nuxt/schema' | ||
import { resolveAlias } from '@nuxt/kit' | ||
import { normalize } from 'pathe' | ||
import MagicString from 'magic-string' | ||
|
||
interface LayerAliasingOptions { | ||
sourcemap?: boolean | ||
transform?: boolean | ||
layers: NuxtConfigLayer[] | ||
} | ||
|
||
const ALIAS_RE = /(?<=['"])[~@]{1,2}(?=\/)/g | ||
|
||
export const LayerAliasingPlugin = createUnplugin((options: LayerAliasingOptions) => { | ||
const aliases = Object.fromEntries(options.layers.map(l => [l.config.srcDir || l.cwd, { | ||
'~': l.config?.alias?.['~'] || l.config.srcDir || l.cwd, | ||
'@': l.config?.alias?.['@'] || l.config.srcDir || l.cwd, | ||
'~~': l.config?.alias?.['~~'] || l.config.rootDir || l.cwd, | ||
'@@': l.config?.alias?.['@@'] || l.config.rootDir || l.cwd | ||
}])) | ||
const layers = Object.keys(aliases).sort((a, b) => b.length - a.length) | ||
|
||
return { | ||
name: 'nuxt:layer-aliasing', | ||
enforce: 'pre', | ||
vite: { | ||
resolveId: { | ||
order: 'pre', | ||
async handler (id, importer) { | ||
if (!importer) { return } | ||
|
||
const layer = layers.find(l => importer.startsWith(l)) | ||
if (!layer) { return } | ||
|
||
const resolvedId = resolveAlias(id, aliases[layer]) | ||
if (resolvedId !== id) { | ||
return await this.resolve(resolvedId, importer, { skipSelf: true }) | ||
} | ||
} | ||
} | ||
}, | ||
|
||
// webpack-only transform | ||
transformInclude: (id) => { | ||
if (!options.transform) { return false } | ||
const _id = normalize(id) | ||
return layers.some(dir => _id.startsWith(dir)) | ||
}, | ||
transform (code, id) { | ||
if (!options.transform) { return } | ||
|
||
const _id = normalize(id) | ||
const layer = layers.find(l => _id.startsWith(l)) | ||
if (!layer || !code.match(ALIAS_RE)) { return } | ||
|
||
const s = new MagicString(code) | ||
s.replace(ALIAS_RE, r => aliases[layer][r as '~'] || r) | ||
|
||
if (s.hasChanged()) { | ||
return { | ||
code: s.toString(), | ||
map: options.sourcemap ? s.generateMap({ source: id, includeContent: true }) : undefined | ||
} | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.