Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Prettier & Linting
  • Loading branch information
samcooke98 committed Dec 30, 2020
commit 61d3dc8fdbda38aa4069b8de8d543c9dde632b92
39 changes: 19 additions & 20 deletions packages/react-server-dom-webpack/src/ReactFlightWebpackPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import ModuleDependency from 'webpack/lib/dependencies/ModuleDependency';
import NullDependency from 'webpack/lib/dependencies/NullDependency';
import AsyncDependenciesBlock from 'webpack/lib/AsyncDependenciesBlock';
import Template from 'webpack/lib/Template';
import { RawSource } from 'webpack-sources'
import {RawSource} from 'webpack-sources';

class ClientReferenceDependency extends ModuleDependency {
constructor(request) {
Expand Down Expand Up @@ -103,11 +103,14 @@ export default class ReactFlightWebpackPlugin {

pluginOutput = {};

recordModule = (id: string, {
getExports,
resource,
chunkIds,
}: { getExports: () => string[], resource: string, chunkIds: string[] }) => {
recordModule = (
id: string,
{
getExports,
resource,
chunkIds,
}: {getExports: () => string[], resource: string, chunkIds: string[]},
) => {
// TODO: Hook into deps instead of the target module.
// That way we know by the type of dep whether to include.
// It also resolves conflicts when the same module is in multiple chunks.
Expand All @@ -129,10 +132,10 @@ export default class ReactFlightWebpackPlugin {
if (href !== undefined) {
this.pluginOutput[href] = moduleExports;
}
}
};

//
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//

output(compilation) {
output(compilation) {
const output = JSON.stringify(this.pluginOutput, null, 2);
compilation.emitAsset(this.manifestFilename, new RawSource(output));
}
Expand Down Expand Up @@ -162,10 +165,6 @@ export default class ReactFlightWebpackPlugin {
);
};

const manifestFilename = this.manifestFilename;
// To target both webpack v4 & webpack v5, we have to listen to certain hooks.
let hooksInstalled = false;

compiler.hooks.run.tapAsync(PLUGIN_NAME, run);
compiler.hooks.watchRun.tapAsync(PLUGIN_NAME, run);
compiler.hooks.compilation.tap(
Expand Down Expand Up @@ -212,7 +211,7 @@ export default class ReactFlightWebpackPlugin {
});

// ProcessAssests hook is only supported by webpack v5.
if(this.webpackVersion !== 'wp5') {
if (this.webpackVersion !== 'wp5') {
return;
}

Expand All @@ -226,12 +225,12 @@ export default class ReactFlightWebpackPlugin {
const chunkGraph = compilation.chunkGraph;
const moduleGraph = compilation.moduleGraph;

compilation.chunkGroups.forEach((chunkGroup) => {
compilation.chunkGroups.forEach(chunkGroup => {
const chunkIds = chunkGroup.chunks.map(function(c) {
return c.id;
});
chunkGroup.chunks.forEach((chunk) => {
chunk.getModules().forEach((mod) => {
chunkGroup.chunks.forEach(chunk => {
chunk.getModules().forEach(mod => {
const id = chunkGraph.getModuleId(mod);
this.recordModule(id, {
getExports: () => moduleGraph.getProvidedExports(mod),
Expand All @@ -240,9 +239,10 @@ export default class ReactFlightWebpackPlugin {
});
// If this is a concatenation, register each child to the parent ID.
if (mod.modules) {
mod.modules.forEach((concatenatedMod) => {
mod.modules.forEach(concatenatedMod => {
this.recordModule(id, {
getExports: () => moduleGraph.getProvidedExports(concatenatedMod),
getExports: () =>
moduleGraph.getProvidedExports(concatenatedMod),
resource: concatenatedMod.resource,
chunkIds,
});
Expand All @@ -252,7 +252,7 @@ export default class ReactFlightWebpackPlugin {
});
});

this.output(compilation)
this.output(compilation);
},
);
},
Expand Down Expand Up @@ -290,7 +290,6 @@ export default class ReactFlightWebpackPlugin {

this.output(compilation);
});

});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,14 @@ describe('ReactFlightWebpackPlugin', () => {
mode: 'development',
});



const originalFileSystem = output.outputFileSystem;

output.outputFileSystem = {
...originalFileSystem,
writeFile: jest.fn((dest, contents, cb) => {
// Call the callback, but don't actually write anything.
cb(null);
})
}),
};

output.run((err, stats) => {
Expand All @@ -106,18 +104,20 @@ describe('ReactFlightWebpackPlugin', () => {
expect(output.outputFileSystem.writeFile).toHaveBeenCalledWith(
expect.stringContaining(fileName),
expect.anything(),
expect.anything()
expect.anything(),
);
const calls = output.outputFileSystem.writeFile.mock.calls;
// Get the idx that was called with the fileName,
const idx = calls.findIndex(val => {
return val[0].includes(fileName);
})
});

const contents = output.outputFileSystem.writeFile.mock.calls[idx][1].toString();
const contents = output.outputFileSystem.writeFile.mock.calls[
idx
][1].toString();

// Check that the contents match with what we expect
assert(contents)
assert(contents);
done();
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export function Form() {
console.log('Form Rendered!')
}
console.log('Form Rendered!');
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


export default function Foo() {
console.log('Form!')
}
console.log('Form!');
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@

const ClientComponent = require('./Form.client').Form;
const ServerComponent = require('./FormServer.server.js');

console.log(
ClientComponent,
ServerComponent
);
console.log(ClientComponent, ServerComponent);