|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { babelLoaderFinder, fileLoaderFinder } = require('./helpers'); |
| 4 | + |
| 5 | +const defaultOptions = {}; |
| 6 | + |
| 7 | +function modify(baseConfig, { target, dev }, webpack, userOptions = {}) { |
| 8 | + const options = Object.assign({}, defaultOptions, userOptions); |
| 9 | + const config = Object.assign({}, baseConfig); |
| 10 | + |
| 11 | + config.resolve.extensions = [...config.resolve.extensions, '.md', '.mdx']; |
| 12 | + |
| 13 | + // Safely locate Babel-Loader in Razzle's webpack internals |
| 14 | + const babelLoader = config.module.rules.find(babelLoaderFinder); |
| 15 | + if (!babelLoader) { |
| 16 | + throw new Error(`'babel-loader' required for nice 'MDX loader' work`); |
| 17 | + } |
| 18 | + |
| 19 | + // Don't import md and mdx files with file-loader |
| 20 | + const fileLoader = config.module.rules.find(fileLoaderFinder); |
| 21 | + fileLoader.exclude = [/\.mdx?$/, ...fileLoader.exclude]; |
| 22 | + |
| 23 | + // Get the correct `include` option, since that hasn't changed. |
| 24 | + // This tells Razzle which directories to transform. |
| 25 | + const { include } = babelLoader; |
| 26 | + |
| 27 | + // Configure @mdx-js/loader |
| 28 | + const mdxLoader = { |
| 29 | + include, |
| 30 | + test: /\.mdx?$/, |
| 31 | + use: [ |
| 32 | + ...babelLoader.use, |
| 33 | + { |
| 34 | + loader: require.resolve('@mdx-js/loader'), |
| 35 | + options: Object.assign({}, options.mdxLoader), |
| 36 | + }, |
| 37 | + ], |
| 38 | + }; |
| 39 | + |
| 40 | + config.module.rules.push(mdxLoader); |
| 41 | + |
| 42 | + return config; |
| 43 | +} |
| 44 | + |
| 45 | +module.exports = modify; |
0 commit comments