Skip to content

Allow packageInfo to be undefined #1564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/twenty-phones-help.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion packages/compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const createViteServer = async ({
source: code,
rootPath: root,
filePath: id,
packageName: pkg.name,
packageName: pkg?.name,
identOption: identifiers,
globalAdapterIdentifier,
});
Expand Down
14 changes: 10 additions & 4 deletions packages/integration/src/addFileScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface AddFileScopeParams {
source: string;
filePath: string;
rootPath: string;
packageName: string;
packageName?: string;
globalAdapterIdentifier?: string;
}
export function addFileScope({
Expand All @@ -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();
`);
Expand Down
2 changes: 1 addition & 1 deletion packages/integration/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
});
Expand Down
8 changes: 3 additions & 5 deletions packages/integration/src/packageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -26,7 +26,7 @@ function getClosestPackageInfo(directory: string) {

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

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);

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/integration/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface TransformParams {
source: string;
filePath: string;
rootPath: string;
packageName: string;
packageName?: string;
identOption: IdentifierOption;
globalAdapterIdentifier?: string;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});

Expand Down
4 changes: 2 additions & 2 deletions packages/vite-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-plugin/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ 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();

transform({
source,
filePath: this.resourcePath,
rootPath: this.rootContext,
packageName: name,
packageName: packageInfo?.name,
identOption: defaultIdentifierOption(this.mode, identifiers),
})
.then((code) => {
Expand Down
Loading