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 363f7d0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
46 changes: 44 additions & 2 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,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) {
Expand Down
38 changes: 38 additions & 0 deletions wranglerjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 363f7d0

Please sign in to comment.