File size: 3,048 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use anyhow::Result;
use next_core::{next_manifests::LoadableManifest, util::NextRuntime};
use rustc_hash::FxHashMap;
use turbo_tasks::{ResolvedVc, TryFlatJoinIterExt, Vc};
use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{
    asset::AssetContent,
    output::{OutputAsset, OutputAssets},
    virtual_output::VirtualOutputAsset,
};
use turbopack_ecmascript::utils::StringifyJs;

use crate::dynamic_imports::DynamicImportedChunks;

#[turbo_tasks::function]
pub async fn create_react_loadable_manifest(
    dynamic_import_entries: Vc<DynamicImportedChunks>,
    client_relative_path: FileSystemPath,
    output_path: FileSystemPath,
    runtime: NextRuntime,
) -> Result<Vc<OutputAssets>> {
    let dynamic_import_entries = &*dynamic_import_entries.await?;

    let mut loadable_manifest: FxHashMap<String, LoadableManifest> = FxHashMap::default();

    for (_, (module_id, chunk_output)) in dynamic_import_entries.into_iter() {
        let chunk_output = chunk_output.await?;

        let id = &*module_id.await?;

        let client_relative_path_value = client_relative_path.clone();
        let files = chunk_output
            .iter()
            .map(move |&file| {
                let client_relative_path_value = client_relative_path_value.clone();
                async move {
                    Ok(client_relative_path_value
                        .get_path_to(&*file.path().await?)
                        .map(|path| path.into()))
                }
            })
            .try_flat_join()
            .await?;

        let manifest_item = LoadableManifest {
            id: id.into(),
            files,
        };

        loadable_manifest.insert(id.to_string(), manifest_item);
    }

    let manifest_json = serde_json::to_string_pretty(&loadable_manifest)?;

    Ok(Vc::cell(match runtime {
        NextRuntime::NodeJs => vec![ResolvedVc::upcast(
            VirtualOutputAsset::new(
                output_path.with_extension("json"),
                AssetContent::file(FileContent::Content(File::from(manifest_json)).cell()),
            )
            .to_resolved()
            .await?,
        )],
        NextRuntime::Edge => vec![
            ResolvedVc::upcast(
                VirtualOutputAsset::new(
                    output_path.with_extension("js"),
                    AssetContent::file(
                        FileContent::Content(File::from(format!(
                            "self.__REACT_LOADABLE_MANIFEST={};",
                            StringifyJs(&manifest_json)
                        )))
                        .cell(),
                    ),
                )
                .to_resolved()
                .await?,
            ),
            ResolvedVc::upcast(
                VirtualOutputAsset::new(
                    output_path.with_extension("json"),
                    AssetContent::file(FileContent::Content(File::from(manifest_json)).cell()),
                )
                .to_resolved()
                .await?,
            ),
        ],
    }))
}