This script was inspired by this blog post - however I found the technique to be a little insufficient. Many thanks to Artem Butusov as I would not have been able to do this without his blog post.
This script is intended to be used with semantic-ui-react projects. If you are just using semantic-ui, then you may need to do some other troubleshooting... I don't know as I haven't tested. From what I can tell everything should work just fine.
This process is completely different from the one described in the blog post above. This script is intended to do everything for you - no manual copying or updating code is required. If you have already followed the blog post above, I highly suggest you start over:
npm uninstall semantic-ui-less
- backup and delete any files you created
All semantic-ui related files will be put into a single directory - and it is time to decide where that directory will be (eg. public/semantic-ui
). This directory will hold your theme config and all of your site-level variables/overrides. Take special note of the path to this directory - we will need it in a minute. I will refer to this directory as the SITE_ROOT
from here on out.
The very first time you run this script, it will create the SITE_ROOT
folder for you and copy all the files you need. If the directory already exists, then we assume this script has already run and many of the files you need will not be copied. I highly recommend that you delete/rename the SITE_ROOT
directory if it already exists before running this script for the first time.
-
Install the modules required for this to work:
npm i -D semantic-ui-less ncp find-in-files
-
Save the
semantic-ui-less-postinstall.js
file to your project -
Update the
SITE_ROOT
variable at the top of this script to point to the directory you decided on earlier:- The path must be relative to your project root - this is very important
- Do not unclude leading or trailing slashes - this is also important
- Do not use
path.resolve
(or anything similar) - this too is important
-
Update your
package.json
to execute this script during postinstall:"scripts": { "postinstall": "node scripts/semantic-ui-less-postinstall" }
-
Now you can run the script:
npm run postinstall
You can read the code to actually see what's going on, but here's the short of it:
- Created the
SITE_ROOT
directory to hold all semantic-ui related files for your project. - Copied the following files into your project from
semantic-ui-less
:/_site/**
/theme.config.example
(renamed totheme.config
)/theme.less
- Updated variables and paths so that everything loads properly.
Nothing - we check to make sure files don't exist before doing anything. If a file already exists, we do nothing. If this script only executed part of the way, then you should be able to run it again and it will pick up where it left off. I recommend reading the code to see what's going on.
You can now load your files like this:
import { Button, Dropdown, Modal } from 'semantic-ui-react';
import 'semantic-ui-less/semantic.less';
It's up to you where/when you load the LESS file. I recommend importing it anywhere you use a semantic-ui component - it will only be imported once, even if it's imported in 10 different components. Other people like to import once as early as possible - in their main app.js
file or wherever. It's up to you.
Take a look at the SITE_ROOT/theme.config
file. All components use the default
theme out of the box, but you can update any component to use a different theme (look here for a list of available themes). For example, if you want all buttons to use the "amazon" theme, then update your theme.config accordingly:
@button : 'amazon';
If you need further customization, then you can use the variables/overrides files which are now in your project.
SITE_ROOT/elements/button.variables
- set your own button variables here. These will override the values set by the theme. There is no need to set all variables, just the ones you want to change. Do not write any CSS in this file, just variables.SITE_ROOT/elements/button.overrides
- write any custom CSS here to override the button CSS. You should only need to do this for advanced situations. Most of the time you should be able to use variables to get the styles you need.
You should be able to update semantic-ui-less
just like you would any other module. If you are worried about updates breaking your app, I recommend locking into a certain version of semantic-ui-less
by removing the caret ^
or tilde ~
from your package.json:
"devDependencies": {
"semantic-ui-less": "2.2.12"
}
Whenever you are ready to update, you can do so explicitly by updating the version number manually or running a script like this:
npm uninstall semantic-ui-less -D && npm i semantic-ui-less -D
Whenever you update, it is up to you to test your app and make sure everything still looks ok.
Thanks for posting this it really helped me get set up and streamlined.. I'm wondering what you'd do if you wanted to create a custom theme and keep this workflow?