-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(scripts-babel): implement v9 babel preset to properly resolve griffel module mappings #27313
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| const { workspaceRoot } = require('@nrwl/devkit'); | ||
|
|
||
| const cwd = process.cwd(); | ||
| const rootOffset = path.relative(cwd, workspaceRoot); | ||
|
|
||
| /** | ||
| * | ||
| * @param {{tsBaseConfigPath:string}} options | ||
| * @returns | ||
| */ | ||
| function createModuleResolverAliases(options) { | ||
| const { tsBaseConfigPath } = options; | ||
| const tsBaseConfigPathAbsolute = path.join(workspaceRoot, tsBaseConfigPath); | ||
| const tsConfigBase = JSON.parse(fs.readFileSync(tsBaseConfigPathAbsolute, 'utf-8')); | ||
|
|
||
| /** | ||
| * @type {Record<string,[string]>} | ||
| */ | ||
| const allPathAliases = tsConfigBase.compilerOptions.paths; | ||
| return Object.entries(allPathAliases).reduce((acc, [packageName, aliasTuple]) => { | ||
| const tsSourceRoot = aliasTuple[0]; | ||
| const jsSourceRoot = tsSourceRoot.replace('src', 'lib').replace('.ts', '.js'); | ||
| const aliasRegex = `^${packageName}$`; | ||
| acc[aliasRegex] = `${rootOffset}/${jsSourceRoot}`; | ||
| return acc; | ||
| }, /** @type {Record<string,string>} */ ({})); | ||
| } | ||
|
|
||
| /** @type {Array<import('./types').BabelPluginItem>} */ | ||
| const presets = []; | ||
|
|
||
| /** @type {Array<import('./types').BabelPluginItem>} */ | ||
| const plugins = []; | ||
|
|
||
| /** | ||
| * @param {import('@babel/core').ConfigAPI} api | ||
| * @param {import('./types').BabelPresetOptions} options | ||
| */ | ||
| const preset = (api, options) => { | ||
| const moduleResolverAliasMappings = createModuleResolverAliases(options); | ||
| /** @type {Array<import('./types').BabelPluginItem>} */ | ||
| const dynamicPresets = [ | ||
| [ | ||
| '@griffel', | ||
| { | ||
| babelOptions: { | ||
| plugins: [ | ||
| [ | ||
| 'babel-plugin-module-resolver', | ||
| { | ||
| alias: moduleResolverAliasMappings, | ||
| }, | ||
| ], | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| ]; | ||
|
|
||
| presets.push(...dynamicPresets); | ||
|
|
||
| return { presets, plugins }; | ||
| }; | ||
|
|
||
| module.exports = preset; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import preset from './preset-v9'; | ||
|
|
||
| describe(`babel preset v9`, () => { | ||
| const babelMockedApi = { | ||
| assertVersion: jest.fn(), | ||
| caller: jest.fn(), | ||
| env: jest.fn(), | ||
| cache: {}, | ||
| version: '0', | ||
| } as unknown as import('@babel/core').ConfigAPI; | ||
| it(`should generate module-resolve alias mappings for @griffel preset`, () => { | ||
| const presetConfig = preset(babelMockedApi, { tsBaseConfigPath: './tsconfig.base.json' }); | ||
|
|
||
| const griffelPreset = presetConfig.presets.find(presetItem => presetItem[0] === '@griffel')!; | ||
| const griffelPresetConfig = griffelPreset[1]; | ||
|
|
||
| expect(griffelPresetConfig).toEqual({ | ||
| babelOptions: { | ||
| plugins: [ | ||
| [ | ||
| 'babel-plugin-module-resolver', | ||
| { | ||
| alias: expect.any(Object), | ||
| }, | ||
| ], | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const alias = (griffelPresetConfig as any).babelOptions.plugins[0][1].alias; | ||
| const aliasesEntries = Object.entries(alias); | ||
|
|
||
| /** | ||
| * we pick 5th entry from the big list of path aliases entries. there is nothing special about it and we can pick any existing entry index. | ||
| * I chose to not pick first nor last. In general this is an integration test that checks if we would introduce some violation within tsconfig.base.json path aliases config | ||
| */ | ||
| const aliasDeclaration = aliasesEntries[5]; | ||
| const [aliasKey, aliasPath] = aliasDeclaration; | ||
|
|
||
| expect(aliasKey).toEqual(expect.stringContaining('^@fluentui/')); | ||
| expect(aliasPath).toEqual(expect.stringMatching(/[../]+[a-z-/]+\/lib\/index\.js/)); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import type { PluginTarget } from '@babel/core'; | ||
|
|
||
| export interface BabelPresetOptions { | ||
| tsBaseConfigPath: string; | ||
| } | ||
|
|
||
| export type BabelPluginItem = [PluginTarget, Record<string, unknown>]; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.