npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@apostrophecms/vite

v1.0.0-beta.2

Published

Vite build flow for ApostropheCMS projects.

Downloads

169

Readme

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