| | use std::io::Write; |
| |
|
| | use anyhow::Result; |
| | use turbo_tasks::{ResolvedVc, Vc}; |
| | use turbo_tasks_env::ProcessEnv; |
| | use turbo_tasks_fs::{File, FileSystemPath, rope::RopeBuilder}; |
| | use turbopack_core::{ |
| | asset::{Asset, AssetContent}, |
| | ident::AssetIdent, |
| | source::Source, |
| | }; |
| | use turbopack_ecmascript::utils::StringifyJs; |
| |
|
| | |
| | |
| | #[turbo_tasks::value] |
| | pub struct ProcessEnvAsset { |
| | |
| | root: FileSystemPath, |
| |
|
| | |
| | env: ResolvedVc<Box<dyn ProcessEnv>>, |
| | } |
| |
|
| | #[turbo_tasks::value_impl] |
| | impl ProcessEnvAsset { |
| | #[turbo_tasks::function] |
| | pub fn new(root: FileSystemPath, env: ResolvedVc<Box<dyn ProcessEnv>>) -> Result<Vc<Self>> { |
| | Ok(ProcessEnvAsset { root, env }.cell()) |
| | } |
| | } |
| |
|
| | #[turbo_tasks::value_impl] |
| | impl Source for ProcessEnvAsset { |
| | #[turbo_tasks::function] |
| | fn ident(&self) -> Result<Vc<AssetIdent>> { |
| | Ok(AssetIdent::from_path(self.root.join(".env.js")?)) |
| | } |
| | } |
| |
|
| | #[turbo_tasks::value_impl] |
| | impl Asset for ProcessEnvAsset { |
| | #[turbo_tasks::function] |
| | async fn content(&self) -> Result<Vc<AssetContent>> { |
| | let env = self.env.read_all().await?; |
| |
|
| | |
| | |
| | |
| | let mut code = RopeBuilder::default(); |
| | code += "const env = process.env = {...process.env};\n\n"; |
| |
|
| | for (name, val) in &*env { |
| | |
| | |
| | |
| | |
| | |
| | writeln!(code, "env[{}] = {};", StringifyJs(name), val)?; |
| | } |
| |
|
| | Ok(AssetContent::file(File::from(code.build()).into())) |
| | } |
| | } |
| |
|