pack 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #! /usr/bin/env node
  2. /*************************************************************
  3. *
  4. * Copyright (c) 2018 The MathJax Consortium
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * @fileoverview webpack the component in the current directory
  20. *
  21. * @author dpvc@mathjax.org (Davide Cervone)
  22. */
  23. const fs = require('fs');
  24. const path = require('path');
  25. const {spawn} = require('child_process');
  26. /**
  27. * @param {string} name The file name to turn into a Regular expression
  28. * @return {RegExp} The regular expression for the name,
  29. */
  30. function fileRegExp(name) {
  31. return new RegExp(name.replace(/([\\.{}[\]()?*^$])/g, '\\$1'), 'g');
  32. }
  33. /**
  34. * @param {Object} The file or asset data whose size is to be returned
  35. * @return {string} The string giving the size in KB
  36. */
  37. function fileSize(file) {
  38. return ' (' + (file.size / 1024).toFixed(2).replace(/\.?0+$/, '') + ' KB)';
  39. }
  40. /**
  41. * Regular expressions for the components directory and the MathJax .js location
  42. */
  43. const compRE = fileRegExp(path.dirname(__dirname));
  44. const rootRE = fileRegExp(path.join(path.dirname(path.dirname(__dirname)), 'js'));
  45. const nodeRE = fileRegExp(path.join(path.dirname(path.dirname(__dirname)), 'node_modules'));
  46. /**
  47. * @return {JSON} The parsed JSON from webpack
  48. */
  49. async function readJSON() {
  50. return new Promise((ok, fail) => {
  51. const buffer = [];
  52. const child = spawn('npx', ['webpack', '--json']);
  53. child.stdout.on('data', (data) => buffer.push(String(data)));
  54. child.stdout.on('close', (code) => {
  55. const json = JSON.parse(buffer.join(''));
  56. if (json.errors && json.errors.length) {
  57. fail(json.errors[0].message);
  58. }
  59. ok(json);
  60. });
  61. });
  62. }
  63. /**
  64. * Run webpack if there is a configuration file for it
  65. *
  66. * @param {string} dir The directory to pack
  67. */
  68. async function webpackLib(dir) {
  69. try {
  70. process.chdir(dir);
  71. const dirRE = fileRegExp(path.resolve(dir));
  72. //
  73. // Get js directory from the webpack.config.js file
  74. //
  75. const jsdir = require(path.resolve(dir, 'webpack.config.js')).plugins[0].definitions.__JSDIR__;
  76. const jsRE = fileRegExp(jsdir);
  77. const libRE = fileRegExp(path.resolve(jsdir, '..', 'components'));
  78. //
  79. // Get the json from webpack and print the asset name and size
  80. //
  81. const json = await readJSON();
  82. for (const asset of json.assets) {
  83. console.log(asset.name + fileSize(asset));
  84. }
  85. //
  86. // Sort the modules and print their names and sizes
  87. //
  88. const modules = json.modules;
  89. for (const module of modules) {
  90. module.name = path.resolve(dir, module.name)
  91. .replace(/ \+ \d+ modules/, '')
  92. .replace(dirRE, '.');
  93. }
  94. const list = [];
  95. for (const module of modules) {
  96. if (module.moduleType.match(/javascript/)) {
  97. let name = module.name
  98. .replace(compRE, '[components]')
  99. .replace(rootRE, '[mathjax]')
  100. .replace(nodeRE, '[node]')
  101. .replace(jsRE, '[js]')
  102. .replace(libRE, '[lib]');
  103. if (name.charAt(0) !== '.' && name.charAt(0) !== '[') {
  104. name = path.relative(dir, name);
  105. }
  106. list.push(' ' + name + fileSize(module));
  107. }
  108. }
  109. console.log(
  110. list
  111. .filter(a => a.slice(2, 4) === './').sort()
  112. .concat(list.filter(a => a.slice(2, 4) !== './').sort())
  113. .join('\n')
  114. );
  115. } catch (err) {
  116. console.error(err);
  117. }
  118. }
  119. webpackLib(process.argv[2] || '.');