-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
254 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 6 additions & 15 deletions
21
packages/reshow-app/src/webpack/refresh/utils/__tests__/isUseEsmTest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 81 additions & 22 deletions
103
packages/reshow-app/src/webpack/refresh/utils/injectRefreshLoader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,93 @@ | ||
// @ts-check | ||
// https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/f1c8b9a44198449093ca95f85af5df97925e1cfc/lib/utils/injectRefreshLoader.js | ||
|
||
import path from "path"; | ||
import { createRequire } from "node:module"; | ||
import { getDirName } from "../../util/getDirName"; | ||
|
||
/** | ||
* @typedef {Object} ESModuleOptions | ||
* @property {string | RegExp | Array<string | RegExp>} [exclude] Files to explicitly exclude from flagged as ES Modules. | ||
* @property {string | RegExp | Array<string | RegExp>} [include] Files to explicitly include for flagged as ES Modules. | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} ReactRefreshLoaderOptions | ||
* @property {boolean} [const] Enables usage of ES6 `const` and `let` in generated runtime code. | ||
* @property {boolean | ESModuleOptions} [esModule] Enables strict ES Modules compatible runtime. | ||
*/ | ||
|
||
/** | ||
* @callback MatchObject | ||
* @param {string} [str] | ||
* @returns {boolean} | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} InjectLoaderOptions | ||
* @property {MatchObject} match A function to include/exclude files to be processed. | ||
* @property {ReactRefreshLoaderOptions} [options] Options passed to the loader. | ||
*/ | ||
|
||
const rootDir = getDirName(); | ||
let myrequire; | ||
if ("undefined" === typeof require) { | ||
// @ts-ignore | ||
myrequire = createRequire(import.meta.url); | ||
} else { | ||
myrequire = require; | ||
} | ||
|
||
const resolvedLoader = path.join(rootDir, "../loader"); | ||
const reactRefreshPath = path.dirname(myrequire.resolve("react-refresh")); | ||
const refreshUtilsPath = path.join(rootDir, "../runtime/RefreshUtils"); | ||
|
||
/** | ||
* @param {any} moduleData | ||
* @param {InjectLoaderOptions} injectOptions | ||
*/ | ||
const injectRefreshLoader = (moduleData, injectOptions) => { | ||
const { match, options } = injectOptions; | ||
const resolvedLoader = require.resolve("../loader"); | ||
|
||
// Include and exclude user-specified files | ||
if (!match(moduleData.matchResource || moduleData.resource)) { | ||
return moduleData; | ||
} | ||
|
||
// Include and exclude dynamically generated modules from other loaders | ||
if (moduleData.matchResource && !match(moduleData.request)) { | ||
return moduleData; | ||
} | ||
|
||
// Exclude files referenced as assets | ||
if (moduleData.type.includes("asset")) { | ||
return moduleData; | ||
} | ||
|
||
// Check to prevent double injection | ||
if (moduleData.loaders.find(({ loader }) => loader === resolvedLoader)) { | ||
return moduleData; | ||
} | ||
|
||
// Skip react-refresh and the plugin's runtime utils to prevent self-referencing - | ||
// this is useful when using the plugin as a direct dependency, | ||
// or when node_modules are specified to be processed. | ||
if ( | ||
// Include and exclude user-specified files | ||
match(moduleData.matchResource || moduleData.resource) && | ||
// Exclude files referenced as assets | ||
!moduleData.type.includes("asset") && | ||
// Skip react-refresh and the plugin's runtime utils to prevent self-referencing - | ||
// this is useful when using the plugin as a direct dependency, | ||
// or when node_modules are specified to be processed. | ||
!moduleData.resource.includes( | ||
path.dirname(require.resolve("react-refresh")), | ||
) && | ||
!moduleData.resource.includes(path.join(__dirname, "../runtime")) && | ||
// Check to prevent double injection | ||
!moduleData.loaders.find(({ loader }) => loader === resolvedLoader) | ||
moduleData.resource.includes(reactRefreshPath) || | ||
moduleData.resource.includes(refreshUtilsPath) | ||
) { | ||
// As we inject runtime code for each module, | ||
// it is important to run the injected loader after everything. | ||
// This way we can ensure that all code-processing have been done, | ||
// and we won't risk breaking tools like Flow or ESLint. | ||
moduleData.loaders.unshift({ | ||
loader: resolvedLoader, | ||
options, | ||
}); | ||
return moduleData; | ||
} | ||
|
||
// As we inject runtime code for each module, | ||
// it is important to run the injected loader after everything. | ||
// This way we can ensure that all code-processing have been done, | ||
// and we won't risk breaking tools like Flow or ESLint. | ||
moduleData.loaders.unshift({ | ||
loader: resolvedLoader, | ||
options, | ||
}); | ||
|
||
return moduleData; | ||
}; | ||
export default injectRefreshLoader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// @ts-check | ||
|
||
import { dirname } from "path"; | ||
|
||
export const getDirName = () => { | ||
let rootDir = ""; | ||
try { | ||
// @ts-expect-error | ||
_NOT_DEFINED; | ||
} catch (e) { | ||
const initiator = e.stack.split("\n").slice(2)[0]; | ||
// @ts-ignore | ||
const file = /(?<path>[^\(\s]+):[0-9]+:[0-9]+/ | ||
?.exec(initiator) | ||
?.groups.path.replace("file://", ""); | ||
rootDir = dirname(file || ""); | ||
} | ||
return rootDir; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.