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
35 changes: 30 additions & 5 deletions package-lock.json

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

83 changes: 32 additions & 51 deletions packages/rollup/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/**
* @import {FormatAwareProcessors} from '@mdx-js/mdx/internal-create-format-aware-processors'
* @import {CompileOptions} from '@mdx-js/mdx'
* @import {FilterPattern} from '@rollup/pluginutils'
* @import {SourceDescription} from 'rollup'
* @import * as vite from 'vite'
*/

/**
* @typedef {Omit<CompileOptions, 'SourceMapGenerator'>} ApplicableOptions
* Applicable compile configuration.
*
* @typedef {string | RegExp | Array<string | RegExp>} FilterPattern
* A Rollup filter pattern.
*
* @typedef ExtraOptions
* Extra configuration.
* @property {FilterPattern | null | undefined} [exclude]
Expand All @@ -23,37 +25,9 @@
* Plugin that is compatible with both Rollup and Vite.
* @property {string} name
* The name of the plugin
* @property {ViteConfig} config
* Function used by Vite to set additional configuration options.
* @property {Transform} transform
* Function to transform the source content.
*
* @callback Transform
* Callback called by Rollup and Vite to transform.
* @param {string} value
* File contents.
* @param {string} id
* Module ID.
* @returns {Promise<SourceDescription | undefined>}
* Result.
*
* @callback ViteConfig
* Callback called by Vite to set additional configuration options.
* @param {unknown} config
* Configuration object (unused).
* @param {ViteEnv} env
* Environment variables.
* @returns {undefined}
* Nothing.
*
* @typedef ViteEnv
* Environment variables used by Vite.
* @property {string} mode
* Mode.
*/

import {createFormatAwareProcessors} from '@mdx-js/mdx/internal-create-format-aware-processors'
import {createFilter} from '@rollup/pluginutils'
import {SourceMapGenerator} from 'source-map'
import {VFile} from 'vfile'

Expand All @@ -69,9 +43,9 @@ export function rollup(options) {
const {exclude, include, ...rest} = options || {}
/** @type {FormatAwareProcessors} */
let formatAwareProcessors
const filter = createFilter(include, exclude)

return {
/** @type {vite.Plugin<never>} */
const plugin = {
name: '@mdx-js/rollup',
config(config, env) {
formatAwareProcessors = createFormatAwareProcessors({
Expand All @@ -80,28 +54,35 @@ export function rollup(options) {
...rest
})
},
async transform(value, id) {
if (!formatAwareProcessors) {
formatAwareProcessors = createFormatAwareProcessors({
SourceMapGenerator,
...rest
})
}
transform: {
filter: {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Actually it looks like support for filter was added in Rollup 4.38.0 (rollup/rollup#5882). So we can’t use it without a breaking change.

id: {
include: /** @type {FilterPattern} */ (include),
exclude: /** @type {FilterPattern} */ (exclude)
}
},
async handler(value, id) {
if (!formatAwareProcessors) {
formatAwareProcessors = createFormatAwareProcessors({
SourceMapGenerator,
...rest
})
}

const [path] = id.split('?')
const file = new VFile({path, value})
const [path] = id.split('?')
const file = new VFile({path, value})

if (
file.extname &&
filter(file.path) &&
formatAwareProcessors.extnames.includes(file.extname)
) {
const compiled = await formatAwareProcessors.process(file)
const code = String(compiled.value)
/** @type {SourceDescription} */
const result = {code, map: compiled.map}
return result
if (
file.extname &&
formatAwareProcessors.extnames.includes(file.extname)
) {
const compiled = await formatAwareProcessors.process(file)
const code = String(compiled.value)
return {code, map: compiled.map}
}
}
}
}

return plugin
}
6 changes: 2 additions & 4 deletions packages/rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@
],
"dependencies": {
"@mdx-js/mdx": "^3.0.0",
"@rollup/pluginutils": "^5.0.0",
"source-map": "^0.7.0",
"vfile": "^6.0.0"
},
"peerDependencies": {
"rollup": ">=2"
"devDependencies": {
"rollup": "^4.0.0"
},
"devDependencies": {},
"scripts": {
"test": "npm run test-coverage",
"test-api": "node --conditions development test/index.js",
Expand Down
Loading