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 #957 from cloudflare/alewis/custom-webpack-config-…
Browse files Browse the repository at this point in the history
…for-sites

Alewis/custom webpack config for sites
  • Loading branch information
ashleymichal authored Dec 17, 2019
2 parents 3773bfa + 2a63ac6 commit 8fd7f22
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 28 deletions.
32 changes: 17 additions & 15 deletions src/commands/build/wranglerjs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ pub fn run_build(target: &Target) -> Result<(), failure::Error> {
let wranglerjs_output: WranglerjsOutput =
serde_json::from_str(&output).expect("could not parse wranglerjs output");

write_wranglerjs_output(&bundle, &wranglerjs_output)
let custom_webpack = target.webpack_config.is_some();
write_wranglerjs_output(&bundle, &wranglerjs_output, custom_webpack)
} else {
failure::bail!("failed to execute `{:?}`: exited with {}", command, status)
}
Expand All @@ -57,6 +58,7 @@ pub fn run_build_and_watch(target: &Target, tx: Option<Sender<()>>) -> Result<()
command.arg("--watch=1");

let is_site = target.site.clone();
let custom_webpack = target.webpack_config.is_some();

log::info!("Running {:?} in watch mode", command);

Expand Down Expand Up @@ -100,7 +102,8 @@ pub fn run_build_and_watch(target: &Target, tx: Option<Sender<()>>) -> Result<()
let wranglerjs_output: WranglerjsOutput =
serde_json::from_str(&output).expect("could not parse wranglerjs output");

if write_wranglerjs_output(&bundle, &wranglerjs_output).is_ok() {
if write_wranglerjs_output(&bundle, &wranglerjs_output, custom_webpack).is_ok()
{
if let Some(tx) = tx.clone() {
tx.send(()).expect("--watch change message failed to send");
}
Expand All @@ -117,12 +120,19 @@ pub fn run_build_and_watch(target: &Target, tx: Option<Sender<()>>) -> Result<()
fn write_wranglerjs_output(
bundle: &Bundle,
output: &WranglerjsOutput,
custom_webpack: bool,
) -> Result<(), failure::Error> {
if output.has_errors() {
message::user_error(output.get_errors().as_str());
failure::bail!(
if custom_webpack {
failure::bail!(
"webpack returned an error. Try configuring `entry` in your webpack config relative to the current working directory, or setting `context = __dirname` in your webpack config."
);
} else {
failure::bail!(
"webpack returned an error. You may be able to resolve this issue by running npm install."
);
}
}

bundle.write(output)?;
Expand Down Expand Up @@ -175,19 +185,11 @@ fn setup_build(target: &Target) -> Result<(Command, PathBuf, Bundle), failure::E
command.arg(format!("--wasm-binding={}", bundle.get_wasm_binding()));

let custom_webpack_config_path = match &target.webpack_config {
Some(webpack_config) => match &target.site {
None => Some(PathBuf::from(&webpack_config)),
Some(_) => {
message::warn("Workers Sites does not support custom webpack configuration files");
None
}
},
Some(webpack_config) => Some(PathBuf::from(&webpack_config)),
None => {
if target.site.is_none() {
let config_path = PathBuf::from("webpack.config.js".to_string());
if config_path.exists() {
message::warn("If you would like to use your own custom webpack configuration, you will need to add this to your wrangler.toml:\nwebpack_config = \"webpack.config.js\"");
}
let config_path = PathBuf::from("webpack.config.js".to_string());
if config_path.exists() {
message::warn("If you would like to use your own custom webpack configuration, you will need to add this to your wrangler.toml:\nwebpack_config = \"webpack.config.js\"");
}
None
}
Expand Down
34 changes: 31 additions & 3 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,35 @@ fn it_builds_webpack() {
let fixture = Fixture::new();
fixture.scaffold_webpack();

let wrangler_toml = WranglerToml::webpack_no_config("test-build-webpack");
let wrangler_toml = WranglerToml::webpack_build("test-build-webpack");
fixture.create_wrangler_toml(wrangler_toml);

build_creates_assets(&fixture, vec!["script.js"]);
}

#[test]
fn it_builds_webpack_site() {
let fixture = Fixture::new_site();

let wrangler_toml = WranglerToml::site("test-build-site");
fixture.create_wrangler_toml(wrangler_toml);

build_creates_assets(&fixture, vec!["script.js"]);
}

#[test]
fn it_builds_webpack_site_with_custom_webpack() {
let fixture = Fixture::new_site();

fixture.create_file(
"workers-site/webpack.worker.js",
r#"
module.exports = { entry: "./workers-site/index.js" };
"#,
);

let mut wrangler_toml = WranglerToml::site("test-build-site-specify-config");
wrangler_toml.webpack_config = Some("workers-site/webpack.worker.js");
fixture.create_wrangler_toml(wrangler_toml);

build_creates_assets(&fixture, vec!["script.js"]);
Expand All @@ -41,7 +69,7 @@ fn it_builds_with_webpack_single_js() {
);
fixture.create_default_package_json();

let wrangler_toml = WranglerToml::webpack_no_config("test-build-webpack-single-js");
let wrangler_toml = WranglerToml::webpack_build("test-build-webpack-single-js");
fixture.create_wrangler_toml(wrangler_toml);

build_creates_assets(&fixture, vec!["script.js"]);
Expand Down Expand Up @@ -137,7 +165,7 @@ fn it_builds_with_webpack_single_js_missing_package_main() {
);

let wrangler_toml =
WranglerToml::webpack_no_config("test-build-webpack-single-js-missing-package-main");
WranglerToml::webpack_build("test-build-webpack-single-js-missing-package-main");
fixture.create_wrangler_toml(wrangler_toml);

build_fails_with(
Expand Down
40 changes: 34 additions & 6 deletions tests/fixture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,39 @@ use toml;

const BUNDLE_OUT: &str = "worker";

pub struct Fixture {
pub struct Fixture<'a> {
// we wrap the fixture's tempdir in a `ManuallyDrop` so that if a test
// fails, its directory isn't deleted, and we have a chance to manually
// inspect its state and figure out what is going on.
dir: ManuallyDrop<TempDir>,
output_path: &'a str,
}

impl Default for Fixture {
impl Default for Fixture<'_> {
fn default() -> Self {
Self::new()
}
}

impl Fixture {
pub fn new() -> Fixture {
impl Fixture<'_> {
pub fn new() -> Fixture<'static> {
let dir = TempDir::new().unwrap();
eprintln!("Created fixture at {}", dir.path().display());
Fixture {
dir: ManuallyDrop::new(dir),
output_path: BUNDLE_OUT,
}
}

pub fn new_site() -> Fixture<'static> {
let mut fixture = Fixture::new();
fixture.output_path = "workers-site/worker";

fixture.scaffold_site();

fixture
}

pub fn get_path(&self) -> PathBuf {
self.dir.path().to_path_buf()
}
Expand All @@ -46,7 +57,7 @@ impl Fixture {
}

pub fn get_output_path(&self) -> PathBuf {
self.get_path().join(BUNDLE_OUT)
self.get_path().join(self.output_path)
}

pub fn create_file(&self, name: &str, content: &str) {
Expand Down Expand Up @@ -80,6 +91,23 @@ impl Fixture {
self.create_file("wrangler.toml", &toml::to_string(&wrangler_toml).unwrap());
}

pub fn scaffold_site(&self) {
self.create_dir("workers-site");
self.create_file(
"workers-site/package.json",
r#"
{
"private": true,
"main": "index.js",
"dependencies": {
"@cloudflare/kv-asset-handler": "^0.0.5"
}
}
"#,
);
self.create_file("workers-site/index.js", "");
}

pub fn lock(&self) -> MutexGuard<'static, ()> {
use std::sync::Mutex;

Expand All @@ -91,7 +119,7 @@ impl Fixture {
}
}

impl Drop for Fixture {
impl Drop for Fixture<'_> {
fn drop(&mut self) {
if !thread::panicking() {
unsafe { ManuallyDrop::drop(&mut self.dir) }
Expand Down
15 changes: 12 additions & 3 deletions tests/fixture/wrangler_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct WranglerToml<'a> {
}

impl WranglerToml<'_> {
pub fn webpack_no_config(name: &str) -> WranglerToml {
pub fn webpack_build(name: &str) -> WranglerToml {
let mut wrangler_toml = WranglerToml::default();
wrangler_toml.name = Some(name);
wrangler_toml.workers_dev = Some(true);
Expand All @@ -67,14 +67,14 @@ impl WranglerToml<'_> {
}

pub fn webpack_std_config(name: &str) -> WranglerToml {
let mut wrangler_toml = WranglerToml::webpack_no_config(name);
let mut wrangler_toml = WranglerToml::webpack_build(name);
wrangler_toml.webpack_config = Some("webpack.config.js");

wrangler_toml
}

pub fn webpack_custom_config<'a>(name: &'a str, webpack_config: &'a str) -> WranglerToml<'a> {
let mut wrangler_toml = WranglerToml::webpack_no_config(name);
let mut wrangler_toml = WranglerToml::webpack_build(name);
wrangler_toml.webpack_config = Some(webpack_config);

wrangler_toml
Expand All @@ -97,4 +97,13 @@ impl WranglerToml<'_> {

wrangler_toml
}

pub fn site(name: &str) -> WranglerToml {
let mut wrangler_toml = WranglerToml::webpack_build(name);
let mut site = SiteConfig::default();
site.bucket = Some("./public");
wrangler_toml.site = Some(site);

wrangler_toml
}
}
2 changes: 1 addition & 1 deletion tests/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn it_can_preview_webpack_project() {
let fixture = Fixture::new();
fixture.scaffold_webpack();

let wrangler_toml = WranglerToml::webpack_no_config("test-preview-webpack");
let wrangler_toml = WranglerToml::webpack_build("test-preview-webpack");
fixture.create_wrangler_toml(wrangler_toml);

preview_succeeds(&fixture);
Expand Down

0 comments on commit 8fd7f22

Please sign in to comment.