diff --git a/README.md b/README.md index 943a6b3..60d1c4e 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,11 @@ module.exports = { // Обычно это ./dist/.bundle.js // Необходимо указать относительный путь output: string || {js: string, css: string}, - + + // Алиасы для внешних импортов + // https://rollupjs.org/configuration-options/#output-globals + globals: { [id: string]: string } + // Неймспейс, в который будут добавлены все экспорты из файла указанного в input // Например 'BX.Main.Filter' namespace: string, diff --git a/src/@types/config.js b/src/@types/config.js index 4228765..2598714 100644 --- a/src/@types/config.js +++ b/src/@types/config.js @@ -6,6 +6,7 @@ interface BundleConfig { js: string, css: string, }, + globals: {[key: string]: string}, name: string, treeshake: boolean, adjustConfigPhp: boolean, diff --git a/src/utils/get-configs.js b/src/utils/get-configs.js index 4b36a03..261d9f1 100644 --- a/src/utils/get-configs.js +++ b/src/utils/get-configs.js @@ -82,6 +82,7 @@ export default function getConfigs(directory: string): BundleConfig { acc.push({ input: path.resolve(context, currentConfig.input), output, + globals: currentConfig.globals || {}, name: currentConfig.namespace || '', treeshake: currentConfig.treeshake !== false, adjustConfigPhp: currentConfig.adjustConfigPhp !== false, diff --git a/src/utils/get-globals.js b/src/utils/get-globals.js index 8d1a204..d931594 100644 --- a/src/utils/get-globals.js +++ b/src/utils/get-globals.js @@ -4,7 +4,7 @@ import path from 'path'; import fs from 'fs'; import type BundleConfig from '../@types/config'; -export default function getGlobals(imports: string[], {context}: BundleConfig): {[key: string]: string} { +export default function getGlobals(imports: string[], {context, globals}: BundleConfig): {[key: string]: string} { return imports.reduce((accumulator, extensionName) => { const parsedExtensionName = extensionName.split('.'); const moduleName = parsedExtensionName.shift(); @@ -44,6 +44,8 @@ export default function getGlobals(imports: string[], {context}: BundleConfig): if (config.namespace && config.namespace.length) { moduleAlias = config.namespace; } + } else if (globals.hasOwnProperty(moduleName)) { + moduleAlias = globals[moduleName]; } accumulator[extensionName] = moduleAlias;