Skip to content

Commit

Permalink
Merge branch 'main' into feat/ws
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Mar 17, 2024
2 parents 372f183 + 7cdbd1a commit c7b5c60
Show file tree
Hide file tree
Showing 22 changed files with 445 additions and 234 deletions.
2 changes: 1 addition & 1 deletion cli/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const { until } = require('@open-draft/until')
const confirm = require('@inquirer/confirm')
const confirm = require('@inquirer/confirm').default
const invariant = require('./invariant')
const { SERVICE_WORKER_BUILD_PATH } = require('../config/constants')

Expand Down
18 changes: 13 additions & 5 deletions config/replaceCoreImports.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
function replaceCoreImports(fileContents, isEsm) {
const importPattern = isEsm
? /from ["'](~\/core(.*))["'](;)?$/gm
: /require\(["'](~\/core(.*))["']\)(;)?/gm
const CORE_ESM_IMPORT_PATTERN = /from ["'](~\/core(.*))["'](;)?/gm
const CORE_CJS_IMPORT_PATTERN = /require\(["'](~\/core(.*))["']\)(;)?/gm

function getCoreImportPattern(isEsm) {
return isEsm ? CORE_ESM_IMPORT_PATTERN : CORE_CJS_IMPORT_PATTERN
}

function hasCoreImports(fileContents, isEsm) {
return getCoreImportPattern(isEsm).test(fileContents)
}

function replaceCoreImports(fileContents, isEsm) {
return fileContents.replace(
importPattern,
getCoreImportPattern(isEsm),
(_, __, maybeSubmodulePath, maybeSemicolon) => {
const submodulePath = maybeSubmodulePath || '/index'
const semicolon = maybeSemicolon || ''
Expand All @@ -17,5 +24,6 @@ function replaceCoreImports(fileContents, isEsm) {
}

module.exports = {
hasCoreImports,
replaceCoreImports,
}
107 changes: 92 additions & 15 deletions config/scripts/patch-ts.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,106 @@
const fs = require('fs')
const path = require('path')
const { replaceCoreImports } = require('../replaceCoreImports')
const fs = require('node:fs')
const { exec } = require('node:child_process')
const path = require('node:path')
const { promisify } = require('node:util')
const { invariant } = require('outvariant')
const glob = require('glob')
const { hasCoreImports, replaceCoreImports } = require('../replaceCoreImports')

const execAsync = promisify(exec)

const BUILD_DIR = path.resolve(__dirname, '../../lib')

async function patchTypeDefs() {
const typeDefsPaths = [
path.resolve(__dirname, '../..', 'lib/browser/index.d.ts'),
path.resolve(__dirname, '../..', 'lib/node/index.d.ts'),
path.resolve(__dirname, '../..', 'lib/native/index.d.ts'),
]
const typeDefsPaths = glob.sync('**/*.d.{ts,mts}', {
cwd: BUILD_DIR,
absolute: true,
})
const typeDefsWithCoreImports = typeDefsPaths
.map((modulePath) => {
const fileContents = fs.readFileSync(modulePath, 'utf8')
if (hasCoreImports(fileContents, true)) {
return [modulePath, fileContents]
}
})
.filter(Boolean)

for (const typeDefsPath of typeDefsPaths) {
if (!fs.existsSync(typeDefsPath)) {
continue
}
if (typeDefsWithCoreImports.length === 0) {
console.log(
'Found no .d.ts modules containing the "~/core" import, skipping...',
)
return process.exit(0)
}

const fileContents = fs.readFileSync(typeDefsPath, 'utf8')
console.log(
'Found %d module(s) with the "~/core" import, resolving...',
typeDefsWithCoreImports.length,
)

for (const [typeDefsPath, fileContents] of typeDefsWithCoreImports) {
// Treat ".d.ts" files as ESM to replace "import" statements.
// Force no extension on the ".d.ts" imports.
const nextFileContents = replaceCoreImports(fileContents, true)

fs.writeFileSync(typeDefsPath, nextFileContents, 'utf8')
console.log('Successfully patched "%s"!', typeDefsPath)
}

console.log(
'Imports resolved in %d file(s), verifying...',
typeDefsWithCoreImports.length,
)

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

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

console.log('Successfully patched at "%s"!', typeDefsPath)
if (result.stdout !== '') {
const modulesWithUnresolvedImports = result.stdout
.split('\n')
.filter(Boolean)

console.error(
`Found .d.ts modules containing unresolved "~/core" import after the patching:
${modulesWithUnresolvedImports.map((path) => ` - ${path}`).join('\n')}
`,
)

return process.exit(1)
}

// Ensure that the .d.ts files compile without errors after resolving the "~/core" imports.
console.log('Compiling the .d.ts modules with tsc...')
const tscCompilation = await execAsync(
`tsc --noEmit --skipLibCheck ${typeDefsPaths.join(' ')}`,
{
cwd: BUILD_DIR,
},
)

if (tscCompilation.stderr !== '') {
console.error(
'Failed to compile the .d.ts modules with tsc. See the original error below.',
tscCompilation.stderr,
)

return process.exit(1)
}

console.log(
'The "~/core" imports resolved successfully in %d .d.ts modules! 🎉',
typeDefsWithCoreImports.length,
)
}

patchTypeDefs()
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@
"url": "https://github.com/kettanaito"
},
"license": "MIT",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/mswjs"
},
"funding": "https://github.com/sponsors/mswjs",
"files": [
"config/constants.js",
"config/scripts/postinstall.js",
Expand Down Expand Up @@ -147,7 +144,7 @@
"@commitlint/config-conventional": "^18.4.4",
"@fastify/websocket": "^8.3.1",
"@open-draft/test-server": "^0.4.2",
"@ossjs/release": "^0.8.0",
"@ossjs/release": "^0.8.1",
"@playwright/test": "^1.40.1",
"@swc/core": "^1.3.102",
"@types/express": "^4.17.21",
Expand All @@ -156,8 +153,8 @@
"@types/json-bigint": "^1.0.4",
"@types/node": "18.x",
"@types/ws": "^8.5.10",
"@typescript-eslint/eslint-plugin": "^5.11.0",
"@typescript-eslint/parser": "^5.11.0",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"@web/dev-server": "^0.1.38",
"axios": "^1.6.5",
"babel-minify": "^0.5.1",
Expand All @@ -167,7 +164,7 @@
"cz-conventional-changelog": "3.3.0",
"esbuild": "^0.19.11",
"esbuild-loader": "^4.0.2",
"eslint": "^8.56.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"express": "^4.18.2",
Expand Down
Loading

0 comments on commit c7b5c60

Please sign in to comment.