Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 3 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #Browsersync - Middleware + CSS example
  2. ## Installation/Usage:
  3. To try this example, follow these 4 simple steps.
  4. **Step 1**: Clone this entire repo
  5. ```bash
  6. $ git clone https://github.com/Browsersync/recipes.git bs-recipes
  7. ```
  8. **Step 2**: Move into the directory containing this example
  9. ```bash
  10. $ cd bs-recipes/recipes/middleware.css.injection
  11. ```
  12. **Step 3**: Install dependencies
  13. ```bash
  14. $ npm install
  15. ```
  16. **Step 4**: Run the example
  17. ```bash
  18. $ npm start
  19. ```
  20. ### Additional Info:
  21. - Perform changes to `app/css/main.less` to see live css injection
  22. ### Preview of `app.js`:
  23. ```js
  24. /**
  25. * Require Browsersync
  26. */
  27. var browserSync = require("browser-sync");
  28. /**
  29. * Run the middleware on files that contain .less
  30. */
  31. function lessMiddleware (req, res, next) {
  32. var parsed = require("url").parse(req.url);
  33. if (parsed.pathname.match(/\.less$/)) {
  34. return less(parsed.pathname).then(function (o) {
  35. res.setHeader('Content-Type', 'text/css');
  36. res.end(o.css);
  37. });
  38. }
  39. next();
  40. }
  41. /**
  42. * Compile less
  43. */
  44. function less(src) {
  45. var f = require('fs').readFileSync('app' + src).toString();
  46. return require('less').render(f);
  47. }
  48. /**
  49. * Run Browsersync with less middleware
  50. */
  51. browserSync({
  52. files: "app/css/*.less",
  53. server: "app",
  54. injectFileTypes: ["less"],
  55. /**
  56. * Catch all requests, if any are for .less files, recompile on the fly and
  57. * send back a CSS response
  58. */
  59. middleware: lessMiddleware
  60. });
  61. ```