MCPcopy
hub / github.com/webpack/webpack / getExternalModuleNodeCommonjsInitFragment

Function getExternalModuleNodeCommonjsInitFragment

lib/ExternalModule.js:206–234  ·  view source on GitHub ↗
(
	runtimeTemplate,
	universal = false
)

Source from the content-addressed store, hash-verified

204 * @returns {InitFragment<ChunkRenderContext>} code
205 */
206const getExternalModuleNodeCommonjsInitFragment = (
207 runtimeTemplate,
208 universal = false
209) => {
210 const importMetaName = runtimeTemplate.outputOptions.importMetaName;
211 const moduleId = runtimeTemplate.renderNodePrefixForCoreModule("module");
212
213 if (!universal) {
214 return new InitFragment(
215 `import { createRequire as __WEBPACK_EXTERNAL_createRequire } from ${moduleId};\n${runtimeTemplate.renderConst()} __WEBPACK_EXTERNAL_createRequire_require = __WEBPACK_EXTERNAL_createRequire(${importMetaName}.url);\n`,
216 InitFragment.STAGE_HARMONY_IMPORTS,
217 0,
218 "external module node-commonjs"
219 );
220 }
221
222 // defensively obtain `createRequire` so the universal bundle still loads off node
223 const content = `${runtimeTemplate.renderConst()} __WEBPACK_EXTERNAL_createRequire_require = ${runtimeTemplate.getBuiltinModule(
224 moduleId,
225 `.createRequire(${importMetaName}.url)`
226 )};\n`;
227
228 return new InitFragment(
229 content,
230 InitFragment.STAGE_HARMONY_IMPORTS,
231 0,
232 "external module node-commonjs universal"
233 );
234};
235
236/**
237 * Gets source for common js external in node module.

Callers 2

applyMethod · 0.85

Calls 3

renderConstMethod · 0.80
getBuiltinModuleMethod · 0.80

Tested by

no test coverage detected