@@ -22,110 +22,23 @@ queue.on(`active`, () => {
22
22
)
23
23
} )
24
24
25
- const findAsset = function ( src , compilation , webpackStatsJson ) {
26
- if ( ! src ) {
27
- const chunkNames = Object . keys ( webpackStatsJson . assetsByChunkName )
28
-
29
- src = chunkNames [ 0 ]
30
- }
31
-
32
- const asset = compilation . assets [ src ]
33
-
34
- if ( asset ) {
35
- return asset
36
- }
37
-
38
- let 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
- const 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
- const webpackStats = compilation . getStats ( )
71
- const webpackStatsJson = webpackStats . toJson ( )
72
-
73
- try {
74
- const asset = findAsset ( self . entry , compilation , webpackStatsJson )
75
-
76
- if ( asset == null ) {
77
- throw new Error ( `Source file not found: "` + self . entry + `"` )
78
- }
79
-
80
- const source = asset . source ( )
81
- let 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
- const plugin = { name : `MdxHtmlBuilderWebpackPlugin` }
107
-
108
- compiler . hooks . afterEmit . tapAsync ( plugin , afterEmit )
109
- } else {
110
- compiler . plugin ( `after-emit` , afterEmit )
111
- }
112
- }
113
- }
114
-
115
25
exports . mdxHTMLLoader = ( { cache, reporter, store } ) =>
116
26
new DataLoader (
117
27
async keys => {
118
28
const webpackConfig = cloneDeep ( store . getState ( ) . webpack )
29
+ const outputPath = path . join ( cache . directory , `webpack` )
119
30
// something sets externals, which will cause React to be undefined
120
31
webpackConfig . externals = undefined
121
32
webpackConfig . entry = require . resolve ( `./wrap-root-render-html-entry.js` )
122
33
webpackConfig . output = {
123
34
filename : `output.js` ,
124
- path : path . join ( cache . directory , `webpack` ) ,
35
+ path : outputPath ,
125
36
libraryTarget : `commonjs` ,
126
37
}
127
38
webpackConfig . plugins = webpackConfig . plugins || [ ]
128
- webpackConfig . plugins . push ( new MdxHtmlBuilderWebpackPlugin ( ) )
39
+ webpackConfig . externalsPresets = {
40
+ node : true ,
41
+ }
129
42
const compiler = webpack ( webpackConfig )
130
43
131
44
return queue . add (
@@ -151,6 +64,9 @@ exports.mdxHTMLLoader = ({ cache, reporter, store }) =>
151
64
reporter . warn ( `gatsby-plugin-mdx\n` + info . warnings )
152
65
}
153
66
67
+ const renderMdxBody = require ( path . join ( outputPath , `output.js` ) )
68
+ . default
69
+
154
70
resolve (
155
71
keys . map ( ( { body } ) =>
156
72
renderMdxBody
0 commit comments