Skip to content

Commit

Permalink
fix: improve error message upon third-party Vite plugin conflicting w…
Browse files Browse the repository at this point in the history
…ith Telefunc
  • Loading branch information
brillout committed Nov 18, 2022
1 parent 6201ca0 commit 98cec22
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion telefunc/node/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function plugin(config?: ConfigUser): any {
transform(),
commonConfig(),
...devConfig(),
buildConfig(),
...buildConfig(),
retrieveDevServer(),
packageJsonFile(),
...importBuild(),
Expand Down
46 changes: 32 additions & 14 deletions telefunc/node/vite/plugins/buildConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@ import type { Plugin, ResolvedConfig } from 'vite'
import type { InputOption } from 'rollup'
import { telefuncFilesGlobFileNameBase } from '../importGlob/telefuncFilesGlobFileNameBase'
import { telefuncFilesGlobFilePath } from '../importGlob/telefuncFilesGlobPath'
import { assert, isObject, determineOutDir } from '../utils'
import { assert, assertUsage, isObject, determineOutDir } from '../utils'

function buildConfig(): Plugin {
return {
name: 'telefunc:buildConfig',
apply: 'build',
enforce: 'post',
configResolved(config) {
setOutDir(config)
addRollupInput(config)
function buildConfig(): Plugin[] {
let config: ResolvedConfig
return [
{
name: 'telefunc:buildConfig',
apply: 'build',
enforce: 'post',
configResolved(config_) {
config = config_
setOutDir(config)
addRollupInput(config)
}
},
{
name: 'telefunc:buildConfig:assert',
apply: 'build',
enforce: 'pre',
generateBundle(_rollupOptions, rollupBundle) {
assertRollupInput(rollupBundle, config)
}
}
}
]
}

function setOutDir(config: ResolvedConfig) {
Expand All @@ -24,13 +36,19 @@ function setOutDir(config: ResolvedConfig) {
}

function addRollupInput(config: ResolvedConfig) {
if (!config.build?.ssr) {
return
}
if (!config.build?.ssr) return
config.build.rollupOptions.input = normalizeRollupInput(config.build.rollupOptions.input)
config.build.rollupOptions.input[telefuncFilesGlobFileNameBase] = telefuncFilesGlobFilePath
}

function assertRollupInput(rollupBundle: Record<string, unknown>, config: ResolvedConfig) {
if (!config.build?.ssr) return
const rollupInputEntries = Object.keys(rollupBundle)
assertUsage(
rollupInputEntries.includes(`${telefuncFilesGlobFileNameBase}.js`) ||
rollupInputEntries.includes(`${telefuncFilesGlobFileNameBase}.mjs`),
"You seem to be using a tool that conflicts with Telefunc. Reach out to a Telefunc maintainer. (Info for maintainer: couldn't find Telefunc's Rollup input entry.)"
)
}
function normalizeRollupInput(input?: InputOption): Record<string, string> {
if (!input) {
return {}
Expand Down

0 comments on commit 98cec22

Please sign in to comment.