Skip to content

Commit 23bba61

Browse files
committed
Allow packageInfo to be undefined
1 parent 6714033 commit 23bba61

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

packages/compiler/src/compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ const createViteServer = async ({
154154
source: code,
155155
rootPath: root,
156156
filePath: id,
157-
packageName: pkg.name,
157+
packageName: pkg?.name,
158158
identOption: identifiers,
159159
globalAdapterIdentifier,
160160
});

packages/integration/src/addFileScope.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface AddFileScopeParams {
1111
source: string;
1212
filePath: string;
1313
rootPath: string;
14-
packageName: string;
14+
packageName?: string;
1515
globalAdapterIdentifier?: string;
1616
}
1717
export function addFileScope({
@@ -29,20 +29,26 @@ export function addFileScope({
2929
if (source.includes('@vanilla-extract/css/fileScope')) {
3030
source = source.replace(
3131
/setFileScope\(((\n|.)*?)\)/,
32-
`setFileScope("${normalizedPath}", "${packageName}")`,
32+
`setFileScope("${normalizedPath}"${
33+
packageName ? `, "${packageName}"` : ''
34+
})`,
3335
);
3436
} else {
3537
if (hasESM && !isMixed) {
3638
source = dedent(`
3739
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
38-
setFileScope("${normalizedPath}", "${packageName}");
40+
setFileScope("${normalizedPath}"${
41+
packageName ? `, "${packageName}"` : ''
42+
});
3943
${source}
4044
endFileScope();
4145
`);
4246
} else {
4347
source = dedent(`
4448
const __vanilla_filescope__ = require("@vanilla-extract/css/fileScope");
45-
__vanilla_filescope__.setFileScope("${normalizedPath}", "${packageName}");
49+
__vanilla_filescope__.setFileScope("${normalizedPath}"${
50+
packageName ? `, "${packageName}"` : ''
51+
});
4652
${source}
4753
__vanilla_filescope__.endFileScope();
4854
`);

packages/integration/src/compile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const vanillaExtractTransformPlugin = ({
2929
source: originalSource,
3030
filePath: path,
3131
rootPath: build.initialOptions.absWorkingDir!,
32-
packageName: packageInfo.name,
32+
packageName: packageInfo?.name,
3333
identOption:
3434
identOption ?? (build.initialOptions.minify ? 'short' : 'debug'),
3535
});

packages/integration/src/packageInfo.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface PackageInfo {
88
dirname: string;
99
}
1010

11-
function getClosestPackageInfo(directory: string) {
11+
function getClosestPackageInfo(directory: string): PackageInfo | undefined {
1212
const packageJsonPath = findUp.sync('package.json', {
1313
cwd: directory,
1414
});
@@ -26,7 +26,7 @@ function getClosestPackageInfo(directory: string) {
2626

2727
const packageInfoCache = new Map<string, PackageInfo>();
2828

29-
export function getPackageInfo(cwd?: string | null): PackageInfo {
29+
export function getPackageInfo(cwd?: string | null): PackageInfo | undefined {
3030
const resolvedCwd = cwd ?? process.cwd();
3131
const cachedValue = packageInfoCache.get(resolvedCwd);
3232

@@ -43,9 +43,7 @@ export function getPackageInfo(cwd?: string | null): PackageInfo {
4343
}
4444

4545
if (!packageInfo || !packageInfo.name) {
46-
throw new Error(
47-
`Couldn't find parent package.json with a name field from '${resolvedCwd}'`,
48-
);
46+
return undefined;
4947
}
5048

5149
packageInfoCache.set(resolvedCwd, packageInfo);

packages/integration/src/transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface TransformParams {
1010
source: string;
1111
filePath: string;
1212
rootPath: string;
13-
packageName: string;
13+
packageName?: string;
1414
identOption: IdentifierOption;
1515
globalAdapterIdentifier?: string;
1616
}

packages/vite-plugin/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function vanillaExtractPlugin({
6565
let config: ResolvedConfig;
6666
let configEnv: ConfigEnv;
6767
let server: ViteDevServer;
68-
let packageName: string;
68+
let packageName: string | undefined;
6969
let compiler: Compiler | undefined;
7070
let isBuild: boolean;
7171
const vitePromise = import('vite');
@@ -154,7 +154,7 @@ export function vanillaExtractPlugin({
154154
async configResolved(_resolvedConfig) {
155155
config = _resolvedConfig;
156156
isBuild = config.command === 'build' && !config.build.watch;
157-
packageName = getPackageInfo(config.root).name;
157+
packageName = getPackageInfo(config.root)?.name;
158158
},
159159
async buildStart() {
160160
// Ensure we re-use the compiler instance between builds, e.g. in watch mode

0 commit comments

Comments
 (0)