From 7fa0024bce9b09352da7773f059d1e40445725a1 Mon Sep 17 00:00:00 2001 From: Avery Harnish Date: Fri, 14 Jun 2019 12:02:40 -0500 Subject: [PATCH] feat: allow custom webpack configuration in wrangler.toml --- README.md | 2 ++ src/commands/build/mod.rs | 20 +++++++++--- src/commands/build/wranglerjs/bundle.rs | 6 ++-- src/commands/build/wranglerjs/mod.rs | 8 ++++- src/main.rs | 6 ++-- src/settings/project.rs | 2 ++ tests/build.rs | 31 ++++++++++++++++++- tests/webpack_multiple_config/index.js | 0 .../webpack_multiple_specify_config/index.js | 0 .../package.json | 1 + .../webpack.worker.js | 4 +++ tests/webpack_specify_config/index.js | 0 tests/webpack_specify_config/package.json | 1 + .../webpack_specify_config/webpack.worker.js | 1 + wranglerjs/index.js | 6 ++-- 15 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 tests/webpack_multiple_config/index.js create mode 100644 tests/webpack_multiple_specify_config/index.js create mode 100644 tests/webpack_multiple_specify_config/package.json create mode 100644 tests/webpack_multiple_specify_config/webpack.worker.js create mode 100644 tests/webpack_specify_config/index.js create mode 100644 tests/webpack_specify_config/package.json create mode 100644 tests/webpack_specify_config/webpack.worker.js diff --git a/README.md b/README.md index 1d3b62940..80fc5893b 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,8 @@ There are two types of configuration that `wrangler` uses: global user and per p - `*example.com/*` - `http://example.com/hello` This key is optional if you are using a workers.dev subdomain and is only required for `publish --release`. + - `webpack_config`: This is the path to the webpack configuration file for your worker. This is optional and + defaults to `webpack.config.js` ## ⚓ Installation diff --git a/src/commands/build/mod.rs b/src/commands/build/mod.rs index 6b04b2cbd..d80b5217b 100644 --- a/src/commands/build/mod.rs +++ b/src/commands/build/mod.rs @@ -1,6 +1,6 @@ pub mod wranglerjs; -use crate::settings::project::ProjectType; +use crate::settings::project::{Project, ProjectType}; use crate::{commands, install}; use binary_install::Cache; use std::env; @@ -9,7 +9,14 @@ use std::process::Command; use crate::emoji; -pub fn build(cache: &Cache, project_type: &ProjectType) -> Result<(), failure::Error> { +pub fn build(cache: &Cache, project: &Project) -> Result<(), failure::Error> { + let project_type = &project.project_type; + let webpack_config_path = PathBuf::from( + &project + .webpack_config + .clone() + .unwrap_or_else(|| "webpack.config.js".to_string()), + ); match project_type { ProjectType::JavaScript => { println!("⚠️ JavaScript project found. Skipping unnecessary build!") @@ -37,8 +44,13 @@ pub fn build(cache: &Cache, project_type: &ProjectType) -> Result<(), failure::E wranglerjs::run_npm_install(current_dir).expect("could not run `npm install`"); let bundle = wranglerjs::Bundle::new(); - let wranglerjs_output = wranglerjs::run_build(wranglerjs_path, wasm_pack_path, &bundle) - .expect("could not run wranglerjs"); + let wranglerjs_output = wranglerjs::run_build( + wranglerjs_path, + wasm_pack_path, + webpack_config_path, + &bundle, + ) + .expect("could not run wranglerjs"); if wranglerjs_output.has_errors() { println!("{}", wranglerjs_output.get_errors()); diff --git a/src/commands/build/wranglerjs/bundle.rs b/src/commands/build/wranglerjs/bundle.rs index add6336e3..ecc3388ff 100644 --- a/src/commands/build/wranglerjs/bundle.rs +++ b/src/commands/build/wranglerjs/bundle.rs @@ -3,7 +3,7 @@ use std::env; use std::fs; use std::fs::File; use std::io::prelude::*; -use std::path::Path; +use std::path::{Path, PathBuf}; use log::info; @@ -76,8 +76,8 @@ impl Bundle { Path::new(&self.wasm_path()).exists() } - pub fn has_webpack_config(&self) -> bool { - Path::new("webpack.config.js").exists() + pub fn has_webpack_config(&self, webpack_config_path: &PathBuf) -> bool { + webpack_config_path.exists() } pub fn get_wasm_binding(&self) -> String { diff --git a/src/commands/build/wranglerjs/mod.rs b/src/commands/build/wranglerjs/mod.rs index 976693d96..7f0fcb1da 100644 --- a/src/commands/build/wranglerjs/mod.rs +++ b/src/commands/build/wranglerjs/mod.rs @@ -34,6 +34,7 @@ fn random_chars(n: usize) -> String { pub fn run_build( wranglerjs_path: PathBuf, wasm_pack_path: PathBuf, + webpack_config_path: PathBuf, bundle: &Bundle, ) -> Result { let node = which::which("node").unwrap(); @@ -55,7 +56,7 @@ pub fn run_build( // if {webpack.config.js} is not present, we infer the entry based on the // {package.json} file and pass it to {wranglerjs}. // https://github.com/cloudflare/wrangler/issues/98 - if !bundle.has_webpack_config() { + if !bundle.has_webpack_config(&webpack_config_path) { let package = Package::new("./")?; let current_dir = env::current_dir()?; let package_main = current_dir @@ -65,6 +66,11 @@ pub fn run_build( .to_string(); command.arg("--no-webpack-config=1"); command.arg(format!("--use-entry={}", package_main)); + } else { + command.arg(format!( + "--webpack-config={}", + &webpack_config_path.to_str().unwrap().to_string() + )); } info!("Running {:?}", command); diff --git a/src/main.rs b/src/main.rs index 2a6050f5b..71909c0a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -202,7 +202,7 @@ fn main() -> Result<(), failure::Error> { let project = settings::project::Project::new()?; if matches.subcommand_matches("build").is_some() { - commands::build(&cache, &project.project_type)?; + commands::build(&cache, &project)?; } if let Some(matches) = matches.subcommand_matches("preview") { @@ -213,7 +213,7 @@ fn main() -> Result<(), failure::Error> { None => None, }; - commands::build(&cache, &project.project_type)?; + commands::build(&cache, &project)?; commands::preview(method, body)?; } } else if matches.subcommand_matches("whoami").is_some() { @@ -238,7 +238,7 @@ fn main() -> Result<(), failure::Error> { 1 => true, _ => false, }; - commands::build(&cache, &project.project_type)?; + commands::build(&cache, &project)?; commands::publish(&user, &project, release)?; } diff --git a/src/settings/project.rs b/src/settings/project.rs index 4aba9d584..d3ec908b2 100644 --- a/src/settings/project.rs +++ b/src/settings/project.rs @@ -16,6 +16,7 @@ pub struct Project { pub project_type: ProjectType, pub zone_id: Option, pub private: Option, + pub webpack_config: Option, pub account_id: String, pub route: Option, pub routes: Option>, @@ -76,6 +77,7 @@ impl Project { route: Some(String::new()), routes: None, kv_namespaces: None, + webpack_config: None, }; let toml = toml::to_string(&project)?; diff --git a/tests/build.rs b/tests/build.rs index 3fea300f3..ef6a535a7 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -55,6 +55,21 @@ fn it_builds_with_webpack_single_js_use_package_main() { cleanup(fixture); } +#[test] +fn it_builds_with_webpack_specify_configs() { + let fixture = "webpack_specify_config"; + create_temporary_copy(fixture); + + settings! {fixture, r#" + type = "Webpack" + webpack_config = "webpack.worker.js" + "#}; + + build(fixture); + assert!(fixture_out_path(fixture).join("script.js").exists()); + cleanup(fixture); +} + #[test] fn it_builds_with_webpack_single_js_missing_package_main() { let fixture = "webpack_single_js_missing_package_main"; @@ -80,7 +95,21 @@ fn it_fails_with_multiple_webpack_configs() { type = "Webpack" "#}; - build_fails_with(fixture, "multiple webpack configurations is not supported."); + build_fails_with(fixture, "Multiple webpack configurations are not supported. You can specify a different path for your webpack configuration file in wrangler.toml with the `webpack_config` field"); + cleanup(fixture); +} + +#[test] +fn it_fails_with_multiple_specify_webpack_configs() { + let fixture = "webpack_multiple_specify_config"; + create_temporary_copy(fixture); + + settings! {fixture, r#" + type = "Webpack" + webpack_config = "webpack.worker.js" + "#}; + + build_fails_with(fixture, "Multiple webpack configurations are not supported. You can specify a different path for your webpack configuration file in wrangler.toml with the `webpack_config` field"); cleanup(fixture); } diff --git a/tests/webpack_multiple_config/index.js b/tests/webpack_multiple_config/index.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/webpack_multiple_specify_config/index.js b/tests/webpack_multiple_specify_config/index.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/webpack_multiple_specify_config/package.json b/tests/webpack_multiple_specify_config/package.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/tests/webpack_multiple_specify_config/package.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/webpack_multiple_specify_config/webpack.worker.js b/tests/webpack_multiple_specify_config/webpack.worker.js new file mode 100644 index 000000000..fb177715e --- /dev/null +++ b/tests/webpack_multiple_specify_config/webpack.worker.js @@ -0,0 +1,4 @@ +module.exports = [ + { entry: "./a.js" }, + { entry: "./b.js" } + ] \ No newline at end of file diff --git a/tests/webpack_specify_config/index.js b/tests/webpack_specify_config/index.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/webpack_specify_config/package.json b/tests/webpack_specify_config/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/tests/webpack_specify_config/package.json @@ -0,0 +1 @@ +{} diff --git a/tests/webpack_specify_config/webpack.worker.js b/tests/webpack_specify_config/webpack.worker.js new file mode 100644 index 000000000..f07507ec6 --- /dev/null +++ b/tests/webpack_specify_config/webpack.worker.js @@ -0,0 +1 @@ +module.exports = { entry: "./index.js" }; diff --git a/wranglerjs/index.js b/wranglerjs/index.js index 9224b4455..aa638cd37 100644 --- a/wranglerjs/index.js +++ b/wranglerjs/index.js @@ -25,13 +25,13 @@ let config; if (args["no-webpack-config"] === "1") { config = { entry: args["use-entry"] }; } else { - config = require(join(process.cwd(), "./webpack.config.js")); + config = require(join(process.cwd(), args["webpack-config"])); } if (Array.isArray(config)) { throw error( - "multiple webpack configurations is not supported.\n" - + "Please make sure that your webpack configuration exports an Object." + "Multiple webpack configurations are not supported. You can specify a different path for your webpack configuration file in wrangler.toml with the `webpack_config` field\n" + + "Please make sure that your webpack configuration exports an Object." ); }