Extensions & Integrations

This extension provides Vite integration for ApostropheCMS projects, enabling module bundling and hot module replacement (HMR) during development.
> npm i @apostrophecms/vite

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

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/my-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.

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

Updated

1 month ago

Version

1.0.0-beta.2

Report a bug