-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
26 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import { telefuncFilesGlobFileNameBase } from './telefuncFilesGlobFileNameBase' | ||
let dir: string | ||
try { | ||
dir = __dirname + (() => '')() // trick to avoid `@vercel/ncc` to glob import | ||
} catch { | ||
dir = '__telefunc_NOT_AVAILABLE' | ||
} | ||
export const telefuncFilesGlobFilePath = `${dir}/${telefuncFilesGlobFileNameBase}.js` | ||
export const telefuncFilesGlobFilePath = `${dir}/telefuncFilesGlob.js` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,21 @@ | ||
export { buildConfig } | ||
|
||
import type { Plugin, ResolvedConfig } from 'vite' | ||
import type { InputOption } from 'rollup' | ||
import { telefuncFilesGlobFileNameBase } from '../importGlob/telefuncFilesGlobFileNameBase' | ||
import { telefuncFilesGlobFilePath } from '../importGlob/telefuncFilesGlobPath' | ||
import { assert, assertUsage, isObject, determineOutDir } from '../utils' | ||
import { determineOutDir } from '../utils' | ||
|
||
function buildConfig(): Plugin[] { | ||
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) | ||
} | ||
return { | ||
name: 'telefunc:buildConfig', | ||
apply: 'build', | ||
enforce: 'post', | ||
configResolved(config_) { | ||
config = config_ | ||
setOutDir(config) | ||
} | ||
] | ||
} | ||
} | ||
|
||
function setOutDir(config: ResolvedConfig) { | ||
const outDir = determineOutDir(config) | ||
if (outDir) config.build.outDir = outDir | ||
} | ||
|
||
function addRollupInput(config: ResolvedConfig) { | ||
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 name1 = `${telefuncFilesGlobFileNameBase}.js` | ||
const name2 = `${telefuncFilesGlobFileNameBase}.mjs` | ||
const entries = Object.keys(rollupBundle) | ||
assertUsage( | ||
entries.includes(name1) || entries.includes(name2), | ||
"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 {} | ||
} | ||
if (typeof input === 'string') { | ||
input = [input] | ||
} | ||
if (Array.isArray(input)) { | ||
return Object.fromEntries(input.map((input) => [input, input])) | ||
} | ||
assert(isObject(input)) | ||
return input | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters