@@ -5,7 +5,7 @@ use bincode::{Decode, Encode};
55use next_taskless:: { expand_next_js_template, expand_next_js_template_no_imports} ;
66use serde:: { Deserialize , de:: DeserializeOwned } ;
77use turbo_rcstr:: { RcStr , rcstr} ;
8- use turbo_tasks:: { FxIndexMap , NonLocalValue , TaskInput , Vc , trace:: TraceRawVcs } ;
8+ use turbo_tasks:: { FxIndexMap , NonLocalValue , TaskInput , Vc , fxindexset , trace:: TraceRawVcs } ;
99use turbo_tasks_fs:: { File , FileContent , FileJsonContent , FileSystem , FileSystemPath , rope:: Rope } ;
1010use turbopack:: module_options:: RuleCondition ;
1111use turbopack_core:: {
@@ -62,9 +62,10 @@ pub fn defines(define_env: &FxIndexMap<RcStr, Option<RcStr>>) -> CompileTimeDefi
6262 CompileTimeDefines ( defines)
6363}
6464
65- /// Emits warnings or errors when inlining frequently changing system env vars
65+ /// Emits warnings or errors when inlining frequently changing Vercel system env vars
6666pub fn free_var_references_with_vercel_system_env_warnings (
6767 defines : CompileTimeDefines ,
68+ has_next_support : bool ,
6869) -> FreeVarReferences {
6970 // constant:
7071 // VERCEL
@@ -98,74 +99,104 @@ pub fn free_var_references_with_vercel_system_env_warnings(
9899 // VERCEL_GIT_COMMIT_AUTHOR_NAME
99100 // VERCEL_GIT_PREVIOUS_SHA
100101
101- let should_error = std:: env:: var ( "NEXT_TURBOPACK_SYSTEM_ENV_ERROR" )
102- . ok ( )
103- . is_some_and ( |v| !v. is_empty ( ) ) ;
102+ let entries = defines
103+ . 0
104+ . into_iter ( )
105+ . map ( |( k, value) | ( k, FreeVarReference :: Value ( value) ) ) ;
106+
107+ let entries = if has_next_support {
108+ let should_error = std:: env:: var ( "NEXT_TURBOPACK_SYSTEM_ENV_ERROR" )
109+ . ok ( )
110+ . is_some_and ( |v| !v. is_empty ( ) ) ;
111+
112+ fn wrap_report_next_public_usage (
113+ public_env_var : & str ,
114+ inner : Option < Box < FreeVarReference > > ,
115+ should_error : bool ,
116+ ) -> FreeVarReference {
117+ let message = match public_env_var {
118+ "NEXT_DEPLOYMENT_ID" | "VERCEL_DEPLOYMENT_ID" => {
119+ rcstr ! (
120+ "The deployment id is being inlined. Use process.env.NEXT_DEPLOYMENT_ID \
121+ instead to access the same value without inlining, for faster deploy \
122+ times and better browser client-side caching."
123+ )
124+ }
125+ _ => format ! (
126+ "A system environment variable is being inlined. This variable changes on \
127+ every deployment, causing slower deploy times and worse browser client-side \
128+ caching. For server-side code, replace with process.env.{} and for browser \
129+ code, try to remove it.",
130+ public_env_var. strip_prefix( "NEXT_PUBLIC_" ) . unwrap( ) ,
131+ )
132+ . into ( ) ,
133+ } ;
134+ FreeVarReference :: ReportUsage {
135+ message,
136+ severity : if should_error {
137+ IssueSeverity :: Error
138+ } else {
139+ IssueSeverity :: Warning
140+ } ,
141+ inner,
142+ }
143+ }
104144
105- FreeVarReferences (
106- defines
107- . 0
108- . into_iter ( )
145+ let mut list = fxindexset ! (
146+ "NEXT_PUBLIC_NEXT_DEPLOYMENT_ID" ,
147+ "NEXT_PUBLIC_VERCEL_BRANCH_URL" ,
148+ "NEXT_PUBLIC_VERCEL_DEPLOYMENT_ID" ,
149+ "NEXT_PUBLIC_VERCEL_GIT_COMMIT_AUTHOR_LOGIN" ,
150+ "NEXT_PUBLIC_VERCEL_GIT_COMMIT_AUTHOR_NAME" ,
151+ "NEXT_PUBLIC_VERCEL_GIT_COMMIT_MESSAGE" ,
152+ "NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF" ,
153+ "NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA" ,
154+ "NEXT_PUBLIC_VERCEL_GIT_PREVIOUS_SHA" ,
155+ "NEXT_PUBLIC_VERCEL_GIT_PULL_REQUEST_ID" ,
156+ "NEXT_PUBLIC_VERCEL_OIDC_TOKEN" ,
157+ "NEXT_PUBLIC_VERCEL_URL" ,
158+ ) ;
159+
160+ let mut entries: FxIndexMap < _ , _ > = entries
109161 . map ( |( k, value) | {
110- const LIST : [ & str ; 12 ] = [
111- "NEXT_DEPLOYMENT_ID" ,
112- "VERCEL_BRANCH_URL" ,
113- "VERCEL_DEPLOYMENT_ID" ,
114- "VERCEL_GIT_COMMIT_AUTHOR_LOGIN" ,
115- "VERCEL_GIT_COMMIT_AUTHOR_NAME" ,
116- "VERCEL_GIT_COMMIT_MESSAGE" ,
117- "VERCEL_GIT_COMMIT_REF" ,
118- "VERCEL_GIT_COMMIT_SHA" ,
119- "VERCEL_GIT_PREVIOUS_SHA" ,
120- "VERCEL_GIT_PULL_REQUEST_ID" ,
121- "VERCEL_OIDC_TOKEN" ,
122- "VERCEL_URL" ,
123- ] ;
124-
125162 let value = if let & [
126163 DefinableNameSegment :: Name ( a) ,
127164 DefinableNameSegment :: Name ( b) ,
128- DefinableNameSegment :: Name ( c ) ,
165+ DefinableNameSegment :: Name ( public_env_var ) ,
129166 ] = & & * k
130167 && a == "process"
131168 && b == "env"
132- && let Some ( env_var) = c. strip_prefix ( "NEXT_PUBLIC_" )
133- && LIST . binary_search ( & env_var) . is_ok ( )
169+ && list. swap_remove ( & * * public_env_var)
134170 {
135- let message = match & * * c {
136- "NEXT_PUBLIC_NEXT_DEPLOYMENT_ID" | "NEXT_PUBLIC_VERCEL_DEPLOYMENT_ID" => {
137- rcstr ! (
138- "The deployment id is being inlined. Use \
139- process.env.NEXT_DEPLOYMENT_ID instead to access the same value \
140- without inlining, for faster deploy times and better browser \
141- client-side caching."
142- )
143- }
144- _ => format ! (
145- "A system environment variable is being inlined. This variable \
146- changes on every deployment, causing slower deploy times and worse \
147- browser client-side caching. For server-side code, replace with \
148- process.env.{} and for browser code, try to remove it.",
149- env_var,
150- )
151- . into ( ) ,
152- } ;
153- FreeVarReference :: ReportUsage {
154- message,
155- severity : if should_error {
156- IssueSeverity :: Error
157- } else {
158- IssueSeverity :: Warning
159- } ,
160- inner : Some ( Box :: new ( FreeVarReference :: Value ( value) ) ) ,
161- }
171+ wrap_report_next_public_usage (
172+ public_env_var,
173+ Some ( Box :: new ( value) ) ,
174+ should_error,
175+ )
162176 } else {
163- FreeVarReference :: Value ( value)
177+ value
164178 } ;
165179 ( k, value)
166180 } )
167- . collect ( ) ,
168- )
181+ . collect ( ) ;
182+
183+ // For the remaining ones, still add a warning, but without replacement
184+ for public_env_var in list {
185+ entries. insert (
186+ vec ! [
187+ rcstr!( "process" ) . into( ) ,
188+ rcstr!( "env" ) . into( ) ,
189+ DefinableNameSegment :: Name ( public_env_var. into( ) ) ,
190+ ] ,
191+ wrap_report_next_public_usage ( public_env_var, None , should_error) ,
192+ ) ;
193+ }
194+ entries
195+ } else {
196+ entries. collect ( )
197+ } ;
198+
199+ FreeVarReferences ( entries)
169200}
170201
171202#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , TaskInput , TraceRawVcs , Encode , Decode ) ]
0 commit comments