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

Alewis/custom webpack config for sites #957

Merged
merged 10 commits into from
Dec 17, 2019
16 changes: 4 additions & 12 deletions src/commands/build/wranglerjs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,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,12 +14,40 @@ 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!["main.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 = { context: __dirname, entry: "./index.js" };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should target: webworker in case somebody tries to copy our test suite instead of looking at our docs :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i... O.o

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we do that for all of them though? because none of the rest of our tests do this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm yeah we probably should... if we dont provide a custom config then it should be using it automatically

"#,
);

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!["main.js"]);
}

#[test]
fn it_builds_with_webpack_single_js() {
let fixture = Fixture::new();
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 = "dist";
ashleymichal marked this conversation as resolved.
Show resolved Hide resolved

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