From a010df5530357b49461a80464c4a15bb50a9b0aa Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Fri, 14 Feb 2020 01:12:05 +0000 Subject: [PATCH] Configure sourcemap file `webpack` will by default emit a sourcemap that maps to a `main.js` file. However, the worker runtime will create the script with a `worker.js` name. In some cases (like devtools) it doesn't matter because it resolves the source infos based on the sourcemap `sources` key. In Sentry for instance, that would fail. Fixes https://github.com/cloudflare/wrangler/issues/681 --- tests/build.rs | 30 ++++++++++++++++++++++++++++++ wranglerjs/index.js | 12 ++++++++++++ 2 files changed, 42 insertions(+) diff --git a/tests/build.rs b/tests/build.rs index d6b99c649..725e1f24e 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -3,6 +3,7 @@ extern crate lazy_static; pub mod fixture; +use std::fs; use std::process::Command; use std::str; @@ -313,6 +314,35 @@ fn it_builds_with_webpack_target_webworker() { build_creates_assets(&fixture, vec!["script.js"]); } +#[test] +fn it_builds_with_webpack_name_output() { + let fixture = Fixture::new(); + fixture.scaffold_webpack(); + + fixture.create_file( + "webpack.config.js", + r#" + module.exports = { + "entry": "./index.js", + "devtool": "cheap-module-source-map" + } + "#, + ); + fixture.create_file("index.js", ""); + + let wrangler_toml = WranglerToml::webpack_std_config("test-build-webpack-name-output"); + fixture.create_wrangler_toml(wrangler_toml); + + build_creates_assets(&fixture, vec!["script.js"]); + + let out = fs::read_to_string(fixture.get_output_path().join("script.js")).unwrap(); + let sourcemap = out.split("\n").last().unwrap(); + assert_eq!( + sourcemap, + r#"//# sourceMappingURL=worker.js.map{"version":3,"file":"worker.js","sources":["webpack:///worker.js"],"mappings":"AAAA","sourceRoot":""}"# + ); +} + fn build_creates_assets(fixture: &Fixture, script_names: Vec<&str>) { let _lock = fixture.lock(); let mut build = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); diff --git a/wranglerjs/index.js b/wranglerjs/index.js index 2e81bfed2..c6b29e902 100644 --- a/wranglerjs/index.js +++ b/wranglerjs/index.js @@ -54,6 +54,18 @@ function filterByExtension(ext) { } config.target = "webworker"; + // The worker runtime will set the name of the script to `worker.js`, + // regardless of what's specified in the sourcemap. + // We can tell webpack to name the generated worker by configuring the output. + // It's also safe to force that configuration because it mirrors what the + // runtime does. + // https://github.com/cloudflare/wrangler/issues/681 + if (config.output === undefined) { + config.output = {}; + } + config.output.filename = 'worker.js'; + config.output.sourceMapFilename = 'worker.js.map'; + const compiler = webpack(config); const fullConfig = compiler.options;