build-i18n.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. 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,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. const fs = require('fs');
  20. const preamble = require('./preamble');
  21. const ts = require('typescript');
  22. const path = require('path');
  23. const fsExtra = require('fs-extra');
  24. const umdWrapperHead = `
  25. ${preamble.js}
  26. /**
  27. * AUTO-GENERATED FILE. DO NOT MODIFY.
  28. */
  29. (function(root, factory) {
  30. if (typeof define === 'function' && define.amd) {
  31. // AMD. Register as an anonymous module.
  32. define(['exports'], factory);
  33. } else if (
  34. typeof exports === 'object' &&
  35. typeof exports.nodeName !== 'string'
  36. ) {
  37. // CommonJS
  38. factory(exports);
  39. } else {
  40. // Browser globals
  41. factory({});
  42. }
  43. })(this, function(exports) {
  44. `;
  45. const umdWrapperHeadWithEcharts = `
  46. ${preamble.js}
  47. /**
  48. * AUTO-GENERATED FILE. DO NOT MODIFY.
  49. */
  50. (function(root, factory) {
  51. if (typeof define === 'function' && define.amd) {
  52. // AMD. Register as an anonymous module.
  53. define(['exports', 'echarts'], factory);
  54. } else if (
  55. typeof exports === 'object' &&
  56. typeof exports.nodeName !== 'string'
  57. ) {
  58. // CommonJS
  59. factory(exports, require('echarts/lib/echarts'));
  60. } else {
  61. // Browser globals
  62. factory({}, root.echarts);
  63. }
  64. })(this, function(exports, echarts) {
  65. `;
  66. const umdWrapperTail = `
  67. });`;
  68. async function buildI18nWrap() {
  69. const targetDir = path.join(__dirname, '../i18n');
  70. const sourceDir = path.join(__dirname, '../src/i18n');
  71. const files = fs.readdirSync(sourceDir);
  72. files.forEach(t => {
  73. if(!t.startsWith('lang')) {
  74. return;
  75. }
  76. const fileName = t.replace(/\.ts$/, '');
  77. const type = fileName.replace(/^lang/, '');
  78. const echartsRegister = `
  79. echarts.registerLocale('${type}', localeObj);
  80. `;
  81. const pureExports = `
  82. for (var key in localeObj) {
  83. if (localeObj.hasOwnProperty(key)) {
  84. exports[key] = localeObj[key];
  85. }
  86. }
  87. `;
  88. const code = fs.readFileSync(path.join(sourceDir, t), 'utf-8');
  89. // const outputText = ts.transpileModule(code, {
  90. // module: ts.ModuleKind.CommonJS,
  91. // }).outputText;
  92. // Simple regexp replace is enough
  93. const outputCode = code.replace(/export\s+?default/, 'var localeObj =')
  94. .replace(/\/\*([\w\W]*?)\*\//, '');
  95. fsExtra.ensureDirSync(targetDir);
  96. fs.writeFileSync(path.join(targetDir, fileName + '.js'), umdWrapperHeadWithEcharts + outputCode + echartsRegister + umdWrapperTail, 'utf-8');
  97. fs.writeFileSync(path.join(targetDir, fileName + '-obj.js'), umdWrapperHead + outputCode + pureExports + umdWrapperTail, 'utf-8');
  98. })
  99. console.log('i18n build completed');
  100. }
  101. buildI18nWrap();
  102. module.exports = {
  103. buildI18n: buildI18nWrap
  104. };