1 | const path = require('path');
|
2 | const validateOptions = require('schema-utils');
|
3 | const webpack = require('webpack');
|
4 | const {
|
5 | createRefreshTemplate,
|
6 | getSocketIntegration,
|
7 | injectRefreshEntry,
|
8 | normalizeOptions,
|
9 | } = require('./helpers');
|
10 | const { errorOverlay, initSocket, refreshUtils } = require('./runtime/globals');
|
11 | const schema = require('./options.json');
|
12 |
|
13 | class ReactRefreshPlugin {
|
14 | |
15 |
|
16 |
|
17 | constructor(options = {}) {
|
18 | validateOptions(schema, options, {
|
19 | name: 'React Refresh Plugin',
|
20 | baseDataPath: 'options',
|
21 | });
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 | this.options = normalizeOptions(options);
|
28 | }
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 | apply(compiler) {
|
36 |
|
37 | if (
|
38 |
|
39 |
|
40 | (compiler.options.mode !== 'development' ||
|
41 |
|
42 |
|
43 | (process.env.NODE_ENV && process.env.NODE_ENV === 'production')) &&
|
44 | !this.options.forceEnable
|
45 | ) {
|
46 | return;
|
47 | }
|
48 |
|
49 |
|
50 | compiler.options.entry = injectRefreshEntry(compiler.options.entry, this.options);
|
51 |
|
52 |
|
53 | let providedModules = {
|
54 | [refreshUtils]: require.resolve('./runtime/refreshUtils'),
|
55 | };
|
56 |
|
57 | if (this.options.overlay === false) {
|
58 |
|
59 | const definePlugin = new webpack.DefinePlugin({ [errorOverlay]: false });
|
60 | definePlugin.apply(compiler);
|
61 | } else {
|
62 | providedModules = {
|
63 | ...providedModules,
|
64 | [errorOverlay]: require.resolve(this.options.overlay.module),
|
65 | [initSocket]: getSocketIntegration(this.options.overlay.sockIntegration),
|
66 | };
|
67 | }
|
68 |
|
69 | const providePlugin = new webpack.ProvidePlugin(providedModules);
|
70 | providePlugin.apply(compiler);
|
71 |
|
72 | compiler.hooks.beforeRun.tap(this.constructor.name, (compiler) => {
|
73 |
|
74 |
|
75 | if (
|
76 | !compiler.options.plugins ||
|
77 | !compiler.options.plugins.find(
|
78 |
|
79 |
|
80 | (plugin) => plugin.constructor.name === 'HotModuleReplacementPlugin'
|
81 | )
|
82 | ) {
|
83 | throw new Error(
|
84 | 'Hot Module Replacement (HMR) is not enabled! React-refresh requires HMR to function properly.'
|
85 | );
|
86 | }
|
87 | });
|
88 |
|
89 | const matchObject = webpack.ModuleFilenameHelpers.matchObject.bind(undefined, this.options);
|
90 | compiler.hooks.normalModuleFactory.tap(this.constructor.name, (nmf) => {
|
91 | nmf.hooks.afterResolve.tap(this.constructor.name, (data) => {
|
92 |
|
93 | if (
|
94 |
|
95 | matchObject(data.resource) &&
|
96 |
|
97 |
|
98 | !data.resource.includes(path.join(__dirname, './overlay')) &&
|
99 | !data.resource.includes(path.join(__dirname, './runtime'))
|
100 | ) {
|
101 | data.loaders.unshift({
|
102 | loader: require.resolve('./loader'),
|
103 | options: undefined,
|
104 | });
|
105 | }
|
106 |
|
107 | return data;
|
108 | });
|
109 | });
|
110 |
|
111 | compiler.hooks.compilation.tap(this.constructor.name, (compilation) => {
|
112 | compilation.mainTemplate.hooks.require.tap(
|
113 | this.constructor.name,
|
114 |
|
115 | (source, chunk, hash) => {
|
116 | const mainTemplate = compilation.mainTemplate;
|
117 |
|
118 |
|
119 |
|
120 | let filename = mainTemplate.outputOptions.filename;
|
121 | if (typeof filename === 'function') {
|
122 |
|
123 |
|
124 |
|
125 | filename = filename({
|
126 | chunk,
|
127 | hash,
|
128 |
|
129 | contentHashType: 'javascript',
|
130 | hashWithLength: (length) => mainTemplate.renderCurrentHashCode(hash, length),
|
131 | noChunkHash: mainTemplate.useChunkHash(chunk),
|
132 | });
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | if (!filename || !filename.includes('.js')) {
|
141 | return source;
|
142 | }
|
143 |
|
144 | return createRefreshTemplate(source);
|
145 | }
|
146 | );
|
147 | });
|
148 | }
|
149 | }
|
150 |
|
151 | module.exports.ReactRefreshPlugin = ReactRefreshPlugin;
|
152 | module.exports = ReactRefreshPlugin;
|