ember-codemod-remove-global-styles 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +9 -0
- package/README.md +56 -0
- package/dist/bin/ember-codemod-remove-global-styles.js +24 -0
- package/dist/src/index.js +6 -0
- package/dist/src/steps/analyze-project.js +11 -0
- package/dist/src/steps/create-css-module-files.js +54 -0
- package/dist/src/steps/create-options.js +7 -0
- package/dist/src/steps/index.js +3 -0
- package/dist/src/types/index.js +1 -0
- package/dist/src/utils/css/get-class-to-styles.js +55 -0
- package/dist/src/utils/css/get-classes.js +83 -0
- package/dist/src/utils/css/get-module-file-path.js +3 -0
- package/dist/src/utils/css/index.js +4 -0
- package/dist/src/utils/css/print-styles.js +14 -0
- package/package.json +63 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Isaac J. Lee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
[](https://github.com/ijlee2/embroider-css-modules/actions/workflows/ci.yml)
|
|
2
|
+
|
|
3
|
+
# ember-codemod-remove-global-styles
|
|
4
|
+
|
|
5
|
+
_Codemod to localize global styles_
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
### Arguments
|
|
11
|
+
|
|
12
|
+
[PROVIDE REQUIRED AND OPTIONAL ARGUMENTS.]
|
|
13
|
+
|
|
14
|
+
<details>
|
|
15
|
+
|
|
16
|
+
<summary>Optional: Specify the project root</summary>
|
|
17
|
+
|
|
18
|
+
Pass `--root` to run the codemod somewhere else (i.e. not in the current directory).
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
npx ember-codemod-remove-global-styles --root <path/to/your/project>
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
</details>
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Limitations
|
|
28
|
+
|
|
29
|
+
The codemod is designed to cover typical cases. It is not designed to cover one-off cases.
|
|
30
|
+
|
|
31
|
+
To better meet your needs, consider cloning the repo and running the codemod locally.
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
cd <path/to/cloned/repo>
|
|
35
|
+
|
|
36
|
+
# Compile TypeScript
|
|
37
|
+
pnpm build
|
|
38
|
+
|
|
39
|
+
# Run codemod
|
|
40
|
+
./dist/bin/ember-codemod-remove-global-styles.js --root <path/to/your/project>
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
## Compatibility
|
|
45
|
+
|
|
46
|
+
- Node.js v20 or above
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
## Contributing
|
|
50
|
+
|
|
51
|
+
See the [Contributing](../../CONTRIBUTING.md) guide for details.
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
## License
|
|
55
|
+
|
|
56
|
+
This project is licensed under the [MIT License](LICENSE.md).
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
import yargs from 'yargs';
|
|
4
|
+
import { hideBin } from 'yargs/helpers';
|
|
5
|
+
import { runCodemod } from '../src/index.js';
|
|
6
|
+
// Provide a title to the process in `ps`
|
|
7
|
+
process.title = 'ember-codemod-remove-global-styles';
|
|
8
|
+
// Set codemod options
|
|
9
|
+
const argv = yargs(hideBin(process.argv))
|
|
10
|
+
.option('root', {
|
|
11
|
+
describe: 'Where to run the codemod',
|
|
12
|
+
type: 'string',
|
|
13
|
+
})
|
|
14
|
+
.option('src', {
|
|
15
|
+
demandOption: true,
|
|
16
|
+
describe: 'Location of the global stylesheet (e.g. app/assets/app.css)',
|
|
17
|
+
type: 'string',
|
|
18
|
+
})
|
|
19
|
+
.parseSync();
|
|
20
|
+
const codemodOptions = {
|
|
21
|
+
projectRoot: argv['root'] ?? process.cwd(),
|
|
22
|
+
src: argv['src'],
|
|
23
|
+
};
|
|
24
|
+
runCodemod(codemodOptions);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { analyzeProject, createCssModuleFiles, createOptions, } from './steps/index.js';
|
|
2
|
+
export function runCodemod(codemodOptions) {
|
|
3
|
+
const options = createOptions(codemodOptions);
|
|
4
|
+
const project = analyzeProject(options);
|
|
5
|
+
createCssModuleFiles(project, options);
|
|
6
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { getClassToStyles } from '../utils/css/index.js';
|
|
4
|
+
export function analyzeProject(options) {
|
|
5
|
+
const { projectRoot, src } = options;
|
|
6
|
+
const stylesheet = readFileSync(join(projectRoot, src), 'utf8');
|
|
7
|
+
const classToStyles = getClassToStyles(stylesheet);
|
|
8
|
+
return {
|
|
9
|
+
classToStyles,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { findTemplateTags } from '@codemod-utils/ast-template-tag';
|
|
4
|
+
import { createFiles, findFiles } from '@codemod-utils/files';
|
|
5
|
+
import { getClasses, getModuleFilePath, printStyles, } from '../utils/css/index.js';
|
|
6
|
+
export function createCssModuleFiles(project, options) {
|
|
7
|
+
const { projectRoot } = options;
|
|
8
|
+
const filePaths = findFiles('app/{components,templates}/**/*.{gjs,gts,hbs}', {
|
|
9
|
+
projectRoot,
|
|
10
|
+
});
|
|
11
|
+
const fileMap = new Map();
|
|
12
|
+
filePaths.forEach((filePath) => {
|
|
13
|
+
const file = readFileSync(join(projectRoot, filePath), 'utf8');
|
|
14
|
+
const classes = [];
|
|
15
|
+
const errors = [];
|
|
16
|
+
if (filePath.endsWith('.hbs')) {
|
|
17
|
+
const output = getClasses(file);
|
|
18
|
+
classes.push(...output.classes);
|
|
19
|
+
errors.push(...output.errors);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
const templateTags = findTemplateTags(file);
|
|
23
|
+
templateTags.forEach(({ contents }) => {
|
|
24
|
+
const output = getClasses(contents);
|
|
25
|
+
classes.push(...output.classes);
|
|
26
|
+
errors.push(...output.errors);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const classesSet = new Set(classes);
|
|
30
|
+
const localStyles = classes.reduce((accumulator, className) => {
|
|
31
|
+
const styles = project.classToStyles.get(className) ?? [];
|
|
32
|
+
const filteredStyles = styles.filter(({ classes }) => {
|
|
33
|
+
return classes.every((className) => classesSet.has(className));
|
|
34
|
+
});
|
|
35
|
+
accumulator.push(...filteredStyles);
|
|
36
|
+
return accumulator;
|
|
37
|
+
}, []);
|
|
38
|
+
if (localStyles.length === 0) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const cssModuleFilePath = getModuleFilePath(filePath);
|
|
42
|
+
let cssModuleFile = existsSync(join(projectRoot, cssModuleFilePath))
|
|
43
|
+
? readFileSync(join(projectRoot, cssModuleFilePath), 'utf8')
|
|
44
|
+
: '';
|
|
45
|
+
cssModuleFile += `${printStyles(localStyles)}\n`;
|
|
46
|
+
fileMap.set(cssModuleFilePath, cssModuleFile);
|
|
47
|
+
if (errors.length > 0) {
|
|
48
|
+
console.warn(`WARNING: ${cssModuleFilePath} may be incorrect.`);
|
|
49
|
+
console.warn(errors.map((error) => `- ${error}`).join('\n'));
|
|
50
|
+
console.log();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
createFiles(fileMap, { projectRoot });
|
|
54
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import postcss from 'postcss';
|
|
2
|
+
function getClasses(selector) {
|
|
3
|
+
const matches = Array.from(selector.matchAll(/\.([\w-]+)/g));
|
|
4
|
+
return matches.map((results) => results[1]);
|
|
5
|
+
}
|
|
6
|
+
function getRootClass(selector) {
|
|
7
|
+
const matches = selector.match(/^\.([\w-]+).*$/);
|
|
8
|
+
if (matches === null) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
return matches[1];
|
|
12
|
+
}
|
|
13
|
+
export function getClassToStyles(file) {
|
|
14
|
+
const classToStyles = new Map();
|
|
15
|
+
function processRule(node) {
|
|
16
|
+
const allSelectors = node.selector.split(/\s*,\s*/);
|
|
17
|
+
const clone = node.clone();
|
|
18
|
+
allSelectors.forEach((selector) => {
|
|
19
|
+
const containerClass = getRootClass(selector);
|
|
20
|
+
if (containerClass === undefined) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
clone.selector = selector;
|
|
24
|
+
const data = {
|
|
25
|
+
classes: getClasses(selector),
|
|
26
|
+
location: {
|
|
27
|
+
end: node.source.end,
|
|
28
|
+
start: node.source.start,
|
|
29
|
+
},
|
|
30
|
+
raw: clone.toString(),
|
|
31
|
+
selector,
|
|
32
|
+
};
|
|
33
|
+
if (classToStyles.has(containerClass)) {
|
|
34
|
+
classToStyles.get(containerClass).push(data);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
classToStyles.set(containerClass, [data]);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
const plugins = [
|
|
42
|
+
{
|
|
43
|
+
postcssPlugin: 'postcss-get-class-to-styles',
|
|
44
|
+
prepare() {
|
|
45
|
+
return {
|
|
46
|
+
Rule: processRule,
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
];
|
|
51
|
+
// @ts-expect-error: Incorrect type
|
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
53
|
+
postcss(plugins).process(file).css;
|
|
54
|
+
return classToStyles;
|
|
55
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { AST } from '@codemod-utils/ast-template';
|
|
2
|
+
export function getClasses(file) {
|
|
3
|
+
const classes = new Set();
|
|
4
|
+
const errors = [];
|
|
5
|
+
function processMustacheStatement(nodeValue) {
|
|
6
|
+
switch (nodeValue.path.type) {
|
|
7
|
+
case 'PathExpression': {
|
|
8
|
+
switch (nodeValue.path.original) {
|
|
9
|
+
case 'if':
|
|
10
|
+
case 'unless': {
|
|
11
|
+
if (nodeValue.params[1]?.type === 'StringLiteral') {
|
|
12
|
+
processStringLiteral(nodeValue.params[1]);
|
|
13
|
+
}
|
|
14
|
+
if (nodeValue.params[2]?.type === 'StringLiteral') {
|
|
15
|
+
processStringLiteral(nodeValue.params[2]);
|
|
16
|
+
}
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
default: {
|
|
20
|
+
const isLocalClass = nodeValue.path.original.startsWith('styles.');
|
|
21
|
+
if (!isLocalClass) {
|
|
22
|
+
errors.push(`Could not analyze {{${nodeValue.path.original}}} in template, line ${nodeValue.loc.start.line}.`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
case 'StringLiteral': {
|
|
29
|
+
processStringLiteral(nodeValue.path);
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function processStringLiteral(nodeValue) {
|
|
35
|
+
const classNames = nodeValue.original.split(/\s+/).filter(Boolean);
|
|
36
|
+
classNames.forEach((className) => {
|
|
37
|
+
classes.add(className);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function processTextNode(nodeValue) {
|
|
41
|
+
const classNames = nodeValue.chars.split(/\s+/).filter(Boolean);
|
|
42
|
+
classNames.forEach((className) => {
|
|
43
|
+
classes.add(className);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
const traverse = AST.traverse();
|
|
47
|
+
traverse(file, {
|
|
48
|
+
AttrNode(node) {
|
|
49
|
+
if (node.name !== 'class') {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
switch (node.value.type) {
|
|
53
|
+
case 'ConcatStatement': {
|
|
54
|
+
node.value.parts.forEach((part) => {
|
|
55
|
+
switch (part.type) {
|
|
56
|
+
case 'MustacheStatement': {
|
|
57
|
+
processMustacheStatement(part);
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case 'TextNode': {
|
|
61
|
+
processTextNode(part);
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
case 'MustacheStatement': {
|
|
69
|
+
processMustacheStatement(node.value);
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
case 'TextNode': {
|
|
73
|
+
processTextNode(node.value);
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
return {
|
|
80
|
+
classes: Array.from(classes),
|
|
81
|
+
errors,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function printStyles(styles) {
|
|
2
|
+
return styles
|
|
3
|
+
.sort((a, b) => {
|
|
4
|
+
if (a.location.start.offset > b.location.start.offset) {
|
|
5
|
+
return 1;
|
|
6
|
+
}
|
|
7
|
+
if (a.location.start.offset < b.location.start.offset) {
|
|
8
|
+
return -1;
|
|
9
|
+
}
|
|
10
|
+
return 0;
|
|
11
|
+
})
|
|
12
|
+
.map(({ raw }) => raw)
|
|
13
|
+
.join('\n\n');
|
|
14
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ember-codemod-remove-global-styles",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Codemod to localize global styles",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"codemod",
|
|
7
|
+
"css-modules",
|
|
8
|
+
"ember-codemod",
|
|
9
|
+
"ember-css-modules",
|
|
10
|
+
"emberjs",
|
|
11
|
+
"embroider-css-modules"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/ijlee2/embroider-css-modules.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "Isaac J. Lee",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "dist/src/index.js",
|
|
21
|
+
"bin": "dist/bin/ember-codemod-remove-global-styles.js",
|
|
22
|
+
"directories": {
|
|
23
|
+
"test": "tests"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@codemod-utils/ast-javascript": "^2.0.9",
|
|
30
|
+
"@codemod-utils/ast-template": "^2.0.4",
|
|
31
|
+
"@codemod-utils/ast-template-tag": "^1.0.1",
|
|
32
|
+
"@codemod-utils/files": "^3.0.6",
|
|
33
|
+
"postcss": "^8.5.6",
|
|
34
|
+
"yargs": "^18.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@codemod-utils/tests": "^2.0.6",
|
|
38
|
+
"@sondr3/minitest": "^0.1.2",
|
|
39
|
+
"@types/node": "^20.19.25",
|
|
40
|
+
"@types/yargs": "^17.0.35",
|
|
41
|
+
"concurrently": "^9.2.1",
|
|
42
|
+
"eslint": "^9.39.1",
|
|
43
|
+
"prettier": "^3.6.2",
|
|
44
|
+
"typescript": "^5.9.3",
|
|
45
|
+
"@shared-configs/eslint-config-node": "0.0.0",
|
|
46
|
+
"@shared-configs/prettier": "0.0.0",
|
|
47
|
+
"@shared-configs/typescript": "0.0.0"
|
|
48
|
+
},
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": "20.* || >= 22"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "./build.sh --production",
|
|
54
|
+
"format": "prettier . --cache --write",
|
|
55
|
+
"lint": "concurrently \"pnpm:lint:*(!fix)\" --names \"lint:\"",
|
|
56
|
+
"lint:fix": "concurrently \"pnpm:lint:*:fix\" --names \"fix:\" && pnpm format",
|
|
57
|
+
"lint:format": "prettier . --cache --check",
|
|
58
|
+
"lint:js": "eslint . --cache",
|
|
59
|
+
"lint:js:fix": "eslint . --fix",
|
|
60
|
+
"lint:types": "tsc --noEmit",
|
|
61
|
+
"test": "./build.sh --test && mt dist-for-testing --quiet"
|
|
62
|
+
}
|
|
63
|
+
}
|