-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Flight] Minimal webpack plugin #20228
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
|
||
# production | ||
/build | ||
/dist | ||
|
||
# misc | ||
.DS_Store | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,20 @@ | ||
'use strict'; | ||
|
||
import {pipeToNodeWritable} from 'react-transport-dom-webpack/server'; | ||
import {readFileSync} from 'fs'; | ||
import {resolve} from 'path'; | ||
import * as React from 'react'; | ||
|
||
import url from 'url'; | ||
|
||
function resolve(path) { | ||
return url.pathToFileURL(require.resolve(path)).href; | ||
} | ||
|
||
module.exports = async function(req, res) { | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
const m = await import('../src/App.server.js'); | ||
// const m = require('../src/App.server.js'); | ||
const App = m.default.default || m.default; | ||
pipeToNodeWritable(<App />, res, { | ||
// TODO: Read from a map on the disk. | ||
[resolve('../src/Counter.client.js')]: { | ||
Counter: { | ||
id: './src/Counter.client.js', | ||
chunks: ['2'], | ||
name: 'Counter', | ||
}, | ||
}, | ||
[resolve('../src/Counter2.client.js')]: { | ||
Counter: { | ||
id: './src/Counter2.client.js', | ||
chunks: ['1'], | ||
name: 'Counter', | ||
}, | ||
}, | ||
[resolve('../src/ShowMore.client.js')]: { | ||
default: { | ||
id: './src/ShowMore.client.js', | ||
chunks: ['3'], | ||
name: 'default', | ||
}, | ||
'': { | ||
id: './src/ShowMore.client.js', | ||
chunks: ['3'], | ||
name: '', | ||
}, | ||
'*': { | ||
id: './src/ShowMore.client.js', | ||
chunks: ['3'], | ||
name: '*', | ||
}, | ||
}, | ||
}); | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
const moduleMap = JSON.parse( | ||
readFileSync( | ||
resolve(__dirname, '../dist/react-transport-manifest.json'), | ||
'utf8' | ||
) | ||
); | ||
pipeToNodeWritable(<App />, res, moduleMap); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,42 @@ | |
* @flow | ||
*/ | ||
|
||
import {mkdirSync, writeFileSync} from 'fs'; | ||
import {dirname, resolve} from 'path'; | ||
import {pathToFileURL} from 'url'; | ||
|
||
export default class ReactFlightWebpackPlugin { | ||
constructor(options: {isServer: boolean}) {} | ||
apply(compiler: any) {} | ||
|
||
apply(compiler: any) { | ||
compiler.hooks.emit.tap('React Transport Plugin', compilation => { | ||
const json = {}; | ||
compilation.chunks.forEach(chunk => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand correctly, in webpack the same module might be duplicated between multiple chunks. Due to forEach here, the last set will win. I don't know what's a good way to pick but this seems like something to be worked out by the person productionizing this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name is associated with the root/entry dependency, not the file. So we'll model the dependency which then points to a chunk. Webpack will decide for us where that dependency will point. |
||
chunk.getModules().forEach(mod => { | ||
if (!/\.client\.js$/.test(mod.resource)) { | ||
return; | ||
} | ||
const moduleExports = {}; | ||
['', '*'].concat(mod.buildMeta.providedExports).forEach(name => { | ||
moduleExports[name] = { | ||
id: mod.id, | ||
chunks: chunk.ids, | ||
name: name, | ||
}; | ||
}); | ||
const href = pathToFileURL(mod.resource).href; | ||
if (href !== undefined) { | ||
json[href] = moduleExports; | ||
} | ||
}); | ||
}); | ||
const output = JSON.stringify(json, null, 2); | ||
const filename = resolve( | ||
compiler.options.output.path, | ||
'react-transport-manifest.json', | ||
); | ||
mkdirSync(dirname(filename), {recursive: true}); | ||
writeFileSync(filename, output); | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might be because it's default webpack output name and CRA only overrides it for prod builds. So we get the default here.