sass.node.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*! sass.js - v0.11.1 (f286436) - built 2019-10-20
  2. providing libsass 3.6.2 (4da7c4bd)
  3. via emscripten 1.38.31 (040e49a)
  4. */
  5. var Sass = require('./sass.sync.js');
  6. var fs = require('fs');
  7. var path = require('path');
  8. function fileExists(path) {
  9. var stat = fs.statSync(path);
  10. return stat && stat.isFile();
  11. }
  12. function removeFileExtension(path) {
  13. return path.slice(0, path.lastIndexOf('.'));
  14. }
  15. function importFileToSass(path, done) {
  16. // any path must be relative to CWD to work in both environments (real FS, and emscripten FS)
  17. var requestedPath = './' + path;
  18. // figure out the *actual* path of the file
  19. var filesystemPath = Sass.findPathVariation(fileExists, requestedPath);
  20. if (!filesystemPath) {
  21. done({
  22. error: 'File "' + requestedPath + '" not found',
  23. });
  24. return;
  25. }
  26. // Make sure to omit the ".css" file extension when it was omitted in requestedPath.
  27. // This allow raw css imports.
  28. // see https://github.com/sass/libsass/pull/754
  29. var isRawCss = !requestedPath.endsWith('.css') && filesystemPath.endsWith('.css');
  30. var targetPath = isRawCss ? removeFileExtension(filesystemPath) : filesystemPath;
  31. // write the file to emscripten FS so libsass internal FS handling
  32. // can engage the scss/sass switch, which apparently does not happen
  33. // for content provided through the importer callback directly
  34. var content = fs.readFileSync(filesystemPath, {encoding: 'utf8'});
  35. Sass.writeFile(filesystemPath, content, function() {
  36. done({
  37. path: targetPath,
  38. });
  39. });
  40. }
  41. function importerCallback(request, done) {
  42. importFileToSass(resolve(request), done);
  43. }
  44. function compileFile(path, options, callback) {
  45. if (!callback) {
  46. callback = options;
  47. options = {};
  48. }
  49. Sass.importer(importerCallback);
  50. importFileToSass(path, function() {
  51. Sass.compileFile(path, options, callback);
  52. });
  53. }
  54. function resolve(request) {
  55. // the request will not have the correct "resolved" path on Windows
  56. // see https://github.com/medialize/sass.js/issues/69
  57. // see https://github.com/medialize/sass.js/issues/86
  58. return path.normalize(
  59. path.join(
  60. // sass.js works in the "/sass/" directory, make that relative to CWD
  61. path.dirname(request.previous.replace(/^\/sass\//, '')),
  62. request.current
  63. )
  64. ).replace(/\\/g, '/');
  65. }
  66. compileFile.importFileToSass = importFileToSass;
  67. compileFile.Sass = Sass;
  68. module.exports = compileFile;