Skip to content

Commit 23bbd70

Browse files
authored
fix(workspace-plugin): stop re-creating raw styles for cjs from non raw source (#34862)
1 parent df356ba commit 23bbd70

4 files changed

Lines changed: 53 additions & 45 deletions

File tree

tools/workspace-plugin/src/executors/build/executor.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ describe('Build Executor', () => {
115115
]);
116116

117117
expect(loggerVerboseSpy.mock.calls.flat()).toEqual([
118+
`Applying transforms: 0`,
118119
`babel: transformed ${workspaceRoot}/libs/proj/lib/greeter.styles.js`,
120+
`Applying transforms: 0`,
119121
]);
120122

121123
expect(rmMock.mock.calls.flat()).toEqual([
@@ -292,6 +294,9 @@ describe('Build Executor', () => {
292294

293295
describe(`#enableGriffelRawStyles`, () => {
294296
it('generates raw styles files when enableGriffelRawStyles is enabled', async () => {
297+
const loggerLogSpy = jest.spyOn(logger, 'log').mockImplementation(() => {
298+
return;
299+
});
295300
const optionsWithRawStyles: BuildExecutorSchema = {
296301
...options,
297302
enableGriffelRawStyles: true,
@@ -300,6 +305,8 @@ describe('Build Executor', () => {
300305
const output = await executor(optionsWithRawStyles, context);
301306
expect(output.success).toBe(true);
302307

308+
expect(loggerLogSpy.mock.calls.flat()).toContain('💅 Griffel RAW styles output enabled');
309+
303310
// =====================
304311
// assert raw styles files are generated
305312
// =====================
@@ -360,6 +367,9 @@ describe('Build Executor', () => {
360367
}, 60000);
361368

362369
it('does not generate raw styles files when enableGriffelRawStyles is disabled', async () => {
370+
const loggerLogSpy = jest.spyOn(logger, 'log').mockImplementation(() => {
371+
return;
372+
});
363373
const optionsWithoutRawStyles: BuildExecutorSchema = {
364374
...options,
365375
enableGriffelRawStyles: false,
@@ -368,6 +378,8 @@ describe('Build Executor', () => {
368378
const output = await executor(optionsWithoutRawStyles, context);
369379
expect(output.success).toBe(true);
370380

381+
expect(loggerLogSpy.mock.calls.flat()).not.toContain('💅 Griffel RAW styles output enabled');
382+
371383
// =====================
372384
// assert raw styles files are NOT generated
373385
// =====================

tools/workspace-plugin/src/executors/build/lib/babel.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* TODO: remove this module and its usage once we will be able to remove griffel AOT from our build output -> https://github.com/microsoft/fluentui/blob/master/docs/react-v9/contributing/rfcs/shared/build-system/stop-styles-transforms.md
44
*/
55

6-
import { writeFile, readFile } from 'node:fs/promises';
6+
import { writeFile, readFile, copyFile } from 'node:fs/promises';
77
import { basename, join } from 'node:path';
88

99
import { type BabelFileResult, transformAsync } from '@babel/core';
@@ -50,7 +50,12 @@ export async function compileWithGriffelStylesAOT(options: NormalizedOptions) {
5050
return processAsyncQueue(compilationQueue);
5151
}
5252

53-
await compileSwc(esmConfig, options);
53+
const transforms = [];
54+
if (options.enableGriffelRawStyles) {
55+
logger.log('💅 Griffel RAW styles output enabled');
56+
transforms.push(createStyleRawOutput);
57+
}
58+
await compileSwc(esmConfig, options, transforms);
5459
await babel(esmConfig, options);
5560

5661
const compilationQueue = restOfConfigs.map(outputConfig => {
@@ -121,6 +126,20 @@ async function babel(esmModuleOutput: NormalizedOptions['moduleOutput'][number],
121126
}
122127
}
123128

129+
/**
130+
*
131+
* Creates a raw styles output file if the original file is a Griffel styles file and the enableGriffelRawStyles option is true.
132+
* The raw styles file is created by copying the original file and renaming it with a .raw suffix.
133+
*/
134+
async function createStyleRawOutput(filePath: string): Promise<void> {
135+
if (!filePath.includes('.styles.')) {
136+
return;
137+
}
138+
const rawFilePath = filePath.replace('.styles.', '.styles.raw.');
139+
await copyFile(filePath, rawFilePath);
140+
logger.verbose(`raw-style: created ${rawFilePath}`);
141+
}
142+
124143
type NonNullableRecord<T> = {
125144
[P in keyof T]-?: NonNullable<T[P]>;
126145
};

tools/workspace-plugin/src/executors/build/lib/file-processor.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

tools/workspace-plugin/src/executors/build/lib/swc.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { mkdir, writeFile, copyFile } from 'node:fs/promises';
2+
import { writeFileSync } from 'node:fs';
23
import { dirname, join } from 'node:path';
34

45
import { globSync } from 'fast-glob';
@@ -7,7 +8,6 @@ import { transformFile, type Config } from '@swc/core';
78
import { logger, readJsonFile } from '@nx/devkit';
89

910
import { type NormalizedOptions } from './shared';
10-
import { FileProcessor, GriffelRawStylesProcessor, applyFileProcessors } from './file-processor';
1111

1212
// extend @swc/core types by missing apis
1313
declare module '@swc/core' {
@@ -21,25 +21,16 @@ interface Options {
2121
outputPath: string;
2222
}
2323

24-
function createFileProcessors(options: NormalizedOptions): FileProcessor[] {
25-
const processors: FileProcessor[] = [];
26-
27-
if (options.enableGriffelRawStyles) {
28-
processors.push(new GriffelRawStylesProcessor());
29-
}
30-
31-
return processors;
32-
}
33-
34-
export async function compileSwc(options: Options, normalizedOptions: NormalizedOptions) {
24+
export async function compileSwc(
25+
options: Options,
26+
normalizedOptions: NormalizedOptions,
27+
transforms?: Array<Transform>,
28+
) {
3529
const { outputPath, module } = options;
36-
const fileProcessors = createFileProcessors(normalizedOptions);
3730
const absoluteOutputPath = join(normalizedOptions.absoluteProjectRoot, outputPath);
3831

3932
logger.log(`Compiling with SWC for module:${options.module}...`);
40-
if (fileProcessors.length > 0) {
41-
logger.log(`Applying ${fileProcessors.length} file processors...`);
42-
}
33+
logger.verbose(`Applying transforms: ${transforms ? transforms.length : 0}`);
4334

4435
const sourceFiles = globSync(`**/*.{js,ts,tsx}`, { cwd: normalizedOptions.absoluteSourceRoot });
4536

@@ -74,12 +65,23 @@ export async function compileSwc(options: Options, normalizedOptions: Normalized
7465
await mkdir(dirname(compiledFilePath), { recursive: true });
7566

7667
await writeFile(compiledFilePath, resultCode);
77-
await applyFileProcessors(compiledFilePath, fileProcessors);
68+
await applyTransforms(compiledFilePath, transforms);
7869

7970
if (result.map) {
8071
const mapFilePath = `${compiledFilePath}.map`;
8172
await writeFile(mapFilePath, result.map);
82-
await applyFileProcessors(mapFilePath, fileProcessors);
73+
await applyTransforms(mapFilePath, transforms);
8374
}
8475
}
8576
}
77+
78+
type Transform = (filePath: string) => Promise<void>;
79+
async function applyTransforms(filePath: string, transforms?: Array<Transform>): Promise<void> {
80+
if (!transforms || !Array.isArray(transforms) || transforms.length === 0) {
81+
return;
82+
}
83+
84+
for (const transform of transforms) {
85+
await transform(filePath);
86+
}
87+
}

0 commit comments

Comments
 (0)