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

Commit

Permalink
Merge pull request #253 from EverlastingBugstopper/avery/specify-webp…
Browse files Browse the repository at this point in the history
…ack-config

feat: allow custom webpack configuration in wrangler.toml
  • Loading branch information
ashleygwilliams authored Jun 21, 2019
2 parents 126a80b + b3b5a0f commit ba729f8
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,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
Expand Down
20 changes: 16 additions & 4 deletions src/commands/build/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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!")
Expand Down Expand Up @@ -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());
Expand Down
6 changes: 3 additions & 3 deletions src/commands/build/wranglerjs/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,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;

Expand Down Expand Up @@ -78,8 +78,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 {
Expand Down
8 changes: 7 additions & 1 deletion src/commands/build/wranglerjs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<WranglerjsOutput, failure::Error> {
let node = which::which("node").unwrap();
Expand All @@ -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
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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() {
Expand All @@ -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)?;
}

Expand Down
2 changes: 2 additions & 0 deletions src/settings/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Project {
pub project_type: ProjectType,
pub zone_id: Option<String>,
pub private: Option<bool>,
pub webpack_config: Option<String>,
pub account_id: String,
pub route: Option<String>,
pub routes: Option<HashMap<String, String>>,
Expand Down Expand Up @@ -76,6 +77,7 @@ impl Project {
route: Some(String::new()),
routes: None,
kv_namespaces: None,
webpack_config: None,
};

let toml = toml::to_string(&project)?;
Expand Down
31 changes: 30 additions & 1 deletion tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}

Expand Down
Empty file.
Empty file.
1 change: 1 addition & 0 deletions tests/webpack_multiple_specify_config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions tests/webpack_multiple_specify_config/webpack.worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = [
{ entry: "./a.js" },
{ entry: "./b.js" }
]
Empty file.
1 change: 1 addition & 0 deletions tests/webpack_specify_config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions tests/webpack_specify_config/webpack.worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { entry: "./index.js" };
6 changes: 3 additions & 3 deletions wranglerjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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."
);
}

Expand Down

0 comments on commit ba729f8

Please sign in to comment.