forked from vercel/turborepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89d0046
commit e857c37
Showing
61 changed files
with
751 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,6 @@ insert_final_newline = false | |
|
||
[LICENSE] | ||
insert_final_newline = false | ||
|
||
[*.rs] | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "@vercel/turbopack-next", | ||
"version": "0.0.0", | ||
"description": "turbopack next runtime", | ||
"license": "UNLICENSED", | ||
"private": true, | ||
"peerDependencies": { | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use anyhow::Result; | ||
use turbo_tasks_fs::{ | ||
attach::AttachedFileSystemVc, embed_directory, FileContentVc, FileSystemPathVc, FileSystemVc, | ||
}; | ||
|
||
pub const VIRTUAL_PACKAGE_NAME: &str = "@vercel/turbopack-next"; | ||
|
||
#[turbo_tasks::function] | ||
pub(crate) fn next_js_fs() -> FileSystemVc { | ||
embed_directory!("next", "$CARGO_MANIFEST_DIR/js") | ||
} | ||
|
||
#[turbo_tasks::function] | ||
pub(crate) fn next_js_file(path: &str) -> FileContentVc { | ||
next_js_fs().root().join(path).read() | ||
} | ||
|
||
#[turbo_tasks::function] | ||
pub(crate) async fn attached_next_js_package_path( | ||
project_path: FileSystemPathVc, | ||
) -> FileSystemPathVc { | ||
project_path.join(&format!("[embedded_modules]/{}", VIRTUAL_PACKAGE_NAME)) | ||
} | ||
|
||
#[turbo_tasks::function] | ||
pub(crate) async fn wrap_with_next_js_fs( | ||
project_path: FileSystemPathVc, | ||
) -> Result<FileSystemPathVc> { | ||
let attached_path = attached_next_js_package_path(project_path); | ||
let fs = AttachedFileSystemVc::new(attached_path, next_js_fs()); | ||
Ok(fs.convert_path(project_path)) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,108 @@ | ||
use turbo_tasks_fs::FileSystemPathVc; | ||
use turbopack_core::{ | ||
asset::AssetVc, | ||
resolve::{ | ||
options::{ImportMap, ImportMapVc, ImportMapping, ImportMappingVc}, | ||
AliasPattern, ResolveResult, | ||
}, | ||
virtual_asset::VirtualAssetVc, | ||
}; | ||
use turbopack_core::resolve::options::{ImportMap, ImportMapVc, ImportMapping, ImportMappingVc}; | ||
|
||
use crate::embed_next_file; | ||
use crate::embed_js::{attached_next_js_package_path, VIRTUAL_PACKAGE_NAME}; | ||
|
||
/// Computes the Next-specific client import map. | ||
#[turbo_tasks::function] | ||
pub fn get_next_client_import_map(pages_dir: FileSystemPathVc) -> ImportMapVc { | ||
pub fn get_next_client_import_map( | ||
project_path: FileSystemPathVc, | ||
pages_dir: FileSystemPathVc, | ||
) -> ImportMapVc { | ||
let mut import_map = ImportMap::empty(); | ||
let package_root = attached_next_js_package_path(project_path); | ||
|
||
insert_next_shared_aliases(&mut import_map, pages_dir); | ||
insert_next_shared_aliases(&mut import_map, package_root); | ||
|
||
insert_alias_to_alternatives( | ||
&mut import_map, | ||
"@vercel/turbopack-next/pages/_app", | ||
request_to_import_mapping(pages_dir, "./_app"), | ||
request_to_import_mapping(pages_dir, "next/app"), | ||
format!("{VIRTUAL_PACKAGE_NAME}/pages/_app"), | ||
vec![ | ||
request_to_import_mapping(pages_dir, "./_app"), | ||
request_to_import_mapping(pages_dir, "next/app"), | ||
], | ||
); | ||
insert_alias_to_alternatives( | ||
&mut import_map, | ||
"@vercel/turbopack-next/pages/_document", | ||
request_to_import_mapping(pages_dir, "./_document"), | ||
request_to_import_mapping(pages_dir, "next/document"), | ||
format!("{VIRTUAL_PACKAGE_NAME}/pages/_document"), | ||
vec![ | ||
request_to_import_mapping(pages_dir, "./_document"), | ||
request_to_import_mapping(pages_dir, "next/document"), | ||
], | ||
); | ||
|
||
import_map.cell() | ||
} | ||
|
||
/// Computes the Next-specific server-side import map. | ||
#[turbo_tasks::function] | ||
pub fn get_next_server_import_map(pages_dir: FileSystemPathVc) -> ImportMapVc { | ||
pub fn get_next_server_import_map( | ||
project_path: FileSystemPathVc, | ||
pages_dir: FileSystemPathVc, | ||
) -> ImportMapVc { | ||
let mut import_map = ImportMap::empty(); | ||
let package_root = attached_next_js_package_path(project_path); | ||
|
||
insert_next_shared_aliases(&mut import_map, pages_dir); | ||
insert_next_shared_aliases(&mut import_map, package_root); | ||
|
||
insert_alias_to_alternatives( | ||
&mut import_map, | ||
"@vercel/turbopack-next/pages/_app", | ||
request_to_import_mapping(pages_dir, "./_app"), | ||
external_request_to_import_mapping("next/app"), | ||
format!("{VIRTUAL_PACKAGE_NAME}/pages/_app"), | ||
vec![ | ||
request_to_import_mapping(pages_dir, "./_app"), | ||
external_request_to_import_mapping("next/app"), | ||
], | ||
); | ||
insert_alias_to_alternatives( | ||
&mut import_map, | ||
"@vercel/turbopack-next/pages/_document", | ||
request_to_import_mapping(pages_dir, "./_document"), | ||
external_request_to_import_mapping("next/document"), | ||
format!("{VIRTUAL_PACKAGE_NAME}/pages/_document"), | ||
vec![ | ||
request_to_import_mapping(pages_dir, "./_document"), | ||
external_request_to_import_mapping("next/document"), | ||
], | ||
); | ||
|
||
import_map.insert_alias( | ||
AliasPattern::exact("next"), | ||
ImportMapping::External(None).into(), | ||
); | ||
import_map.insert_alias( | ||
AliasPattern::wildcard("next/", ""), | ||
ImportMapping::External(None).into(), | ||
); | ||
import_map.insert_alias( | ||
AliasPattern::exact("react"), | ||
ImportMapping::External(None).into(), | ||
); | ||
import_map.insert_alias( | ||
AliasPattern::wildcard("react/", ""), | ||
ImportMapping::External(None).into(), | ||
); | ||
import_map.insert_exact_alias("next", ImportMapping::External(None).into()); | ||
import_map.insert_wildcard_alias("next/", ImportMapping::External(None).into()); | ||
import_map.insert_exact_alias("react", ImportMapping::External(None).into()); | ||
import_map.insert_wildcard_alias("react/", ImportMapping::External(None).into()); | ||
|
||
import_map.cell() | ||
} | ||
|
||
fn insert_next_shared_aliases(import_map: &mut ImportMap, pages_dir: FileSystemPathVc) { | ||
import_map.insert_alias( | ||
AliasPattern::exact("@vercel/turbopack-next/internal/shims"), | ||
asset_to_import_mapping(get_internal_shims_asset(pages_dir)), | ||
fn insert_next_shared_aliases(import_map: &mut ImportMap, package_root: FileSystemPathVc) { | ||
insert_package_alias( | ||
import_map, | ||
&format!("{VIRTUAL_PACKAGE_NAME}/"), | ||
package_root, | ||
); | ||
} | ||
|
||
#[turbo_tasks::function] | ||
fn get_internal_shims_asset(pages_dir: FileSystemPathVc) -> AssetVc { | ||
VirtualAssetVc::new( | ||
pages_dir.root().join("next_js/internal/shims.js"), | ||
embed_next_file!("internal/shims.js").into(), | ||
) | ||
.into() | ||
} | ||
|
||
/// Inserts an alias to an alternative of import mappings into an import map. | ||
fn insert_alias_to_alternatives( | ||
import_map: &mut ImportMap, | ||
alias: &str, | ||
alt1: ImportMappingVc, | ||
alt2: ImportMappingVc, | ||
alias: impl ToString, | ||
alternatives: Vec<ImportMappingVc>, | ||
) { | ||
import_map.insert_alias( | ||
AliasPattern::Exact(alias.to_string()), | ||
ImportMapping::Alternatives(vec![alt1, alt2]).into(), | ||
import_map.insert_exact_alias(alias, ImportMapping::Alternatives(alternatives).into()); | ||
} | ||
|
||
/// Inserts an alias to an import mapping into an import map. | ||
fn insert_package_alias(import_map: &mut ImportMap, prefix: &str, package_root: FileSystemPathVc) { | ||
import_map.insert_wildcard_alias( | ||
prefix, | ||
ImportMapping::PrimaryAlternative("./*".to_string(), Some(package_root)).cell(), | ||
); | ||
} | ||
|
||
/// Creates a direct import mapping to the result of resolving a request | ||
/// in a context. | ||
fn request_to_import_mapping(context_path: FileSystemPathVc, request: &str) -> ImportMappingVc { | ||
ImportMapping::PrimaryAlternative(request.to_string(), Some(context_path)).into() | ||
ImportMapping::PrimaryAlternative(request.to_string(), Some(context_path)).cell() | ||
} | ||
|
||
/// Creates a direct import mapping to the result of resolving an external | ||
/// request. | ||
fn external_request_to_import_mapping(request: &str) -> ImportMappingVc { | ||
ImportMapping::External(Some(request.to_string())).into() | ||
} | ||
|
||
/// Creates a direct import mapping to a single asset. | ||
fn asset_to_import_mapping(asset: AssetVc) -> ImportMappingVc { | ||
ImportMapping::Direct(ResolveResult::Single(asset, vec![]).into()).into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.