Skip to content

Commit

Permalink
Special case plain CJS requires and conditional imports using __esModule
Browse files Browse the repository at this point in the history
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 21, 2020
1 parent 0e002b9 commit 5451e71
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
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;
}
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;

0 comments on commit 5451e71

Please sign in to comment.