Realtime css / scss edition with meteor avoiding server restart

后端 未结 1 1574
[愿得一人]
[愿得一人] 2020-12-30 17:19

While building large applications with meteor, we do face the regular problem of editing the stylesheets files. Once a file is edited, the whole application reloads which ta

相关标签:
1条回答
  • 2020-12-30 17:52

    Here is what you need (it looks fastidious but do not be afraid, it worth it):

    Setup your project to externalise .css files

    • Meteor compiles all the .css files into one monolithic one, most of the css editors are not expecting this behaviour. For the development phase, I do recommend to use the traditional approach of including the stylesheet to the html page itself. to do so:
      • Create a public folder in the root of your meteor project: meteorProject/public
      • Add a css file into this folder: meteorProject/public/style.css
      • Import the stylesheet in your main html code <link rel="stylesheet" type="text/css" href="/style.css" />

    What you did ? You created a public repository accessible through the path localhost:3000/ then you added the stylesheet style.css to this repository, that one became available through the relative path /style.css. By using this technique, meteor will not compile neither include by itself the stylesheet to your project, you just load it manually in the regular way using the link tag. Now it is time to configure an editor.

    • Now that the styles are imported the way they were 10 year ago, you can use compatible tools which will override the style to allow live editing. A simple one but only for css is the well known Espresso (formerly CSSEdit), open the page and override the styles… but that one is currently not supporting .scss files.

    Editing .scss files in realtime with meteor:

    • To achieve this, you will need to use Sublime Text 2 or 3 as the editor, you can get it here: http://www.sublimetext.com/3 it is not free but there is no feature nor time restriction. So if you continue using it, just buy it to support the developers team.
    • You will need the awesome tool to allow the live edition which is takana, get it here: https://github.com/mechio/takana

    That takana is freaking awesome! the concept is the following: Once installed and ran it will create a server interacting with the sublimetext editor, then you are requested to add a js snippet to your code so that the browser will get connected with the takana server and reload the .css or .scss files in realtime without having to reboot meteor. To setup the meteor project with takana just do the following:

    • open the terminal
    • sudo npm install -g takana (enter your password if requested)
    • start takana in another terminal by providing it the absolute path of the meteorProject/public folder created above is might look something like: takana /Users/aUser/meteorProject/public
    • Add to your main html page the js snippet <script type="text/javascript" src="http://localhost:48626/takana.js"></script>

    You are all set, now to use it

    • Start your meteor project in a second terminal. command meteor from the right path…
    • Open any browser to your meteor page i.e. probably http://localhost:3000
    • Open sublimetext, try editing your style.css file, the css changes are automatically displayed on the browser page without saving anything.
    • This is also working with .scss file. Just rename the style.css to style.css.scss and edit it within sublime text. Here the magic happen, you are editing a scss file with live result on a meteor application without having to reload anything.

    Once you are satisfied with the result you can either compile the .scss to a .css file and add it the regular way to the project, or use the meteor .scss package which will do this for you at each restart. Note: Don't forget to remove the js and style snippet one to your code once in production.

    Last but not least: you can open the project in several browsers and see them be refreshed in live while you edit the file in SublimeText, also it worked fine with Safari, FF but for some reasons I had to use "Google Chrome Canary" instead of "Chrome". Please comment if you made it work on other browsers such as IE, Opera or even if it worked with the regular "Chrome" on your computer.

    0 讨论(0)
提交回复
热议问题