Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 6 additions & 5 deletions packages/rollup/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
*/

import {createFormatAwareProcessors} from '@mdx-js/mdx/internal-create-format-aware-processors'
import {extnamesToRegex} from '@mdx-js/mdx/internal-extnames-to-regex'
import {createFilter} from '@rollup/pluginutils'
import {SourceMapGenerator} from 'source-map'
import {VFile} from 'vfile'
Expand All @@ -69,6 +70,8 @@ export function rollup(options) {
const {exclude, include, ...rest} = options || {}
/** @type {FormatAwareProcessors} */
let formatAwareProcessors
/** @type {RegExp} */
let extnameRegex
const filter = createFilter(include, exclude)

return {
Expand All @@ -79,22 +82,20 @@ export function rollup(options) {
development: env.mode === 'development',
...rest
})
extnameRegex = extnamesToRegex(formatAwareProcessors.extnames)
},
async transform(value, path) {
Copy link
Member

Choose a reason for hiding this comment

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

Apparently this path variable is not really a path. Instead, it’s an ID (constructed from the path). It may contain query parameters.

I think the proper fix would be to rename this variable to id (including in the types). Then construct the path using:

const [path] = id.split('?')

The main difference is that remark/rehype/recma plugins also have access to the VFile. It’s important that it has the correct data.

if (!formatAwareProcessors) {
formatAwareProcessors = createFormatAwareProcessors({
SourceMapGenerator,
...rest
})
extnameRegex = extnamesToRegex(formatAwareProcessors.extnames)
}

const file = new VFile({path, value})

if (
file.extname &&
filter(file.path) &&
formatAwareProcessors.extnames.includes(file.extname)
) {
if (file.extname && filter(file.path) && extnameRegex.test(file.path)) {
const compiled = await formatAwareProcessors.process(file)
const code = String(compiled.value)
/** @type {SourceDescription} */
Expand Down
23 changes: 23 additions & 0 deletions packages/rollup/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,27 @@ test('@mdx-js/rollup', async function (t) {
assert.doesNotMatch(code, /jsxs?\(/)
assert.match(code, /jsxDEV\(/)
})

await t.test('should handle query parameters in vite', async () => {
const result = /** @type {Array<RollupOutput>} */ (
await build({
build: {
lib: {
entry:
fileURLToPath(new URL('vite-entry.mdx', import.meta.url)) +
'?query=param',
name: 'query'
},
write: false
},
logLevel: 'silent',
plugins: [rollupMdx()]
})
)

const code = result[0].output[0].code

assert.match(code, /Hello Vite/)
assert.match(code, /jsxs?\(/)
})
})
Loading