Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add processor cache in webpack loader #1912

Merged
merged 1 commit into from Jan 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 20 additions & 9 deletions packages/loader/lib/index.js
@@ -1,12 +1,18 @@
/**
* @typedef {import('vfile').VFileCompatible} VFileCompatible
* @typedef {import('vfile').VFile} VFile
* @typedef {import('@mdx-js/mdx').CompileOptions} CompileOptions
* @typedef {Pick<CompileOptions, 'SourceMapGenerator'>} Defaults
* @typedef {Omit<CompileOptions, 'SourceMapGenerator'>} Options
* @typedef {import('webpack').LoaderContext<unknown>} LoaderContext
* @typedef {(vfileCompatible: VFileCompatible) => Promise<VFile>} Process
*/

import {SourceMapGenerator} from 'source-map'
import {compile} from '@mdx-js/mdx'
import {createFormatAwareProcessors} from '@mdx-js/mdx/lib/util/create-format-aware-processors.js'

/** @type {WeakMap<CompileOptions, Process>} */
const cache = new WeakMap()

/**
* A Webpack (5+) loader for MDX.
Expand All @@ -21,20 +27,25 @@ export function loader(value, callback) {
/** @type {Defaults} */
const defaults = this.sourceMap ? {SourceMapGenerator} : {}
const options = /** @type {CompileOptions} */ (this.getOptions())
const config = {...defaults, ...options}
Copy link
Member

Choose a reason for hiding this comment

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

The config object is constructed here. This is always a new object by reference. It’s then used as a key in the weak map cache. Once the loader is done processing a file, the config object is unused and garbage collected, meaning it’s removed from the cache.

This means the cache is never used.

If options is always the same object, that could be used.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is options a reference to the same object? Seems to weird that this.getOptions() would do that.

Copy link
Member

Choose a reason for hiding this comment

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

I don’t know. That’s why I wrote if.

I think it might be the same object if options are passed to the loader as an object, but not when they are passed as a query parameter. I’m really just guessing here though.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can still use this.query though?

Copy link
Member Author

Choose a reason for hiding this comment

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

…assuming that worked before, you tested it?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I tested it back then. If query is the raw object, that’s probably our best option. Just beware it might also be a string, which can’t be used as a key for weak maps.

It was heavily inspired by ts-loader. They use a similar approach here.

Also when I implemented it, this was compatible with Webpack 4. The use of getOptions() indicate this is no longer the case. I don’t know if the use of Webpack 5 opens up better alternatives.

Copy link
Member Author

Choose a reason for hiding this comment

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

ts-loader has a similar approach but caches on webpack compilers. Should we use those instead?

Copy link
Member

Choose a reason for hiding this comment

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

I think it doesn’t affect most users, as most users instantiate a single compiler, but it’s still a good idea.

Copy link
Member Author

Choose a reason for hiding this comment

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


/* Removed option. */
/* c8 ignore next 5 */
if ('renderer' in options) {
if ('renderer' in config) {
throw new Error(
'`options.renderer` is no longer supported. Please see <https://mdxjs.com/migrating/v2/> for more information'
)
}

compile({value, path: this.resourcePath}, {...defaults, ...options}).then(
(file) => {
callback(null, file.value, file.map)
return file
},
callback
)
let process = cache.get(config)

if (!process) {
process = createFormatAwareProcessors(config).process
cache.set(config, process)
}

process({value, path: this.resourcePath}).then((file) => {
callback(null, file.value, file.map)
return file
}, callback)
}