Skip to content

Commit efb7a63

Browse files
authored
Add "meaninglessFileNames" option (#304)
1 parent 90ef53d commit efb7a63

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

src/utils/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const useTopLevelImportPathMatchers = state =>
99
getOption(state, 'topLevelImportPaths', []).map(pattern => new RegExp(pattern))
1010
export const useSSR = state => getOption(state, 'ssr', true)
1111
export const useFileName = state => getOption(state, 'fileName')
12+
export const useMeaninglessFileNames = state => getOption(state, 'meaninglessFileNames', ['index'])
1213
export const useMinify = state => getOption(state, 'minify')
1314
export const useTranspileTemplateLiterals = state =>
1415
getOption(state, 'transpileTemplateLiterals')

src/visitors/displayNameAndId.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
useFileName,
55
useDisplayName,
66
useSSR,
7+
useMeaninglessFileNames,
78
useNamespace,
89
} from '../utils/options'
910
import getName from '../utils/getName'
@@ -111,21 +112,22 @@ const getExistingConfig = t => path => {
111112
}
112113
}
113114

114-
const getBlockName = file => {
115+
const getBlockName = (file, meaninglessFileNames) => {
115116
const name = path.basename(
116117
file.opts.filename,
117118
path.extname(file.opts.filename)
118119
)
119-
return name !== 'index'
120-
? name
121-
: path.basename(path.dirname(file.opts.filename))
120+
121+
return meaninglessFileNames.includes(name)
122+
? path.basename(path.dirname(file.opts.filename))
123+
: name
122124
}
123125

124126
const getDisplayName = t => (path, state) => {
125127
const { file } = state
126128
const componentName = getName(t)(path)
127129
if (file) {
128-
const blockName = getBlockName(file)
130+
const blockName = getBlockName(file, useMeaninglessFileNames(state))
129131
if (blockName === componentName) {
130132
return componentName
131133
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"plugins": [
3+
[
4+
"../../../src",
5+
{
6+
"meaninglessFileNames": ["code"],
7+
"transpileTemplateLiterals": false,
8+
"ssr": true
9+
}
10+
]
11+
]
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import styled from "styled-components";
2+
3+
const Test = styled.div`color: red;`;
4+
const before = styled.div`color: blue;`;
5+
styled.div``;
6+
export default styled.button``;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import styled from "styled-components";
2+
const Test = styled.div.withConfig({
3+
displayName: "use-directory-name__Test",
4+
componentId: "sc-193y009-0"
5+
})`color:red;`;
6+
const before = styled.div.withConfig({
7+
displayName: "use-directory-name__before",
8+
componentId: "sc-193y009-1"
9+
})`color:blue;`;
10+
styled.div.withConfig({
11+
displayName: "use-directory-name",
12+
componentId: "sc-193y009-2"
13+
})``;
14+
export default styled.button.withConfig({
15+
displayName: "use-directory-name",
16+
componentId: "sc-193y009-3"
17+
})``;

0 commit comments

Comments
 (0)