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

Commit

Permalink
feat: use target webworker
Browse files Browse the repository at this point in the history
Fixes #477. We want to
configure webpack to emit only webworker compatible code.
  • Loading branch information
xtuc committed Sep 3, 2019
1 parent a5b1dbe commit e7416ff
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 12 deletions.
13 changes: 1 addition & 12 deletions src/commands/build/wranglerjs/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@ impl Bundle {
}

let mut script_file = File::create(self.script_path())?;
let mut script = create_prologue();
script += &wranglerjs_output.script;

if let Some(encoded_wasm) = &wranglerjs_output.wasm {
let wasm = decode(encoded_wasm).expect("could not decode Wasm in base64");
let mut wasm_file = File::create(self.wasm_path())?;
wasm_file.write_all(&wasm)?;
}

script_file.write_all(script.as_bytes())?;
script_file.write_all(wranglerjs_output.script.as_bytes())?;

Ok(())
}
Expand Down Expand Up @@ -81,15 +79,6 @@ impl Bundle {
}
}

// We inject some code at the top-level of the Worker; called {prologue}.
// This aims to provide additional support, for instance providing {window}.
pub fn create_prologue() -> String {
r#"
const window = this;
"#
.to_string()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
56 changes: 56 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,49 @@ fn it_builds_with_webpack_wast() {
cleanup(fixture);
}

#[test]
fn it_fails_with_webpack_target_web() {
let fixture = "webpack_target_web";
create_temporary_copy(fixture);

webpack_config(
fixture,
r#"{
entry: "./index.js",
target: "node",
}"#,
);
settings! {fixture, r#"
type = "webpack"
"#};

build_fails_with(
fixture,
"Building a Cloudflare Worker with target \"node\" is not supported",
);
cleanup(fixture);
}

#[test]
fn it_builds_with_webpack_target_webworker() {
let fixture = "webpack_target_webworker";
create_temporary_copy(fixture);

webpack_config(
fixture,
r#"{
entry: "./index.js",
target: "webworker",
}"#,
);
settings! {fixture, r#"
type = "webpack"
"#};

build(fixture);
cleanup(fixture);
}

fn cleanup(fixture: &str) {
let path = fixture_path(fixture);
assert!(path.exists(), format!("{:?} does not exist", path));
Expand Down Expand Up @@ -236,3 +279,16 @@ fn create_temporary_copy(fixture: &str) {
options.overwrite = true;
copy(src, dest, &options).unwrap();
}

// TODO: remove once https://github.com/cloudflare/wrangler/pull/489 is merged
pub fn webpack_config(fixture: &str, config: &str) {
let file_path = fixture_path(fixture).join("webpack.config.js");
let mut file = File::create(file_path).unwrap();
let content = format!(
r#"
module.exports = {};
"#,
config
);
file.write_all(content.as_bytes()).unwrap();
}
Empty file.
Empty file.
10 changes: 10 additions & 0 deletions wranglerjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ function filterByExtension(ext) {
);
}

if (config.target !== undefined && config.target !== "webworker") {
throw error(
"Building a Cloudflare Worker with target " +
JSON.stringify(config.target) +
" is not supported. Wrangler will set webworker by default, please remove " +
"the `target` key in your webpack configuration."
);
}
config.target = "webworker";

const compiler = webpack(config);
const fullConfig = compiler.options;

Expand Down

0 comments on commit e7416ff

Please sign in to comment.