MCPcopy
hub / github.com/vercel/next.js / webpack4

Function webpack4

packages/react-refresh-utils/ReactRefreshWebpackPlugin.ts:33–86  ·  view source on GitHub ↗
(this: ReactFreshWebpackPlugin, compiler: WebpackCompiler)

Source from the content-addressed store, hash-verified

31}
32
33function webpack4(this: ReactFreshWebpackPlugin, compiler: WebpackCompiler) {
34 const { Template } = this
35 // Webpack 4 does not have a method to handle interception of module
36 // execution.
37 // The closest thing we have to emulating this is mimicking the behavior of
38 // `strictModuleExceptionHandling` in `MainTemplate`:
39 // https://github.com/webpack/webpack/blob/4c644bf1f7cb067c748a52614500e0e2182b2700/lib/MainTemplate.js#L200
40
41 compiler.hooks.compilation.tap('ReactFreshWebpackPlugin', (compilation) => {
42 injectRefreshFunctions(compilation, Template)
43
44 const hookRequire: any = (compilation.mainTemplate.hooks as any).require
45
46 // @ts-ignore webpack 5 types compat
47 hookRequire.tap('ReactFreshWebpackPlugin', (source: string) => {
48 // Webpack 4 evaluates module code on the following line:
49 // ```
50 // modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
51 // ```
52 // https://github.com/webpack/webpack/blob/4c644bf1f7cb067c748a52614500e0e2182b2700/lib/MainTemplate.js#L200
53
54 const lines = source.split('\n')
55 // @ts-ignore webpack 5 types compat
56 const evalIndex = lines.findIndex((l) =>
57 l.includes('modules[moduleId].call(')
58 )
59 // Unable to find the module execution, that's OK:
60 if (evalIndex === -1) {
61 return source
62 }
63
64 // Legacy CSS implementations will `eval` browser code in a Node.js
65 // context to extract CSS. For backwards compatibility, we need to check
66 // we're in a browser context before continuing.
67 return Template.asString([
68 ...lines.slice(0, evalIndex),
69 `
70 var hasRefresh = typeof self !== "undefined" && !!self.$RefreshInterceptModuleExecution$;
71 var cleanup = hasRefresh
72 ? self.$RefreshInterceptModuleExecution$(moduleId)
73 : function() {};
74 try {
75 `,
76 lines[evalIndex],
77 `
78 } finally {
79 cleanup();
80 }
81 `,
82 ...lines.slice(evalIndex + 1),
83 ])
84 })
85 })
86}
87
88function webpack5(this: ReactFreshWebpackPlugin, compiler: WebpackCompiler) {
89 const { RuntimeGlobals, RuntimeModule, Template } = this

Callers

nothing calls this directly

Calls 6

injectRefreshFunctionsFunction · 0.85
splitMethod · 0.80
includesMethod · 0.80
tapMethod · 0.45
asStringMethod · 0.45
sliceMethod · 0.45

Tested by

no test coverage detected