Skip to content

Commit

Permalink
feat(build-config): allow to custom modules by process.env.SWIPER_BUI…
Browse files Browse the repository at this point in the history
…LD_MODULES (#6043)
  • Loading branch information
0x0a0d authored Oct 6, 2022
1 parent 739783c commit b3c51c5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
6 changes: 5 additions & 1 deletion scripts/build-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const modules = [
import { parseSwiperBuildModulesEnv } from './utils/helper.js';

const envBuildModules = parseSwiperBuildModulesEnv();

export const modules = envBuildModules || [
'virtual',
'keyboard',
'mousewheel',
Expand Down
15 changes: 2 additions & 13 deletions scripts/build-js-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 });
Expand Down
15 changes: 2 additions & 13 deletions scripts/build-js-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 });
Expand Down
18 changes: 18 additions & 0 deletions scripts/utils/helper.js
Original file line number Diff line number Diff line change
@@ -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`.<br>
* 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;
}

0 comments on commit b3c51c5

Please sign in to comment.