-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] Update
styled_components_files.js
to include all files that i…
…mport `styled-components` (#206084) ## Summary This is a manual backport of #205011 with regenerated `styled_components_files.js` to match `8.x` code. --------- Co-authored-by: kibanamachine <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
8024079
commit 434d008
Showing
30 changed files
with
919 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
11 changes: 11 additions & 0 deletions
11
.buildkite/scripts/steps/checks/styled_components_mapping.sh
This file contains 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,11 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
source .buildkite/scripts/common/util.sh | ||
|
||
echo --- Check styled-components mapping | ||
cmd="node scripts/styled_components_mapping" | ||
|
||
eval "$cmd" | ||
check_for_changed_files "$cmd" true |
This file contains 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,10 @@ | ||
# @kbn/styled-components-mapping-cli | ||
|
||
A helper tool that looks up components using `styled-components` and generates | ||
a mapping file for Babel to help with proper stylesheet loading. | ||
|
||
## Usage | ||
|
||
```shell | ||
node scripts/styled_components_mapping | ||
``` |
This file contains 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,14 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test/jest_node', | ||
rootDir: '../..', | ||
roots: ['<rootDir>/packages/kbn-styled-components-mapping-cli'], | ||
}; |
This file contains 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,6 @@ | ||
{ | ||
"type": "shared-common", | ||
"id": "@kbn/styled-components-mapping-cli", | ||
"owner": ["@elastic/kibana-operations", "@elastic/eui-team"], | ||
"devOnly": true | ||
} |
This file contains 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 @@ | ||
{ | ||
"name": "@kbn/styled-components-mapping-cli", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "Elastic License 2.0 OR AGPL-3.0-only OR SSPL-1.0", | ||
"main": "./src/run_styled_components_mapping_cli.ts" | ||
} |
79 changes: 79 additions & 0 deletions
79
packages/kbn-styled-components-mapping-cli/src/find_files.ts
This file contains 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,79 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import * as fs from 'node:fs/promises'; | ||
import * as path from 'node:path'; | ||
|
||
const SOURCE_DIRS = ['x-pack', 'src', 'packages']; | ||
const SOURCE_FILE_REGEX = /(^.?|\.[^d]|[^.]d|[^.][^d])\.tsx?$/; | ||
const STYLED_COMPONENTS_IMPORT_REGEX = | ||
/import\s+(?:{[^{}]+}|.*?)\s*(?:from)?\s*['"](styled-components)['"]/; | ||
const EMOTION_STYLED_IMPORT_REGEX = | ||
/import\s+(?:{[^{}]+}|.*?)\s*(?:from)?\s*['"](@emotion\/styled)['"]/; | ||
|
||
export interface Meta { | ||
path: string; | ||
usesStyledComponents: boolean; | ||
usesOnlyStyledComponents?: boolean; | ||
files?: Meta[]; | ||
} | ||
|
||
const walkDirectory = async (dirPath: string): Promise<Meta> => { | ||
const files: Meta[] = []; | ||
let usesStyledComponents = false; | ||
let usesOnlyStyledComponents = true; | ||
|
||
for (const file of await fs.readdir(dirPath, { withFileTypes: true })) { | ||
const fullPath = path.join(file.path, file.name); | ||
|
||
if (file.isDirectory()) { | ||
const meta = await walkDirectory(fullPath); | ||
if (meta.usesStyledComponents) { | ||
usesStyledComponents = true; | ||
} | ||
if (usesOnlyStyledComponents && !meta.usesOnlyStyledComponents) { | ||
usesOnlyStyledComponents = false; | ||
} | ||
files.push(meta); | ||
continue; | ||
} | ||
|
||
if (!SOURCE_FILE_REGEX.test(file.name) || !file.isFile()) { | ||
continue; | ||
} | ||
|
||
const meta: Meta = { | ||
path: fullPath, | ||
usesStyledComponents: false, | ||
}; | ||
|
||
const contents = await fs.readFile(fullPath, 'utf8'); | ||
const usesEmotionStyled = EMOTION_STYLED_IMPORT_REGEX.test(contents); | ||
meta.usesStyledComponents = STYLED_COMPONENTS_IMPORT_REGEX.test(contents); | ||
|
||
if (usesEmotionStyled) { | ||
usesOnlyStyledComponents = false; | ||
} | ||
|
||
if (usesEmotionStyled || meta.usesStyledComponents) { | ||
files.push(meta); | ||
} | ||
} | ||
|
||
return { | ||
path: dirPath, | ||
files, | ||
usesStyledComponents, | ||
usesOnlyStyledComponents: usesStyledComponents && usesOnlyStyledComponents, | ||
}; | ||
}; | ||
|
||
export const findFiles = async (rootDirPath: string) => { | ||
return Promise.all(SOURCE_DIRS.map((dir) => walkDirectory(path.join(rootDirPath, dir)))); | ||
}; |
35 changes: 35 additions & 0 deletions
35
packages/kbn-styled-components-mapping-cli/src/generate_regex_array.ts
This file contains 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,35 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import * as path from 'node:path'; | ||
import type { Meta } from './find_files'; | ||
|
||
export const pathToRegexString = (modulePath: string): string => { | ||
modulePath = modulePath.replace(/[\\\/]/g, '[\\/\\\\]'); | ||
|
||
return `/${modulePath}/`; | ||
}; | ||
|
||
export const generateRegexStringArray = (files: Meta[], rootDirPath: string): string[] => { | ||
const array: string[] = []; | ||
|
||
for (const meta of files) { | ||
if (meta.files) { | ||
if (meta.usesOnlyStyledComponents) { | ||
array.push(pathToRegexString(path.relative(rootDirPath, meta.path))); | ||
} else { | ||
array.push(...generateRegexStringArray(meta.files, rootDirPath)); | ||
} | ||
} else if (meta.usesStyledComponents) { | ||
array.push(pathToRegexString(path.relative(rootDirPath, meta.path))); | ||
} | ||
} | ||
|
||
return array; | ||
}; |
40 changes: 40 additions & 0 deletions
40
packages/kbn-styled-components-mapping-cli/src/run_styled_components_mapping_cli.ts
This file contains 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,40 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import * as path from 'node:path'; | ||
import { run } from '@kbn/dev-cli-runner'; | ||
import { createFailError } from '@kbn/dev-cli-errors'; | ||
import { REPO_ROOT } from '@kbn/repo-info'; | ||
import { findFiles } from './find_files'; | ||
import { generateRegexStringArray } from './generate_regex_array'; | ||
import { updateFile } from './update_file'; | ||
|
||
const mappingFilePath = 'packages/kbn-babel-preset/styled_components_files.js'; | ||
const mappingFileAbsolutePath = path.join(REPO_ROOT, mappingFilePath); | ||
|
||
run( | ||
async ({ log }) => { | ||
log.info(`Looking for source files importing 'styled-components'...`); | ||
|
||
const files = await findFiles(REPO_ROOT); | ||
const regexStringArray = generateRegexStringArray(files, REPO_ROOT); | ||
|
||
try { | ||
await updateFile(mappingFileAbsolutePath, regexStringArray); | ||
} catch (err) { | ||
createFailError(err); | ||
} | ||
|
||
log.info(`Generated ${mappingFilePath} with ${regexStringArray.length} regex expressions`); | ||
}, | ||
{ | ||
usage: `node scripts/styled_components_mapping`, | ||
description: 'Update styled-components babel mapping when converting styles to Emotion', | ||
} | ||
); |
48 changes: 48 additions & 0 deletions
48
packages/kbn-styled-components-mapping-cli/src/update_file.ts
This file contains 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,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import * as fs from 'node:fs/promises'; | ||
|
||
const babelFilePrefix = `/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
module.exports = { | ||
/** | ||
* Synchronized list of all source files that use styled-components. | ||
* Please keep this list up-to-date when converting component styles | ||
* from styled-components to Emotion. | ||
* | ||
* Babel's MatchPattern can be a regex or a string which follows standard | ||
* Node.js path logic as described here: | ||
* https://babeljs.io/docs/options#matchpattern | ||
* | ||
* Used by \`kbn-babel-preset\` and \`kbn-eslint-config\`. | ||
*/ | ||
USES_STYLED_COMPONENTS: [ | ||
/packages[\\/\\\\]kbn-ui-shared-deps-npm[\\/\\\\]/, | ||
/packages[\\/\\\\]kbn-ui-shared-deps-src[\\/\\\\]/, | ||
`; | ||
|
||
const babelFileSuffix = ` ], | ||
}; | ||
`; | ||
|
||
export const updateFile = async (filePath: string, regexStringArray: string[]) => { | ||
const contents = `${babelFilePrefix}\n ${regexStringArray.join( | ||
',\n ' | ||
)},\n${babelFileSuffix}`; | ||
|
||
return fs.writeFile(filePath, contents); | ||
}; |
This file contains 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,21 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "target/types", | ||
"types": [ | ||
"jest", | ||
"node" | ||
] | ||
}, | ||
"include": [ | ||
"**/*.ts", | ||
], | ||
"exclude": [ | ||
"target/**/*" | ||
], | ||
"kbn_references": [ | ||
"@kbn/dev-cli-runner", | ||
"@kbn/dev-cli-errors", | ||
"@kbn/repo-info", | ||
] | ||
} |
This file contains 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,11 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
require('../src/setup_node_env'); | ||
require('@kbn/styled-components-mapping-cli'); |
This file contains 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 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 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 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 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 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 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 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 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 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
Oops, something went wrong.