Skip to content

Commit

Permalink
feat: rename package files .esm.js to .mjs
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Jun 23, 2023
1 parent cabc817 commit 7a17821
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
6 changes: 3 additions & 3 deletions scripts/build-browser-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ export default async function buildBrowserModules() {
code = code
.replace(/from"\.\//g, 'from"./modules/_chunks/')
.replace(/import"\.\//g, 'import"./modules/_chunks/');
fs.writeFileSync(`./${outputDir}/swiper.esm.browser.js`, code);
fs.writeFileSync(`./${outputDir}/swiper.browser.mjs`, 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);
fs.writeFileSync(`./${outputDir}/modules/${folder}/${folder}.browser.mjs`, code);
} else {
fs.writeFileSync(`./${outputDir}/modules/_chunks/${file}`, code);
fs.writeFileSync(`./${outputDir}/modules/_chunks/${file.replace('.js', '.mjs')}`, code);
}
fs.unlinkSync(`./${outputDir}/browser-modules/${file}`);
}
Expand Down
14 changes: 7 additions & 7 deletions scripts/build-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,27 +188,27 @@ export default async function buildElement() {
.replace('//IMPORT_SWIPER', `import Swiper from 'swiper/bundle';`)
.replace('//EXPORT', `export { SwiperContainer, SwiperSlide, register };`);

fs.writeFileSync(path.resolve(outputDir, 'element/swiper-element-bundle.js'), esmBundleContent);
fs.writeFileSync(path.resolve(outputDir, 'element/swiper-element-bundle.mjs'), esmBundleContent);

const esmContent = elementContent
.replace('//SWIPER_STYLES', `const SwiperCSS = \`${cssStylesCore}\`;`)
.replace('//SWIPER_SLIDE_STYLES', `const SwiperSlideCSS = \`${cssStylesSlide}\`;`)
.replace('//IMPORT_SWIPER', `import Swiper from 'swiper';`)
.replace('//EXPORT', `export { SwiperContainer, SwiperSlide, register };`);
fs.writeFileSync(path.resolve(outputDir, 'element/swiper-element.js'), esmContent);
fs.writeFileSync(path.resolve(outputDir, 'element/swiper-element.mjs'), esmContent);

// Browser
const browser = async (isBundle) => {
const suffix = isBundle ? '-bundle' : '';
const bundle = await rollup({
input: `${outputDir}/element/swiper-element${suffix}.js`,
input: `${outputDir}/element/swiper-element${suffix}.mjs`,
plugins: [
replace({
delimiters: ['', ''],
'//SWIPER_STYLES': `const SwiperCSS = \`${cssStylesBundle}\`;`,
'//SWIPER_SLIDE_STYLES': `const SwiperSlideCSS = \`${cssStylesSlide}\`;`,
[`import Swiper from 'swiper/bundle';`]: `import Swiper from '../swiper-bundle.esm.js';`,
[`import Swiper from 'swiper';`]: `import Swiper from '../swiper.esm.js';`,
[`import Swiper from 'swiper/bundle';`]: `import Swiper from '../swiper-bundle.mjs';`,
[`import Swiper from 'swiper';`]: `import Swiper from '../swiper.mjs';`,
'//BROWSER_REGISTER': `register()`,
'export { SwiperContainer, SwiperSlide, register };': ``,
}),
Expand Down Expand Up @@ -245,6 +245,6 @@ export default async function buildElement() {
await browser();
await browser(true);

addBannerToFile(path.resolve(outputDir, 'element/swiper-element-bundle.js'), 'Custom Element');
addBannerToFile(path.resolve(outputDir, 'element/swiper-element.js'), 'Custom Element');
addBannerToFile(path.resolve(outputDir, 'element/swiper-element-bundle.mjs'), 'Custom Element');
addBannerToFile(path.resolve(outputDir, 'element/swiper-element.mjs'), 'Custom Element');
}
15 changes: 8 additions & 7 deletions scripts/build-js-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ async function buildEntry(modules, format, browser = false) {
const needSourceMap = isProd && (isUMD || (isESM && browser));
const external = isUMD || browser ? [] : () => true;
let filename = 'swiper-bundle';
if (isESM) filename += `.esm`;
let fileExt = '.js';
if (isESM) fileExt = `.mjs`;
if (isESM && browser) filename += '.browser';

return rollup({
Expand All @@ -45,9 +46,9 @@ async function buildEntry(modules, format, browser = false) {
name: 'Swiper',
strict: true,
sourcemap: needSourceMap,
sourcemapFile: `./${outputDir}/${filename}.js.map`,
sourcemapFile: `./${outputDir}/${filename}${fileExt}.map`,
banner: banner(),
file: `./${outputDir}/${filename}.js`,
file: `./${outputDir}/${filename}${fileExt}`,
}),
)
.then(async (bundle) => {
Expand All @@ -58,17 +59,17 @@ async function buildEntry(modules, format, browser = false) {
const { code, map } = await minify(result.code, {
sourceMap: {
content: needSourceMap ? result.map : undefined,
filename: needSourceMap ? `${filename}.min.js` : undefined,
url: `${filename}.min.js.map`,
filename: needSourceMap ? `${filename}.min${fileExt}` : undefined,
url: `${filename}.min${fileExt}.map`,
},
output: {
preamble: banner(),
},
}).catch((err) => {
console.error(`Terser failed on file ${filename}: ${err.toString()}`);
});
await fs.writeFile(`./${outputDir}/${filename}.min.js`, code);
await fs.writeFile(`./${outputDir}/${filename}.min.js.map`, map);
await fs.writeFile(`./${outputDir}/${filename}.min${fileExt}`, code);
await fs.writeFile(`./${outputDir}/${filename}.min${fileExt}.map`, map);
})
.then(async () => {
if (isProd && isESM && browser === false) return buildEntry(modules, format, true);
Expand Down
4 changes: 2 additions & 2 deletions scripts/build-js-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { capitalizeString } from './utils/helper.js';
const exec = execSh.promise;

async function buildCore(modules) {
const filename = `swiper.esm`;
const filename = `swiper`;
const coreContent = [
banner(),
`export { default as Swiper, default } from './core/core.js';`,
Expand All @@ -21,7 +21,7 @@ async function buildCore(modules) {
),
].join('\n');
await Promise.all([
fs.writeFile(`./${outputDir}/${filename}.js`, coreContent),
fs.writeFile(`./${outputDir}/${filename}.mjs`, coreContent),
exec(
`npx babel src --out-dir ${outputDir} --config-file ./scripts/babel/babel.config.core.json`,
),
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class Build {
.add('types', buildTypes)
.add('styles', buildStyles)
.add('core', buildJsCore)
.add('browser-modules', buildBrowserModules)
.add('bundle', buildJsBundle)
.add('element', buildElement)
.add('browser-modules', buildBrowserModules)
.add('react', buildReact)
.add('vue', buildVue)
.run();
Expand Down
26 changes: 13 additions & 13 deletions src/copy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
"description": "Most modern mobile touch slider and framework with hardware accelerated transitions",
"typings": "swiper.d.ts",
"type": "module",
"main": "./swiper.esm.js",
"module": "./swiper.esm.js",
"main": "./swiper.mjs",
"module": "./swiper.mjs",
"exports": {
".": {
"types": "./swiper.d.ts",
"default": "./swiper.esm.js"
"default": "./swiper.mjs"
},
"./core": {
"types": "./swiper.d.ts",
"default": "./swiper.esm.js"
"default": "./swiper.mjs"
},
"./swiper.esm.js": {
"./swiper.mjs": {
"types": "./swiper.d.ts",
"default": "./swiper.esm.js"
"default": "./swiper.mjs"
},
"./bundle": {
"types": "./swiper.d.ts",
"default": "./swiper-bundle.esm.js"
"default": "./swiper-bundle.mjs"
},
"./swiper-bundle.esm.js": {
"./swiper-bundle.mjs": {
"types": "./swiper.d.ts",
"default": "./swiper-bundle.esm.js"
"default": "./swiper-bundle.mjs"
},
"./css": "./swiper.min.css",
"./swiper.min.css": "./swiper.min.css",
Expand Down Expand Up @@ -106,15 +106,15 @@
"./scss/zoom": "./modules/zoom/zoom.scss",
"./element": {
"types": "./element/swiper-element.d.ts",
"default": "./element/swiper-element.js"
"default": "./element/swiper-element.mjs"
},
"./element/bundle": {
"types": "./element/swiper-element.d.ts",
"default": "./element/swiper-element-bundle.js"
"default": "./element/swiper-element-bundle.mjs"
},
"./element-bundle": {
"types": "./element/swiper-element.d.ts",
"default": "./element/swiper-element-bundle.js"
"default": "./element/swiper-element-bundle.mjs"
},
"./element/css": "./swiper-element.min.css",
"./swiper-element.min.css": "./swiper-element.min.css",
Expand Down Expand Up @@ -217,4 +217,4 @@
"ssr-window": "^4.0.2"
},
"releaseDate": "June 23, 2023"
}
}

0 comments on commit 7a17821

Please sign in to comment.