From 39865b4c09c1eed6ae7f462f7af4774247a66484 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 1 Jun 2023 16:35:25 +0300 Subject: [PATCH] bundler changes --- Gruntfile.js | 39 ++++++----- frontend/bundle-config.json | 125 ++++++++++++++++++++---------------- js/bundler-general.js | 75 ++++++++++++++++++++++ js/template-precompiler.js | 6 +- 4 files changed, 170 insertions(+), 75 deletions(-) create mode 100644 js/bundler-general.js diff --git a/Gruntfile.js b/Gruntfile.js index 605966f08f..ca2685f6b5 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -33,8 +33,7 @@ const fs = require('fs'); const cp = require('child_process'); const path = require('path'); const buildUtils = require('./js/build-utils'); -const Bundler = require("./js/bundler"); -const Precompiler = require('./js/template-precompiler'); +const BundlerGeneral = require("./js/bundler-general"); const LayoutTypeBundler = require('./js/layout-template-bundler'); const bundleConfig = require('./frontend/bundle-config.json'); const libs = require('./frontend/libs.json'); @@ -257,33 +256,41 @@ module.exports = grunt => { }); grunt.registerTask('bundle-espo', () => { - let chunks = (new Bundler()).bundle({ - files: bundleConfig.files, - patterns: bundleConfig.patterns, + let chunkConfig = bundleConfig.chunks.main; + + let contents = (new BundlerGeneral()).bundle({ + files: chunkConfig.files, + patterns: chunkConfig.patterns, allPatterns: ['client/src/**/*.js'], libs: libs, }); - let contents = chunks[0] + '\n' + + contents += '\n' + (new LayoutTypeBundler()).bundle(); let file = originalLibDir + `/espo.js`; - fs.writeFileSync(file, contents, 'utf8'); - }); - - grunt.registerTask('template-precompile', () => { - let contents = (new Precompiler()).precompile({ - patterns: bundleConfig.templatePatterns, - modulePaths: {'crm': 'client/modules/crm'}, - }); - if (!fs.existsSync(originalLibDir)) { fs.mkdirSync(originalLibDir); } + fs.writeFileSync(file, contents, 'utf8'); + }); + + grunt.registerTask('bundle-templates', () => { + let chunkConfig = bundleConfig.chunks.templates; + + let contents = (new BundlerGeneral()).bundle({ + templatePatterns: chunkConfig.templatePatterns, + modulePaths: {'crm': 'client/modules/crm'}, + }); + let file = originalLibDir + `/espo-templates.js`; + if (!fs.existsSync(originalLibDir)) { + fs.mkdirSync(originalLibDir); + } + fs.writeFileSync(file, contents, 'utf8'); }); @@ -470,7 +477,7 @@ module.exports = grunt => { 'less', 'cssmin', 'bundle-espo', - 'template-precompile', + 'bundle-templates', 'prepare-lib-original', 'uglify:bundle', 'copy:frontendLib', diff --git a/frontend/bundle-config.json b/frontend/bundle-config.json index 408825403d..e9e9f9a8a8 100644 --- a/frontend/bundle-config.json +++ b/frontend/bundle-config.json @@ -1,59 +1,70 @@ { - "files": [ - "client/src/namespace.js", - "client/src/loader.js", - "client/src/loader-init-libs.js", - "client/src/exceptions.js", - "client/src/utils.js" - ], - "patterns": [ - "client/src/!(pre-loader).js", - "client/src/ui/**/*.js", - "client/src/views/*.js", - "client/src/views/site/*.js", - "client/src/views/fields/*.js", - "client/src/views/record/**/*.js", - "client/src/views/search/**/*.js", - "client/src/views/modals/*.js", - "client/src/views/dashlets/**/*.js", - "client/src/views/global-search/*.js", - "client/src/views/stream/*.js", - "client/src/views/note/*.js", - "client/src/views/notification/*.js", - "client/src/controllers/*.js", - "client/src/models/*.js", - "client/src/collections/*.js", - "client/src/acl/*.js" - ], - "templatePatterns": [ - "client/res/templates/*.tpl", - "client/res/templates/site/**/*.tpl", - "client/res/templates/record/**/*.tpl", - "client/res/templates/note/*.tpl", - "client/res/templates/stream/*.tpl", - "client/res/templates/global-search/*.tpl", - "client/res/templates/fields/address/*.tpl", - "client/res/templates/fields/array/*.tpl", - "client/res/templates/fields/attachments-multiple/*.tpl", - "client/res/templates/fields/base/*.tpl", - "client/res/templates/fields/bool/*.tpl", - "client/res/templates/fields/currency/*.tpl", - "client/res/templates/fields/date/*.tpl", - "client/res/templates/fields/datetime/*.tpl", - "client/res/templates/fields/datetime-short/*.tpl", - "client/res/templates/fields/duration/*.tpl", - "client/res/templates/fields/email/*.tpl", - "client/res/templates/fields/enum/*.tpl", - "client/res/templates/fields/file/*.tpl", - "client/res/templates/fields/float/*.tpl", - "client/res/templates/fields/int/*.tpl", - "client/res/templates/fields/multi-enum/*.tpl", - "client/res/templates/fields/person-name/*.tpl", - "client/res/templates/fields/text/*.tpl", - "client/res/templates/fields/url/*.tpl", - "client/res/templates/fields/user/*.tpl", - "client/res/templates/fields/user-with-avatar/*.tpl", - "client/res/templates/fields/varchar/*.tpl", - "client/res/templates/fields/wysiwyg/*.tpl" - ] + "chunks": { + "main": { + "files": [ + "client/src/namespace.js", + "client/src/loader.js", + "client/src/loader-init-libs.js", + "client/src/exceptions.js", + "client/src/utils.js" + ], + "patterns": [ + "client/src/!(pre-loader).js", + "client/src/ui/**/*.js", + "client/src/views/*.js", + "client/src/views/site/*.js", + "client/src/views/fields/*.js", + "client/src/views/record/**/*.js", + "client/src/views/search/**/*.js", + "client/src/views/modals/*.js", + "client/src/views/dashlets/**/*.js", + "client/src/views/global-search/*.js", + "client/src/views/stream/**/*.js", + "client/src/views/note/**/*.js", + "client/src/views/notification/*.js", + "client/src/controllers/!(admin|inbound-email|layout-set|api-user|role|portal-role).js", + "client/src/models/*.js", + "client/src/collections/*.js", + "client/src/acl/*.js" + ] + }, + "templates": { + "templatePatterns": [ + "client/res/templates/*.tpl", + "client/res/templates/site/**/*.tpl", + "client/res/templates/record/**/*.tpl", + "client/res/templates/note/**/*.tpl", + "client/res/templates/stream/**/*.tpl", + "client/res/templates/notification/*.tpl", + "client/res/templates/global-search/*.tpl", + "client/res/templates/fields/address/*.tpl", + "client/res/templates/fields/array/*.tpl", + "client/res/templates/fields/attachments-multiple/*.tpl", + "client/res/templates/fields/base/*.tpl", + "client/res/templates/fields/bool/*.tpl", + "client/res/templates/fields/currency/*.tpl", + "client/res/templates/fields/date/*.tpl", + "client/res/templates/fields/datetime/*.tpl", + "client/res/templates/fields/datetime-short/*.tpl", + "client/res/templates/fields/duration/*.tpl", + "client/res/templates/fields/email/*.tpl", + "client/res/templates/fields/enum/*.tpl", + "client/res/templates/fields/phone/*.tpl", + "client/res/templates/fields/file/*.tpl", + "client/res/templates/fields/float/*.tpl", + "client/res/templates/fields/int/*.tpl", + "client/res/templates/fields/link/*.tpl", + "client/res/templates/fields/link-multiple/*.tpl", + "client/res/templates/fields/link-parent/*.tpl", + "client/res/templates/fields/multi-enum/*.tpl", + "client/res/templates/fields/person-name/*.tpl", + "client/res/templates/fields/text/*.tpl", + "client/res/templates/fields/url/*.tpl", + "client/res/templates/fields/user/*.tpl", + "client/res/templates/fields/user-with-avatar/*.tpl", + "client/res/templates/fields/varchar/*.tpl", + "client/res/templates/fields/wysiwyg/*.tpl" + ] + } + } } diff --git a/js/bundler-general.js b/js/bundler-general.js new file mode 100644 index 0000000000..7881f1664b --- /dev/null +++ b/js/bundler-general.js @@ -0,0 +1,75 @@ +/************************************************************************ + * This file is part of EspoCRM. + * + * EspoCRM - Open Source CRM application. + * Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko + * Website: https://www.espocrm.com + * + * EspoCRM is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EspoCRM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EspoCRM. If not, see http://www.gnu.org/licenses/. + * + * The interactive user interfaces in modified source and object code versions + * of this program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU General Public License version 3. + * + * In accordance with Section 7(b) of the GNU General Public License version 3, + * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. + ************************************************************************/ + +const Bundler = require("./bundler"); +const Precompiler = require('./template-precompiler'); + +class BundlerGeneral { + + /** + * @param {{ + * files: string[], + * patterns?: string[], + * allPatterns?: string[], + * libs: { + * src?: string, + * bundle?: boolean, + * key?: string, + * }[], + * templatePatterns?: string[], + * modulePaths?: Object., + * }} params + * @return {string} + */ + bundle(params) { + let contents = ''; + + if (params.patterns) { + let chunks = (new Bundler()).bundle({ + files: params.files, + patterns: params.patterns, + allPatterns: params.allPatterns, + libs: params.libs, + }); + + contents += chunks[0]; + } + + if (params.templatePatterns) { + contents += '\n' + + (new Precompiler()).precompile({ + patterns: params.templatePatterns, + modulePaths: params.modulePaths, + }); + } + + return contents; + } +} + +module.exports = BundlerGeneral; diff --git a/js/template-precompiler.js b/js/template-precompiler.js index 0011008f47..30388e9a3e 100644 --- a/js/template-precompiler.js +++ b/js/template-precompiler.js @@ -81,7 +81,9 @@ class TemplatePrecompiler { nameMap[file] = name }); - let contents = 'Espo.preCompiledTemplates = {\n'; + let contents = + 'Espo.preCompiledTemplates = Espo.preCompiledTemplates || {};\n' + + 'Object.assign(Espo.preCompiledTemplates, {\n'; for (let file in nameMap) { let name = nameMap[file]; @@ -92,7 +94,7 @@ class TemplatePrecompiler { contents += `'${name}': Handlebars.template(\n${compiled}\n),\n`; } - contents += `\n}`; + contents += `\n});`; return contents; }