Skip to content

Commit 276150f

Browse files
authored
port js bundle regression fix (#5521)
* port js bundle regression fix * clippy, fmt
1 parent d4e9805 commit 276150f

23 files changed

Lines changed: 973 additions & 51 deletions

File tree

packages/cli/src/build/assets.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ fn legacy_asset_to_modern_asset(
276276
.with_minify(js.minified())
277277
.with_preload(js.preloaded())
278278
.with_static_head(js.static_head())
279+
// Legacy 0.7 manganis has no module concept; default to classic script.
280+
.with_module(false)
279281
.into_asset_options(),
280282
_ => AssetOptions::builder()
281283
.with_hash_suffix(add_hash)

packages/cli/src/build/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,7 @@ impl AppBuilder {
19911991
use std::fmt::Write as _;
19921992

19931993
let total_ms = time_taken as usize;
1994-
let timeline_width = 96usize;
1994+
let timeline_width = 48usize;
19951995
let max_label_width = self
19961996
.profile_spans
19971997
.iter()

packages/cli/src/build/web.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
//! ```
3535
3636
use crate::{BuildContext, BundleFormat, Result, TraceSrc, WasmBindgen, WasmOptConfig};
37-
use crate::{BuildMode, BuildRequest, opt::AppManifest};
37+
use crate::{
38+
BuildMode, BuildRequest,
39+
opt::{AppManifest, js_is_module},
40+
};
3841
use anyhow::Context;
3942
use dioxus_cli_config::format_base_path_meta_element;
4043
use manganis::AssetOptions;
@@ -486,9 +489,15 @@ __wbg_init({{module_or_path: "/{}/{wasm_path}"}}).then((wasm) => {{
486489
);
487490
}
488491
if js_options.static_head() {
492+
let source = std::path::Path::new(asset.absolute_source_path());
493+
let module_attr = if js_is_module(js_options, source) {
494+
r#" type="module""#
495+
} else {
496+
""
497+
};
489498
_ = write!(
490499
head_resources,
491-
r#"<script src="/{{base_path}}/assets/{asset_path}"></script>"#
500+
r#"<script{module_attr} src="/{{base_path}}/assets/{asset_path}"></script>"#
492501
);
493502
}
494503
}

packages/cli/src/cli/bundle.rs

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl Bundle {
5959

6060
// Fill platform-specific defaults for package types when omitted.
6161
if self.package_types.is_none() {
62-
self.package_types = Self::default_package_types(client.bundle);
62+
self.package_types = Some(Self::default_package_types(client.bundle));
6363
}
6464

6565
Self::validate_package_types_for_bundle(client.bundle, self.package_types.as_deref())?;
@@ -125,11 +125,9 @@ impl Bundle {
125125
}
126126
}
127127

128+
println!("Bundled {} bundles:", bundles.len());
128129
for bundle_path in bundles.iter() {
129-
tracing::info!(
130-
"Bundled app at: {}",
131-
bundle_path.absolutize().unwrap().display()
132-
);
130+
println!("{}", bundle_path.absolutize().unwrap().display());
133131
}
134132

135133
let client = client_artifacts.into_structured_output();
@@ -195,11 +193,14 @@ impl Bundle {
195193
Ok(bundles)
196194
}
197195

198-
fn default_package_types(bundle: BundleFormat) -> Option<Vec<PackageType>> {
196+
fn default_package_types(bundle: BundleFormat) -> Vec<PackageType> {
199197
match bundle {
200-
BundleFormat::Ios => Some(vec![crate::PackageType::Ipa]),
201-
BundleFormat::Android => Some(vec![crate::PackageType::Aab]),
202-
_ => None,
198+
BundleFormat::Web | BundleFormat::Server => Vec::new(),
199+
BundleFormat::MacOS => vec![PackageType::MacOsBundle, PackageType::Dmg],
200+
BundleFormat::Windows => vec![PackageType::Nsis],
201+
BundleFormat::Linux => vec![PackageType::AppImage],
202+
BundleFormat::Ios => vec![PackageType::Ipa],
203+
BundleFormat::Android => vec![PackageType::Aab],
203204
}
204205
}
205206

@@ -271,11 +272,55 @@ mod tests {
271272
};
272273

273274
#[test]
274-
fn ios_bundle_defaults_to_ipa() {
275+
fn bundle_formats_have_default_package_types() {
275276
assert_eq!(
276277
Bundle::default_package_types(BundleFormat::Ios),
277-
Some(vec![PackageType::Ipa])
278+
vec![PackageType::Ipa]
279+
);
280+
assert_eq!(
281+
Bundle::default_package_types(BundleFormat::Android),
282+
vec![PackageType::Aab]
283+
);
284+
assert_eq!(
285+
Bundle::default_package_types(BundleFormat::MacOS),
286+
vec![PackageType::MacOsBundle, PackageType::Dmg]
278287
);
288+
assert_eq!(
289+
Bundle::default_package_types(BundleFormat::Windows),
290+
vec![PackageType::Nsis]
291+
);
292+
assert_eq!(
293+
Bundle::default_package_types(BundleFormat::Linux),
294+
vec![PackageType::AppImage]
295+
);
296+
assert_eq!(Bundle::default_package_types(BundleFormat::Web), vec![]);
297+
assert_eq!(Bundle::default_package_types(BundleFormat::Server), vec![]);
298+
}
299+
300+
#[test]
301+
fn default_package_types_are_valid_for_their_bundle_format() {
302+
for bundle_format in [
303+
BundleFormat::Web,
304+
BundleFormat::MacOS,
305+
BundleFormat::Windows,
306+
BundleFormat::Linux,
307+
BundleFormat::Server,
308+
BundleFormat::Ios,
309+
BundleFormat::Android,
310+
] {
311+
let package_types = Bundle::default_package_types(bundle_format);
312+
Bundle::validate_package_types_for_bundle(bundle_format, Some(&package_types)).unwrap();
313+
}
314+
}
315+
316+
#[test]
317+
fn omitted_package_types_are_filled_with_defaults() {
318+
let mut package_types = None;
319+
if package_types.is_none() {
320+
package_types = Some(Bundle::default_package_types(BundleFormat::Linux));
321+
}
322+
323+
assert_eq!(package_types, Some(vec![PackageType::AppImage]));
279324
}
280325

281326
#[test]

packages/cli/src/opt/file.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@ pub(crate) fn process_file_to(
1616
source: &Path,
1717
output_path: &Path,
1818
esbuild_path: Option<&Path>,
19-
) -> anyhow::Result<()> {
20-
process_file_to_with_options(options, source, output_path, false, esbuild_path)
21-
}
22-
23-
/// Process a specific file asset with additional options
24-
pub(crate) fn process_file_to_with_options(
25-
options: &AssetOptions,
26-
source: &Path,
27-
output_path: &Path,
28-
in_folder: bool,
29-
esbuild_path: Option<&Path>,
3019
) -> anyhow::Result<()> {
3120
// If the file already exists and this is a hashed asset, then we must have a file
3221
// with the same hash already. The hash has the file contents and options, so if we
@@ -63,7 +52,7 @@ pub(crate) fn process_file_to_with_options(
6352
process_scss(options, source, &temp_path)?;
6453
}
6554
ResolvedAssetType::Js(options) => {
66-
process_js(options, source, &temp_path, !in_folder, esbuild_path)?;
55+
process_js(options, source, &temp_path, esbuild_path)?;
6756
}
6857
ResolvedAssetType::Image(options) => {
6958
process_image(options, source, &temp_path)?;

packages/cli/src/opt/folder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::path::Path;
22

33
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
44

5-
use crate::opt::file::process_file_to_with_options;
5+
use crate::opt::file::process_file_to;
66

77
/// Process a folder, optimizing and copying all assets into the output folder
88
pub fn process_folder(
@@ -40,11 +40,10 @@ fn process_file_minimal(
4040
output_path: &Path,
4141
esbuild_path: Option<&Path>,
4242
) -> anyhow::Result<()> {
43-
process_file_to_with_options(
43+
process_file_to(
4444
&manganis_core::AssetOptions::builder().into_asset_options(),
4545
input_path,
4646
output_path,
47-
true,
4847
esbuild_path,
4948
)?;
5049
Ok(())

packages/cli/src/opt/hash.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn hash_file(options: &AssetOptions, source: &Path) -> anyhow::Result<AssetHash>
5050

5151
// Hash the version of the CLI
5252
hash.write(crate::dx_build_info::PKG_VERSION.as_bytes());
53-
hash_file_with_options(options, source, &mut hash, false)?;
53+
hash_file_with_options(options, source, &mut hash)?;
5454

5555
let hash = hash.finish();
5656
Ok(AssetHash::new(hash))
@@ -61,7 +61,6 @@ pub(crate) fn hash_file_with_options(
6161
options: &AssetOptions,
6262
source: &Path,
6363
hasher: &mut impl Hasher,
64-
in_folder: bool,
6564
) -> anyhow::Result<()> {
6665
let resolved_options = resolve_asset_options(source, options.variant());
6766

@@ -73,7 +72,7 @@ pub(crate) fn hash_file_with_options(
7372
}
7473

7574
ResolvedAssetType::Js(options) => {
76-
hash_js(options, source, hasher, !in_folder)?;
75+
hash_js(options, source, hasher)?;
7776
}
7877

7978
// Otherwise, we can just hash the file contents
@@ -100,7 +99,6 @@ pub(crate) fn hash_file_with_options(
10099
.into_asset_options(),
101100
&path,
102101
hasher,
103-
true,
104102
)?;
105103
}
106104
}

0 commit comments

Comments
 (0)