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

Do not install wasm-pack for webpack projects #849

Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c60727f
Empty webpack fixture directories
EverlastingBugstopper Nov 6, 2019
0c1171b
Create Fixture struct to kill fixtures dir
EverlastingBugstopper Nov 6, 2019
fbb344e
Rename utils mod to fixture mod
EverlastingBugstopper Nov 6, 2019
717b7cf
Merge branch 'master' into avery/clean-up-clean-up-everybody-contribu…
EverlastingBugstopper Nov 7, 2019
633b1e5
Merge branch 'master' into avery/clean-up-clean-up-everybody-contribu…
EverlastingBugstopper Nov 7, 2019
90dbec8
Add webpack_wasm_pack fixture
EverlastingBugstopper Nov 5, 2019
b8601c5
Change install::install to install
EverlastingBugstopper Nov 7, 2019
076c6d5
Only install wasm_pack when it's needed
EverlastingBugstopper Nov 7, 2019
55f7c13
Do not install wasm-pack for webpack projects
EverlastingBugstopper Nov 13, 2019
f8bce77
Merge branch 'master' into avery/bob-the-builder-becomes-more-efficie…
EverlastingBugstopper Nov 13, 2019
a2acba9
Merge branch 'master' into avery/bob-the-builder-becomes-more-efficie…
EverlastingBugstopper Dec 6, 2019
d3b79d4
Merge branch 'master' into avery/bob-the-builder-becomes-more-efficie…
EverlastingBugstopper Dec 6, 2019
046f06a
Merge branch 'master' into avery/bob-the-builder-becomes-more-efficie…
EverlastingBugstopper Dec 13, 2019
1052289
Bump wasm-pack version
EverlastingBugstopper Dec 13, 2019
b348fb2
Move parameters to newlines
EverlastingBugstopper Dec 13, 2019
7a4b0c2
WIP: Test broken CI
EverlastingBugstopper Dec 13, 2019
4e2fc39
Export APPDATA and XDG_CONFIG_HOME in Azure
EverlastingBugstopper Dec 13, 2019
e7ff2e1
Export APPDATA and XDG_CONFIG_HOME in Azure
EverlastingBugstopper Dec 13, 2019
063ab9b
Merge branch 'avery/bob-the-builder-becomes-more-efficient-by-laying-…
EverlastingBugstopper Dec 13, 2019
5f38fe8
Remove env var steps from azure
EverlastingBugstopper Dec 13, 2019
66918d9
Merge branch 'master' into avery/bob-the-builder-becomes-more-efficie…
EverlastingBugstopper Jan 10, 2020
c6155c3
Merge branch 'master' into avery/bob-the-builder-becomes-more-efficie…
EverlastingBugstopper Feb 5, 2020
5557134
Merge branch 'master' into avery/bob-the-builder-becomes-more-efficie…
EverlastingBugstopper Feb 14, 2020
f6d6e30
Merge branch 'master' into avery/bob-the-builder-becomes-more-efficie…
EverlastingBugstopper Mar 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/commands/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ pub mod wranglerjs;
mod watch;
pub use watch::watch_and_build;

use crate::commands;
use crate::install::install;
use crate::settings::target::{Target, TargetType};
use crate::terminal::message;
use crate::{commands, install};

use std::path::PathBuf;
use std::process::Command;
Expand All @@ -18,7 +19,7 @@ pub fn build(target: &Target) -> Result<(), failure::Error> {
}
TargetType::Rust => {
let tool_name = "wasm-pack";
let binary_path = install::install(tool_name, "rustwasm")?.binary(tool_name)?;
let binary_path = install(tool_name, "rustwasm")?.binary(tool_name)?;
let args = ["build", "--target", "no-modules"];

let command = command(&args, &binary_path);
Expand Down
5 changes: 3 additions & 2 deletions src/commands/build/watch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use ignore::overrides::OverrideBuilder;
use ignore::WalkBuilder;
pub use watcher::wait_for_changes;

use crate::commands;
use crate::commands::build::{command, wranglerjs};
use crate::install::install;
use crate::settings::target::{Target, TargetType};
use crate::terminal::message;
use crate::{commands, install};

use notify::{self, RecursiveMode, Watcher};
use std::sync::mpsc;
Expand Down Expand Up @@ -52,7 +53,7 @@ pub fn watch_and_build(
}
TargetType::Rust => {
let tool_name = "wasm-pack";
let binary_path = install::install(tool_name, "rustwasm")?.binary(tool_name)?;
let binary_path = install(tool_name, "rustwasm")?.binary(tool_name)?;
let args = ["build", "--target", "no-modules"];

thread::spawn(move || {
Expand Down
13 changes: 5 additions & 8 deletions src/commands/build/wranglerjs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::commands::build::watch::wait_for_changes;
use crate::commands::build::watch::COOLDOWN_PERIOD;
use crate::commands::publish::package::Package;
use crate::install;
use crate::install::install_artifact;
use crate::settings::target::Target;
use crate::terminal::message;
use crate::util;
Expand Down Expand Up @@ -152,14 +153,9 @@ fn setup_build(target: &Target) -> Result<(Command, PathBuf, Bundle), failure::E

let node = which::which("node").unwrap();
let mut command = Command::new(node);
let wranglerjs_path = install().expect("could not install wranglerjs");
let wranglerjs_path = install_wranglerjs().expect("could not install wranglerjs");
command.arg(wranglerjs_path);

// export WASM_PACK_PATH for use by wasm-pack-plugin
// https://github.com/wasm-tool/wasm-pack-plugin/blob/caca20df84782223f002735a8a2e99b2291f957c/plugin.js#L13
let wasm_pack_path = install::install("wasm-pack", "rustwasm")?.binary("wasm-pack")?;
command.env("WASM_PACK_PATH", wasm_pack_path);

// create a temp file for IPC with the wranglerjs process
let mut temp_file = env::temp_dir();
temp_file.push(format!(".wranglerjs_output{}", random_chars(5)));
Expand Down Expand Up @@ -292,16 +288,17 @@ fn get_source_dir() -> PathBuf {
}

// Install {wranglerjs} from our GitHub releases
fn install() -> Result<PathBuf, failure::Error> {
fn install_wranglerjs() -> Result<PathBuf, failure::Error> {
let wranglerjs_path = if install::target::DEBUG {
let source_path = get_source_dir();
let wranglerjs_path = source_path.join("wranglerjs");
log::info!("wranglerjs at: {:?}", wranglerjs_path);
wranglerjs_path
} else {
let tool_name = "wranglerjs";
let author = "cloudflare";
let version = env!("CARGO_PKG_VERSION");
let wranglerjs_path = install::install_artifact(tool_name, "cloudflare", version)?;
let wranglerjs_path = install_artifact(tool_name, author, version)?;
log::info!("wranglerjs downloaded at: {:?}", wranglerjs_path.path());
wranglerjs_path.path()
};
Expand Down
5 changes: 3 additions & 2 deletions src/commands/generate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::path::PathBuf;
use std::process::Command;

use crate::commands;
use crate::commands::validate_worker_name;
use crate::install::install;
use crate::settings::target::{Manifest, Site, TargetType};
use crate::{commands, install};

pub fn generate(
name: &str,
Expand Down Expand Up @@ -31,7 +32,7 @@ pub fn generate(

pub fn run_generate(name: &str, template: &str) -> Result<(), failure::Error> {
let tool_name = "cargo-generate";
let binary_path = install::install(tool_name, "ashleygwilliams")?.binary(tool_name)?;
let binary_path = install(tool_name, "ashleygwilliams")?.binary(tool_name)?;

let args = ["generate", "--git", template, "--name", name, "--force"];

Expand Down
63 changes: 62 additions & 1 deletion tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::str;
use std::sync::Mutex;

use assert_cmd::prelude::*;
use fixture::Fixture;
use fixture::{rust, Fixture};

lazy_static! {
static ref BUILD_LOCK: Mutex<u8> = Mutex::new(0);
Expand Down Expand Up @@ -332,6 +332,67 @@ fn it_builds_with_webpack_target_webworker() {
build_creates_assets(fixture, vec!["script.js"]);
}

#[test]
fn it_builds_with_webpack_wasm_pack() {
let fixture = Fixture::new("webpack_wasm_pack");
EverlastingBugstopper marked this conversation as resolved.
Show resolved Hide resolved

fixture.create_dir("crate");
fixture.create_file("crate/Cargo.toml", &rust::get_cargo_toml());

fixture.create_dir("crate/src");
fixture.create_file("crate/src/lib.rs", &rust::get_lib());
fixture.create_file("crate/src/utils.rs", &rust::get_utils());

fixture.create_wrangler_toml(
r#"
type = "webpack"
webpack_config = "webpack.config.js"
"#,
);

fixture.create_file(
"webpack.config.js",
r#"
const path = require("path");
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");

module.exports = {
"entry": "./index.js",
"target": "webworker",
plugins: [
new WasmPackPlugin({
crateDirectory: path.resolve(__dirname, "crate"),
}),
]
}
"#,
);

fixture.create_file(
"package.json",
r#"
{
"name": "webpack_wasm_pack",
"main": "./index.js",
"dependencies": {
"@wasm-tool/wasm-pack-plugin": "^1.0.1"
}
}
"#,
);

fixture.create_file(
"index.js",
r#"
import("./crate/pkg/index.js").then(module => {
module.greet();
});
"#,
);

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

fn build_creates_assets(fixture: Fixture, script_names: Vec<&str>) {
// Lock to avoid having concurrent builds
let _g = BUILD_LOCK.lock().unwrap();
Expand Down
2 changes: 2 additions & 0 deletions tests/fixture/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod rust;

use std::env;
use std::fs;
use std::fs::File;
Expand Down
91 changes: 91 additions & 0 deletions tests/fixture/rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
pub fn get_lib() -> String {
return r#"
extern crate cfg_if;
extern crate wasm_bindgen;

mod utils;

use cfg_if::cfg_if;
use wasm_bindgen::prelude::*;

cfg_if! {
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
if #[cfg(feature = "wee_alloc")] {
extern crate wee_alloc;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
}
}

#[wasm_bindgen]
pub fn greet() -> String {
"Hello, wasm-worker!".to_string()
}
"#
.to_string();
}

pub fn get_utils() -> String {
return r#"
use cfg_if::cfg_if;

cfg_if! {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
if #[cfg(feature = "console_error_panic_hook")] {
extern crate console_error_panic_hook;
pub use self::console_error_panic_hook::set_once as set_panic_hook;
} else {
#[inline]
pub fn set_panic_hook() {}
}
}
"#
.to_string();
}

pub fn get_cargo_toml() -> String {
return r#"
[package]
name = "worker"
version = "0.1.0"
authors = ["The Wrangler Team <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
cfg-if = "0.1.2"
wasm-bindgen = "0.2"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.1", optional = true }

# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.2", optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.2"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
"#
.to_string();
}
Loading