4
1

lessc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. #!/usr/bin/env node
  2. /* eslint indent: [2, 2, {"SwitchCase": 1}] */
  3. "use strict";
  4. var path = require('path');
  5. var fs = require('../lib/less-node/fs').default;
  6. var os = require('os');
  7. var utils = require('../lib/less/utils');
  8. var Constants = require('../lib/less/constants');
  9. var less = require('../lib/less-node').default;
  10. var errno;
  11. var mkdirp;
  12. try {
  13. errno = require('errno');
  14. } catch (err) {
  15. errno = null;
  16. }
  17. var pluginManager = new less.PluginManager(less);
  18. var fileManager = new less.FileManager();
  19. var plugins = [];
  20. var queuePlugins = [];
  21. var args = process.argv.slice(1);
  22. var silent = false;
  23. var verbose = false;
  24. var options = less.options;
  25. options.plugins = plugins;
  26. options.reUsePluginManager = true;
  27. var sourceMapOptions = {};
  28. var continueProcessing = true;
  29. var checkArgFunc = function checkArgFunc(arg, option) {
  30. if (!option) {
  31. console.error("".concat(arg, " option requires a parameter"));
  32. continueProcessing = false;
  33. process.exitCode = 1;
  34. return false;
  35. }
  36. return true;
  37. };
  38. var checkBooleanArg = function checkBooleanArg(arg) {
  39. var onOff = /^((on|t|true|y|yes)|(off|f|false|n|no))$/i.exec(arg);
  40. if (!onOff) {
  41. console.error(" unable to parse ".concat(arg, " as a boolean. use one of on/t/true/y/yes/off/f/false/n/no"));
  42. continueProcessing = false;
  43. process.exitCode = 1;
  44. return false;
  45. }
  46. return Boolean(onOff[2]);
  47. };
  48. var parseVariableOption = function parseVariableOption(option, variables) {
  49. var parts = option.split('=', 2);
  50. variables[parts[0]] = parts[1];
  51. };
  52. var sourceMapFileInline = false;
  53. function printUsage() {
  54. less.lesscHelper.printUsage();
  55. pluginManager.Loader.printUsage(plugins);
  56. continueProcessing = false;
  57. }
  58. function render() {
  59. if (!continueProcessing) {
  60. return;
  61. }
  62. var input = args[1];
  63. if (input && input != '-') {
  64. input = path.resolve(process.cwd(), input);
  65. }
  66. var output = args[2];
  67. var outputbase = args[2];
  68. if (output) {
  69. output = path.resolve(process.cwd(), output);
  70. }
  71. if (options.disablePluginRule && queuePlugins.length > 0) {
  72. console.error('--plugin and --disable-plugin-rule may not be used at the same time');
  73. process.exitCode = 1;
  74. return;
  75. }
  76. if (options.sourceMap) {
  77. sourceMapOptions.sourceMapInputFilename = input;
  78. if (!sourceMapOptions.sourceMapFullFilename) {
  79. if (!output && !sourceMapFileInline) {
  80. console.error('the sourcemap option only has an optional filename if the css filename is given');
  81. console.error('consider adding --source-map-map-inline which embeds the sourcemap into the css');
  82. process.exitCode = 1;
  83. return;
  84. } // its in the same directory, so always just the basename
  85. if (output) {
  86. sourceMapOptions.sourceMapOutputFilename = path.basename(output);
  87. sourceMapOptions.sourceMapFullFilename = "".concat(output, ".map");
  88. } // its in the same directory, so always just the basename
  89. if ('sourceMapFullFilename' in sourceMapOptions) {
  90. sourceMapOptions.sourceMapFilename = path.basename(sourceMapOptions.sourceMapFullFilename);
  91. }
  92. } else if (options.sourceMap && !sourceMapFileInline) {
  93. var mapFilename = path.resolve(process.cwd(), sourceMapOptions.sourceMapFullFilename);
  94. var mapDir = path.dirname(mapFilename);
  95. var outputDir = path.dirname(output); // find the path from the map to the output file
  96. // eslint-disable-next-line max-len
  97. sourceMapOptions.sourceMapOutputFilename = path.join(path.relative(mapDir, outputDir), path.basename(output)); // make the sourcemap filename point to the sourcemap relative to the css file output directory
  98. sourceMapOptions.sourceMapFilename = path.join(path.relative(outputDir, mapDir), path.basename(sourceMapOptions.sourceMapFullFilename));
  99. }
  100. if (sourceMapOptions.sourceMapURL && sourceMapOptions.disableSourcemapAnnotation) {
  101. console.error('You cannot provide flag --source-map-url with --source-map-no-annotation.');
  102. console.error('Please remove one of those flags.');
  103. process.exitcode = 1;
  104. return;
  105. }
  106. }
  107. if (sourceMapOptions.sourceMapBasepath === undefined) {
  108. sourceMapOptions.sourceMapBasepath = input ? path.dirname(input) : process.cwd();
  109. }
  110. if (sourceMapOptions.sourceMapRootpath === undefined) {
  111. var pathToMap = path.dirname((sourceMapFileInline ? output : sourceMapOptions.sourceMapFullFilename) || '.');
  112. var pathToInput = path.dirname(sourceMapOptions.sourceMapInputFilename || '.');
  113. sourceMapOptions.sourceMapRootpath = path.relative(pathToMap, pathToInput);
  114. }
  115. if (!input) {
  116. console.error('lessc: no input files');
  117. console.error('');
  118. printUsage();
  119. process.exitCode = 1;
  120. return;
  121. }
  122. var ensureDirectory = function ensureDirectory(filepath) {
  123. var dir = path.dirname(filepath);
  124. var cmd;
  125. var existsSync = fs.existsSync || path.existsSync;
  126. if (!existsSync(dir)) {
  127. if (mkdirp === undefined) {
  128. try {
  129. mkdirp = require('make-dir');
  130. } catch (e) {
  131. mkdirp = null;
  132. }
  133. }
  134. cmd = mkdirp && mkdirp.sync || fs.mkdirSync;
  135. cmd(dir);
  136. }
  137. };
  138. if (options.depends) {
  139. if (!outputbase) {
  140. console.error('option --depends requires an output path to be specified');
  141. process.exitCode = 1;
  142. return;
  143. }
  144. process.stdout.write("".concat(outputbase, ": "));
  145. }
  146. if (!sourceMapFileInline) {
  147. var writeSourceMap = function writeSourceMap() {
  148. var output = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  149. var onDone = arguments.length > 1 ? arguments[1] : undefined;
  150. var filename = sourceMapOptions.sourceMapFullFilename;
  151. ensureDirectory(filename);
  152. // To fix https://github.com/less/less.js/issues/3646
  153. output = output.toString();
  154. fs.writeFile(filename, output, 'utf8', function (err) {
  155. if (err) {
  156. var description = 'Error: ';
  157. if (errno && errno.errno[err.errno]) {
  158. description += errno.errno[err.errno].description;
  159. } else {
  160. description += "".concat(err.code, " ").concat(err.message);
  161. }
  162. console.error("lessc: failed to create file ".concat(filename));
  163. console.error(description);
  164. process.exitCode = 1;
  165. } else {
  166. less.logger.info("lessc: wrote ".concat(filename));
  167. }
  168. onDone();
  169. });
  170. };
  171. }
  172. var writeSourceMapIfNeeded = function writeSourceMapIfNeeded(output, onDone) {
  173. if (options.sourceMap && !sourceMapFileInline) {
  174. writeSourceMap(output, onDone);
  175. } else {
  176. onDone();
  177. }
  178. };
  179. var writeOutput = function writeOutput(output, result, onSuccess) {
  180. if (options.depends) {
  181. onSuccess();
  182. } else if (output) {
  183. ensureDirectory(output);
  184. fs.writeFile(output, result.css, {
  185. encoding: 'utf8'
  186. }, function (err) {
  187. if (err) {
  188. var description = 'Error: ';
  189. if (errno && errno.errno[err.errno]) {
  190. description += errno.errno[err.errno].description;
  191. } else {
  192. description += "".concat(err.code, " ").concat(err.message);
  193. }
  194. console.error("lessc: failed to create file ".concat(output));
  195. console.error(description);
  196. process.exitCode = 1;
  197. } else {
  198. less.logger.info("lessc: wrote ".concat(output));
  199. onSuccess();
  200. }
  201. });
  202. } else if (!options.depends) {
  203. process.stdout.write(result.css);
  204. onSuccess();
  205. }
  206. };
  207. var logDependencies = function logDependencies(options, result) {
  208. if (options.depends) {
  209. var depends = '';
  210. for (var i = 0; i < result.imports.length; i++) {
  211. depends += "".concat(result.imports[i], " ");
  212. }
  213. console.log(depends);
  214. }
  215. };
  216. var parseLessFile = function parseLessFile(e, data) {
  217. if (e) {
  218. console.error("lessc: ".concat(e.message));
  219. process.exitCode = 1;
  220. return;
  221. }
  222. data = data.replace(/^\uFEFF/, '');
  223. options.paths = [path.dirname(input)].concat(options.paths);
  224. options.filename = input;
  225. if (options.lint) {
  226. options.sourceMap = false;
  227. }
  228. sourceMapOptions.sourceMapFileInline = sourceMapFileInline;
  229. if (options.sourceMap) {
  230. options.sourceMap = sourceMapOptions;
  231. }
  232. less.logger.addListener({
  233. info: function info(msg) {
  234. if (verbose) {
  235. console.log(msg);
  236. }
  237. },
  238. warn: function warn(msg) {
  239. // do not show warning if the silent option is used
  240. if (!silent) {
  241. console.warn(msg);
  242. }
  243. },
  244. error: function error(msg) {
  245. console.error(msg);
  246. }
  247. });
  248. less.render(data, options).then(function (result) {
  249. if (!options.lint) {
  250. writeOutput(output, result, function () {
  251. writeSourceMapIfNeeded(result.map, function () {
  252. logDependencies(options, result);
  253. });
  254. });
  255. }
  256. }, function (err) {
  257. if (!options.silent) {
  258. console.error(err.toString({
  259. stylize: options.color && less.lesscHelper.stylize
  260. }));
  261. }
  262. process.exitCode = 1;
  263. });
  264. };
  265. if (input != '-') {
  266. fs.readFile(input, 'utf8', parseLessFile);
  267. } else {
  268. process.stdin.resume();
  269. process.stdin.setEncoding('utf8');
  270. var buffer = '';
  271. process.stdin.on('data', function (data) {
  272. buffer += data;
  273. });
  274. process.stdin.on('end', function () {
  275. parseLessFile(false, buffer);
  276. });
  277. }
  278. }
  279. function processPluginQueue() {
  280. var x = 0;
  281. function pluginError(name) {
  282. console.error("Unable to load plugin ".concat(name, " please make sure that it is installed under or at the same level as less"));
  283. process.exitCode = 1;
  284. }
  285. function pluginFinished(plugin) {
  286. x++;
  287. plugins.push(plugin);
  288. if (x === queuePlugins.length) {
  289. render();
  290. }
  291. }
  292. queuePlugins.forEach(function (queue) {
  293. var context = utils.clone(options);
  294. pluginManager.Loader.loadPlugin(queue.name, process.cwd(), context, less.environment, fileManager).then(function (data) {
  295. pluginFinished({
  296. fileContent: data.contents,
  297. filename: data.filename,
  298. options: queue.options
  299. });
  300. }).catch(function () {
  301. pluginError(queue.name);
  302. });
  303. });
  304. } // self executing function so we can return
  305. (function () {
  306. args = args.filter(function (arg) {
  307. var match;
  308. match = arg.match(/^-I(.+)$/);
  309. if (match) {
  310. options.paths.push(match[1]);
  311. return false;
  312. }
  313. match = arg.match(/^--?([a-z][0-9a-z-]*)(?:=(.*))?$/i);
  314. if (match) {
  315. arg = match[1];
  316. } else {
  317. return arg;
  318. }
  319. switch (arg) {
  320. case 'v':
  321. case 'version':
  322. console.log("lessc ".concat(less.version.join('.'), " (Less Compiler) [JavaScript]"));
  323. continueProcessing = false;
  324. break;
  325. case 'verbose':
  326. verbose = true;
  327. break;
  328. case 's':
  329. case 'silent':
  330. silent = true;
  331. break;
  332. case 'l':
  333. case 'lint':
  334. options.lint = true;
  335. break;
  336. case 'strict-imports':
  337. options.strictImports = true;
  338. break;
  339. case 'h':
  340. case 'help':
  341. printUsage();
  342. break;
  343. case 'x':
  344. case 'compress':
  345. options.compress = true;
  346. break;
  347. case 'insecure':
  348. options.insecure = true;
  349. break;
  350. case 'M':
  351. case 'depends':
  352. options.depends = true;
  353. break;
  354. case 'max-line-len':
  355. if (checkArgFunc(arg, match[2])) {
  356. options.maxLineLen = parseInt(match[2], 10);
  357. if (options.maxLineLen <= 0) {
  358. options.maxLineLen = -1;
  359. }
  360. }
  361. break;
  362. case 'no-color':
  363. options.color = false;
  364. break;
  365. case 'js':
  366. options.javascriptEnabled = true;
  367. break;
  368. case 'no-js':
  369. // eslint-disable-next-line max-len
  370. console.error('The "--no-js" argument is deprecated, as inline JavaScript is disabled by default. Use "--js" to enable inline JavaScript (not recommended).');
  371. break;
  372. case 'include-path':
  373. if (checkArgFunc(arg, match[2])) {
  374. // ; supported on windows.
  375. // : supported on windows and linux, excluding a drive letter like C:\ so C:\file:D:\file parses to 2
  376. options.paths = match[2].split(os.type().match(/Windows/) ? /:(?!\\)|;/ : ':').map(function (p) {
  377. if (p) {
  378. return path.resolve(process.cwd(), p);
  379. }
  380. });
  381. }
  382. break;
  383. case 'line-numbers':
  384. if (checkArgFunc(arg, match[2])) {
  385. options.dumpLineNumbers = match[2];
  386. }
  387. break;
  388. case 'source-map':
  389. options.sourceMap = true;
  390. if (match[2]) {
  391. sourceMapOptions.sourceMapFullFilename = match[2];
  392. }
  393. break;
  394. case 'source-map-rootpath':
  395. if (checkArgFunc(arg, match[2])) {
  396. sourceMapOptions.sourceMapRootpath = match[2];
  397. }
  398. break;
  399. case 'source-map-basepath':
  400. if (checkArgFunc(arg, match[2])) {
  401. sourceMapOptions.sourceMapBasepath = match[2];
  402. }
  403. break;
  404. case 'source-map-inline':
  405. case 'source-map-map-inline':
  406. sourceMapFileInline = true;
  407. options.sourceMap = true;
  408. break;
  409. case 'source-map-include-source':
  410. case 'source-map-less-inline':
  411. sourceMapOptions.outputSourceFiles = true;
  412. break;
  413. case 'source-map-url':
  414. if (checkArgFunc(arg, match[2])) {
  415. sourceMapOptions.sourceMapURL = match[2];
  416. }
  417. break;
  418. case 'source-map-no-annotation':
  419. sourceMapOptions.disableSourcemapAnnotation = true;
  420. break;
  421. case 'rp':
  422. case 'rootpath':
  423. if (checkArgFunc(arg, match[2])) {
  424. options.rootpath = match[2].replace(/\\/g, '/');
  425. }
  426. break;
  427. case 'ie-compat':
  428. console.warn('The --ie-compat option is deprecated, as it has no effect on compilation.');
  429. break;
  430. case 'relative-urls':
  431. console.warn('The --relative-urls option has been deprecated. Use --rewrite-urls=all.');
  432. options.rewriteUrls = Constants.RewriteUrls.ALL;
  433. break;
  434. case 'ru':
  435. case 'rewrite-urls':
  436. var m = match[2];
  437. if (m) {
  438. if (m === 'local') {
  439. options.rewriteUrls = Constants.RewriteUrls.LOCAL;
  440. } else if (m === 'off') {
  441. options.rewriteUrls = Constants.RewriteUrls.OFF;
  442. } else if (m === 'all') {
  443. options.rewriteUrls = Constants.RewriteUrls.ALL;
  444. } else {
  445. console.error("Unknown rewrite-urls argument ".concat(m));
  446. continueProcessing = false;
  447. process.exitCode = 1;
  448. }
  449. } else {
  450. options.rewriteUrls = Constants.RewriteUrls.ALL;
  451. }
  452. break;
  453. case 'sm':
  454. case 'strict-math':
  455. console.warn('The --strict-math option has been deprecated. Use --math=strict.');
  456. if (checkArgFunc(arg, match[2])) {
  457. if (checkBooleanArg(match[2])) {
  458. options.math = Constants.Math.PARENS;
  459. }
  460. }
  461. break;
  462. case 'm':
  463. case 'math':
  464. var m = match[2];
  465. if (checkArgFunc(arg, m)) {
  466. if (m === 'always') {
  467. console.warn('--math=always is deprecated and will be removed in the future.');
  468. options.math = Constants.Math.ALWAYS;
  469. } else if (m === 'parens-division') {
  470. options.math = Constants.Math.PARENS_DIVISION;
  471. } else if (m === 'parens' || m === 'strict') {
  472. options.math = Constants.Math.PARENS;
  473. } else if (m === 'strict-legacy') {
  474. console.warn('--math=strict-legacy has been removed. Defaulting to --math=strict');
  475. options.math = Constants.Math.PARENS;
  476. }
  477. }
  478. break;
  479. case 'su':
  480. case 'strict-units':
  481. if (checkArgFunc(arg, match[2])) {
  482. options.strictUnits = checkBooleanArg(match[2]);
  483. }
  484. break;
  485. case 'global-var':
  486. if (checkArgFunc(arg, match[2])) {
  487. if (!options.globalVars) {
  488. options.globalVars = {};
  489. }
  490. parseVariableOption(match[2], options.globalVars);
  491. }
  492. break;
  493. case 'modify-var':
  494. if (checkArgFunc(arg, match[2])) {
  495. if (!options.modifyVars) {
  496. options.modifyVars = {};
  497. }
  498. parseVariableOption(match[2], options.modifyVars);
  499. }
  500. break;
  501. case 'url-args':
  502. if (checkArgFunc(arg, match[2])) {
  503. options.urlArgs = match[2];
  504. }
  505. break;
  506. case 'plugin':
  507. var splitupArg = match[2].match(/^([^=]+)(=(.*))?/);
  508. var name = splitupArg[1];
  509. var pluginOptions = splitupArg[3];
  510. queuePlugins.push({
  511. name: name,
  512. options: pluginOptions
  513. });
  514. break;
  515. case 'disable-plugin-rule':
  516. options.disablePluginRule = true;
  517. break;
  518. default:
  519. queuePlugins.push({
  520. name: arg,
  521. options: match[2],
  522. default: true
  523. });
  524. break;
  525. }
  526. });
  527. if (queuePlugins.length > 0) {
  528. processPluginQueue();
  529. } else {
  530. render();
  531. }
  532. })();