Skip to content

Commit

Permalink
feat: ability to define custom AEM path segments
Browse files Browse the repository at this point in the history
You may now pass through custom AEM paths that need to be proxied
  • Loading branch information
cshawaus committed Apr 30, 2023
1 parent cadc5ad commit e378c58
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,51 @@
import { bundlesImportRewriter } from '@aem-vite/import-rewriter'

import { configureAemProxy, isObject, setBundleEntries, setResolvedConfig } from './helpers'
import { configureAemProxy, debug, isObject, setBundleEntries, setResolvedConfig } from './helpers'

import type { PluginOption, ProxyOptions } from 'vite'
import type { PluginOptions } from './types'

export function viteForAem(options: PluginOptions): PluginOption[] {
if (!options) {
throw new Error('No options were provided.')
}

const aemOptions = options.aem
const aemUrl = `http://${aemOptions?.host ?? 'localhost'}:${aemOptions?.port ?? 4502}`

if (!options.publicPath || !options.publicPath.length) {
throw new Error('A public path is required for the proxy server to find and inject Vite DevServer!')
}

debug('using AEM URL: %s', aemUrl)
debug('options:', aemOptions)

const aemProxySegments = [
...(options.aemProxySegments ?? []),
'aem',
'apps',
'bin',
'conf',
'content',
'crx',
'etc',
'etc.clientlibs',
'home',
'libs',
'login',
'mnt',
'system',
'var',
'(assets|editor|sites|screens)',
]

const aemProxySegmentsExp = new RegExp(`^/(${aemProxySegments.join('|')}(.html)?)/.*`).source

const aemContentPathsExp = `^/content/(${options.contentPaths.join('|')})(/.*)?`

debug('aem content paths:', aemContentPathsExp)
debug('aem request segments:', aemProxySegmentsExp)

const plugins: PluginOption[] = [
{
enforce: 'pre',
Expand All @@ -36,6 +69,7 @@ export function viteForAem(options: PluginOptions): PluginOption[] {
}

debug('proxy options:', baseProxyOptions)

config.build = {
...(config.build || {}),

Expand All @@ -50,7 +84,7 @@ export function viteForAem(options: PluginOptions): PluginOption[] {
strictPort: true,

proxy: {
[`^/content/(${options.contentPaths.join('|')})(/.*)?`]: {
[aemContentPathsExp]: {
...baseProxyOptions,
protocolRewrite: 'http',
selfHandleResponse: true,
Expand All @@ -60,10 +94,9 @@ export function viteForAem(options: PluginOptions): PluginOption[] {
},

// Handle all other AEM based requests
'^/(aem|apps|bin|conf|content|crx|etc|etc.clientlibs|home|libs|login|mnt|system|var|(assets|editor|sites|screens)\\.html)/.*':
{
...baseProxyOptions,
},
[aemProxySegmentsExp]: {
...baseProxyOptions,
},

// Handle the initial interaction between the Vite DevServer and AEM
'^/(index.html)?$': {
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export interface PluginOptions {
*/
aem?: AemServerOptions

/**
* Define a list of AEM paths that need to be proxied.
*/
aemProxySegments?: string[]

/**
* The expression to use when matching ClientLibs on a page.
*
Expand Down

0 comments on commit e378c58

Please sign in to comment.