From 0bb2035266cc9457178ab276013413040a99f259 Mon Sep 17 00:00:00 2001 From: Marat Shamshutdinov Date: Wed, 14 Apr 2021 16:57:22 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=BB=D1=83=D1=87=D1=88=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=20=D1=80=D0=B5=D0=B7=D0=BE=D0=BB=D0=B2=D0=B8=D0=BD=D0=B3?= =?UTF-8?q?=20=D1=8D=D0=BA=D1=81=D1=82=D0=B5=D0=BD=D1=88=D0=B5=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=B2=20import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Добавляет поддержку партнерских модулей с точкой в названии - Повторяет алгоритм поиска пути к расширению по php-версии: `\Bitrix\Main\UI\Extension::getPath()` Fixes bitrix-tools/cli#7, fixes bitrix-tools/cli#33 --- src/path/get-extension-path.js | 66 +++++++++++++++++ src/utils/get-globals.js | 11 ++- .../mymodule/myext-in-local/bundle.config.js | 5 ++ .../js/vendor/mymodule/myext/bundle.config.js | 5 ++ .../myext-in-module-only/bundle.config.js | 5 ++ .../js/vendor/mymodule/myext/bundle.config.js | 5 ++ .../mymodule/myext-in-local/bundle.config.js | 5 ++ test/utils/get-globals/get-globals.test.js | 73 +++++++++++++++++++ 8 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 src/path/get-extension-path.js create mode 100644 test/utils/get-globals/data/bitrix/js/vendor/mymodule/myext-in-local/bundle.config.js create mode 100644 test/utils/get-globals/data/bitrix/js/vendor/mymodule/myext/bundle.config.js create mode 100644 test/utils/get-globals/data/bitrix/modules/vendor.module/install/js/vendor/mymodule/myext-in-module-only/bundle.config.js create mode 100644 test/utils/get-globals/data/bitrix/modules/vendor.module/install/js/vendor/mymodule/myext/bundle.config.js create mode 100644 test/utils/get-globals/data/local/js/vendor/mymodule/myext-in-local/bundle.config.js create mode 100644 test/utils/get-globals/get-globals.test.js diff --git a/src/path/get-extension-path.js b/src/path/get-extension-path.js new file mode 100644 index 0000000..7635427 --- /dev/null +++ b/src/path/get-extension-path.js @@ -0,0 +1,66 @@ +// @flow +import {existsSync} from 'fs'; +import {join} from 'path'; + +/** + * Port of bitrix getLocalPath() + * + * @link https://bxapi.ru/src/?module_id=main&name=getLocalPath + * @param {string} projectRoot Absolute path to project root + * @param {string} relativePath Path relative to bitrix folder + * @returns {string|null} + */ +function getLocalPath(projectRoot: string, relativePath: string, baseFolder: string = 'bitrix') { + if (existsSync(join(projectRoot, 'local', relativePath))) { + return join(projectRoot, 'local', relativePath); + } + if (existsSync(join(projectRoot, baseFolder, relativePath))) { + return join(projectRoot, baseFolder, relativePath); + } + return null; +} + +/** + * Guess absolute path to project root + * + * @param {string} context Project folder + * @returns {string} + */ +function guessProjectRoot(context: string) { + return context.split(/\/(bitrix|local)\//)[0]; +} + + +/** + * Port of bitrix \Bitrix\Main\UI\Extension::getPath() + * + * @link https://bxapi.ru/src/?module_id=main&name=getPath + * @param {string} context Absolute path to built bundle + * @param {string} extensionName + * @returns {string|null} + */ +export function getExtensionPath(extensionName: string, context: string): ?string { + if (!(typeof extensionName === 'string')) { + return null; + } + + const namespaces = extensionName.split('.'); + if (namespaces.length < 2) { + return null; + } + + const projectRoot = guessProjectRoot(context); + if (!projectRoot) { + return null; + } + + const pathParts = ['js']; + for (let i = 0; i < namespaces.length; i++) { + if (!namespaces[i].match(/^[a-z0-9_\\.\-]+$/)) { + return null; + } + pathParts.push(namespaces[i]); + } + + return getLocalPath(projectRoot, join(...pathParts)); +} diff --git a/src/utils/get-globals.js b/src/utils/get-globals.js index 65845b5..e24f4be 100644 --- a/src/utils/get-globals.js +++ b/src/utils/get-globals.js @@ -1,7 +1,7 @@ // @flow - import {join} from 'path'; import {existsSync} from 'fs'; +import {getExtensionPath} from '../path/get-extension-path'; import type BundleConfig from '../@types/config'; export default function getGlobals(imports: string[], {context}: BundleConfig): {[key: string]: string} { @@ -21,6 +21,13 @@ export default function getGlobals(imports: string[], {context}: BundleConfig): configPath = join(extensionPath, 'bundle.config.js'); } + if (!existsSync(configPath)) { + const extensionPath = getExtensionPath(extensionName, context); + if (extensionPath) { + configPath = join(extensionPath, 'bundle.config.js'); + } + } + let moduleAlias = 'BX'; if (existsSync(configPath)) { @@ -38,4 +45,4 @@ export default function getGlobals(imports: string[], {context}: BundleConfig): return accumulator; }, {}); -} \ No newline at end of file +} diff --git a/test/utils/get-globals/data/bitrix/js/vendor/mymodule/myext-in-local/bundle.config.js b/test/utils/get-globals/data/bitrix/js/vendor/mymodule/myext-in-local/bundle.config.js new file mode 100644 index 0000000..e9817bd --- /dev/null +++ b/test/utils/get-globals/data/bitrix/js/vendor/mymodule/myext-in-local/bundle.config.js @@ -0,0 +1,5 @@ +module.exports = { + input: './src/app.js', + output: './dist/app.bundle.js', + namespace: 'BX.Vendor.MyModule.MyExtInBitrix' +}; \ No newline at end of file diff --git a/test/utils/get-globals/data/bitrix/js/vendor/mymodule/myext/bundle.config.js b/test/utils/get-globals/data/bitrix/js/vendor/mymodule/myext/bundle.config.js new file mode 100644 index 0000000..64e6689 --- /dev/null +++ b/test/utils/get-globals/data/bitrix/js/vendor/mymodule/myext/bundle.config.js @@ -0,0 +1,5 @@ +module.exports = { + input: './src/app.js', + output: './dist/app.bundle.js', + namespace: 'BX.Vendor.MyModule.MyExt' +}; \ No newline at end of file diff --git a/test/utils/get-globals/data/bitrix/modules/vendor.module/install/js/vendor/mymodule/myext-in-module-only/bundle.config.js b/test/utils/get-globals/data/bitrix/modules/vendor.module/install/js/vendor/mymodule/myext-in-module-only/bundle.config.js new file mode 100644 index 0000000..ab3a303 --- /dev/null +++ b/test/utils/get-globals/data/bitrix/modules/vendor.module/install/js/vendor/mymodule/myext-in-module-only/bundle.config.js @@ -0,0 +1,5 @@ +module.exports = { + input: './src/app.js', + output: './dist/app.bundle.js', + namespace: 'BX.Vendor.MyModule.MyExtInModuleOnly' +}; \ No newline at end of file diff --git a/test/utils/get-globals/data/bitrix/modules/vendor.module/install/js/vendor/mymodule/myext/bundle.config.js b/test/utils/get-globals/data/bitrix/modules/vendor.module/install/js/vendor/mymodule/myext/bundle.config.js new file mode 100644 index 0000000..9e8a875 --- /dev/null +++ b/test/utils/get-globals/data/bitrix/modules/vendor.module/install/js/vendor/mymodule/myext/bundle.config.js @@ -0,0 +1,5 @@ +module.exports = { + input: './src/app.js', + output: './dist/app.bundle.js', + namespace: 'BX.Vendor.MyModule.MyExt.WithWrongNamespace' +}; \ No newline at end of file diff --git a/test/utils/get-globals/data/local/js/vendor/mymodule/myext-in-local/bundle.config.js b/test/utils/get-globals/data/local/js/vendor/mymodule/myext-in-local/bundle.config.js new file mode 100644 index 0000000..3d53cc1 --- /dev/null +++ b/test/utils/get-globals/data/local/js/vendor/mymodule/myext-in-local/bundle.config.js @@ -0,0 +1,5 @@ +module.exports = { + input: './src/app.js', + output: './dist/app.bundle.js', + namespace: 'BX.Vendor.MyModule.MyExtInLocal' +}; \ No newline at end of file diff --git a/test/utils/get-globals/get-globals.test.js b/test/utils/get-globals/get-globals.test.js new file mode 100644 index 0000000..7235b2c --- /dev/null +++ b/test/utils/get-globals/get-globals.test.js @@ -0,0 +1,73 @@ +import getGlobals from '../../../src/utils/get-globals'; +import assert from 'assert'; +import path from 'path'; + +const projectRoot = path.resolve(__dirname, 'data'); +const vendorModuleDir = path.resolve(__dirname, 'data/bitrix/modules/vendor.mymodule'); +const vendorModuleJsDir = path.resolve(__dirname, 'data/bitrix/js/vendor/mymodule'); + +const testInclusionFrom = (context) => { + it('Should resolve partner extension to path under bitrix/js', () => { + const expected = { + 'vendor.mymodule.myext': 'BX.Vendor.MyModule.MyExt' + }; + const actual = getGlobals(['vendor.mymodule.myext'], {context}); + + assert.deepEqual(actual, expected); + }); + it('Should not resolve partner extension to bitrix/modules//install/js', () => { + const expected = { + 'vendor.mymodule.myext-in-module-only': 'BX' + }; + const actual = getGlobals(['vendor.mymodule.myext-in-module-only'], {context}); + + assert.deepEqual(actual, expected); + }); + + it('bitrix/js should have precedence over bitrix/modules//install/js', () => { + const expected = { + 'vendor.mymodule.myext': 'BX.Vendor.MyModule.MyExt' + }; + const notExpected = { + 'vendor.mymodule.myext': 'BX.Vendor.MyModule.MyExtInModuleOnly' + }; + const actual = getGlobals(['vendor.mymodule.myext'], {context}); + + assert.deepEqual(actual, expected); + assert.notDeepEqual(actual, notExpected); + }); + + it('local/ should have precedence over bitrix/ paths', () => { + const expected = { + 'vendor.mymodule.myext-in-local': 'BX.Vendor.MyModule.MyExtInLocal' + }; + const notExpected = { + 'vendor.mymodule.myext-in-local': 'BX.Vendor.MyModule.MyExtInBitrix' + }; + const actual = getGlobals(['vendor.mymodule.myext-in-local'], {context}); + + assert.deepEqual(actual, expected); + assert.notDeepEqual(actual, notExpected); + }); +} + +describe('utils/get-globals', () => { + it('Should be exported as function', () => { + assert(typeof getGlobals === 'function'); + }); + + describe('With site templates context', () => { + const context = path.join(projectRoot, 'bitrix/templates/mytemplate/components/vendor.component/templates/.default'); + testInclusionFrom(context); + }); + + describe('With component template context', () => { + const context = path.join(projectRoot, 'bitrix/components/vendor/component/templates/my-template'); + testInclusionFrom(context); + }); + + describe('With third-party extension\'s context', () => { + const context = path.join(projectRoot, 'bitrix/js/vendor2/third-patry-extenion'); + testInclusionFrom(context); + }); +}); \ No newline at end of file