From e7416ffe4abff5a8b13b40619181f180edae8d1f Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Fri, 30 Aug 2019 13:23:48 +0100 Subject: [PATCH] feat: use target webworker Fixes https://github.com/cloudflare/wrangler/issues/477. We want to configure webpack to emit only webworker compatible code. --- src/commands/build/wranglerjs/bundle.rs | 13 +---- tests/build.rs | 56 +++++++++++++++++++ tests/fixtures/webpack_target_web/index.js | 0 .../webpack_target_webworker/index.js | 0 wranglerjs/index.js | 10 ++++ 5 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 tests/fixtures/webpack_target_web/index.js create mode 100644 tests/fixtures/webpack_target_webworker/index.js diff --git a/src/commands/build/wranglerjs/bundle.rs b/src/commands/build/wranglerjs/bundle.rs index 86b47eb05..a2a1a6c90 100644 --- a/src/commands/build/wranglerjs/bundle.rs +++ b/src/commands/build/wranglerjs/bundle.rs @@ -38,8 +38,6 @@ 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"); @@ -47,7 +45,7 @@ impl Bundle { wasm_file.write_all(&wasm)?; } - script_file.write_all(script.as_bytes())?; + script_file.write_all(wranglerjs_output.script.as_bytes())?; Ok(()) } @@ -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::*; diff --git a/tests/build.rs b/tests/build.rs index c2ac0a989..2529ab183 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -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)); @@ -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(); +} diff --git a/tests/fixtures/webpack_target_web/index.js b/tests/fixtures/webpack_target_web/index.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/webpack_target_webworker/index.js b/tests/fixtures/webpack_target_webworker/index.js new file mode 100644 index 000000000..e69de29bb diff --git a/wranglerjs/index.js b/wranglerjs/index.js index db3a7279c..2468863d4 100644 --- a/wranglerjs/index.js +++ b/wranglerjs/index.js @@ -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;