/
index.ts
149 lines (133 loc) · 4.89 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { createProjectGenerator } from '@teleporthq/teleport-project-generator'
import { createComponentGenerator } from '@teleporthq/teleport-component-generator'
import reactAppRoutingPlugin from '@teleporthq/teleport-plugin-react-app-routing'
import reactBasePlugin from '@teleporthq/teleport-plugin-react-base-component'
import {
createCSSModulesPlugin,
createStyleSheetPlugin,
} from '@teleporthq/teleport-plugin-css-modules'
import {
createReactStyledComponentsPlugin,
createStyleSheetPlugin as createStyledComponentsStyleSheetPlugin,
} from '@teleporthq/teleport-plugin-react-styled-components'
import reactProptypes from '@teleporthq/teleport-plugin-jsx-proptypes'
import importStatementsPlugin from '@teleporthq/teleport-plugin-import-statements'
import headConfigPlugin from '@teleporthq/teleport-plugin-jsx-head-config'
import prettierJS from '@teleporthq/teleport-postprocessor-prettier-js'
import prettierJSX from '@teleporthq/teleport-postprocessor-prettier-jsx'
import {
Mapping,
ComponentPlugin,
FileType,
ReactStyleVariation,
ProjectStrategy,
} from '@teleporthq/teleport-types'
import GatsbyProjectMapping from './gatsby-mapping.json'
import GatsbyTemplate from './project-template'
import {
createCustomHTMLEntryFile,
appendToConfigFile,
styleSheetDependentConfigGenerator,
} from './utils'
const cssModulesPlugin = createCSSModulesPlugin({
moduleExtension: true,
camelCaseClassNames: true,
})
const styledComponentsPlugin = createReactStyledComponentsPlugin()
interface GatsbyProjectConfig {
variation?: ReactStyleVariation
}
const createGatsbyProjectGenerator = (config?: GatsbyProjectConfig) => {
const variation = config?.variation || ReactStyleVariation.CSSModules
const reactComponentGenerator = createCustomReactComponentGenerator(variation)
const reactPagesGenerator = createCustomReactComponentGenerator(variation, [headConfigPlugin])
const routingComponentGenerator = createComponentGenerator()
routingComponentGenerator.addPlugin(reactAppRoutingPlugin)
routingComponentGenerator.addPlugin(importStatementsPlugin)
routingComponentGenerator.addPostProcessor(prettierJS)
const htmlFileGenerator = createComponentGenerator()
htmlFileGenerator.addPostProcessor(prettierJS)
const configGenerator = createComponentGenerator()
configGenerator.addPlugin(importStatementsPlugin)
configGenerator.addPostProcessor(prettierJS)
const styleSheetGenerator = createComponentGenerator()
if (variation === ReactStyleVariation.StyledComponents) {
styleSheetGenerator.addPlugin(createStyledComponentsStyleSheetPlugin())
styleSheetGenerator.addPlugin(importStatementsPlugin)
styleSheetGenerator.addPostProcessor(prettierJS)
} else {
styleSheetGenerator.addPlugin(createStyleSheetPlugin({ moduleExtension: true }))
}
const strategy: ProjectStrategy = {
components: {
generator: reactComponentGenerator,
path: ['src', 'components'],
},
pages: {
generator: reactPagesGenerator,
path: ['src', 'pages'],
options: {
useFileNameForNavigation: true,
},
},
entry: {
generator: htmlFileGenerator,
path: ['src'],
fileName: 'html',
chunkGenerationFunction: createCustomHTMLEntryFile,
},
static: {
prefix: '',
path: ['static'],
},
projectStyleSheet: {
generator: styleSheetGenerator,
fileName: 'style',
path: ['src'],
importFile: true,
},
}
if (variation === ReactStyleVariation.CSSModules) {
strategy.framework = {
config: {
fileName: 'gatsby-browser',
fileType: FileType.JS,
path: [''],
generator: configGenerator,
configContentGenerator: styleSheetDependentConfigGenerator,
isGlobalStylesDependent: true,
},
}
}
if (variation === ReactStyleVariation.StyledComponents) {
strategy.framework = {
replace: {
fileName: 'gatsby-config',
fileType: FileType.JS,
path: [''],
replaceFile: appendToConfigFile,
},
}
}
const generator = createProjectGenerator(strategy)
return generator
}
const createCustomReactComponentGenerator = (
styleVariation: string,
extraPlugins: ComponentPlugin[] = []
) => {
const reactComponentGenerator = createComponentGenerator()
reactComponentGenerator.addPlugin(reactBasePlugin)
if (styleVariation === ReactStyleVariation.StyledComponents) {
reactComponentGenerator.addPlugin(styledComponentsPlugin)
} else {
reactComponentGenerator.addPlugin(cssModulesPlugin)
}
reactComponentGenerator.addPlugin(reactProptypes)
extraPlugins.forEach((plugin) => reactComponentGenerator.addPlugin(plugin))
reactComponentGenerator.addPlugin(importStatementsPlugin)
reactComponentGenerator.addMapping(GatsbyProjectMapping as Mapping)
reactComponentGenerator.addPostProcessor(prettierJSX)
return reactComponentGenerator
}
export { createGatsbyProjectGenerator, GatsbyProjectMapping, GatsbyTemplate }