Skip to content

Commit

Permalink
Merge pull request #242 from nextcloud-libraries/backport/240/stable1
Browse files Browse the repository at this point in the history
[stable1] feat(EmptyJSDirPlugin): Allow to specify other directories to clear
  • Loading branch information
susnux authored Jul 12, 2024
2 parents b3634aa + 9e07b8e commit 3a68f36
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
16 changes: 12 additions & 4 deletions lib/appConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { mergeConfig } from 'vite'
import { createBaseConfig } from './baseConfig.js'
import { findAppinfo } from './utils/appinfo.js'

import EmptyJSDirPlugin from './plugins/EmptyJSDir.js'
import EmptyJSDirPlugin, { EmptyJSDirPluginOptions } from './plugins/EmptyJSDir.js'
import replace from '@rollup/plugin-replace'
import injectCSSPlugin from 'vite-plugin-css-injected-by-js'
import { CSSEntryPointsPlugin } from './plugins/CSSEntryPoints.js'
Expand Down Expand Up @@ -49,10 +49,12 @@ export interface AppOptions extends Omit<BaseOptions, 'inlineCSS'> {
createEmptyCSSEntryPoints?: boolean

/**
* Whether to empty the output directory (`js/`)
* Whether to empty the 'js' directory
* Pass `false` to disable clearing the directory,
* it is also possible to pass options to the plugin.
* @default true
*/
emptyOutputDirectory?: boolean
emptyOutputDirectory?: boolean | EmptyJSDirPluginOptions

/**
* Inject polyfills for node packages
Expand Down Expand Up @@ -146,7 +148,13 @@ export const createAppConfig = (entries: { [entryAlias: string]: string }, optio
// defaults to true so only not adding if explicitly set to false
if (options?.emptyOutputDirectory !== false) {
// Ensure `js/` is empty as we can not use the build in option (see below)
plugins.push(EmptyJSDirPlugin())
plugins.push(
EmptyJSDirPlugin(
typeof options.emptyOutputDirectory === 'object'
? options.emptyOutputDirectory
: undefined
),
)
}

// When building in serve mode (e.g. unit tests with vite) the intro option below will be ignored, so we must replace that values
Expand Down
22 changes: 19 additions & 3 deletions lib/plugins/EmptyJSDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,34 @@ import type { Plugin } from 'vite'
import { existsSync, rmSync } from 'node:fs'
import * as path from 'node:path'

export interface EmptyJSDirPluginOptions {
/**
* Additional directories to clear (e.g. 'css')
* @default []
*/
additionalDirectories?: string[]
}

/**
* Vite plugin to clear the `js/` directory before emitting files
*
* @param options Options to pass to the plugin
*/
const EmptyJSDirPlugin = () => {
function EmptyJSDirPlugin(options?: EmptyJSDirPluginOptions) {
let emptyJSDir = false

return {
name: 'nextcloud-empty-js',
generateBundle() {
if (emptyJSDir !== true) {
if (existsSync(path.resolve('js/'))) {
rmSync(path.resolve('js/'), { recursive: true })
const dirs = [
...(options?.additionalDirectories ?? []),
'js',
]
for (const dir of dirs) {
if (existsSync(path.resolve(dir))) {
rmSync(path.resolve(dir), { recursive: true })
}
}
emptyJSDir = true
}
Expand Down

0 comments on commit 3a68f36

Please sign in to comment.