diff --git a/scripts/build-config.js b/scripts/build-config.js index dfd0e2baf..931368aa8 100644 --- a/scripts/build-config.js +++ b/scripts/build-config.js @@ -1,4 +1,8 @@ -export const modules = [ +import { parseSwiperBuildModulesEnv } from './utils/helper.js'; + +const envBuildModules = parseSwiperBuildModulesEnv(); + +export const modules = envBuildModules || [ 'virtual', 'keyboard', 'mousewheel', diff --git a/scripts/build-js-bundle.js b/scripts/build-js-bundle.js index bfab4055f..c743b05cc 100644 --- a/scripts/build-js-bundle.js +++ b/scripts/build-js-bundle.js @@ -10,6 +10,7 @@ import { modules as configModules } from './build-config.js'; import { outputDir } from './utils/output-dir.js'; import { banner } from './utils/banner.js'; import isProd from './utils/isProd.js'; +import { capitalizeString } from './utils/helper.js'; async function buildEntry(modules, format, browser = false) { const isUMD = format === 'umd'; @@ -83,19 +84,7 @@ export default async function buildJsBundle() { elapsed.start('bundle'); const modules = []; configModules.forEach((name) => { - // eslint-disable-next-line - const capitalized = name - .split('-') - .map((word) => { - return word - .split('') - .map((char, index) => { - if (index === 0) return char.toUpperCase(); - return char; - }) - .join(''); - }) - .join(''); + const capitalized = capitalizeString(name); const jsFilePath = `./src/modules/${name}/${name}.js`; if (fs.existsSync(jsFilePath)) { modules.push({ name, capitalized }); diff --git a/scripts/build-js-core.js b/scripts/build-js-core.js index 0a479dd9d..f51bbe2be 100644 --- a/scripts/build-js-core.js +++ b/scripts/build-js-core.js @@ -5,6 +5,7 @@ import elapsed from 'elapsed-time-logger'; import { modules as configModules } from './build-config.js'; import { outputDir } from './utils/output-dir.js'; import { banner } from './utils/banner.js'; +import { capitalizeString } from './utils/helper.js'; /* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */ /* eslint no-console: "off" */ const exec = execSh.promise; @@ -31,19 +32,7 @@ export default async function build() { elapsed.start('core'); const modules = []; configModules.forEach((name) => { - // eslint-disable-next-line - const capitalized = name - .split('-') - .map((word) => { - return word - .split('') - .map((char, index) => { - if (index === 0) return char.toUpperCase(); - return char; - }) - .join(''); - }) - .join(''); + const capitalized = capitalizeString(name); const jsFilePath = `./src/modules/${name}/${name}.js`; if (fs.existsSync(jsFilePath)) { modules.push({ name, capitalized }); diff --git a/scripts/utils/helper.js b/scripts/utils/helper.js new file mode 100644 index 000000000..5a675d5a1 --- /dev/null +++ b/scripts/utils/helper.js @@ -0,0 +1,18 @@ +export function capitalizeString(str) { + return str.replace(/(?:^|-)([a-z])/g, (m, char) => char.toUpperCase()); +} + +/** + * Parse build modules from env `SWIPER_BUILD_MODULES`.
+ * Will return null if env is not set or empty + * @returns {string[]|null} + */ +export function parseSwiperBuildModulesEnv() { + if (process.env.SWIPER_BUILD_MODULES) { + const buildModules = process.env.SWIPER_BUILD_MODULES.split(/[,\s]+/).filter( + (moduleName) => moduleName, // ensure to empty will be removed + ); + if (buildModules.length) return buildModules; + } + return null; +}