From 363f7d0d7092b0647e2c10d8a7a5220b5793b6bf 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 | 46 +++++++++++++++++++++++++++++++++++++++++++-- wranglerjs/index.js | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/tests/build.rs b/tests/build.rs index d6b99c649..6ef452d39 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,14 +314,55 @@ fn it_builds_with_webpack_target_webworker() { build_creates_assets(&fixture, vec!["script.js"]); } -fn build_creates_assets(fixture: &Fixture, script_names: Vec<&str>) { +#[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); + + let (_, stderr) = 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":""}"# + ); + + println!("stderr : {}", stderr); + assert_eq!(stderr, "foo"); +} + +fn build_creates_assets(fixture: &Fixture, script_names: Vec<&str>) -> (String, String) { let _lock = fixture.lock(); let mut build = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); build.current_dir(fixture.get_path()); - build.arg("build").assert().success(); + build.arg("build"); + + let output = build.output().expect("failed to execute process"); + assert!(output.status.success()); + for script_name in script_names { assert!(fixture.get_output_path().join(script_name).exists()); } + + ( + str::from_utf8(&output.stdout).unwrap().to_string(), + str::from_utf8(&output.stderr).unwrap().to_string(), + ) } fn build_fails_with(fixture: &Fixture, expected_message: &str) { diff --git a/wranglerjs/index.js b/wranglerjs/index.js index 2e81bfed2..690070368 100644 --- a/wranglerjs/index.js +++ b/wranglerjs/index.js @@ -3,11 +3,17 @@ const { join } = require("path"); const fs = require("fs"); const WasmMainTemplatePlugin = require("webpack/lib/wasm/WasmMainTemplatePlugin"); +const WEBPACK_OUTPUT_FILENAME = "worker.js"; +const WEBPACK_OUTPUT_SOURCEMAPFILENAME = WEBPACK_OUTPUT_FILENAME + ".map"; + function error(msg) { console.error("Error: " + msg); process.exit(1); return new Error("error"); } +function warn(...msg) { + console.warn("Warning: " + msg.join(" ")); +} function filterByExtension(ext) { return v => v.indexOf("." + ext) !== -1; @@ -54,6 +60,38 @@ 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 = {}; + } + if ( + config.output.filename !== undefined && + config.output.filename !== WEBPACK_OUTPUT_FILENAME + ) { + warn( + "webpack's output filename is being renamed to", + WEBPACK_OUTPUT_FILENAME, + "because of requirements from the Workers runtime" + ); + } + if ( + config.output.sourceMapFilename !== undefined && + config.output.sourceMapFilename !== WEBPACK_OUTPUT_SOURCEMAPFILENAME + ) { + warn( + "webpack's output sourcemap filename is being renamed to", + WEBPACK_OUTPUT_SOURCEMAPFILENAME, + "because of requirements from the Workers runtime" + ); + } + config.output.filename = WEBPACK_OUTPUT_FILENAME; + config.output.sourceMapFilename = WEBPACK_OUTPUT_SOURCEMAPFILENAME; + const compiler = webpack(config); const fullConfig = compiler.options;