-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
82 additions
and
32 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
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,66 @@ | ||
import { join } from 'node:path'; | ||
|
||
/** | ||
* Resolve `apos` and `public` builds alias `Modules/`. The `sourceRoot` option | ||
* should be the absolute path to `apos-build/.../src` folder. | ||
* The `id` option should be either `apos` or `src` depending on the build it's | ||
* being used for (apos or public respectively). | ||
* | ||
* @param {{ id: 'src' | 'apos', sourceRoot: string }} options | ||
* @returns {import('vite').Plugin} | ||
*/ | ||
export default function VitePluginApos({ sourceRoot, id } = {}) { | ||
if (!id) { | ||
throw new Error('[vite-plugin-apostrophe-alias] `id` option is required.'); | ||
} | ||
if (!sourceRoot) { | ||
throw new Error( | ||
'[vite-plugin-apostrophe-alias] `sourceRoot` option is required.' | ||
); | ||
} | ||
const pluginOptions = { | ||
id, | ||
sourceRoot | ||
}; | ||
|
||
return { | ||
name: 'vite-plugin-apostrophe-alias', | ||
|
||
async resolveId(source, importer, options) { | ||
if (!source.startsWith('Modules/')) { | ||
return null; | ||
} | ||
const chunks = source.replace('Modules/', '').split('/'); | ||
let moduleName = chunks.shift(); | ||
if (moduleName.startsWith('@')) { | ||
moduleName += '/' + chunks.shift(); | ||
} | ||
const absPath = join( | ||
pluginOptions.sourceRoot, | ||
moduleName, | ||
pluginOptions.id, | ||
...chunks | ||
); | ||
const resolved = await this.resolve( | ||
absPath, | ||
importer, | ||
options | ||
); | ||
if (!resolved) { | ||
// For internal debugging purposes | ||
this.warn( | ||
`Resolve attempt failed: "${source}" -> "${absPath}"` | ||
); | ||
// For user-facing error messages | ||
this.error( | ||
`Unable to resolve module source "${moduleName}/ui/${id}/${join(...chunks)}" ` + | ||
`from alias "${source}".\n` + | ||
'Please be sure to use the correct alias path. ' + | ||
'For more information, see:\n' + | ||
'https://docs.apostrophecms.org/guide/vite.html#resolve-alias-errors' | ||
); | ||
} | ||
return resolved; | ||
} | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
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