This repository has been archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpreset.ts
171 lines (156 loc) · 4.01 KB
/
preset.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
export default definePreset({
name: 'laravel:vite',
options: {
base: true,
tailwindcss: true,
https: false,
},
postInstall: ({ hl }) => [
`Run the development server with ${hl('npm run dev')}`,
`Edit your scripts in ${hl('resources/scripts')}`,
`Build for production with ${hl('npm run build')}`,
],
handler: async(context) => {
if (context.options.base) {
await installVite(context.options.https)
}
if (context.options.tailwindcss) {
await installTailwind()
}
},
})
async function installVite(https: boolean) {
await group({
title: 'install Node dependencies',
handler: async() => {
await editFiles({
files: 'package.json',
operations: [
{ type: 'edit-json', delete: ['scripts', 'devDependencies'] },
{ type: 'edit-json', merge: { scripts: { dev: 'vite', build: 'vite build' } } },
],
title: 'update package.json',
})
await installPackages({
title: 'install front-end dependencies',
for: 'node',
install: [
'vite-plugin-laravel',
'vite',
],
dev: true,
})
},
})
await installPackages({
title: 'install PHP dependencies',
for: 'php',
install: ['innocenzi/laravel-vite:0.2.*'],
})
await group({
title: 'extract Vite scaffolding',
handler: async() => {
await extractTemplates({
title: 'extract templates',
from: 'default',
})
await deletePaths({
title: 'remove some default files',
paths: ['resources/js', 'resources/css', 'webpack.mix.js', 'vite.config.js'],
})
await editFiles({
title: 'update .gitignore',
files: '.gitignore',
operations: [{ type: 'add-line', position: 'prepend', lines: '/public/build' }],
})
await editFiles({
title: 'update environment files',
files: ['.env', '.env.example'],
operations: [{
type: 'add-line',
position: 'append',
skipIf: (content) => ['DEV_SERVER_URL', 'DEV_SERVER_KEY', 'DEV_SERVER_CERT'].some((key) => content.includes(key)),
lines: [
`DEV_SERVER_URL=http${https ? 's' : ''}://localhost:3000`,
'DEV_SERVER_KEY=',
'DEV_SERVER_CERT=',
],
}],
})
await editFiles({
title: 'add @vite directive',
files: 'resources/views/welcome.blade.php',
operations: [{
type: 'add-line',
match: /<\/head>/,
position: 'before',
indent: ' ',
lines: ['', '<!-- Scripts and CSS import -->', '@vite'],
}],
})
await executeCommand({
command: 'php',
arguments: ['artisan', 'vendor:publish', '--tag=vite-config'],
title: 'publish Laravel Vite configuration',
})
},
})
}
async function installTailwind() {
await installPackages({
title: 'install Tailwind CSS',
for: 'node',
install: ['tailwindcss', 'autoprefixer', 'postcss'],
dev: true,
})
await group({
title: 'extract Tailwind scaffoling',
handler: async() => {
await extractTemplates({
title: 'extract Tailwind CSS config',
from: 'tailwind',
})
await editFiles({
title: 'add Tailwind CSS imports',
files: 'config/vite.php',
operations: [
{
skipIf: (content) => content.includes("'resources/css/tailwind.css'"),
type: 'add-line',
match: /resources\/scripts\/main.ts/,
lines: "'resources/css/tailwind.css',",
position: 'before',
},
],
})
await editFiles({
title: 'register PostCSS plugins',
files: 'vite.config.ts',
operations: [
{
skipIf: (content) => content.includes('import tailwindcss') || content.includes('import autoprefixer'),
type: 'add-line',
lines: ["import tailwindcss from 'tailwindcss'", "import autoprefixer from 'autoprefixer'"],
position: 'prepend',
},
{
type: 'update-content',
update: (content) => content.replace('laravel()', `laravel({
postcss: [
tailwindcss(),
autoprefixer()
]
})`),
},
],
})
await editFiles({
title: 'remove inline CSS',
files: 'resources/views/welcome.blade.php',
operations: [
{ type: 'remove-line', match: /<style>/, start: 0, count: 4 },
],
})
},
})
}