Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Dec 14, 2023
1 parent a085605 commit 5e4d30e
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 73 deletions.
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

3 changes: 1 addition & 2 deletions telefunc/node/vite/importGlob/telefuncFilesGlobPath.ts
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`
2 changes: 1 addition & 1 deletion telefunc/node/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function plugin(configUser?: ConfigUser): any {
transform(),
commonConfig(),
...devConfig(),
...buildConfig(),
buildConfig(),
retrieveDevServer(),
packageJsonFile(),
...importBuild(),
Expand Down
64 changes: 10 additions & 54 deletions telefunc/node/vite/plugins/buildConfig.ts
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
}
15 changes: 7 additions & 8 deletions telefunc/node/vite/plugins/importBuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { importBuild as importBuild_ } from '@brillout/vite-plugin-import-build/
import type { Plugin, ResolvedConfig } from 'vite'
import { assert, assertPosixPath, getOutDirAbsolute, projectInfo, toPosixPath } from '../../utils'
import path from 'path'
import { telefuncFilesGlobFileNameBase } from '../../importGlob/telefuncFilesGlobFileNameBase'
import { getTelefuncManifest } from './getTelefuncManifest'
import { telefuncFilesGlobFilePath } from '../../importGlob/telefuncFilesGlobPath'

function importBuild(): Plugin[] {
let config: ResolvedConfig
Expand All @@ -18,26 +18,25 @@ function importBuild(): Plugin[] {
}
},
importBuild_({
getImporterCode: ({ findBuildEntry }) => {
const telefuncFilesEntry = findBuildEntry(telefuncFilesGlobFileNameBase)
return getImporterCode(config, telefuncFilesEntry)
getImporterCode: () => {
return getImporterCode(config)
},
libraryName: projectInfo.projectName
})
]
}

function getImporterCode(config: ResolvedConfig, telefuncFilesEntry: string) {
function getImporterCode(config: ResolvedConfig) {
const importPath = getImportPath(config)

const telefuncManifest = getTelefuncManifest()

// console.log(`\n importPath: ${importPath}\n outDirServer: ${outDirServer}\n importPathAbsolute: ${importPathAbsolute}\n config.build.outDir: ${config.build.outDir}`)
const importerCode = [
'{',
` const { setLoaders } = require('${importPath}');`,
' setLoaders({',
` loadTelefuncFiles: () => import('./${telefuncFilesEntry}'),`,
` import { setTelfuncLoaders } import '${importPath}';`,
' setTelfuncLoaders({',
` loadTelefuncFiles: () => import('${telefuncFilesGlobFilePath}'),`,
` loadManifest: () => (${JSON.stringify(telefuncManifest, null, 2)})`,
' });',
'}',
Expand Down
4 changes: 2 additions & 2 deletions telefunc/node/vite/plugins/importBuild/loadBuild.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { loadTelefuncFilesWithImportBuild }
export { setLoaders }
export { setTelfuncLoaders }

import { config } from '../../../server/serverConfig'
import { getGlobalObject } from '../../utils'
Expand All @@ -12,7 +12,7 @@ const globalObject = getGlobalObject<{
type LoadTelefuncFiles = () => Promise<unknown>
type LoadManifest = () => Record<string, unknown>

function setLoaders({
function setTelfuncLoaders({
loadTelefuncFiles,
loadManifest
}: {
Expand Down
2 changes: 1 addition & 1 deletion telefunc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"dependencies": {
"@brillout/import": "^0.2.3",
"@brillout/json-serializer": "^0.5.6",
"@brillout/vite-plugin-import-build": "^0.2.22",
"@brillout/vite-plugin-import-build": "0.2.22-commit-0fb79b3",
"es-module-lexer": "^0.7.1",
"picocolors": "^1.0.0",
"ts-morph": "^19.0.0"
Expand Down

0 comments on commit 5e4d30e

Please sign in to comment.