@apostrophecms/vite

中文文档
1.2.0发布于 2026/10/01MITESM + CJS近 30 天下载 4326

Vite build flow for ApostropheCMS projects.

原始使用文档
文档目录24 节
  1. Installation
  2. Usage
  3. Configuration
  4. Hot Module Replacement Configuration
  5. Enable Admin UI HMR
  6. Disable HMR
  7. Change the underlying Websocket server port
  8. Enable Source Maps in Production
  9. Inject code only when HMR is enabled
  10. Provided Vite Configuration
  11. Pre-configured Aliases
  12. Importing Static Assets and Sass
  13. Configuring Your Code Editor
  14. Extending the Vite Configuration
  15. 1. Via Any Module build.vite Property
  16. 2. Via Project Configuration File
  17. Limitations and Known Issues
  18. Hot Module Replacement
  19. Public Assets
  20. Vite Alias Resolution
  21. Code Migration Guidelines
  22. Import Paths
  23. ApostropheCMS Module Imports
  24. Module System
ApostropheCMS logo

Apostrophe Vite Bundling And HMR

This extension provides Vite integration for ApostropheCMS projects, enabling module bundling and hot module replacement (HMR) during development.

Installation

To install the module, use the command line to run this command in an Apostrophe project's root directory:

npm install @apostrophecms/vite

Usage

Add the module in the app.js file:

require('apostrophe')({
  shortName: 'my-project',
  modules: {
    '@apostrophecms/vite': {},
  }
});

Configuration

Hot Module Replacement Configuration

By default, HMR is enabled for your project's public UI code. All configuration is handled through ApostropheCMS's core asset module options, simplifying setup and maintenance.

Enable Admin UI HMR

For development work on the ApostropheCMS admin interface, you can switch HMR to target the admin UI instead of public-facing components:

require('apostrophe')({
  shortName: 'my-project',
  modules: {
    '@apostrophecms/vite': {},
    '@apostrophecms/asset': {
      options: {
        hmr: 'apos', // 'public' targets the project UI (default)
      },
    },
  }
});

Disable HMR

You can disable hot module replacement when it is not needed or desired, while still using Vite for builds:

require('apostrophe')({
  shortName: 'my-project',
  modules: {
    '@apostrophecms/vite': {},
    '@apostrophecms/asset': {
      options: {
        hmr: false,
      },
    },
  }
});

Change the underlying Websocket server port

During development, the hot module reload (HMR) server uses WebSocket and runs on the same port as your ApostropheCMS instance. For advanced configurations, you can run the development server as a standalone HTTP server on a different port by setting the hmrPort option. This can be useful when you need to avoid port conflicts or work with specific network configurations:

require('apostrophe')({
  shortName: 'my-project',
  modules: {
    '@apostrophecms/vite': {},
    '@apostrophecms/asset': {
      options: {
        hmrPort: 3001,
      },
    },
  }
});

Enable Source Maps in Production

You can enable source maps in production to help debug minified code and view original source files in the browser DevTools. While this slightly increases the initial download size, it's valuable for debugging production issues.

require('apostrophe')({
  shortName: 'my-project',
  modules: {
    '@apostrophecms/vite': {},
    '@apostrophecms/asset': {
      options: {
        productionSourceMaps: true,
      },
    },
  }
});

Inject code only when HMR is enabled

If you want to inject some code in your site only when in development mode and HMR is enabled, you can use the Apostrophe nunjucks components.

{# module-name/views/myComponent.html #}
<!-- Shown only when HMR is enabled and in development mode. -->
// module-name/index.js
module.exports = {
  components(self) {
    return {
      myComponent(req, data) {
        return {};
      }
    };
  },
  init(self) {
    self.apos.template.prepend({
      where: 'head',
      when: 'hmr',
      bundler: 'vite',
      component: 'module-name:myComponent'
    });
  }
};

The when option controls when your component appears:

when: 'hmr'   // Only visible when HMR is active
when: 'dev'   // Visible in any development mode
when: 'prod'  // Only visible in production

The bundler option allows you to specify which bundler must be active for the component to appear:

bundler: 'vite'    // Only visible when using Vite
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 theui/apos/ directory of every module) 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:

  • A PostCSS plugin to handle core features such as "Breakpoint Preview" (when enabled)
  • 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

The 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.

// Current file: modules/another-module/ui/src/index.js
// Actual import path: modules/some-module/ui/src/lib/utils.js
import utils from 'Modules/some-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. It follows the same path as your original source code, but skips the ui/ part of the path.

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

// Actual path: modules/some-module/ui/apos/mixins/SomeMixin.js
import SomeMixin from '@/some-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. On the 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.

Importing Static Assets and Sass

The way we integrate Vite with ApostropheCMS allows now direct imports (including dynamic imports) of assets like images, fonts, and other files. You can import them directly in your vanilla JS/JS framework code:

// You can use aliases to import assets or a relative path when in the same module.
// Actual path: modules/some-module/ui/assets/logo.svg
import logo from '@/some-module/assets/logo.svg';
// Logo now contains the path to the image and will be normalized and correctly
// injected when building the project for production.

You can import Sass as well:

/* You can use aliases to import assets */
/* Actual path: modules/some-module/ui/scss/_styles.scss */
@use '@/some-module/scss/styles';

Vue JS supports importing assets directly in the template:

<template>
  <img src="@/some-module/assets/logo.svg" alt="My logo" />
</template>

In other frameworks (but also in Vue), you can use the import statement to reference the asset:

import logo from '@/some-module/assets/logo.svg';

function MyComponent() {
  return <img src={logo} alt="My logo" />;
}

CSS URL can be resolved in two ways. You can use the documented in the Apostrophe docs some-module/public folder and /modules/some-module/font.ttf URL where your file is located in ./modules/some-module/public/font.ttf

@font-face {
  font-family: MyFont;
  src: url("/modules/some-module/font.ttf") format("truetype");
}

Or you can use the absolute sources root path /src/some-module/fonts/font.ttf where your file is located in ./modules/some-module/ui/fonts/font.ttf. You can inspect the sources of your project that are copied in the central location apos-build/@postrophecms/vite/default directory. This is the root that Vite uses to resolve the paths and build the project.

@font-face {
  font-family: Inter;
  src: url("/src/some-module/fonts/font.ttf") format("truetype");
}

The same rules apply to paths in the url() function in CSS files.

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:

{
  "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.

Note: If you follow the import in your editor (e.g. Ctrl + Click in VSCode) it will lead to the apos-build directory and NOT the original source code. This is because the apos-build directory contains a copy of the entire project source code (including Admin UI) from all modules (local and npm) and is the actual source directory used by Vite to build the project.

Extending the Vite Configuration

You can customize the Vite configuration for your ApostropheCMS project in two ways:

1. Via Any Module build.vite Property

Use this approach to configure Vite settings within individual ApostropheCMS modules:

// modules/some-module/index.js
module.exports = {
  build: {
    vite: {
      myViteConfig: {
        // Standard Vite configuration
        define: {
          __MY_ENV__: '1',
        },
      }
    },
  },
};

2. Via Project Configuration File

For project-wide Vite configuration, create one of these files in your project root:

  • apos.vite.config.js (for ESM projects)
  • apos.vite.config.mjs (for CommonJS projects)

This method supports the full Vite configuration API and applies to your project's UI build. You can import Vite's configuration utilities directly from the ApostropheCMS Vite module:

// apos.vite.config.js
import { defineConfig } from '@apostrophecms/vite/vite';
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [ vue() ]
});

The configuration format follows the standard Vite configuration options. Common use cases include adding plugins, defining environment variables, and customizing build settings.

Note: All Vite configurations are merged sequentially - first across modules (following module registration order, with later modules taking precedence), and finally with the project configuration file, which takes ultimate precedence.

Limitations and Known Issues

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 contain two Vue instances (core and reactive) instances, which is not currently supported. We are researching solutions to this issue.

Public Assets

  • Changes to ui/public directories don't trigger HMR or page reloads as they require a process restart
  • Workaround: Add ui/public/ folders to your nodemon watch list in either nodemon.json or package.json
  • Future support for this feature will depend on user needs

Vite Alias Resolution

  • When setting custom resolve.alias in Vite configuration, paths must resolve to the appropriate apos-build/... source code rather than the original source
  • Future enhancement planned: We will provide templating (e.g., {srcRoot}) or function arguments (e.g., aposRoot) to simplify correct path resolution

Code Migration Guidelines

Import Paths

  • Remove all ~ prefixes from CSS/Sass imports
    /* Instead of: @import "~normalize.css" */
    @import "normalize.css"
    

ApostropheCMS Module Imports

  • Recommended: Use the Modules/module-name/components/... alias instead of direct paths like apostrophe/modules/module-name/ui/apos/components/...
  • This alias is available only for apos source code; project code can define its own aliases

Module System

  • Use only ESM syntax in UI source code:
    • ✅ import abc from 'xxx' or const abc = await import('xxx')
    • ✅ export default ... or export something
    • ❌ No CommonJS: require(), module.exports, exports.xxx

版本与发布记录

@apostrophecms/vite 共发布 8 个版本,首次发布于 2024/10/31;过去 12 个月发布了 4 个版本。

版本发布日期距今
1.2.0latest2026/10/011 个月前
1.1.22026/07/113 个月前
1.1.12025/11/2611 个月前
1.1.1-alpha.1alpha2025/11/1111 个月前
1.1.02025/06/121.4 年前
1.0.02024/12/191.9 年前

其他发布通道:beta → 1.0.0-beta.1,alpha → 1.1.1-alpha.1。

关于 @apostrophecms/vite 的常见问题

@apostrophecms/vite 是什么?

Vite build flow for ApostropheCMS projects.

如何安装 @apostrophecms/vite?

在项目目录运行 npm install @apostrophecms/vite,或使用 pnpm add / yarn add / bun add。

@apostrophecms/vite 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/apostrophecms__vite 是否存在。

@apostrophecms/vite 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@apostrophecms/vite 可以商用吗?

@apostrophecms/vite 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@apostrophecms/vite 的下载量有多少?

根据 npm 官方下载统计,@apostrophecms/vite 在 2026/10/07 – 2026/11/05 这 30 天内被下载 4,326 次,日均约 144 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apostrophecms/vite 还在维护吗?

最新版本 1.2.0 发布于 2026/10/01(1 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 4 个;首次发布于 2024/10/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。