Skip to content
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
85 changes: 50 additions & 35 deletions config/scripts/patch-ts.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
import fs from 'node:fs'
import { exec } from 'node:child_process'
import { promisify } from 'node:util'
import { invariant } from 'outvariant'
import { fileURLToPath } from 'node:url'
import path from 'path'
import * as glob from 'glob'
import { hasCoreImports, replaceCoreImports } from '../replaceCoreImports.js'

const execAsync = promisify(exec)

const BUILD_DIR = new URL('../../lib/', import.meta.url)

function searchFilesForPattern(filePattern, searchPattern, errorMessage) {
const filePaths = glob.sync(filePattern, {
cwd: BUILD_DIR,
absolute: true,
posix: true,
dotRelative: true,
})

let matchingFiles = []

try {
matchingFiles = filePaths.filter((path) => {
const fileContents = fs.readFileSync(path, 'utf8')
return fileContents.includes(searchPattern)
})
} catch (error) {
console.error(errorMessage, error)
process.exit(1)
}

return matchingFiles
}

function getRelativePaths(paths) {
return paths.map((p) => {
const normalisedFilePath = p
.replace(/^\/\/\?\//, '')
.replace(/\\/g, path.sep)
return path.relative(fileURLToPath(BUILD_DIR), normalisedFilePath)
})
}

async function patchTypeDefs() {
const typeDefsPaths = glob.sync('**/*.d.{ts,mts}', {
cwd: BUILD_DIR,
absolute: true,
posix: true,
dotRelative: true,
})
const typeDefsWithCoreImports = typeDefsPaths
.map((modulePath) => {
Expand Down Expand Up @@ -57,29 +92,19 @@ async function patchTypeDefs() {
)

// Next, validate that we left no "~/core" imports unresolved.
const result = await execAsync(
`grep "~/core" ./**/*.d.{ts,mts} -R -l || exit 0`,
{
cwd: BUILD_DIR,
shell: '/bin/bash',
},
)

invariant(
result.stderr === '',
const modulesWithUnresolvedImports = searchFilesForPattern(
'**/*.d.{ts,mts}',
'~/core',
'Failed to validate the .d.ts modules for the presence of the "~/core" import. See the original error below.',
result.stderr,
)

if (result.stdout !== '') {
const modulesWithUnresolvedImports = result.stdout
.split('\n')
.filter(Boolean)

if (modulesWithUnresolvedImports.length > 0) {
console.error(
`Found .d.ts modules containing unresolved "~/core" import after the patching:
${modulesWithUnresolvedImports.map((path) => ` - ${new URL(path, BUILD_DIR).pathname}`).join('\n')}
${getRelativePaths(modulesWithUnresolvedImports)
.map((p) => ` - ${p}`)
.join('\n')}
`,
)

Expand All @@ -105,29 +130,19 @@ ${modulesWithUnresolvedImports.map((path) => ` - ${new URL(path, BUILD_DIR).pat
}

// Ensure that CJS .d.ts file never reference .mjs files.
const mjsInCjsResult = await execAsync(
`grep ".mjs" ./**/*.d.ts -R -l || exit 0`,
{
cwd: BUILD_DIR,
shell: '/bin/bash',
},
)

invariant(
mjsInCjsResult.stderr === '',
const mjsInCjsResults = searchFilesForPattern(
'**/*.d.ts',
'.mjs',
'Failed to validate the .d.ts modules not referencing ".mjs" files. See the original error below.',
mjsInCjsResult.stderr,
)

if (mjsInCjsResult.stdout !== '') {
const modulesWithUnresolvedImports = mjsInCjsResult.stdout
.split('\n')
.filter(Boolean)

if (mjsInCjsResults.length > 0) {
console.error(
`Found .d.ts modules referencing ".mjs" files after patching:
${modulesWithUnresolvedImports.map((path) => ` - ${new URL(path, BUILD_DIR).pathname}`).join('\n')}
${getRelativePaths(mjsInCjsResults)
.map((p) => ` - ${p}`)
.join('\n')}
`,
)

Expand Down
7 changes: 6 additions & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ const SERVICE_WORKER_CHECKSUM = getWorkerChecksum()
const shimConfig: Options = {
name: 'shims',
platform: 'neutral',
entry: glob.sync('./src/shims/**/*.ts'),
entry: glob.sync('./src/shims/**/*.ts', {
posix: true,
dotRelative: true,
}),
format: ['esm', 'cjs'],
noExternal: Object.keys(packageJson.dependencies),
outDir: './lib/shims',
Expand All @@ -43,6 +46,8 @@ const coreConfig: Options = {
platform: 'neutral',
entry: glob.sync('./src/core/**/*.ts', {
ignore: '**/*.test.ts',
posix: true,
dotRelative: true,
}),
external: [ecosystemDependencies],
noExternal: ['cookie'],
Expand Down