@@ -10,7 +10,10 @@ use turbo_tasks_fs::{File, FileContent, FileJsonContent, FileSystem, FileSystemP
1010use turbopack:: module_options:: RuleCondition ;
1111use turbopack_core:: {
1212 asset:: AssetContent ,
13- compile_time_info:: { CompileTimeDefineValue , CompileTimeDefines , DefinableNameSegment } ,
13+ compile_time_info:: {
14+ CompileTimeDefineValue , CompileTimeDefines , DefinableNameSegment , FreeVarReference ,
15+ FreeVarReferences ,
16+ } ,
1417 condition:: ContextCondition ,
1518 source:: Source ,
1619 virtual_source:: VirtualSource ,
@@ -58,6 +61,99 @@ pub fn defines(define_env: &FxIndexMap<RcStr, Option<RcStr>>) -> CompileTimeDefi
5861 CompileTimeDefines ( defines)
5962}
6063
64+ /// Emits warnings or errors when inlining frequently changing system env vars
65+ pub fn free_var_references_with_vercel_system_env_warnings (
66+ defines : CompileTimeDefines ,
67+ ) -> FreeVarReferences {
68+ // constant:
69+ // VERCEL
70+ // CI
71+ // VERCEL_PROJECT_PRODUCTION_URL
72+ // VERCEL_REGION
73+ // VERCEL_SKEW_PROTECTION_ENABLED
74+ // VERCEL_AUTOMATION_BYPASS_SECRET
75+ // VERCEL_PROJECT_ID
76+ // VERCEL_GIT_PROVIDER
77+ // VERCEL_GIT_REPO_SLUG
78+ // VERCEL_GIT_REPO_OWNER
79+ // VERCEL_GIT_REPO_ID
80+
81+ // suboptimal (changes production main branch VS preview branches):
82+ // VERCEL_ENV
83+ // VERCEL_TARGET_ENV
84+
85+ // bad (changes per branch):
86+ // VERCEL_BRANCH_URL
87+ // VERCEL_GIT_COMMIT_REF
88+ // VERCEL_GIT_PULL_REQUEST_ID
89+
90+ // catastrophic (changes per commit):
91+ // VERCEL_URL
92+ // VERCEL_DEPLOYMENT_ID
93+ // VERCEL_OIDC_TOKEN
94+ // VERCEL_GIT_COMMIT_SHA
95+ // VERCEL_GIT_COMMIT_MESSAGE
96+ // VERCEL_GIT_COMMIT_AUTHOR_LOGIN
97+ // VERCEL_GIT_COMMIT_AUTHOR_NAME
98+ // VERCEL_GIT_PREVIOUS_SHA
99+
100+ let should_error = std:: env:: var ( "NEXT_TURBOPACK_SYSTEM_ENV_ERROR" )
101+ . ok ( )
102+ . is_some_and ( |v| !v. is_empty ( ) ) ;
103+
104+ FreeVarReferences (
105+ defines
106+ . 0
107+ . into_iter ( )
108+ . map ( |( k, value) | {
109+ const LIST : [ & str ; 11 ] = [
110+ "VERCEL_BRANCH_URL" ,
111+ "VERCEL_DEPLOYMENT_ID" ,
112+ "VERCEL_GIT_COMMIT_AUTHOR_LOGIN" ,
113+ "VERCEL_GIT_COMMIT_AUTHOR_NAME" ,
114+ "VERCEL_GIT_COMMIT_MESSAGE" ,
115+ "VERCEL_GIT_COMMIT_REF" ,
116+ "VERCEL_GIT_COMMIT_SHA" ,
117+ "VERCEL_GIT_PREVIOUS_SHA" ,
118+ "VERCEL_GIT_PULL_REQUEST_ID" ,
119+ "VERCEL_OIDC_TOKEN" ,
120+ "VERCEL_URL" ,
121+ ] ;
122+
123+ let value = if let & [
124+ DefinableNameSegment :: Name ( a) ,
125+ DefinableNameSegment :: Name ( b) ,
126+ DefinableNameSegment :: Name ( c) ,
127+ ] = & & * k
128+ && a == "process"
129+ && b == "env"
130+ && c. strip_prefix ( "NEXT_PUBLIC_" )
131+ . is_some_and ( |n| LIST . binary_search ( & n) . is_ok ( ) )
132+ {
133+ let message = format ! (
134+ "The system environment variable {} is being inlined. This variable \
135+ changes on every deployment, causing slower deploy times and worse \
136+ browser client-side caching.",
137+ c
138+ )
139+ . into ( ) ;
140+ if should_error {
141+ FreeVarReference :: Error ( message)
142+ } else {
143+ FreeVarReference :: Warning {
144+ message,
145+ inner : Box :: new ( FreeVarReference :: Value ( value) ) ,
146+ }
147+ }
148+ } else {
149+ FreeVarReference :: Value ( value)
150+ } ;
151+ ( k, value)
152+ } )
153+ . collect ( ) ,
154+ )
155+ }
156+
61157#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , TaskInput , TraceRawVcs , Encode , Decode ) ]
62158pub enum PathType {
63159 PagesPage ,
0 commit comments