Skip to content

Commit

Permalink
fix: fix @vercel/ncc bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Feb 18, 2022
1 parent 30dbb4f commit 2d5532c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 deletions telefunc/node/utils/moduleExists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { assert, nodeRequire } from '../utils'

export { moduleExists }

function moduleExists(modulePath: string, __dirname?: string): boolean {
function moduleExists(modulePath: string, dir?: string): boolean {
if (!isAbsolute(modulePath)) {
assert(__dirname)
modulePath = resolve(__dirname, modulePath)
assert(dir)
modulePath = resolve(dir, modulePath)
}
assert(isAbsolute(modulePath))

Expand Down
5 changes: 3 additions & 2 deletions telefunc/node/vite/distLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { posix } from 'path'
import type { Plugin } from 'vite'
import { assert, toPosixPath } from '../utils'
import { isSSR_config } from './utils'
const telefuncFilesGlobFromDistPath = `${__dirname}/telefuncFilesGlobFromDist.js`
const dir = __dirname + (() => '')() // trick to avoid `@vercel/ncc` to glob import
const telefuncFilesGlobFromDistPath = `${dir}/telefuncFilesGlobFromDist.js`

function distLinkOff() {
writeFileSync(telefuncFilesGlobFromDistPath, ['exports.distLinkOff = true', ''].join('\n'))
Expand All @@ -29,7 +30,7 @@ function distLinkOn(): Plugin {
}
assert(root)
// To `require()` an absolute path doesn't seem to work on Vercel
const rootRelative = posix.relative(toPosixPath(__dirname), root)
const rootRelative = posix.relative(toPosixPath(dir), root)
writeFileSync(
telefuncFilesGlobFromDistPath,
[
Expand Down
3 changes: 2 additions & 1 deletion telefunc/node/vite/importGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export { importGlobOff }
export { importGlobOn }

import { writeFileSync } from 'fs'
const telefuncFilesGlobPath = `${__dirname}/telefuncFilesGlob.js`
const dir = __dirname + (() => '')() // trick to avoid `@vercel/ncc` to glob import
const telefuncFilesGlobPath = `${dir}/telefuncFilesGlob.js`
// Pattern `*([a-zA-Z0-9])` is an Extglob: https://github.com/micromatch/micromatch#extglobs
const importGlob = 'import.meta.glob("/**/*.telefunc.*([a-zA-Z0-9])")'

Expand Down
3 changes: 2 additions & 1 deletion telefunc/node/vite/telefuncFilesGlobPath.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const telefuncFilesGlobFileNameBase = 'telefuncFilesGlob'
export const telefuncFilesGlobFilePath = `${__dirname}/${telefuncFilesGlobFileNameBase}.js`
const dir = __dirname + (() => '')() // trick to avoid `@vercel/ncc` to glob import
export const telefuncFilesGlobFilePath = `${dir}/${telefuncFilesGlobFileNameBase}.js`
3 changes: 2 additions & 1 deletion telefunc/node/webpack/install.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export { install }

import { resolve } from 'path'
const loader = resolve(__dirname, './loader.js')
const dir = __dirname + (() => '')() // trick to avoid `@vercel/ncc` to glob import
const loader = resolve(dir, './loader.js')

function install<T extends any[]>(config: { module?: { rules?: T } }) {
config.module!.rules!.push({
Expand Down

0 comments on commit 2d5532c

Please sign in to comment.