-
-
Notifications
You must be signed in to change notification settings - Fork 826
feat: support import.meta.hot hmr alias #14555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
intellild
wants to merge
5
commits into
main
Choose a base branch
from
codex/import-meta-hot-alias
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
abbb181
feat: support import.meta.hot hmr alias
intellild 21ed178
chore: format HMR parser imports
intellild 766aeee
fix: preserve HMR typeof checks at runtime
intellild 267b01d
fix: honor importMeta.hot parser option
intellild c3946ac
docs: document import.meta.hot availability
intellild File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| export default "import.meta.hot"; |
32 changes: 32 additions & 0 deletions
32
tests/rspack-test/configCases/parsing/import-meta-hot/index.js
This file contains hidden or 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,32 @@ | ||
| import value from "./dep"; | ||
|
|
||
| const fs = require("fs"); | ||
|
|
||
| let typeofGuardMatched = false; | ||
| let webpackHotTypeofGuardMatched = false; | ||
|
|
||
| if (typeof import.meta.hot !== "undefined") { | ||
| typeofGuardMatched = true; | ||
| } | ||
|
|
||
| if (typeof import.meta.webpackHot !== "undefined") { | ||
| webpackHotTypeofGuardMatched = true; | ||
| } | ||
|
|
||
| if (import.meta.hot) { | ||
| import.meta.hot.accept(["./dep"], () => {}); | ||
| } | ||
|
|
||
| it("should support import.meta.hot as an HMR alias", () => { | ||
| expect(value).toBe("import.meta.hot"); | ||
| expect(typeofGuardMatched).toBe(true); | ||
| expect(webpackHotTypeofGuardMatched).toBe(true); | ||
| expect(typeof import.meta.hot).toBe("object"); | ||
| expect(typeof import.meta.hot.accept).toBe("function"); | ||
| expect(typeof import.meta.webpackHot).toBe("object"); | ||
|
|
||
| const source = fs.readFileSync(__filename, "utf-8"); | ||
| const typeofKeyword = ["type", "of"].join(""); | ||
| const runtimeTypeof = `${typeofKeyword} module.hot !== "undefined"`; | ||
| expect(source.split(runtimeTypeof)).toHaveLength(3); | ||
| }); |
15 changes: 15 additions & 0 deletions
15
tests/rspack-test/configCases/parsing/import-meta-hot/parser-options.js
This file contains hidden or 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,15 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| it("should disable the hot alias independently from the canonical HMR field", () => { | ||
| expect(typeof import.meta.hot).toBe("undefined"); | ||
| expect(typeof import.meta.webpackHot).toBe("object"); | ||
| if (import.meta.hot) { | ||
| import.meta.hot.accept(); | ||
| import.meta.hot.decline(); | ||
| } | ||
|
|
||
| const source = fs.readFileSync(__filename, "utf-8"); | ||
| const importMeta = ["import", "meta"].join("."); | ||
| expect(source.split(`${importMeta}.hot`)).toHaveLength(5); | ||
| expect(source).not.toContain(`${importMeta}.webpackHot`); | ||
| }); |
9 changes: 9 additions & 0 deletions
9
tests/rspack-test/configCases/parsing/import-meta-hot/production.js
This file contains hidden or 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,9 @@ | ||
| it("should transform import.meta.hot to false without HMR", () => { | ||
| let hot = false; | ||
| if (import.meta.hot) { | ||
| hot = true; | ||
| import.meta.hot.accept(); | ||
| } | ||
|
|
||
| expect(hot).toBe(false); | ||
| }); |
47 changes: 47 additions & 0 deletions
47
tests/rspack-test/configCases/parsing/import-meta-hot/rspack.config.js
This file contains hidden or 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,47 @@ | ||
| const { HotModuleReplacementPlugin } = require('@rspack/core'); | ||
|
|
||
| /** @type {import("@rspack/core").Configuration[]} */ | ||
| module.exports = [ | ||
| { | ||
| entry: './index.js', | ||
| devtool: false, | ||
| externals: { | ||
| fs: 'node-commonjs fs', | ||
| }, | ||
| node: { | ||
| __filename: false, | ||
| }, | ||
| plugins: [new HotModuleReplacementPlugin()], | ||
| }, | ||
| { | ||
| entry: './production.js', | ||
| mode: 'production', | ||
| target: 'web', | ||
| devServer: { | ||
| hot: true, | ||
| }, | ||
| }, | ||
| { | ||
| entry: './parser-options.js', | ||
| mode: 'development', | ||
| devtool: false, | ||
| target: 'node', | ||
| experiments: { | ||
| outputModule: true, | ||
| }, | ||
| output: { | ||
| module: true, | ||
| chunkFormat: 'module', | ||
| }, | ||
| module: { | ||
| parser: { | ||
| javascript: { | ||
| importMeta: { | ||
| hot: false, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| plugins: [new HotModuleReplacementPlugin()], | ||
| }, | ||
| ]; |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.