@varlock/nextjs-integration 1.0.1 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/loader.js +1 -1
- package/dist/loader.js.map +1 -1
- package/dist/next-env-compat.js +2413 -2379
- package/dist/next-env-compat.js.map +1 -1
- package/package.json +3 -3
package/dist/loader.js
CHANGED
|
@@ -7,7 +7,7 @@ var __commonJS = (cb, mod) => function __require() {
|
|
|
7
7
|
|
|
8
8
|
// src/loader.ts
|
|
9
9
|
var require_loader = __commonJS({
|
|
10
|
-
"src/loader.ts"(exports
|
|
10
|
+
"src/loader.ts"(exports, module) {
|
|
11
11
|
function debug(...args) {
|
|
12
12
|
if (!process.env.DEBUG_VARLOCK_NEXT_INTEGRATION) return;
|
|
13
13
|
console.log("[varlock-loader]", ...args);
|
package/dist/loader.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/loader.ts"],"names":[
|
|
1
|
+
{"version":3,"sources":["../src/loader.ts"],"names":[],"mappings":";;;;;;;;AAAA,IAAA,cAAA,GAAA,UAAA,CAAA;AAAA,EAAA,eAAA,CAAA,OAAA,EAAA,MAAA,EAAA;AAEA,IAAA,SAAS,SAAS,IAAA,EAAkB;AAClC,MAAA,IAAI,CAAC,OAAA,CAAQ,GAAA,CAAI,8BAAA,EAAgC;AAEjD,MAAA,OAAA,CAAQ,GAAA,CAAI,kBAAA,EAAoB,GAAG,IAAI,CAAA;AAAA,IACzC;AASA,IAAA,SAAS,iBAAA,GAAoB;AAC3B,MAAA,OAAO,CAAC,EACN,OAAA,CAAQ,GAAA,CAAI,SAAA,IACT,OAAA,CAAQ,GAAA,CAAI,aAAA,IACZ,OAAA,CAAQ,GAAA,CAAI,eAAA,IACZ,OAAA,CAAQ,GAAA,CAAI,oBAAA,CAAA;AAAA,IAEnB;AAEA,IAAA,SAAS,aAAa,GAAA,EAAa;AACjC,MAAA,OAAO,GAAA,CAAI,O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AA,GAAS,OAAO,OAAA,CAAQ,OAAA,EAAS,KAAK,SAAA,CAAU,IAAA,CAAK,KAAK,CAAC,CAAA;AAAA,QAC7D;AAAA,MACF;AAEA,MAAA,OAAO,MAAA;AAAA,IACT;AAGA,IAAA,MAAA,CAAO,OAAA,GAAU,aAAA;AAAA,EAAA;AAAA,CAAA,CAAA","file":"loader.js","sourcesContent":["import type { SerializedEnvGraph } from 'varlock';\n\nfunction debug(...args: Array<any>) {\n if (!process.env.DEBUG_VARLOCK_NEXT_INTEGRATION) return;\n // eslint-disable-next-line no-console\n console.log('[varlock-loader]', ...args);\n}\n\ntype LoaderContext = {\n cacheable(flag: boolean): void;\n resourcePath: string;\n rootContext: string;\n getOptions?(): { bundler?: 'webpack' | 'turbopack'; isEdge?: boolean };\n};\n\nfunction isTurbopackWorker() {\n return !!(\n process.env.TURBOPACK\n || process.env.TURBOPACK_DEV\n || process.env.TURBOPACK_BUILD\n || process.env.npm_config_turbopack\n );\n}\n\nfunction escapeRegExp(str: string) {\n return str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n\n// detect 'use client' directive at top of file (before any code, after optional comments)\n// uses [^\\S\\n]* (horizontal whitespace) instead of \\s* to avoid exponential backtracking\nconst USE_CLIENT_RE = /^(?:[^\\S\\n]*\\/\\/[^\\n]*\\n|[^\\S\\n]*\\/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*\\/[^\\S\\n]*\\n|[^\\S\\n]*\\n)*[^\\S\\n]*['\"]use client['\"]/;\n\n// match directive prologue ('use server', 'use client', etc.) + optional trailing semicolons/newlines\n// captures the directive block so we can inject code after it\nconst DIRECTIVE_PROLOGUE_RE = /^((?:[^\\S\\n]*\\/\\/[^\\n]*\\n|[^\\S\\n]*\\/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*\\/[^\\S\\n]*\\n|[^\\S\\n]*\\n)*[^\\S\\n]*['\"]use (?:server|client|strict)['\"][^\\S\\n]*;?[^\\S\\n]*\\n?)/;\n\n/** Prepend code after any directive prologue (e.g. 'use server') to avoid breaking it */\nfunction prependAfterDirectives(source: string, codeToPrepend: string): string {\n const match = source.match(DIRECTIVE_PROLOGUE_RE);\n if (match) {\n return `${match[1] + codeToPrepend}\\n${source.slice(match[1].length)}`;\n }\n return `${codeToPrepend}\\n${source}`;\n}\n\n/**\n * Webpack/Turbopack loader that:\n * 1. Injects resolved env config into instrumentation and proxy files.\n * 2. Replaces `ENV.KEY` references for non-sensitive vars with literal JSON values.\n *\n * SECURITY: Sensitive env values are ONLY embedded into server-side files\n * (never client components). The static ENV.KEY replacements explicitly skip\n * sensitive vars.\n */\nfunction webpackLoader(this: LoaderContext, source: string) {\n // only transform files within the project root\n // this skips node_modules AND symlinked workspace packages (e.g. varlock/env)\n // which turbopack resolves to their real paths outside node_modules\n const projectRoot = this.rootContext || process.cwd();\n if (!this.resourcePath.startsWith(projectRoot)) {\n return source;\n }\n // still skip node_modules within the project root\n const relPath = this.resourcePath.slice(projectRoot.length);\n if (relPath.includes('/node_modules/') || relPath.includes('\\\\node_modules\\\\')) {\n return source;\n }\n\n debug('processing:', relPath);\n\n const isClientComponent = USE_CLIENT_RE.test(source);\n if (isClientComponent) debug('client component:', relPath);\n\n const rawEnv = process.env.__VARLOCK_ENV;\n if (!rawEnv) {\n throw new Error('expected __VARLOCK_ENV to be set');\n }\n\n // TODO: avoid parsing on every file\n let envGraph: SerializedEnvGraph;\n try {\n envGraph = JSON.parse(rawEnv);\n } catch {\n return source;\n }\n\n const loaderOptions = this.getOptions?.() ?? {};\n\n const isWebpack = loaderOptions.bundler === 'webpack';\n const isTurbopack = loaderOptions.bundler === 'turbopack' || isTurbopackWorker();\n const isEdge = loaderOptions.isEdge ?? false;\n\n let result = source;\n\n if (!isClientComponent) {\n // Inject a tiny guarded init snippet into every server file.\n // Pre-rendering workers receive compiled code via IPC (not from disk), so runtime\n // file injection doesn't help them. This ensures initVarlockEnv() and\n // patchGlobalConsole() run once per process — the globalThis guard makes it\n // idempotent, and turbopack deduplicates the require() targets.\n let initGuard: string;\n if (isEdge) {\n // Edge compilation: can't use require(), so use globalThis.__varlockPatchConsole\n // which the init-edge bundle exposes. The init guard is skipped since edge init\n // is handled by the runtime file injection (processAssets hook).\n initGuard = 'if(globalThis.__varlockPatchConsole)globalThis.__varlockPatchConsole();';\n } else {\n initGuard = 'if(!globalThis.__varlockBuildInit){globalThis.__varlockBuildInit=true;require(\\'varlock/env\\').initVarlockEnv();require(\\'varlock/patch-console\\').patchGlobalConsole();}';\n // When used from webpack, React wraps console for RSC dev replay AFTER our initial\n // patch in the runtime file. Re-patching outside the once-guard ensures our redaction\n // wraps React's wrapper so secrets are redacted before React captures them.\n // patchGlobalConsole() no-ops if console.log still has _varlockPatchedFn.\n if (isWebpack) {\n initGuard += 'require(\\'varlock/patch-console\\').patchGlobalConsole();';\n }\n }\n result = prependAfterDirectives(result, initGuard);\n }\n\n // static replacements for non-sensitive env vars\n // webpack uses DefinePlugin for this, so only needed for turbopack\n if (isTurbopack && source.includes('ENV.')) {\n // disable caching only for files that reference ENV — their output depends on env values\n this.cacheable(false);\n for (const [key, item] of Object.entries(envGraph.config)) {\n if (item.isSensitive) continue;\n\n // TODO: smarter replacement (vite version uses AST?)\n\n // match ENV.KEY as a member expression (word boundary before ENV, not followed by more identifier chars)\n const pattern = new RegExp(`\\\\bENV\\\\.${escapeRegExp(key)}(?![\\\\w$])`, 'g');\n result = result.replace(pattern, JSON.stringify(item.value));\n }\n }\n\n return result;\n}\n\n// CJS export required for webpack loader-runner compatibility\nmodule.exports = webpackLoader;\n"]}
|