Skip to content

Commit

Permalink
PRO-6789: public alias (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
myovchev authored Nov 11, 2024
1 parent a4b0cc3 commit 2e17abb
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 32 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Adds module debug logs when in asset debug mode (`APOS_ASSET_DEBUG=1`).
* Adds an option for disabling the module preload polyfill.
* Adds support for `synthetic` entrypoints, that will only process the entrypoint `prologue`.
* Adds support for `Modules/...` alias for the public builds (Webpack BC).
* Our Vite alias plugin now throws with an useful error message when `Modules/` alias resolver fails.

### Fixes

Expand Down
1 change: 1 addition & 0 deletions lib/internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,7 @@ module.exports = (self) => {
const vite = await import('vite');
// The base public config
const config = await vitePublicConfig({
sourceRoot: self.buildRootSource,
input: self.getBuildInputs('public')
});
const postcssConfig = await self.getPostcssConfig(self.buildOptions, 'public');
Expand Down
7 changes: 5 additions & 2 deletions lib/vite-apos-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = async ({
sourceRoot, input
}) => {
const vue = await import('@vitejs/plugin-vue');
const apos = await import('./vite-plugin-apostrophe-apos.mjs');
const apos = await import('./vite-plugin-apostrophe-alias.mjs');

/** @type {import('vite').UserConfig} */
return {
Expand All @@ -21,7 +21,10 @@ module.exports = async ({
}
},
plugins: [
apos.default({ sourceRoot }),
apos.default({
id: 'apos',
sourceRoot
}),
vue.default()
],
build: {
Expand Down
66 changes: 66 additions & 0 deletions lib/vite-plugin-apostrophe-alias.mjs
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;
}
};
}
29 changes: 0 additions & 29 deletions lib/vite-plugin-apostrophe-apos.mjs

This file was deleted.

9 changes: 8 additions & 1 deletion lib/vite-public-config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
module.exports = async ({
input
sourceRoot, input
}) => {
const apos = await import('./vite-plugin-apostrophe-alias.mjs');

/** @type {import('vite').UserConfig} */
return {
plugins: [
apos.default({
id: 'src',
sourceRoot
})
],
build: {
rollupOptions: {
input
Expand Down

0 comments on commit 2e17abb

Please sign in to comment.