Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch @typescript/vfs to remove Critical dependency: the request of a dependency is an expression warnings #3330

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@
} 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 @@
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
}
Loading