Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @/ alias and fix config polution #13

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* 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.
* Adds sass resolver for `Modules/` alias. It works for both public and private builds in exactly the same way as the JS resolver.
* Adds alias `@/` for all builds, that points to the project build source root. This works for both JS and SCSS.

### Fixes

Expand Down
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,61 @@ bundler: 'webpack' // Only visible when using webpack

You can combine these options to precisely control when your component appears. For example, to show a component only when using Vite with HMR active, you would use both `when: 'hmr'` and `bundler: 'vite'`.

## Provided Vite Configuration

While the `apos` build (the code living in every module `ui/apos` directory) is fully preconfigured and doesn't allow for customization, the `public` build (the code imported within `ui/src/` ) is fully customizable and contains a minimal configuration to get you started:
myovchev marked this conversation as resolved.
Show resolved Hide resolved
- A PostCSS plugin to handle core features as "Breakpoint Preview" (when enabled)
myovchev marked this conversation as resolved.
Show resolved Hide resolved
- `Modules/` alias to simplify module within the same build
- `@/` alias to allow easy access to cross-module and cross-build source code

### Pre-configured Aliases

`Modules/` alias is available for both public and admin UI builds and allows you to import modules in your project without worrying about the relative path, but restricts you to only sources inside of `ui/src` directories.
myovchev marked this conversation as resolved.
Show resolved Hide resolved

```javascript
// Current file: modules/another-module/ui/src/index.js
// Actual import path: modules/my-module/ui/src/lib/utils.js
import utils from 'Modules/my-module/lib/utils.js';
```

`@/` alias is available for both public and admin UI builds and allows you to import files from the entire project source code.

```javascript
// Current file: any file in any module inside of the `ui/` folder
// Actual path: modules/my-module/ui/src/lib/utils.js
import utils from '@/modules/my-module/src/lib/utils.js';

// Actual path: modules/my-module/ui/apos/mixins/SomeMixin.js
import SomeMixin from '@/modules/my-module/apos/mixins/SomeMixin.js';
```

> Warning: You gain access to `public` builds from within the `apos` build, and vice versa, when using the `@/` alias. You should use it with caution, because it might lead to situations where imports are not resolved correctly. This would happen if the imported file (or its deep imports) contains `Modules/` aliased imports. In other hand `@/` is more developer friendly, allows auto-completion and is more intuitive and readable. Be sure to include mostly sources from your current build and ensure no imported sources contain `Modules/` aliased imports when cross-importing from another build.
myovchev marked this conversation as resolved.
Show resolved Hide resolved

## Configuring Your Code Editor

Every editor, that understands the `jsconfig.json` or `tsconfig.json` file, can be configured to understand the `@/` alias provided by this module. Here is an example of a `jsconfig.json` file that you can place in your project root:

```json
{
"compilerOptions": {
"baseUrl": "./apos-build/@apostrophecms/vite/default",
"paths": {
"@/*": ["./src/*"]
},
"module": "ESNext",
"moduleResolution": "bundler"
},
"exclude": [
"apos-build/@apostrophecms/vite/default/dist",
"node_modules",
"public",
"data"
]
}
```

> Note: If you change your project asset namespace you have to adjust the `baseUrl` and `exclude` path accordingly. For example, if your project namespace is `my-namespace`, the `baseUrl` should be `./apos-build/@apostrophecms/vite/my-namespace` and the `exclude` path - `apos-build/@apostrophecms/vite/my-namespace/dist`.

## Extending the Vite Configuration

You can customize the Vite configuration for your ApostropheCMS project in two ways:
Expand Down Expand Up @@ -212,6 +267,7 @@ The configuration format follows the standard [Vite configuration options](https

### Hot Module Replacement
- HMR only monitors existing `anyModule/ui` directories. If you add a new `ui` directory to a module, restart the server to enable HMR for that module. With default ApostropheCMS starter kits using `nodemon`, simply type `rs` in the terminal and press Enter.
- The `apos` HMR won't work when the `public` build contains Vue sources (transformed by the `@vitejs/plugin-vue` plugin). The HMR for the `public` build should still work as expected. The problem is related to the fact that the page would contains two Vue instances (core and reactive) instances, which is not currently supported. We are researching solutions to this issue.
myovchev marked this conversation as resolved.
Show resolved Hide resolved

### Public Assets
- Changes to `ui/public` directories don't trigger HMR or page reloads as they require a process restart
Expand Down
21 changes: 11 additions & 10 deletions lib/internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ module.exports = (self) => {

self.printLabels('apos', true);
const { build, config } = await self.getViteBuild('apos', options);
self.printDebug('build-apos', { viteConfig: config });
await build(config);
self.printLabels('apos', false);

self.printDebug('build-apos', { viteConfig: config });

return Date.now();
},

Expand All @@ -104,10 +103,9 @@ module.exports = (self) => {
}
self.printLabels('public', true);
const { build, config } = await self.getViteBuild('public', options);
self.printDebug('build-public', { viteConfig: config });
await build(config);
self.printLabels('public', false);

self.printDebug('build-public', { viteConfig: config });
},

// Create an entrypoint configuration for the vite client.
Expand Down Expand Up @@ -968,12 +966,15 @@ module.exports = (self) => {

// The `apos.vite.config.js` at the project root can be used to extend
// the public config.
const userConfig = (await vite.loadConfigFromFile(
configEnv,
self.userConfigFile,
self.apos.rootDir,
'silent'
))?.config || {};
const userConfig = self.userConfigFile
? (await vite.loadConfigFromFile(
configEnv,
self.userConfigFile,
self.apos.rootDir,
'silent'
))?.config || {}
: {};

merged = vite.mergeConfig(merged, userConfig);

return merged;
Expand Down
7 changes: 6 additions & 1 deletion lib/vite-plugin-apostrophe-alias.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function VitePluginApos({ sourceRoot, id } = {}) {

return {
name: 'vite-plugin-apostrophe-alias',

enforce: 'pre',
config() {
return {
css: {
Expand All @@ -35,6 +35,11 @@ export default function VitePluginApos({ sourceRoot, id } = {}) {
importers: [ { findFileUrl } ]
}
}
},
resolve: {
alias: {
'@': `${sourceRoot}/`
}
}
};
},
Expand Down