Skip to content

Commit

Permalink
Use assets API + writeToDisk instead of directly writing to disk (fac…
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage authored and koto committed Jun 15, 2021
1 parent 3889916 commit d47c7d0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
3 changes: 3 additions & 0 deletions fixtures/flight/config/webpackDevServer.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ module.exports = function(proxy, allowedHost) {
watchOptions: {
ignored: ignoredFiles(paths.appSrc),
},
writeToDisk: filePath => {
return /react-client-manifest\.json$/.test(filePath);
},
https: getHttpsConfig(),
host,
overlay: false,
Expand Down
2 changes: 1 addition & 1 deletion fixtures/flight/server/handler.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function(req, res) {
import('../src/App.server.js').then(m => {
const dist = process.env.NODE_ENV === 'development' ? 'dist' : 'build';
readFile(
resolve(__dirname, `../${dist}/react-transport-manifest.json`),
resolve(__dirname, `../${dist}/react-client-manifest.json`),
'utf8',
(err, data) => {
if (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* @flow
*/

import {mkdirSync, writeFileSync} from 'fs';
import {dirname, resolve, join} from 'path';
import {join} from 'path';
import {pathToFileURL} from 'url';

import asyncLib from 'neo-async';
Expand Down Expand Up @@ -48,13 +47,16 @@ type Options = {
isServer: boolean,
clientReferences?: ClientReferencePath | $ReadOnlyArray<ClientReferencePath>,
chunkName?: string,
manifestFilename?: string,
};

const PLUGIN_NAME = 'React Transport Plugin';

export default class ReactFlightWebpackPlugin {
clientReferences: $ReadOnlyArray<ClientReferencePath>;
chunkName: string;
manifestFilename: string;

constructor(options: Options) {
if (!options || typeof options.isServer !== 'boolean') {
throw new Error(
Expand Down Expand Up @@ -88,6 +90,8 @@ export default class ReactFlightWebpackPlugin {
} else {
this.chunkName = 'client[index]';
}
this.manifestFilename =
options.manifestFilename || 'react-client-manifest.json';
}

apply(compiler: any) {
Expand Down Expand Up @@ -189,13 +193,14 @@ export default class ReactFlightWebpackPlugin {
});
});
const output = JSON.stringify(json, null, 2);
const filename = resolve(
compiler.options.output.path,
'react-transport-manifest.json',
);
mkdirSync(dirname(filename), {recursive: true});
// TODO: Use webpack's emit API and read from the devserver.
writeFileSync(filename, output);
compilation.assets[this.manifestFilename] = {
source() {
return output;
},
size() {
return output.length;
},
};
});
}

Expand Down

0 comments on commit d47c7d0

Please sign in to comment.