From faead480aceb8a07e2812244d351cdc5d57fa0ad Mon Sep 17 00:00:00 2001 From: Lukas Oppermann Date: Tue, 3 Sep 2024 11:15:53 +0200 Subject: [PATCH] fix commonJs format (#1043) * fix commonJs format * added changeset --- .changeset/bright-lions-push.md | 5 +++++ integration/integration.test.ts | 2 +- package.json | 3 ++- src/formats/javascriptCommonJs.test.ts | 4 ++-- src/formats/javascriptCommonJs.ts | 2 +- 5 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 .changeset/bright-lions-push.md diff --git a/.changeset/bright-lions-push.md b/.changeset/bright-lions-push.md new file mode 100644 index 000000000..13753069b --- /dev/null +++ b/.changeset/bright-lions-push.md @@ -0,0 +1,5 @@ +--- +'@primer/primitives': patch +--- + +Update exports.default to module.exports in commonJs diff --git a/integration/integration.test.ts b/integration/integration.test.ts index f2cfdbf96..1976da613 100644 --- a/integration/integration.test.ts +++ b/integration/integration.test.ts @@ -167,7 +167,7 @@ describe('PrimerStyleDictionary', () => { it('runs commonJs format', () => { const output = fs.readFileSync(`${basePath}/build/js/common.js`, 'utf8') - const expectedOutput = `exports.default = { + const expectedOutput = `module.exports = { PREFIX: { base: { color: { diff --git a/package.json b/package.json index b09c59a4e..2209cbb46 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,8 @@ "prepack": "npm run build", "release": "changeset publish", "start:storybook": "npm run build && cd docs/storybook && npm run storybook", - "test": "vitest run --coverage", + "test": "npm run test:unit && npm run test:integration", + "test:unit": "vitest run --coverage", "test:integration": "vitest run -r integration" }, "prettier": "@github/prettier-config", diff --git a/src/formats/javascriptCommonJs.test.ts b/src/formats/javascriptCommonJs.test.ts index befd3aac5..995ff0b58 100644 --- a/src/formats/javascriptCommonJs.test.ts +++ b/src/formats/javascriptCommonJs.test.ts @@ -11,7 +11,7 @@ describe('Format: CommonJs', () => { }) const expectedOutput = syncPrettier.format( - `exports.default = { + `module.exports = { test: { tokens: { subgroup: { @@ -29,7 +29,7 @@ describe('Format: CommonJs', () => { it('Formats tokens without prefix', () => { const input = getMockFormatterArguments() const expectedOutput = syncPrettier.format( - `exports.default = { + `module.exports = { tokens: { subgroup: { red: "transformedValue", diff --git a/src/formats/javascriptCommonJs.ts b/src/formats/javascriptCommonJs.ts index 2570573b0..aa0bdb09a 100644 --- a/src/formats/javascriptCommonJs.ts +++ b/src/formats/javascriptCommonJs.ts @@ -15,7 +15,7 @@ export const javascriptCommonJs: StyleDictionary.Formatter = ({dictionary, file, // add prefix if defined const tokens = prefixTokens(dictionary.tokens, platform) // add file header and convert output - const output = `${fileHeader({file})}exports.default = ${JSON.stringify(jsonToNestedValue(tokens), null, 2)}\n` + const output = `${fileHeader({file})}module.exports = ${JSON.stringify(jsonToNestedValue(tokens), null, 2)}\n` // return prettified return syncPrettier.format(output, {parser: 'typescript', printWidth: 500}) }