Skip to content
Merged
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
Next Next commit
Special case plain CJS requires and conditional imports using __esModule
This models if the server tries to import .default or a plain require.
We should replicate the same thing on the client when we load that
module reference.
  • Loading branch information
sebmarkbage committed Nov 30, 2020
commit d4e458555321fe1d6242c0c5042039c830d66b63
10 changes: 10 additions & 0 deletions fixtures/flight/server/handler.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ module.exports = async function(req, res) {
chunks: ['3'],
name: 'default',
},
'': {
id: './src/ShowMore.client.js',
chunks: ['3'],
name: '',
},
'*': {
id: './src/ShowMore.client.js',
chunks: ['3'],
name: '*',
},
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,16 @@ export function requireModule<T>(moduleData: ModuleReference<T>): T {
throw entry;
}
}
return __webpack_require__(moduleData.id)[moduleData.name];
const moduleExports = __webpack_require__(moduleData.id);
if (moduleData.name === '*') {
// This is a placeholder value that represents that the caller imported this
// as a CommonJS module as is.
return moduleExports;
Copy link
Collaborator Author

@sebmarkbage sebmarkbage Nov 21, 2020

Choose a reason for hiding this comment

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

I usually test CJS server paths by removing the "type": "module" definition in package.json. In this case I need to use raw CJS (require(...) + module.exports) without using Babel/Webpack. Because otherwise we fall into the case below.

I couldn't test the Webpack client paths for true CJS. I spent a few hours trying to figure out how to make the react-script config let me write raw CJS instead of transpiling from ESM and I couldn't because webpack kept treating them as ESM which means that assigning to module.exports causes and error.

I think this works though.

}
if (moduleData.name === '') {
// This is a placeholder value that represents that the caller accessed the
// default property of this if it was an ESM interop module.
return moduleExports.__esModule ? moduleExports.default : moduleExports;
}
return moduleExports[moduleData.name];
}
2 changes: 1 addition & 1 deletion scripts/flow/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ declare module 'EventListener' {
}

declare function __webpack_chunk_load__(id: string): Promise<mixed>;
declare function __webpack_require__(id: string): {default: any};
declare function __webpack_require__(id: string): any;