Skip to content

Commit b2d3c0a

Browse files
committed
chore: eslint config to prevent usage of import.meta.env.X without optional chaining or qTest
1 parent 6042ca9 commit b2d3c0a

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

eslint.config.mjs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,64 @@ export default tseslint.config(
133133
'qwik-local/loop-style': 'error',
134134
},
135135
},
136+
{
137+
// Webpack-safety: every `import.meta.env.X` access must use optional chaining.
138+
// Applies to both core and qwik-router source — non-Vite consumers ship both.
139+
files: ['packages/qwik/src/**/*.{ts,tsx}', 'packages/qwik-router/src/**/*.{ts,tsx}'],
140+
ignores: ['**/*.unit.*', '**/*.spec.*', '**/*.d.ts'],
141+
rules: {
142+
'no-restricted-syntax': [
143+
'error',
144+
{
145+
selector:
146+
"MemberExpression[object.type='MemberExpression']" +
147+
"[object.object.type='MetaProperty']" +
148+
"[object.property.name='env']" +
149+
':not([optional=true])',
150+
message:
151+
'Use `import.meta.env?.X` (optional chaining). Non-Vite consumers (webpack, ' +
152+
'plain Node, etc.) ship our published bundles where `import.meta.env` is ' +
153+
'undefined and `.X` access throws a TypeError.',
154+
},
155+
],
156+
},
157+
},
158+
{
159+
// Test-mode gating: prefer the `qTest` const over `import.meta.env?.TEST`.
160+
// Scoped to qwik core only. qwik-router source can't reach `qTest` (it's in core's
161+
// internal `shared/utils/qdev`, not part of the public API), so this rule doesn't
162+
// apply there — its `import.meta.env?.TEST` usage is intentional.
163+
files: ['packages/qwik/src/**/*.{ts,tsx}'],
164+
ignores: ['**/*.unit.*', '**/*.spec.*', '**/*.d.ts'],
165+
rules: {
166+
'no-restricted-syntax': [
167+
'error',
168+
{
169+
selector:
170+
"MemberExpression[object.type='MemberExpression']" +
171+
"[object.object.type='MetaProperty']" +
172+
"[object.property.name='env']" +
173+
':not([optional=true])',
174+
message:
175+
'Use `import.meta.env?.X` (optional chaining). Non-Vite consumers (webpack, ' +
176+
'plain Node, etc.) ship our published bundles where `import.meta.env` is ' +
177+
'undefined and `.X` access throws a TypeError.',
178+
},
179+
{
180+
selector:
181+
'MemberExpression[optional=true]' +
182+
"[object.type='MemberExpression']" +
183+
"[object.object.type='MetaProperty']" +
184+
"[object.property.name='env']" +
185+
"[property.name='TEST']",
186+
message:
187+
'Use the `qTest` const from `<...>/shared/utils/qdev` instead of ' +
188+
'`import.meta.env?.TEST`. `qTest` reads `globalThis.qTest` (webpack-safe) AND ' +
189+
'lets Terser fold it via `global_defs` for prod tree-shaking.',
190+
},
191+
],
192+
},
193+
},
136194
{
137195
files: ['packages/qwik/src/server/**/*.ts'],
138196
ignores: ['packages/qwik/src/server/qwik-copy.ts'],

0 commit comments

Comments
 (0)