Skip to content

Commit cbc0b35

Browse files
feat(create-gatsby): Add Tailwind as a styling choice (#37944)
Co-authored-by: LekoArts <[email protected]>
1 parent 768581f commit cbc0b35

File tree

8 files changed

+105
-7
lines changed

8 files changed

+105
-7
lines changed

packages/create-gatsby/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/create-gatsby#readme",
1414
"files": [
1515
"lib/index.js",
16-
"cli.js"
16+
"cli.js",
17+
"stubs/"
1718
],
1819
"devDependencies": {
1920
"@ascorbic/worker-threads-shim": "^1.0.0",

packages/create-gatsby/src/index.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import styles from "./questions/styles.json"
44
import features from "./questions/features.json"
55
import languages from "./questions/languages.json"
66
import { initStarter, getPackageManager, gitSetup } from "./init-starter"
7+
import { writeFiles, IFile } from "./write-files"
78
import { installPlugins } from "./install-plugins"
89
import colors from "ansi-colors"
910
import path from "path"
@@ -27,7 +28,6 @@ export const DEFAULT_STARTERS: Record<keyof typeof languages, string> = {
2728
js: `https://github.com/gatsbyjs/gatsby-starter-minimal.git`,
2829
ts: `https://github.com/gatsbyjs/gatsby-starter-minimal-ts.git`,
2930
}
30-
3131
interface IAnswers {
3232
name: string
3333
project: string
@@ -58,6 +58,14 @@ interface IPluginEntry {
5858
* Keys must match plugin names or name:key combinations from the plugins array
5959
*/
6060
options?: PluginConfigMap
61+
/**
62+
* If the item is not a valid Gatsby plugin, set this to `false`
63+
*/
64+
isGatsbyPlugin?: boolean
65+
/**
66+
* Additional files that should be written to the filesystem
67+
*/
68+
files?: Array<IFile>
6169
}
6270

6371
export type PluginMap = Record<string, IPluginEntry>
@@ -203,8 +211,12 @@ ${center(colors.blueBright.bold.underline(`Welcome to Gatsby!`))}
203211
)} for styling your site`
204212
)
205213
const extraPlugins = styles[answers.styling].plugins || []
206-
207-
plugins.push(answers.styling, ...extraPlugins)
214+
// If the key is not a valid Gatsby plugin, don't add it to the plugins array
215+
if (styles[answers.styling]?.isGatsbyPlugin === false) {
216+
plugins.push(...extraPlugins)
217+
} else {
218+
plugins.push(answers.styling, ...extraPlugins)
219+
}
208220
packages.push(
209221
answers.styling,
210222
...(styles[answers.styling].dependencies || []),
@@ -305,6 +317,12 @@ ${colors.bold(`Thanks! Here's what we'll now do:`)}
305317
reporter.info(`${maybeUseEmoji(`🔌 `)}Setting-up plugins...`)
306318
await installPlugins(plugins, pluginConfig, fullPath, [])
307319
}
320+
321+
if (answers.styling && styles[answers.styling]?.files) {
322+
reporter.info(`${maybeUseEmoji(`🎨 `)}Adding necessary styling files...`)
323+
await writeFiles(answers.project, styles[answers.styling].files)
324+
}
325+
308326
await setSiteMetadata(fullPath, `title`, siteName)
309327

310328
await gitSetup(answers.project)

packages/create-gatsby/src/questions/styles.json

+24-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,30 @@
2323
},
2424
"gatsby-plugin-vanilla-extract": {
2525
"message": "vanilla-extract",
26-
"dependencies": [
27-
"@vanilla-extract/webpack-plugin",
28-
"@vanilla-extract/css"
26+
"dependencies": ["@vanilla-extract/webpack-plugin", "@vanilla-extract/css"]
27+
},
28+
"tailwindcss": {
29+
"message": "Tailwind CSS",
30+
"plugins": ["gatsby-plugin-postcss"],
31+
"dependencies": ["postcss", "autoprefixer", "gatsby-plugin-postcss"],
32+
"isGatsbyPlugin": false,
33+
"files": [
34+
{
35+
"source": "stubs/tailwindcss/gatsby-browser.js",
36+
"targetPath": "gatsby-browser.js"
37+
},
38+
{
39+
"source": "stubs/tailwindcss/global.css",
40+
"targetPath": "src/styles/global.css"
41+
},
42+
{
43+
"source": "stubs/tailwindcss/postcss.config.js",
44+
"targetPath": "postcss.config.js"
45+
},
46+
{
47+
"source": "stubs/tailwindcss/tailwind.config.js",
48+
"targetPath": "tailwind.config.js"
49+
}
2950
]
3051
}
3152
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as fs from "fs-extra"
2+
import path from "path"
3+
4+
export interface IFile {
5+
source: string
6+
targetPath: string
7+
}
8+
9+
async function writeFile({ source, targetPath }: IFile): Promise<void> {
10+
// Read the stub
11+
const stubData = await fs.readFile(source)
12+
// Write stub to targetPath
13+
await fs.outputFile(targetPath, stubData)
14+
}
15+
16+
export async function writeFiles(
17+
rootPath: string,
18+
files: Array<IFile> | undefined
19+
): Promise<void> {
20+
if (!files) {
21+
return
22+
}
23+
24+
// Necessary to grab files from the stub/ dir
25+
const createGatsbyRoot = path.join(__dirname, `..`)
26+
// Creating files in parallel
27+
const results = []
28+
29+
for (const file of files) {
30+
const source = path.resolve(createGatsbyRoot, file.source)
31+
const targetPath = path.resolve(rootPath, file.targetPath)
32+
33+
results.push(writeFile({ source, targetPath }))
34+
}
35+
36+
await Promise.all(results)
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./src/styles/global.css"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
plugins: {
3+
tailwindcss: {},
4+
autoprefixer: {},
5+
},
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** @type {import('tailwindcss').Config} */
2+
module.exports = {
3+
content: [
4+
`./src/pages/**/*.{js,jsx,ts,tsx}`,
5+
`./src/components/**/*.{js,jsx,ts,tsx}`,
6+
],
7+
theme: {
8+
extend: {},
9+
},
10+
plugins: [],
11+
}

0 commit comments

Comments
 (0)