Skip to content

Commit

Permalink
patch @typescript/vfs to remove `Critical dependency: the request o…
Browse files Browse the repository at this point in the history
…f a dependency is an expression` warnings
  • Loading branch information
dimaMachina committed Sep 30, 2024
1 parent fb2fc08 commit 23af82b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-seas-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nextra': patch
---

patch `@typescript/vfs` to remove `Critical dependency: the request of a dependency is an expression` warnings
37 changes: 37 additions & 0 deletions packages/nextra/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-env node */
import fs from 'node:fs'

Check failure on line 2 in packages/nextra/src/server/index.ts

View workflow job for this annotation

GitHub Actions / lint

'node:fs' import is restricted from being used by a pattern. Use `graceful-fs` instead
import { createRequire } from 'node:module'
import { join, sep } from 'node:path'
import type { NextConfig } from 'next'
import { fromZodError } from 'zod-validation-error'
Expand All @@ -20,6 +22,8 @@ const DEFAULT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx']

const AGNOSTIC_PAGE_MAP_PATH = `.next${sep}static${sep}chunks${sep}nextra-page-map`

const require = createRequire(import.meta.url)

const nextra: Nextra = nextraConfig => {
const { error } = nextraConfigSchema.safeParse(nextraConfig)
if (error) {
Expand Down Expand Up @@ -136,8 +140,14 @@ const nextra: Nextra = nextraConfig => {
} else {
alias[join(alias.next, 'dist', 'pages', '_app')] = appESM
}
// alias[require.resolve('@typescript/vfs')] = require.resolve('@typescript/vfs/dist/vfs.esm.patched.js')
// alias[require.resolve('@typescript/vfs/dist/vfs.cjs.development.js')] = require.resolve('@typescript/vfs/dist/vfs.cjs.development.patched.js')
// alias[require.resolve('@typescript/vfs/dist/vfs.cjs.production.min.js')] = require.resolve('@typescript/vfs/dist/vfs.cjs.production.min.patched.js')
// alias[require.resolve('@typescript/vfs/dist/vfs.esm.js')] = require.resolve('@typescript/vfs/dist/vfs.esm.patched.js')
const rules = config.module.rules as RuleSetRule[]

patchTypeScriptVfs()

if (IMPORT_FRONTMATTER) {
rules.push({
test: MARKDOWN_EXTENSION_REGEX,
Expand Down Expand Up @@ -226,3 +236,30 @@ type RuleSetRule = {
export default nextra

export type * from '../types'

let isTsVfsPatched = false

function patchTypeScriptVfs(): void {
if (isTsVfsPatched) return
const tsVfs = require.resolve('@typescript/vfs')
const tsVfsDir = join(tsVfs, '..')

const paths = {
dev: join(tsVfsDir, 'vfs.cjs.development.js'),
prod: join(tsVfsDir, 'vfs.cjs.production.min.js'),
esm: join(tsVfsDir, 'vfs.esm.js')
}

for (const filePath of Object.values(paths)) {
const sourceCode = fs.readFileSync(filePath, 'utf8')
if (sourceCode.includes('String.fromCharCode')) {
fs.writeFileSync(
filePath, //.replace(/\.js$/, '.patched.js'),
sourceCode
.replace(/String\.fromCharCode\(112, ?97, ?116, ?104\)/, '"path"')
.replace(/String\.fromCharCode\(102, ?115\)/, '"fs"')
)
}
}
isTsVfsPatched = true
}

0 comments on commit 23af82b

Please sign in to comment.