Skip to content

Commit decafe9

Browse files
committed
Add processor cache in webpack loader
Related-to: mdx-js/mdx#1468.
1 parent 94fc1df commit decafe9

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

lib/integration/webpack.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
/**
2+
* @typedef {import('vfile').VFileCompatible} VFileCompatible
3+
* @typedef {import('vfile').VFile} VFile
24
* @typedef {import('webpack').LoaderContext<unknown>} LoaderContext
35
* @typedef {import('../compile.js').CompileOptions} CompileOptions
46
* @typedef {Pick<CompileOptions, 'SourceMapGenerator'>} Defaults
57
* @typedef {Omit<CompileOptions, 'SourceMapGenerator'>} Options
8+
* @typedef {(vfileCompatible: VFileCompatible) => Promise<VFile>} Process
69
*/
710

811
import {SourceMapGenerator} from 'source-map'
9-
import {compile} from '../compile.js'
12+
import {createFormatAwareProcessors} from '../util/create-format-aware-processors.js'
13+
14+
/** @type {WeakMap<CompileOptions, Process>} */
15+
const cache = new WeakMap()
1016

1117
/**
1218
* A Webpack (4+) loader for xdm.
@@ -21,11 +27,16 @@ export function loader(value, callback) {
2127
/** @type {Defaults} */
2228
const defaults = this.sourceMap ? {SourceMapGenerator} : {}
2329
const options = /** @type {CompileOptions} */ (this.getOptions())
24-
compile({value, path: this.resourcePath}, {...defaults, ...options}).then(
25-
(file) => {
26-
callback(null, file.value, file.map)
27-
return file
28-
},
29-
callback
30-
)
30+
const config = {...defaults, ...options}
31+
let process = cache.get(config)
32+
33+
if (!process) {
34+
process = createFormatAwareProcessors(config).process
35+
cache.set(config, process)
36+
}
37+
38+
process({value, path: this.resourcePath}).then((file) => {
39+
callback(null, file.value, file.map)
40+
return file
41+
}, callback)
3142
}

0 commit comments

Comments
 (0)