0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-24 02:07:30 +01:00

chore(build): validate if exporting is correct in package.json and jsr.json (#3638)

* feat(build): for both exports to be the same

* some fix

* fix exclude of coverage

* update

* stylish error message and add comment

* revert auto lint

* chore: format
This commit is contained in:
EdamAmex 2024-11-07 17:37:53 +09:00 committed by GitHub
parent 48d2adcf73
commit a6ccfa29ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 91 additions and 5 deletions

View File

@ -7,14 +7,15 @@
/// <reference types="bun-types/bun" /> /// <reference types="bun-types/bun" />
import fs, { write } from 'fs'
import path from 'path'
import arg from 'arg' import arg from 'arg'
import { $, stdout } from 'bun'
import { build } from 'esbuild' import { build } from 'esbuild'
import type { Plugin, PluginBuild, BuildOptions } from 'esbuild' import type { Plugin, PluginBuild, BuildOptions } from 'esbuild'
import * as glob from 'glob' import * as glob from 'glob'
import fs from 'fs'
import path from 'path'
import { removePrivateFields } from './remove-private-fields' import { removePrivateFields } from './remove-private-fields'
import { $, stdout } from 'bun' import { validateExports } from './validate-exports'
const args = arg({ const args = arg({
'--watch': Boolean, '--watch': Boolean,
@ -22,6 +23,14 @@ const args = arg({
const isWatch = args['--watch'] || false const isWatch = args['--watch'] || false
const readJsonExports = (path: string) => JSON.parse(fs.readFileSync(path, 'utf-8')).exports
const [packageJsonExports, jsrJsonExports] = ['./package.json', './jsr.json'].map(readJsonExports)
// Validate exports of package.json and jsr.json
validateExports(packageJsonExports, jsrJsonExports, 'jsr.json')
validateExports(jsrJsonExports, packageJsonExports, 'package.json')
const entryPoints = glob.sync('./src/**/*.ts', { const entryPoints = glob.sync('./src/**/*.ts', {
ignore: ['./src/**/*.test.ts', './src/mod.ts', './src/middleware.ts', './src/deno/**/*.ts'], ignore: ['./src/**/*.test.ts', './src/mod.ts', './src/middleware.ts', './src/deno/**/*.ts'],
}) })

View File

@ -0,0 +1,31 @@
/// <reference types="vitest/globals" />
import { validateExports } from './validate-exports'
const mockExports1 = {
'./a': './a.ts',
'./b': './b.ts',
'./c/a': './c.ts',
'./d/*': './d/*.ts',
}
const mockExports2 = {
'./a': './a.ts',
'./b': './b.ts',
'./c/a': './c.ts',
'./d/a': './d/a.ts',
}
const mockExports3 = {
'./a': './a.ts',
'./c/a': './c.ts',
'./d/*': './d/*.ts',
}
describe('validateExports', () => {
it('Works', async () => {
expect(() => validateExports(mockExports1, mockExports1, 'package.json')).not.toThrowError()
expect(() => validateExports(mockExports1, mockExports2, 'jsr.json')).not.toThrowError()
expect(() => validateExports(mockExports1, mockExports3, 'package.json')).toThrowError()
})
})

37
build/validate-exports.ts Normal file
View File

@ -0,0 +1,37 @@
export const validateExports = (
source: Record<string, unknown>,
target: Record<string, unknown>,
fileName: string
) => {
const isEntryInTarget = (entry: string): boolean => {
if (entry in target) {
return true
}
// e.g., "./utils/*" -> "./utils"
const wildcardPrefix = entry.replace(/\/\*$/, '')
if (entry.endsWith('/*')) {
return Object.keys(target).some(
(targetEntry) =>
targetEntry.startsWith(wildcardPrefix + '/') && targetEntry !== wildcardPrefix
)
}
const separatedEntry = entry.split('/')
while (separatedEntry.length > 0) {
const pattern = `${separatedEntry.join('/')}/*`
if (pattern in target) {
return true
}
separatedEntry.pop()
}
return false
}
Object.keys(source).forEach((sourceEntry) => {
if (!isEntryInTarget(sourceEntry)) {
throw new Error(`Missing "${sourceEntry}" in '${fileName}'`)
}
})
}

View File

@ -80,6 +80,7 @@
"./testing": "./src/helper/testing/index.ts", "./testing": "./src/helper/testing/index.ts",
"./dev": "./src/helper/dev/index.ts", "./dev": "./src/helper/dev/index.ts",
"./ws": "./src/helper/websocket/index.ts", "./ws": "./src/helper/websocket/index.ts",
"./conninfo": "./src/helper/conninfo/index.ts",
"./utils/body": "./src/utils/body.ts", "./utils/body": "./src/utils/body.ts",
"./utils/buffer": "./src/utils/buffer.ts", "./utils/buffer": "./src/utils/buffer.ts",
"./utils/color": "./src/utils/color.ts", "./utils/color": "./src/utils/color.ts",

View File

@ -39,6 +39,11 @@
"import": "./dist/index.js", "import": "./dist/index.js",
"require": "./dist/cjs/index.js" "require": "./dist/cjs/index.js"
}, },
"./request": {
"types": "./dist/types/request.d.ts",
"import": "./dist/request.js",
"require": "./dist/cjs/request.js"
},
"./types": { "./types": {
"types": "./dist/types/types.d.ts", "types": "./dist/types/types.d.ts",
"import": "./dist/types.js", "import": "./dist/types.js",
@ -387,6 +392,9 @@
}, },
"typesVersions": { "typesVersions": {
"*": { "*": {
"request": [
"./dist/types/request"
],
"types": [ "types": [
"./dist/types/types" "./dist/types/types"
], ],

View File

@ -8,7 +8,7 @@ export default defineConfig({
}, },
test: { test: {
globals: true, globals: true,
include: ['**/src/**/(*.)+(spec|test).+(ts|tsx|js)', '**/scripts/**/(*.)+(spec|test).+(ts|tsx|js)'], include: ['**/src/**/(*.)+(spec|test).+(ts|tsx|js)', '**/scripts/**/(*.)+(spec|test).+(ts|tsx|js)', '**/build/**/(*.)+(spec|test).+(ts|tsx|js)'],
exclude: [...configDefaults.exclude, '**/sandbox/**', '**/*.case.test.+(ts|tsx|js)'], exclude: [...configDefaults.exclude, '**/sandbox/**', '**/*.case.test.+(ts|tsx|js)'],
setupFiles: ['./.vitest.config/setup-vitest.ts'], setupFiles: ['./.vitest.config/setup-vitest.ts'],
coverage: { coverage: {
@ -20,7 +20,7 @@ export default defineConfig({
...(configDefaults.coverage.exclude ?? []), ...(configDefaults.coverage.exclude ?? []),
'benchmarks', 'benchmarks',
'runtime-tests', 'runtime-tests',
'build.ts', 'build/build.ts',
'src/test-utils', 'src/test-utils',
'perf-measures', 'perf-measures',