Skip to content

Commit e07830c

Browse files
jeetissjaredpalmer
authored andcommitted
add mdx loader (#711)
1 parent e199219 commit e07830c

File tree

9 files changed

+25768
-2
lines changed

9 files changed

+25768
-2
lines changed

package-lock.json

Lines changed: 9365 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/create-razzle-app/lib/utils/copy-dir.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ module.exports = function copyDir(opts) {
1616
return new Promise(function(resolve, reject) {
1717
const stopCopySpinner = output.wait('Copying files');
1818

19-
fs
20-
.copy(templatePath, projectPath)
19+
fs.copy(templatePath, projectPath)
2120
.then(function() {
2221
return fs.move(
2322
path.resolve(projectPath, './gitignore'),
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"env": {
3+
"jest": true,
4+
"node": true
5+
}
6+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# razzle-plugin-mdx
2+
3+
This package contains a plugin for using mdx with Razzle
4+
5+
## Usage in Razzle Projects
6+
7+
```
8+
npm i razzle-plugin-mdx
9+
```
10+
11+
or
12+
13+
```
14+
yarn add razzle-plugin-mdx
15+
```
16+
17+
18+
### Using the plugin with the default options
19+
20+
```js
21+
// razzle.config.js
22+
23+
module.exports = {
24+
plugins: ['mdx'],
25+
};
26+
```
27+
28+
### With custom options:
29+
30+
```js
31+
// razzle.config.js
32+
const images = require('remark-images')
33+
const emoji = require('remark-emoji')
34+
35+
module.exports = {
36+
plugins: [
37+
{
38+
name: 'mdx',
39+
options: {
40+
mdPlugins: [images, emoji]
41+
},
42+
},
43+
],
44+
};
45+
```
46+
47+
## Options
48+
49+
Check all the options here: [mdx-js options](https://github.com/mdx-js/mdx#options).
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
const makeLoaderFinder = require('razzle-dev-utils/makeLoaderFinder');
3+
4+
const babelLoaderFinder = makeLoaderFinder('babel-loader');
5+
const fileLoaderFinder = makeLoaderFinder('file-loader');
6+
const mdxLoaderFinder = makeLoaderFinder('@mdx-js/loader');
7+
8+
module.exports = {
9+
fileLoaderFinder,
10+
babelLoaderFinder,
11+
mdxLoaderFinder,
12+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)