diff --git a/.changeset/twenty-phones-help.md b/.changeset/twenty-phones-help.md new file mode 100644 index 000000000..0bf809006 --- /dev/null +++ b/.changeset/twenty-phones-help.md @@ -0,0 +1,9 @@ +--- +'@vanilla-extract/jest-transform': patch +'@vanilla-extract/webpack-plugin': patch +'@vanilla-extract/integration': patch +'@vanilla-extract/vite-plugin': patch +'@vanilla-extract/compiler': patch +--- + +Allow `packageInfo` to be undefined diff --git a/packages/compiler/src/compiler.ts b/packages/compiler/src/compiler.ts index 9f524922b..674dd088a 100644 --- a/packages/compiler/src/compiler.ts +++ b/packages/compiler/src/compiler.ts @@ -154,7 +154,7 @@ const createViteServer = async ({ source: code, rootPath: root, filePath: id, - packageName: pkg.name, + packageName: pkg?.name, identOption: identifiers, globalAdapterIdentifier, }); diff --git a/packages/integration/src/addFileScope.ts b/packages/integration/src/addFileScope.ts index 1e705ed72..91a8e1432 100644 --- a/packages/integration/src/addFileScope.ts +++ b/packages/integration/src/addFileScope.ts @@ -11,7 +11,7 @@ interface AddFileScopeParams { source: string; filePath: string; rootPath: string; - packageName: string; + packageName?: string; globalAdapterIdentifier?: string; } export function addFileScope({ @@ -29,20 +29,26 @@ export function addFileScope({ if (source.includes('@vanilla-extract/css/fileScope')) { source = source.replace( /setFileScope\(((\n|.)*?)\)/, - `setFileScope("${normalizedPath}", "${packageName}")`, + `setFileScope("${normalizedPath}"${ + packageName ? `, "${packageName}"` : '' + })`, ); } else { if (hasESM && !isMixed) { source = dedent(` import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope"; - setFileScope("${normalizedPath}", "${packageName}"); + setFileScope("${normalizedPath}"${ + packageName ? `, "${packageName}"` : '' + }); ${source} endFileScope(); `); } else { source = dedent(` const __vanilla_filescope__ = require("@vanilla-extract/css/fileScope"); - __vanilla_filescope__.setFileScope("${normalizedPath}", "${packageName}"); + __vanilla_filescope__.setFileScope("${normalizedPath}"${ + packageName ? `, "${packageName}"` : '' + }); ${source} __vanilla_filescope__.endFileScope(); `); diff --git a/packages/integration/src/compile.ts b/packages/integration/src/compile.ts index dfa16b377..89fa1b1d3 100644 --- a/packages/integration/src/compile.ts +++ b/packages/integration/src/compile.ts @@ -29,7 +29,7 @@ export const vanillaExtractTransformPlugin = ({ source: originalSource, filePath: path, rootPath: build.initialOptions.absWorkingDir!, - packageName: packageInfo.name, + packageName: packageInfo?.name, identOption: identOption ?? (build.initialOptions.minify ? 'short' : 'debug'), }); diff --git a/packages/integration/src/packageInfo.ts b/packages/integration/src/packageInfo.ts index b0d6da1e8..5f9ec385a 100644 --- a/packages/integration/src/packageInfo.ts +++ b/packages/integration/src/packageInfo.ts @@ -8,7 +8,7 @@ export interface PackageInfo { dirname: string; } -function getClosestPackageInfo(directory: string) { +function getClosestPackageInfo(directory: string): PackageInfo | undefined { const packageJsonPath = findUp.sync('package.json', { cwd: directory, }); @@ -26,7 +26,7 @@ function getClosestPackageInfo(directory: string) { const packageInfoCache = new Map(); -export function getPackageInfo(cwd?: string | null): PackageInfo { +export function getPackageInfo(cwd?: string | null): PackageInfo | undefined { const resolvedCwd = cwd ?? process.cwd(); const cachedValue = packageInfoCache.get(resolvedCwd); @@ -43,9 +43,7 @@ export function getPackageInfo(cwd?: string | null): PackageInfo { } if (!packageInfo || !packageInfo.name) { - throw new Error( - `Couldn't find parent package.json with a name field from '${resolvedCwd}'`, - ); + return undefined; } packageInfoCache.set(resolvedCwd, packageInfo); diff --git a/packages/integration/src/transform.ts b/packages/integration/src/transform.ts index 2caf9dc27..e22191bf7 100644 --- a/packages/integration/src/transform.ts +++ b/packages/integration/src/transform.ts @@ -10,7 +10,7 @@ interface TransformParams { source: string; filePath: string; rootPath: string; - packageName: string; + packageName?: string; identOption: IdentifierOption; globalAdapterIdentifier?: string; } diff --git a/packages/jest-transform/src/index.ts b/packages/jest-transform/src/index.ts index 28cfdbfa2..7f9a0e306 100644 --- a/packages/jest-transform/src/index.ts +++ b/packages/jest-transform/src/index.ts @@ -19,13 +19,13 @@ const vanillaTransformer: Transformer = { }; } - const { name: packageName } = getPackageInfo(options.config.rootDir); + const packageInfo = getPackageInfo(options.config.rootDir); const code = transformSync({ source, filePath, rootPath: options.config.rootDir, - packageName: packageName, + packageName: packageInfo?.name, identOption: 'debug', }); diff --git a/packages/vite-plugin/src/index.ts b/packages/vite-plugin/src/index.ts index f1ad964ea..0f21baccc 100644 --- a/packages/vite-plugin/src/index.ts +++ b/packages/vite-plugin/src/index.ts @@ -65,7 +65,7 @@ export function vanillaExtractPlugin({ let config: ResolvedConfig; let configEnv: ConfigEnv; let server: ViteDevServer; - let packageName: string; + let packageName: string | undefined; let compiler: Compiler | undefined; let isBuild: boolean; const vitePromise = import('vite'); @@ -154,7 +154,7 @@ export function vanillaExtractPlugin({ async configResolved(_resolvedConfig) { config = _resolvedConfig; isBuild = config.command === 'build' && !config.build.watch; - packageName = getPackageInfo(config.root).name; + packageName = getPackageInfo(config.root)?.name; }, async buildStart() { // Ensure we re-use the compiler instance between builds, e.g. in watch mode diff --git a/packages/webpack-plugin/src/loader.ts b/packages/webpack-plugin/src/loader.ts index 71302b779..86b80fa85 100644 --- a/packages/webpack-plugin/src/loader.ts +++ b/packages/webpack-plugin/src/loader.ts @@ -52,7 +52,7 @@ const defaultIdentifierOption = ( export default function (this: LoaderContext, source: string): void { const { identifiers } = loaderUtils.getOptions(this) as InternalLoaderOptions; - const { name } = getPackageInfo(this.rootContext); + const packageInfo = getPackageInfo(this.rootContext); const callback = this.async(); @@ -60,7 +60,7 @@ export default function (this: LoaderContext, source: string): void { source, filePath: this.resourcePath, rootPath: this.rootContext, - packageName: name, + packageName: packageInfo?.name, identOption: defaultIdentifierOption(this.mode, identifiers), }) .then((code) => {