Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Configure sourcemap file
Browse files Browse the repository at this point in the history
`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 #681
  • Loading branch information
xtuc committed Feb 14, 2020
1 parent ab5c372 commit a010df5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate lazy_static;

pub mod fixture;

use std::fs;
use std::process::Command;
use std::str;

Expand Down Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions wranglerjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit a010df5

Please sign in to comment.