diff --git a/.changeset/gold-trainers-sing.md b/.changeset/gold-trainers-sing.md new file mode 100644 index 000000000..e936bf8ed --- /dev/null +++ b/.changeset/gold-trainers-sing.md @@ -0,0 +1,5 @@ +--- +'@vanilla-extract/css': patch +--- + +Fix spaces in debug IDs for variable names. diff --git a/.changeset/hungry-ties-judge.md b/.changeset/hungry-ties-judge.md new file mode 100644 index 000000000..686d057d4 --- /dev/null +++ b/.changeset/hungry-ties-judge.md @@ -0,0 +1,16 @@ +--- +'@vanilla-extract/css': minor +--- + +Support excluding file names from `generateIdentifier` output. This is available by passing a newly-added options object rather than a string. + +**Example usage** + +```ts +import { generateIdentifier } from '@vanilla-extract/css'; + +const identifier = generateIdentifier({ + debugId, + debugFileName: false, +}); +``` diff --git a/.changeset/popular-seas-relate.md b/.changeset/popular-seas-relate.md new file mode 100644 index 000000000..71420dbd1 --- /dev/null +++ b/.changeset/popular-seas-relate.md @@ -0,0 +1,5 @@ +--- +'@vanilla-extract/css': patch +--- + +Fix file name prefix in debug names when file extension is `.cjs` or `.mjs`. diff --git a/packages/css/src/identifier.test.ts b/packages/css/src/identifier.test.ts index 93d9f5f98..3ef01d6ef 100644 --- a/packages/css/src/identifier.test.ts +++ b/packages/css/src/identifier.test.ts @@ -3,26 +3,58 @@ import { generateIdentifier } from './identifier'; describe('identifier', () => { beforeAll(() => { - setFileScope('test'); + setFileScope('path/to/file.css.ts'); }); afterAll(() => { endFileScope(); }); - it(`should create a valid identifier`, () => { + it(`should ignore file scopes without a file extension when creating a path prefix`, () => { + setFileScope('test'); expect(generateIdentifier(undefined)).toMatchInlineSnapshot(`"skkcyc0"`); + endFileScope(); + }); + + it(`should create a valid identifier`, () => { + expect(generateIdentifier(undefined)).toMatchInlineSnapshot( + `"file__18bazsm0"`, + ); }); it('should create a valid identifier with a debug id', () => { expect(generateIdentifier('debug')).toMatchInlineSnapshot( - `"debug__skkcyc1"`, + `"file_debug__18bazsm1"`, ); }); it('should create a valid identifier with a debug id with whitespace', () => { expect(generateIdentifier('debug and more')).toMatchInlineSnapshot( - `"debug_and_more__skkcyc2"`, + `"file_debug_and_more__18bazsm2"`, ); }); + + describe('options object', () => { + it(`should create a valid identifier`, () => { + expect(generateIdentifier({})).toMatchInlineSnapshot(`"file__18bazsm3"`); + }); + + it('should create a valid identifier with a debug id and with file name debugging explicitly enabled', () => { + expect( + generateIdentifier({ debugId: 'debug', debugFileName: true }), + ).toMatchInlineSnapshot(`"file_debug__18bazsm4"`); + }); + + it('should create a valid identifier with a debug id and without file name debugging', () => { + expect( + generateIdentifier({ debugId: 'debug', debugFileName: false }), + ).toMatchInlineSnapshot(`"debug__18bazsm5"`); + }); + + it('should create a valid identifier without a debug ID or file name', () => { + expect( + generateIdentifier({ debugFileName: false }), + ).toMatchInlineSnapshot(`"_18bazsm6"`); + }); + }); }); diff --git a/packages/css/src/identifier.ts b/packages/css/src/identifier.ts index da4431b43..5dfe5b1ad 100644 --- a/packages/css/src/identifier.ts +++ b/packages/css/src/identifier.ts @@ -3,23 +3,46 @@ import hash from '@emotion/hash'; import { getIdentOption } from './adapter'; import { getAndIncrementRefCounter, getFileScope } from './fileScope'; -function getDevPrefix(debugId: string | undefined) { +function getDevPrefix({ + debugId, + debugFileName, +}: { + debugId?: string; + debugFileName: boolean; +}) { const parts = debugId ? [debugId.replace(/\s/g, '_')] : []; - const { filePath } = getFileScope(); - const matches = filePath.match( - /(?