Skip to content

Commit 8542a08

Browse files
committed
Compat export.
1 parent 711a215 commit 8542a08

File tree

10 files changed

+49
-39
lines changed

10 files changed

+49
-39
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"parserOptions": { "sourceType": "module", "ecmaVersion": 2022 },
55
"overrides": [
66
{
7-
"files": ["src/js/*.js"],
7+
"files": ["src/js/*.mjs"],
88
"extends": ["eslint:recommended", "prettier"],
99
"parserOptions": {
1010
"project": ["./tsconfig.json"]

dist/js/lightbox.cjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/js/lightbox.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/lightbox.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,16 @@
2020
},
2121
"exports": {
2222
".": {
23-
"import": "./dist/js/lightbox.mjs",
24-
"require": "./dist/js/lightbox.cjs",
23+
"node": {
24+
"import": "./dist/js/lightbox.mjs",
25+
"require": "./dist/js/lightbox.cjs",
26+
"default": "./dist/js/lightbox.js"
27+
},
28+
"browser": {
29+
"import": "./dist/js/lightbox.mjs",
30+
"require": "./dist/js/lightbox.cjs",
31+
"default": "./dist/js/lightbox.js"
32+
},
2533
"default": "./dist/js/lightbox.js"
2634
},
2735
"./css": "./dist/css/*.css",
@@ -56,7 +64,7 @@
5664
"format:js": "prettier --write src/ts",
5765
"lint": "run-p lint:*",
5866
"lint:css": "stylelint src/css/lightbox.css",
59-
"lint:js": "eslint src/js/*.js",
67+
"lint:js": "eslint src/js/*.mjs",
6068
"prebuild:js": "npm run clean:js",
6169
"watch": "run-p watch:*",
6270
"watch:css": "npm run build:css -- -w",

scripts/build.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const errors = [];
2121
for (const result of results) {
2222
if (result.status === 'rejected') {
2323
errors.push(result.reason);
24-
continue;
2524
} else if (result.value.errors.length > 0) {
2625
errors.push(...result.value.errors);
2726
}

scripts/config.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ const browserslistPlugin = esbuildPluginBrowserslist(browserslist(), {
1010

1111
/** @type {BuildOptions} */
1212
export const commonConfig = {
13-
entryPoints: ['src/js/lightbox.js'],
1413
outbase: 'src/js',
15-
bundle: false,
14+
bundle: true,
1615
banner: {
1716
js: `/*!
1817
* Lightbox v2.11.2
@@ -32,13 +31,15 @@ export const commonConfig = {
3231

3332
/** @type {BuildOptions} */
3433
export const esmConfig = {
34+
entryPoints: ['src/js/lightbox.mjs'],
3535
format: 'esm',
3636
outfile: 'dist/js/lightbox.mjs',
3737
plugins: [browserslistPlugin]
3838
};
3939

4040
/** @type {BuildOptions} */
4141
export const cjsConfig = {
42+
entryPoints: ['src/js/lightbox.cjs'],
4243
format: 'cjs',
4344
platform: 'node',
4445
outfile: 'dist/js/lightbox.cjs',
@@ -47,9 +48,9 @@ export const cjsConfig = {
4748

4849
/** @type {BuildOptions} */
4950
export const browserConfig = {
51+
entryPoints: ['src/js/browser.mjs'],
5052
format: 'iife',
5153
platform: 'browser',
52-
globalName: 'lightbox',
5354
outfile: 'dist/js/lightbox.js',
5455
plugins: [browserslistPlugin]
5556
};

src/js/browser.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import lightbox from './lightbox.mjs';
2+
window.lightbox = lightbox;

src/js/lightbox.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const { lightbox } = require('./lightbox.mjs');
2+
module.exports = lightbox;

src/js/lightbox.js renamed to src/js/lightbox.mjs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const domReady = (callback) => {
1515
class Lightbox {
1616
album = [];
1717
currentImageIndex = 0;
18-
options;
18+
options = Object.assign(Object.create(null), Lightbox.defaults);
1919
/** @type {HTMLElement} */
2020
lightbox;
2121
/** @type {HTMLElement} */
@@ -52,9 +52,6 @@ class Lightbox {
5252

5353
constructor(options) {
5454
this.init();
55-
56-
// options
57-
this.options = Object.assign({}, Lightbox.defaults);
5855
this.option(options);
5956
}
6057

@@ -253,7 +250,7 @@ class Lightbox {
253250
* events as usual.
254251
*/
255252
this.nav.addEventListener('mousedown', (event) => {
256-
if (event.which === 3) {
253+
if (event.button === 2) {
257254
this.nav.style.pointerEvents = 'none';
258255

259256
this.lightbox.addEventListener(
@@ -308,21 +305,19 @@ class Lightbox {
308305
imageNumber = i;
309306
}
310307
}
308+
} else if (link.rel === 'lightbox') {
309+
// If image is not part of a set
310+
addToAlbum(link);
311311
} else {
312-
if (link.rel === 'lightbox') {
313-
// If image is not part of a set
314-
addToAlbum(link);
315-
} else {
316-
// If image is part of a set
317-
/** @type {NodeListOf<HTMLAnchorElement|HTMLAreaElement>} */
318-
const links = document.querySelectorAll(
319-
`${link.tagName}[rel="${link.rel}"]`
320-
);
321-
for (let j = 0; j < links.length; j = ++j) {
322-
addToAlbum(links[j]);
323-
if (links[j] === link) {
324-
imageNumber = j;
325-
}
312+
// If image is part of a set
313+
/** @type {NodeListOf<HTMLAnchorElement|HTMLAreaElement>} */
314+
const links = document.querySelectorAll(
315+
`${link.tagName}[rel="${link.rel}"]`
316+
);
317+
for (let j = 0; j < links.length; j = ++j) {
318+
addToAlbum(links[j]);
319+
if (links[j] === link) {
320+
imageNumber = j;
326321
}
327322
}
328323
}
@@ -334,7 +329,10 @@ class Lightbox {
334329
lightbox.style.top = `${top}px`;
335330
lightbox.style.left = `${left}px`;
336331
lightbox.style.display = 'block';
337-
lightbox.animate({ opacity: 1 }, { duration: this.options.fadeDuration });
332+
lightbox.animate(
333+
{ opacity: [0, 1] },
334+
{ duration: this.options.fadeDuration }
335+
);
338336

339337
// Disable scrolling of the page while open
340338
if (this.options.disableScrolling) {
@@ -348,21 +346,21 @@ class Lightbox {
348346
changeImage(imageNumber) {
349347
const filename = this.album[imageNumber].link;
350348
const filetype = filename.split('.').slice(-1)[0];
351-
const { image } = this;
349+
const { image, overlay, loader } = this;
352350

353351
// Disable keyboard nav during transitions
354352
this.disableKeyboardNav();
355353

356354
// Show loading state
357-
this.overlay.style.display = 'block';
358-
this.overlay.animate(
355+
overlay.style.display = 'block';
356+
overlay.animate(
359357
{ opacity: [0, 0.8] },
360358
{
361359
duration: this.options.fadeDuration
362360
}
363361
);
364-
this.loader.style.display = 'block';
365-
this.loader.animate({ opacity: [0, 1] }, { duration: 600 });
362+
loader.style.display = 'block';
363+
loader.animate({ opacity: [0, 1] }, { duration: 600 });
366364
for (const element of [
367365
image,
368366
this.nav,
@@ -381,8 +379,7 @@ class Lightbox {
381379
image.alt = this.album[imageNumber].alt;
382380
image.src = filename;
383381

384-
let width = preloader.width;
385-
let height = preloader.height;
382+
let { width, height } = preloader;
386383

387384
// Calculate the max image dimensions for the current viewport.
388385
// Take into account the border around the image and an additional 10px gutter on each side.
@@ -701,4 +698,5 @@ class Lightbox {
701698
}
702699
}
703700

704-
export default new Lightbox();
701+
export const lightbox = new Lightbox();
702+
export { lightbox as default };

0 commit comments

Comments
 (0)