Skip to content

Commit 83966de

Browse files
authored
feat: Add hot-reload (target to master) (#13)
authored-by: bluesd <[email protected]>
1 parent 32db240 commit 83966de

File tree

4 files changed

+106
-84
lines changed

4 files changed

+106
-84
lines changed

README.md

Lines changed: 53 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,97 +2,85 @@
22

33
A simple way to create an antd theme css file with zero configuration.
44

5-
[Ant Design](https://ant.design/) provides plenty of UI components, styled with [less](http://lesscss.org/).
6-
You can customize the default theme modifying less variables, [check the doc](https://ant.design/docs/react/customize-theme) for multiple ways to achieve this.\
7-
The main issue being that you don't have a simple way to create a static `css` file with zero configuration.
5+
[Ant Design](https://ant.design/) provides plenty of UI components, styled with less.
6+
You can customize the default theme modifying less variables (check [their doc](https://ant.design/docs/react/customize-theme) for multiple ways to achieve this), but the main issue being that you don't have a simple way to create a static `.css` file with zero configuration.
87

9-
This package was created with `Antd Design v4.6.6` and aims to ease the theme's customization process without having to install more dependencies yourself, set up `less` or eject your [React](reactjs.org) project.
8+
This package was created with `Antd Design v4.6.6` and aims to ease the theme's customization process without having to install more dependencies yourself, set up [less](https://lesscss.org/) or eject your [React](reactjs.org) project.
109

1110
## Usage
1211

1312
### Generate custom theme styles
1413

15-
1. Create your own custom theme overriding Antd's less variables (by default: `./custom-theme.less`).
16-
17-
2. Generate theme's custom `.css` file.
18-
19-
- **Option A:** Without installing this plugin:
20-
```sh
21-
$ npx @emeks/antd-custom-theme-generator
22-
23-
output > ./custom-theme.css
24-
```
25-
26-
- **Option B:** Installing the package as a dev dependency:
27-
28-
```sh
29-
$ npm i -E --save-dev @emeks/antd-custom-theme-generator
30-
```
31-
32-
Then generating your theme:
33-
```sh
34-
$ generate-theme
35-
36-
output > ./custom-theme.css
37-
```
38-
39-
3. Replace `antd/dist/antd.css` import in your project, with the output file (by default: `./custom-theme.css`) generated by this plugin.
14+
1. Define your custom theme creating a `.less` file overriding Antd's variables (by default: `./custom-theme.less`).
15+
- _Example:_
16+
```less
17+
@primary-color: #9063cd;
18+
@success-color: #a8d3c4;
19+
```
20+
21+
2. Generate custom theme's `.css` file.
22+
- _Option A:_\
23+
Without installing this package:
24+
```sh
25+
$ npx @emeks/antd-custom-theme-generator
26+
27+
output > ./custom-theme.css
28+
```
29+
- _Option B:_\
30+
Installing this package as a dev dependency:
31+
```sh
32+
$ npm i -E --save-dev @emeks/antd-custom-theme-generator
33+
```
34+
Then generating your theme:
35+
```sh
36+
$ generate-theme
37+
38+
default output > ./custom-theme.css
39+
```
40+
3. Replace `"antd/dist/antd.css"` import in your project, with the output file of your custom theme generated by this script (by default: `./custom-theme.css`).
4041
4142
### Options
4243
4344
| Name | Default | Description |
4445
| --- | --- | --- |
45-
| `--verbose` or `-v` | | Print more info for debugging |
46-
| `--antd` | ./node_modules/antd | Path of the antd library |
47-
| `--theme` | default | Antd theme you want to override (e.g. `dark` or `compact`) - [Check available options](https://github.com/ant-design/ant-design/tree/master/components/style/themes). |
48-
| `<customThemeFilePath>` | ./custom-theme.less | Path of the less file with the variables your want to override |
49-
| `<generatedThemeFilePath>` | ./custom-theme.css | Output path where the script give you the css file with your new theme |
50-
51-
> **Notes**:
52-
>
53-
> The order of the paths **is** important.
54-
> ```sh
55-
> $ generate-theme [--verbose] [--antd] [--theme] [<customThemeFilePath>] [<generatedThemeFilePath>]
56-
> ```
57-
>
58-
59-
### Examples
60-
61-
- Generate dark theme
46+
| `--verbose` or `-v` | | Prints a little bit more info for debugging purposes |
47+
| `--watch` or `-w` | | Keeps script running, recompile on `<customThemeFilePath>` change |
48+
| `--antd` | ./node_modules/antd | Path to the antd library directory |
49+
| `--theme` | default | Antd theme you want to use as base (e.g. `dark` or `compact`) - [Check available options](https://github.com/ant-design/ant-design/tree/master/components/style/themes) |
50+
| `<customThemeFilePath>` | ./custom-theme.less | Path to the custom `.less` file with Antd variables overriden |
51+
| `<generatedThemeFilePath>` | ./custom-theme.css | Output Path to the compiled `.css` file containing your new theme |
52+
53+
> **Notes**:\
54+
> The order of paths **is** important.
55+
```sh
56+
$ generate-theme [--verbose] [--watch] [--antd <dir>] [--theme <theme>] [<customThemeFilePath>] [<generatedThemeFilePath>]
57+
```
6258
59+
#### Examples
60+
- Generate a custom theme using dark theme as base:
6361
```sh
6462
$ npx @emeks/antd-custom-theme-generator --theme dark
6563
```
66-
67-
- Generate default theme with non default paths
64+
- Generate a custom theme using default theme as base specifying non default paths:
6865
```sh
6966
$ npx @emeks/antd-custom-theme-generator ./styles/custom-theme.less ./styles/custom-theme.css
7067
```
7168
7269
### FAQ
73-
7470
> **Q1: Which variables can I override?**
7571
7672
You may override any variable exposed by Antd in [this file](https://github.com/ant-design/ant-design/blob/master/components/style/themes/default.less).
7773
7874
> **Q2: Should I run this command every time I change my `custom-theme.less` file?**
7975
80-
Yes, we don't provide hot-reload yet.
76+
No, you could pass the `-w` argument to let the script observe `<customThemeFilePath>` file and recompile it if it changes.
8177
8278
## Development
83-
8479
- Inside package folder, link it globally:
85-
86-
```sh
87-
$ npm link
88-
```
89-
90-
- Use the package in your project (with antd already installed and a `custom-theme.less` file created as well):
91-
92-
```sh
93-
$ generate-theme
94-
```
95-
96-
## TODO List:
97-
98-
- [ ] Add hot-reload generation.
80+
```sh
81+
$ npm link
82+
```
83+
- Use the package in your project (with antd already installed and a `<customThemeFilePath>` file created as well):
84+
```sh
85+
$ generate-theme
86+
```

index.js

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
11
#!/usr/bin/env node
22
const fs = require("fs");
33
const less = require("less");
4+
const watch = require('node-watch');
45
const argv = require("minimist")(
56
process.argv.slice(2),
6-
{alias: {"verbose": ["v"]}}
7+
{alias: {"verbose": ["v"], "watch": ['w']}}
78
);
89

910
const DEFAULTS = {
1011
verbose: false,
12+
watch: false,
1113
in: "./custom-theme.less",
1214
out: "./custom-theme.css",
1315
antd: "./node_modules/antd",
1416
theme: "default"
1517
};
1618

1719
const verbose = argv["verbose"] || DEFAULTS.verbose;
20+
const shouldWatch = argv["watch"] || DEFAULTS.watch;
1821
const inFilePath = argv._[0] || DEFAULTS.in;
1922
const outFilePath = argv._[1] || DEFAULTS.out;
2023
const antdLibPath = argv["antd"] || DEFAULTS.antd;
2124
const theme = argv["theme"] || DEFAULTS.theme;
2225

23-
verbose && console.debug(`
26+
verbose && console.debug(`Verbose::
2427
\x1b[34m[Params]\x1b[0m
2528
Args:
2629
verbose: ${argv["verbose"] || ''}
30+
watch: ${argv["watch"] || ''}
2731
antd: ${argv['antd'] || ''}
2832
theme: ${argv['theme'] || ''}
2933
in: ${argv._[0] || ''}
3034
out: ${argv._[1] || ''}
3135
CWD: ${process.cwd()}
32-
__dirname: ${__dirname}
3336
3437
\x1b[34m[Vars]\x1b[0m
3538
customTheme: ${inFilePath}
3639
generatedTheme: ${outFilePath}
3740
antdLib: ${antdLibPath}
3841
theme: ${theme}
39-
40-
Generating theme...
4142
`);
4243

4344
const imports = [
@@ -46,15 +47,33 @@ const imports = [
4647
`@import url('${inFilePath}');`
4748
].join('');
4849

49-
less.render(imports, {javascriptEnabled: true})
50-
.then(
51-
({css}) => {
52-
try {
53-
fs.writeFileSync(outFilePath, css);
54-
console.log(`AntDesign theme (${outFilePath}) successfully generated.`);
55-
} catch(e) {
56-
console.error(`Could not write into file (${outFilePath}):`, e);
57-
}
58-
},
59-
(error) => console.error(error)
60-
);
50+
const compile = () => {
51+
console.log('Generating theme...');
52+
less.render(imports, {javascriptEnabled: true})
53+
.then(
54+
({css}) => {
55+
try {
56+
fs.writeFileSync(outFilePath, css);
57+
console.log(`AntDesign theme (${outFilePath}) successfully generated.`);
58+
} catch(e) {
59+
console.error(`Could not write into file (${outFilePath}):`, e);
60+
}
61+
},
62+
(error) => console.error(error)
63+
);
64+
};
65+
66+
compile();
67+
if(shouldWatch){
68+
const watcher = watch(inFilePath, ()=> {
69+
console.log(`Watcher:: ${inFilePath} changed, recompiling.`);
70+
compile();
71+
});
72+
const closeWatcher = () => {
73+
console.log('Watcher:: Removing watcher...');
74+
watcher.close();
75+
};
76+
process.on('SIGTERM', closeWatcher);
77+
process.on('SIGINT', closeWatcher);
78+
process.on('SIGQUIT', closeWatcher);
79+
};

package-lock.json

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
},
77
"dependencies": {
88
"less": "3.11.3",
9-
"minimist": "1.2.5"
9+
"minimist": "1.2.5",
10+
"node-watch": "0.7.1"
1011
},
1112
"bin": {
1213
"generate-theme": "index.js"

0 commit comments

Comments
 (0)