-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-tailwind.js
More file actions
executable file
·92 lines (77 loc) · 2.3 KB
/
add-tailwind.js
File metadata and controls
executable file
·92 lines (77 loc) · 2.3 KB
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const util = require('./util');
const packages = [
'tailwindcss',
'postcss-cli',
'@fullhuman/postcss-purgecss',
'cssnano',
'autoprefixer',
'concurrently',
];
const configFiles = ['postcss.config.js'];
const tailwindConfig = `@tailwind base;
html, body, #root {
height: 100%;
}
@tailwind components;
@tailwind utilities;
`;
async function main() {
const appName = process.argv[2];
if (!appName) {
console.error('You must provide an app name.');
process.exit(1);
}
try {
fs.accessSync(appName);
} catch (err) {
// empty
console.error(`${appName} does not exist.`);
process.exit(1);
}
try {
// Install packages
await util.execSyncWithOutput(
`yarn --cwd ./${appName} add ${packages.join(' ')}`
);
// Copy PostCSS config
configFiles.forEach(file => {
const src = path.join(__dirname, file);
const dst = path.join(appName, file);
console.log(`${src} -> ${dst}`);
fs.copyFileSync(src, dst);
});
// Set up style path
const stylePath = path.join(appName, 'src/style');
fs.mkdirSync(stylePath);
fs.writeFileSync(path.join(stylePath, 'tailwind.css'), tailwindConfig);
// Compile CSS in build and dev scripts
await util.modifyJson(path.join(appName, 'package.json'), obj => {
const postcssScript =
'postcss src/style/tailwind.css -o src/style/tailwind.min.css';
obj.scripts['build:app'] = obj.scripts['build'];
obj.scripts[
'build:style'
] = `${postcssScript} --env production --verbose`;
obj.scripts['build'] = 'yarn build:style && yarn build:app';
obj.scripts['dev:app'] = obj.scripts['dev'];
// Only --watch without --poll seemed to get stuck after a few changes
obj.scripts[
'dev:style'
] = `${postcssScript} --watch --poll 500 --verbose`;
obj.scripts['dev'] = 'concurrently "yarn dev:style" "yarn dev:app"';
});
// Config files
process.chdir(appName);
await util.execSyncWithOutput('npx tailwind init');
await util.execSyncWithOutput(`yarn build:style`);
// Add CSS import to app
await util.runSed('src/App.tsx', '2iimport \'style/tailwind.min.css\';');
} catch (err) {
console.error(err);
process.exit(1);
}
}
main();