@@ -22,110 +22,23 @@ queue.on(`active`, () => {
2222 )
2323} )
2424
25- var findAsset = function ( src , compilation , webpackStatsJson ) {
26- if ( ! src ) {
27- var chunkNames = Object . keys ( webpackStatsJson . assetsByChunkName )
28-
29- src = chunkNames [ 0 ]
30- }
31-
32- var asset = compilation . assets [ src ]
33-
34- if ( asset ) {
35- return asset
36- }
37-
38- var chunkValue = webpackStatsJson . assetsByChunkName [ src ]
39-
40- if ( ! chunkValue ) {
41- return null
42- }
43- // Webpack outputs an array for each chunk when using sourcemaps
44- if ( chunkValue instanceof Array ) {
45- // Is the main bundle always the first element?
46- chunkValue = chunkValue . find ( function ( filename ) {
47- return / \. j s $ / . test ( filename )
48- } )
49- }
50- return compilation . assets [ chunkValue ]
51- }
52-
53- let renderMdxBody = undefined
54- class MdxHtmlBuilderWebpackPlugin {
55- apply ( compiler ) {
56- const self = this
57- var afterEmit = ( compilation , callback ) => {
58- // var options = compiler.options;
59- /* var stats = compilation.getStats().toJson({
60- * hash: true,
61- * publicPath: true,
62- * assets: true,
63- * chunks: false,
64- * modules: false,
65- * source: false,
66- * errorDetails: false,
67- * timings: false
68- * }); */
69- // console.log(Object.keys(compilation.assets));
70- var webpackStats = compilation . getStats ( )
71- var webpackStatsJson = webpackStats . toJson ( )
72-
73- try {
74- var asset = findAsset ( self . entry , compilation , webpackStatsJson )
75-
76- if ( asset == null ) {
77- throw new Error ( `Source file not found: "` + self . entry + `"` )
78- }
79-
80- var source = asset . source ( )
81- var render = evaluate (
82- source ,
83- /* filename: */ self . entry ,
84- /* scope: */ self . globals ,
85- /* includeGlobals: */ true
86- )
87-
88- if ( render . hasOwnProperty ( `default` ) ) {
89- render = render [ `default` ]
90- }
91-
92- if ( typeof render !== `function` ) {
93- throw new Error (
94- `Export from '${ self . entry } ' must be a function that returns a htmlString value.`
95- )
96- }
97- // use function here
98- renderMdxBody = render
99- callback ( )
100- } catch ( err ) {
101- compilation . errors . push ( err . stack )
102- callback ( )
103- }
104- }
105- if ( compiler . hooks ) {
106- var plugin = { name : `MdxHtmlBuilderWebpackPlugin` }
107-
108- compiler . hooks . afterEmit . tapAsync ( plugin , afterEmit )
109- } else {
110- compiler . plugin ( `after-emit` , afterEmit )
111- }
112- }
113- }
114-
11525exports . mdxHTMLLoader = ( { cache, reporter, store } ) =>
11626 new DataLoader (
11727 async keys => {
11828 const webpackConfig = cloneDeep ( store . getState ( ) . webpack )
29+ const outputPath = path . join ( cache . directory , `webpack` )
11930 // something sets externals, which will cause React to be undefined
12031 webpackConfig . externals = undefined
12132 webpackConfig . entry = require . resolve ( `./wrap-root-render-html-entry.js` )
12233 webpackConfig . output = {
12334 filename : `output.js` ,
124- path : path . join ( cache . directory , `webpack` ) ,
35+ path : outputPath ,
12536 libraryTarget : `commonjs` ,
12637 }
12738 webpackConfig . plugins = webpackConfig . plugins || [ ]
128- webpackConfig . plugins . push ( new MdxHtmlBuilderWebpackPlugin ( ) )
39+ webpackConfig . externalsPresets = {
40+ node : true ,
41+ }
12942 const compiler = webpack ( webpackConfig )
13043
13144 return queue . add (
@@ -151,6 +64,9 @@ exports.mdxHTMLLoader = ({ cache, reporter, store }) =>
15164 reporter . warn ( `gatsby-plugin-mdx\n` + info . warnings )
15265 }
15366
67+ const renderMdxBody = require ( path . join ( outputPath , `output.js` ) )
68+ . default
69+
15470 resolve (
15571 keys . map ( ( { body } ) =>
15672 renderMdxBody
0 commit comments