Skip to content

Commit

Permalink
Refactor tsconfig.json
Browse files Browse the repository at this point in the history
Closes GH-2250.
  • Loading branch information
wooorm committed Feb 9, 2023
1 parent a5728c7 commit 8f85b30
Show file tree
Hide file tree
Showing 59 changed files with 553 additions and 278 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Expand Up @@ -98,7 +98,6 @@
"remark-squeeze-paragraphs": "^5.0.0",
"remark-strip-badges": "^6.0.0",
"remark-toc": "^8.0.0",
"rimraf": "^3.0.0",
"rodemirror": "^2.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
Expand All @@ -119,7 +118,6 @@
"scripts": {
"postinstall": "patch-package",
"prepare": "husky install",
"clean": "npm exec -c \"rimraf node_modules\" --workspaces",
"docs-prep": "node --unhandled-rejections=strict website/prep.js && postcss docs/_asset/index.css -o public/index.css",
"docs-bundle-dev": "cross-env NODE_ENV=development node --unhandled-rejections=strict website/bundle.js",
"docs-bundle-prod": "cross-env NODE_ENV=production node --unhandled-rejections=strict website/bundle.js",
Expand Down
28 changes: 16 additions & 12 deletions packages/esbuild/lib/index.js
Expand Up @@ -7,10 +7,12 @@
* @typedef {import('esbuild').Message} Message
* @typedef {import('vfile').VFileValue} VFileValue
* @typedef {import('vfile-message').VFileMessage} VFileMessage
* @typedef {import('unist').Point} Point
* @typedef {import('@mdx-js/mdx/lib/core.js').ProcessorOptions} ProcessorOptions
*
* @typedef {ProcessorOptions & {allowDangerousRemoteMdx?: boolean}} Options
*/

/**
* @typedef {ProcessorOptions & {allowDangerousRemoteMdx?: boolean | null | undefined}} Options
* Configuration.
*/

import assert from 'node:assert'
Expand All @@ -32,11 +34,11 @@ const p = process
/**
* Compile MDX w/ esbuild.
*
* @param {Options} [options]
* @param {Options | null | undefined} [options]
* @return {Plugin}
*/
export function esbuild(options = {}) {
const {allowDangerousRemoteMdx, ...rest} = options
export function esbuild(options) {
const {allowDangerousRemoteMdx, ...rest} = options || {}
const name = '@mdx-js/esbuild'
const remoteNamespace = name + '-remote'
const {extnames, process} = createFormatAwareProcessors(rest)
Expand Down Expand Up @@ -123,22 +125,24 @@ export function esbuild(options = {}) {
}

/**
* @param {Omit<OnLoadArgs, 'pluginData'> & {pluginData?: {contents?: string|Buffer}}} data
* @param {Omit<OnLoadArgs, 'pluginData'> & {pluginData?: {contents?: Buffer | string | null | undefined}}} data
* @returns {Promise<OnLoadResult>}
*/
async function onload(data) {
/** @type {string} */
const doc = String(
data.pluginData && data.pluginData.contents !== undefined
data.pluginData &&
data.pluginData.contents !== null &&
data.pluginData.contents !== undefined
? data.pluginData.contents
: /* eslint-disable-next-line security/detect-non-literal-fs-filename */
await fs.readFile(data.path)
)

let file = new VFile({value: doc, path: data.path})
/** @type {VFileValue|undefined} */
/** @type {VFileValue | undefined} */
let value
/** @type {Array<VFileMessage|Error>} */
/** @type {Array<Error | VFileMessage>} */
let messages = []
/** @type {Array<Message>} */
const errors = []
Expand All @@ -150,7 +154,7 @@ export function esbuild(options = {}) {
value = file.value
messages = file.messages
} catch (error_) {
const error = /** @type {VFileMessage|Error} */ (error_)
const error = /** @type {Error | VFileMessage} */ (error_)
if ('fatal' in error) error.fatal = true
messages.push(error)
}
Expand Down Expand Up @@ -225,7 +229,7 @@ export function esbuild(options = {}) {
// V8 on Erbium.
/* c8 ignore next 9 */
return {
contents: value,
contents: value || '',
errors,
warnings,
resolveDir: http.test(file.path)
Expand Down
2 changes: 1 addition & 1 deletion packages/esbuild/package.json
Expand Up @@ -56,7 +56,7 @@
},
"scripts": {
"prepack": "npm run build",
"build": "rimraf \"lib/**/*.d.ts\" \"test/**/*.d.ts\" \"*.d.ts\" && tsc && type-coverage",
"build": "tsc --build --clean && tsc --build && type-coverage",
"test-api": "uvu test \"\\.js$\"",
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
"test": "npm run build && npm run test-coverage"
Expand Down
4 changes: 1 addition & 3 deletions packages/esbuild/test/index.test.js
@@ -1,11 +1,10 @@
/**
* @typedef {import('esbuild').BuildFailure} BuildFailure
* @typedef {import('esbuild').Message} Message
* @typedef {import('hast').Root} Root
* @typedef {import('vfile').VFile} VFile
* @typedef {import('mdx/types.js').MDXContent} MDXContent
*
* @typedef {import('remark-mdx')}
* @typedef {import('remark-mdx')} DoNotTouchIncludeMathInTree
*/

import {promises as fs} from 'fs'
Expand Down Expand Up @@ -251,7 +250,6 @@ test('@mdx-js/esbuild', async () => {
assert.ok(text && text.type === 'text')
const jsx = head.children[1] // JSX in heading
assert.ok(jsx && jsx.type === 'mdxJsxTextElement')
console.log(head)
file.message('1')
file.message('2', eol)
file.message('3', tree)
Expand Down
3 changes: 2 additions & 1 deletion packages/esbuild/tsconfig.json
@@ -1,4 +1,5 @@
{
"extends": "../../tsconfig.json",
"include": ["lib/**/*.js", "test/**/*.js", "index.js"]
"include": ["**/*.cjs", "**/*.js", "**/*.jsx"],
"exclude": ["coverage/", "node_modules/"]
}
5 changes: 5 additions & 0 deletions packages/loader/index.cjs
@@ -1,10 +1,15 @@
/**
* @typedef {import('webpack').LoaderContext<unknown>} LoaderContext
*/

'use strict'

/**
* Webpack loader
*
* @todo once webpack supports ESM loaders, remove this wrapper.
*
* @this {LoaderContext}
* @param {string} code
*/
module.exports = function (code) {
Expand Down
7 changes: 7 additions & 0 deletions packages/loader/index.d.cts
@@ -0,0 +1,7 @@
// Some TS versions use this file, some `index.d.ts`.
type LoaderContext = import('webpack').LoaderContext<unknown>

declare function mdxLoader(this: LoaderContext, code: string): void
export = mdxLoader

export type Options = import('@mdx-js/mdx/lib/core.js').ProcessorOptions
1 change: 1 addition & 0 deletions packages/loader/index.d.ts
@@ -1,3 +1,4 @@
// Some TS versions use this file, some `index.d.cts`.
import type {ProcessorOptions} from '@mdx-js/mdx/lib/core.js'
import type {LoaderContext} from 'webpack'

Expand Down
24 changes: 18 additions & 6 deletions packages/loader/lib/index.js
@@ -1,13 +1,23 @@
/**
* @typedef {import('@mdx-js/mdx').CompileOptions} CompileOptions
* @typedef {import('vfile').VFileCompatible} VFileCompatible
* @typedef {import('vfile').VFile} VFile
* @typedef {import('vfile-message').VFileMessage} VFileMessage
* @typedef {import('@mdx-js/mdx').CompileOptions} CompileOptions
* @typedef {Pick<CompileOptions, 'SourceMapGenerator'>} Defaults
* @typedef {Omit<CompileOptions, 'SourceMapGenerator'>} Options
* @typedef {import('webpack').LoaderContext<unknown>} LoaderContext
* @typedef {import('webpack').Compiler} WebpackCompiler
* @typedef {(vfileCompatible: VFileCompatible) => Promise<VFile>} Process
*/

/**
* @typedef {Pick<CompileOptions, 'SourceMapGenerator'>} Defaults
* @typedef {Omit<CompileOptions, 'SourceMapGenerator'>} Options
* Configuration.
*
* @callback Process
* Process.
* @param {VFileCompatible} vfileCompatible
* Input.
* @returns {Promise<VFile>}
* File.
*/

import {createHash} from 'node:crypto'
Expand All @@ -30,7 +40,7 @@ const cache = new WeakMap()
*
* @this {LoaderContext}
* @param {string} value
* @param {(error: Error|null|undefined, content?: string|Buffer, map?: Object) => void} callback
* @param {LoaderContext['callback']} callback
*/
export function loader(value, callback) {
/** @type {Defaults} */
Expand Down Expand Up @@ -69,7 +79,9 @@ export function loader(value, callback) {

process({value, path: this.resourcePath}).then(
(file) => {
callback(null, file.value, file.map || undefined)
// @ts-expect-error: `webpack` is not compiled with `exactOptionalPropertyTypes`,
// so it does not allow `file.map` to be `undefined` here.
callback(null, file.value, file.map)
},
(/** @type VFileMessage */ error) => {
const fpath = path.relative(this.context, this.resourcePath)
Expand Down
7 changes: 4 additions & 3 deletions packages/loader/package.json
Expand Up @@ -39,8 +39,9 @@
"types": "index.d.ts",
"files": [
"lib/",
"index.d.ts",
"index.cjs"
"index.cjs",
"index.d.cts",
"index.d.ts"
],
"dependencies": {
"@mdx-js/mdx": "^2.0.0",
Expand All @@ -65,7 +66,7 @@
},
"scripts": {
"prepack": "npm run build",
"build": "rimraf \"lib/**/*.d.ts\" \"test/**/*.d.ts\" && tsc && type-coverage",
"build": "tsc --build --clean && tsc --build && type-coverage",
"test-api": "uvu test \"\\.js$\"",
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
"test": "npm run build && npm run test-coverage"
Expand Down
1 change: 0 additions & 1 deletion packages/loader/test/index.test.js
Expand Up @@ -2,7 +2,6 @@
* @typedef {import('mdx/types.js').MDXContent} MDXContent
* @typedef {import('preact').FunctionComponent<unknown>} PreactComponent
* @typedef {import('vue').Component} VueComponent
* @typedef {import('vue').SetupContext} SetupContext
*/

import {promises as fs} from 'fs'
Expand Down
3 changes: 2 additions & 1 deletion packages/loader/tsconfig.json
@@ -1,4 +1,5 @@
{
"extends": "../../tsconfig.json",
"include": ["lib/**/*.js", "test/**/*.js"]
"include": ["**/*.cjs", "**/*.js", "**/*.jsx", "index.d.ts", "index.d.cts"],
"exclude": ["coverage/", "node_modules/"]
}
19 changes: 15 additions & 4 deletions packages/mdx/lib/compile.js
@@ -1,14 +1,21 @@
/**
* @typedef {import('vfile').VFileCompatible} VFileCompatible
* @typedef {import('vfile').VFile} VFile
* @typedef {import('vfile').VFileCompatible} VFileCompatible
* @typedef {import('./core.js').PluginOptions} PluginOptions
* @typedef {import('./core.js').BaseProcessorOptions} BaseProcessorOptions
*/

/**
* @typedef {Omit<BaseProcessorOptions, 'format'>} CoreProcessorOptions
* Core configuration.
*
* @typedef ExtraOptions
* @property {'detect'|'mdx'|'md'} [format='detect'] Format of `file`
* Extra configuration.
* @property {'detect' | 'mdx' | 'md' | null | undefined} [format='detect']
* Format of `file`.
*
* @typedef {CoreProcessorOptions & PluginOptions & ExtraOptions} CompileOptions
* Configuration.
*/

import {createProcessor} from './core.js'
Expand All @@ -20,8 +27,10 @@ import {resolveFileAndOptions} from './util/resolve-file-and-options.js'
* @param {VFileCompatible} vfileCompatible
* MDX document to parse (`string`, `Buffer`, `vfile`, anything that can be
* given to `vfile`).
* @param {CompileOptions} [compileOptions]
* @param {CompileOptions | null | undefined} [compileOptions]
* Compile configuration.
* @return {Promise<VFile>}
* File.
*/
export function compile(vfileCompatible, compileOptions) {
const {file, options} = resolveFileAndOptions(vfileCompatible, compileOptions)
Expand All @@ -34,8 +43,10 @@ export function compile(vfileCompatible, compileOptions) {
* @param {VFileCompatible} vfileCompatible
* MDX document to parse (`string`, `Buffer`, `vfile`, anything that can be
* given to `vfile`).
* @param {CompileOptions} [compileOptions]
* @param {CompileOptions | null | undefined} [compileOptions]
* Compile configuration.
* @return {VFile}
* File.
*/
export function compileSync(vfileCompatible, compileOptions) {
const {file, options} = resolveFileAndOptions(vfileCompatible, compileOptions)
Expand Down

1 comment on commit 8f85b30

@vercel
Copy link

@vercel vercel bot commented on 8f85b30 Feb 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mdx – ./

mdx-mdx.vercel.app
mdxjs.com
v2.mdxjs.com
mdx-git-main-mdx.vercel.app

Please sign in to comment.