Skip to content

Commit

Permalink
fix: should forward sourcemaps for refresh loaders (web-infra-dev#7872)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a authored Sep 12, 2024
1 parent ef82144 commit 501c12f
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 14 deletions.
9 changes: 5 additions & 4 deletions crates/rspack_loader_preact_refresh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ impl PreactRefreshLoader {
#[async_trait::async_trait]
impl Loader<RunnerContext> for PreactRefreshLoader {
async fn run(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
let content = loader_context
.take_content()
.expect("Content should be available");
let Some(content) = loader_context.take_content() else {
return Ok(());
};
let mut source = content.try_into_string()?;
source += "\n";
source += include_str!("runtime.js");
loader_context.finish_with(source);
let sm = loader_context.take_source_map();
loader_context.finish_with((source, sm));
Ok(())
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_loader_react_refresh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ Promise.resolve().then(function() {
$ReactRefreshRuntime$.refresh(__webpack_module__.id, __webpack_module__.hot);
});
"#;
loader_context.finish_with(source);
let sm = loader_context.take_source_map();
loader_context.finish_with((source, sm));
Ok(())
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/rspack-test-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
"@monaco-editor/react": "^4.6.0",
"@rspack/cli": "workspace:*",
"@rspack/core": "workspace:*",
"@rspack/plugin-react-refresh": "1.0.0",
"@rspack/plugin-preact-refresh": "1.0.0",
"@swc/helpers": "0.5.8",
"@swc/plugin-remove-console": "^2.0.8",
"@types/babel__generator": "7.6.8",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function component () {
return <div></div>
}

it('should work with preact-refresh', () => {
const fs = require("fs")
let map = fs.readFileSync(__filename + ".map", "utf-8")
expect(map).toContain(STUB)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const rspack = require("@rspack/core")
const PreactRefreshPlugin = require("@rspack/plugin-preact-refresh");
const { ConcatSource, RawSource } = require("webpack-sources")
/** @type {import("@rspack/core").Configuration} */
module.exports = {
entry: "./index.jsx",
mode: "development",
resolve: {
extensions: ["...", ".ts", ".tsx", ".jsx"]
},
devtool: "source-map",
module: {
rules: [
{
test: /\.jsx$/,
loader: "builtin:swc-loader",
options: {
jsc: {
parser: {
syntax: "ecmascript",
jsx: true,
sourceMap: true,
},
transform: {
react: {
runtime: "classic",
pragma: "React.createElement",
pragmaFrag: "React.Fragment",
throwIfNamespace: true,
useBuiltins: false
}
}
}
}
}
]
},
plugins: [
new rspack.HotModuleReplacementPlugin(),
new PreactRefreshPlugin(),
new rspack.DefinePlugin({
STUB: JSON.stringify("<div></div>")
}),
{
apply(compiler) {
compiler.hooks.compilation.tap("_", (compilation) => {
compilation.hooks.processAssets.tap("_", (assets) => {
compilation.updateAsset("bundle0.js",new ConcatSource(
new RawSource("const self = globalThis;"), // mock self to NodeJs specific global object
assets["bundle0.js"]
))
})
})
}
}
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function component () {
return <div></div>
}

it('should work with react refresh', () => {
const fs = require("fs")
let map = fs.readFileSync(__filename + ".map", "utf-8")
expect(map).toContain(STUB)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const rspack = require("@rspack/core")
const ReactRefreshPlugin = require("@rspack/plugin-react-refresh");
/** @type {import("@rspack/core").Configuration} */
module.exports = {
entry: "./index.jsx",
mode: "development",
resolve: {
extensions: ["...", ".ts", ".tsx", ".jsx"]
},
devtool: "source-map",
module: {
rules: [
{
test: /\.jsx$/,
loader: "builtin:swc-loader",
options: {
jsc: {
parser: {
syntax: "ecmascript",
jsx: true,
sourceMap: true,
},
transform: {
react: {
runtime: "classic",
pragma: "React.createElement",
pragmaFrag: "React.Fragment",
throwIfNamespace: true,
useBuiltins: false
}
}
}
}
}
]
},
plugins: [
new rspack.HotModuleReplacementPlugin(),
new ReactRefreshPlugin(),
new rspack.DefinePlugin({
STUB: JSON.stringify("<div></div>")
})
]
};
Loading

0 comments on commit 501c12f

Please sign in to comment.