languages-development.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict';
  2. /**
  3. * Config responsible for building not minified Handsontable `languages/` files.
  4. */
  5. const NEW_LINE_CHAR = '\n';
  6. const SOURCE_LANGUAGES_DIRECTORY = 'src/i18n/languages';
  7. const OUTPUT_LANGUAGES_DIRECTORY = 'languages';
  8. const path = require('path');
  9. const StringReplacePlugin = require('string-replace-webpack-plugin');
  10. const WebpackOnBuildPlugin = require('on-build-webpack');
  11. const fs = require('fs');
  12. const fsExtra = require('fs-extra');
  13. function getEntryJsFiles() {
  14. const entryObject = {};
  15. const filesInLanguagesDirectory = fs.readdirSync(SOURCE_LANGUAGES_DIRECTORY);
  16. filesInLanguagesDirectory.forEach((fileName) => {
  17. const jsExtensionRegExp = /\.js$/;
  18. if (jsExtensionRegExp.test(fileName)) {
  19. let fileNameWithoutExtension = fileName.replace(jsExtensionRegExp, '');
  20. if (fileNameWithoutExtension === 'index') {
  21. fileNameWithoutExtension = 'all';
  22. }
  23. entryObject[fileNameWithoutExtension] = path.resolve(SOURCE_LANGUAGES_DIRECTORY, fileName);
  24. }
  25. });
  26. return entryObject;
  27. }
  28. const ruleForSnippetsInjection = {
  29. test: /\.js$/,
  30. loader: StringReplacePlugin.replace({
  31. replacements: [
  32. {
  33. pattern: /import.+constants.+/,
  34. replacement: function() {
  35. const snippet1 = `import Handsontable from '../../handsontable';`;
  36. const snippet2 = `const C = Handsontable.languages.dictionaryKeys;`;
  37. return `${snippet1}${NEW_LINE_CHAR.repeat(2)}${snippet2}`;
  38. }
  39. },
  40. {
  41. pattern: /export default dictionary.+/,
  42. replacement: function(matchingPhrase) {
  43. const snippet = `Handsontable.languages.registerLanguageDictionary(dictionary);`;
  44. return `${snippet}${NEW_LINE_CHAR.repeat(2)}${matchingPhrase}`;
  45. }
  46. }
  47. ]
  48. })
  49. };
  50. module.exports.create = function create() {
  51. const config = {
  52. entry: getEntryJsFiles(),
  53. output: {
  54. path: path.resolve(__dirname, '../' + OUTPUT_LANGUAGES_DIRECTORY),
  55. libraryTarget: 'umd',
  56. filename: '[name].js',
  57. // Workaround below: Without this option webpack would export all language packs as globals
  58. libraryExport: '___',
  59. umdNamedDefine: true
  60. },
  61. externals: {
  62. '../../handsontable': {
  63. root: 'Handsontable',
  64. commonjs2: '../../handsontable',
  65. commonjs: '../../handsontable',
  66. amd: '../../handsontable',
  67. },
  68. },
  69. module: {
  70. rules: [
  71. {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'},
  72. ruleForSnippetsInjection
  73. ]
  74. },
  75. plugins: [
  76. new WebpackOnBuildPlugin(() => {
  77. const filesInOutputLanguagesDirectory = fs.readdirSync(OUTPUT_LANGUAGES_DIRECTORY);
  78. const indexFileName = 'index.js';
  79. const allLanguagesFileName = 'all.js';
  80. // copy files from `languages` directory to `dist/languages` directory
  81. filesInOutputLanguagesDirectory.forEach((fileName) => {
  82. if (fileName !== indexFileName) {
  83. fsExtra.copySync(`${OUTPUT_LANGUAGES_DIRECTORY}/${fileName}`, `dist/languages/${fileName}`);
  84. }
  85. });
  86. // copy from `languages/all.js` to `languages/index.js`
  87. if (filesInOutputLanguagesDirectory.includes(allLanguagesFileName)) {
  88. fsExtra.copySync(`${OUTPUT_LANGUAGES_DIRECTORY}/${allLanguagesFileName}`, `${OUTPUT_LANGUAGES_DIRECTORY}/${indexFileName}`);
  89. }
  90. })
  91. ]
  92. };
  93. return [config];
  94. };