Skip to content

Commit

Permalink
feat: browser ES modules
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Jun 23, 2023
1 parent 1e469ff commit ab20bd1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
65 changes: 65 additions & 0 deletions scripts/build-browser-modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import fs from 'fs';
import { rollup } from 'rollup';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import { minify } from 'terser';
import config from './build-config.js';
import { outputDir } from './utils/output-dir.js';

export default async function buildBrowserModules() {
// eslint-disable-next-line
const modulesPaths = config.modules.map((name) => {
return `./src/modules/${name}/${name}.js`;
});

const bundle = await rollup({
input: ['./src/swiper.js', ...modulesPaths],
plugins: [
replace({
delimiters: ['', ''],
'//IMPORT_MODULES': '',
'//INSTALL_MODULES': '',
'//EXPORT': 'export default Swiper;',
}),
nodeResolve({ mainFields: ['module', 'main', 'jsnext'], rootDir: './src' }),
],
onwarn() {},
});

await bundle.write({
dir: `./${outputDir}/browser-modules`,
format: 'esm',
});

const folders = fs.readdirSync(`./${outputDir}/modules`);
const files = fs.readdirSync(`./${outputDir}/browser-modules`);

if (!fs.existsSync(`./${outputDir}/modules/_chunks`)) {
fs.mkdirSync(`./${outputDir}/modules/_chunks`);
}
for (const file of files) {
const folder = file.split('.js')[0];
const content = fs.readFileSync(`./${outputDir}/browser-modules/${file}`, 'utf-8');
// eslint-disable-next-line
let { code } = await minify(content).catch((err) => {
console.error(`Terser failed on file ${file}: ${err.toString()}`);
});
if (file === 'swiper.js') {
code = code
.replace(/from"\.\//g, 'from"./modules/_chunks/')
.replace(/import"\.\//g, 'import"./modules/_chunks/');
fs.writeFileSync(`./${outputDir}/swiper.esm.browser.js`, code);
} else if (folders.includes(folder)) {
code = code
.replace(/from"\.\//g, 'from"../_chunks/')
.replace(/import"\.\//g, 'import"../_chunks/');

fs.writeFileSync(`./${outputDir}/modules/${folder}/${folder}.esm.browser.js`, code);
} else {
fs.writeFileSync(`./${outputDir}/modules/_chunks/${file}`, code);
}
fs.unlinkSync(`./${outputDir}/browser-modules/${file}`);
}
fs.rmdirSync(`./${outputDir}/browser-modules/`);
}
buildBrowserModules();
2 changes: 2 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import buildReact from './build-react.js';
import buildVue from './build-vue.js';
import buildElement from './build-element.js';
import buildStyles from './build-styles.js';
import buildBrowserModules from './build-browser-modules.js';
import { outputDir } from './utils/output-dir.js';

class Build {
Expand Down Expand Up @@ -50,6 +51,7 @@ class Build {
.add('types', buildTypes)
.add('styles', buildStyles)
.add('core', buildJsCore)
.add('browser-modules', buildBrowserModules)
.add('bundle', buildJsBundle)
.add('element', buildElement)
.add('react', buildReact)
Expand Down

0 comments on commit ab20bd1

Please sign in to comment.