Skip to content

Commit 58afdb4

Browse files
committed
fix namespace collision for iife bundle
1 parent 21a0221 commit 58afdb4

File tree

8 files changed

+920
-851
lines changed

8 files changed

+920
-851
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.1.1
4+
5+
- FIXED: Namespace collision of `iife` bundle when using multiple [js4y](https://github.com/bukacekd) libraries on the same html page.
6+
37
## 1.1.0
48

59
- ADDED: TypeScript declaration file.

docs/assets/index-26db65a7.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/assets/index-D0rGkZWZ.css

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

docs/index.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
<link rel="mask-icon" href="/lock-scroll/safari-pinned-tab.svg" color="#5bbad5">
1515
<meta name="msapplication-TileColor" content="#da532c">
1616
<meta name="theme-color" content="#ffffff">
17-
18-
<script type="module" crossorigin src="/lock-scroll/assets/index-6caea2f0.js"></script>
19-
<link rel="stylesheet" href="/lock-scroll/assets/index-26db65a7.css">
17+
<script type="module" crossorigin src="/lock-scroll/assets/index-CyIeJ1Qs.js"></script>
18+
<link rel="stylesheet" crossorigin href="/lock-scroll/assets/index-D0rGkZWZ.css">
2019
</head>
2120

2221
<body>
@@ -144,6 +143,5 @@ <h2>JavaScript source code</h2>
144143
Built by <a href="https://github.com/bukacekd"><b>David Bukacek</b></a>
145144
</footer>
146145

147-
148146
</body>
149147
</html>

esbuild.config.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,50 @@
11
import esbuild from 'esbuild';
2+
import {readFile, writeFile} from 'node:fs/promises';
3+
4+
const OptimizePlugin = {
5+
name: 'OptimizePlugin',
6+
setup: build => {
7+
build.onLoad({filter: /.ts/}, async args => {
8+
const content = await readFile(args.path, 'utf8');
9+
10+
return {
11+
contents: content.replace(/\s\s+/g, ''),
12+
loader: 'ts'
13+
}
14+
});
15+
16+
build.onEnd(async () => {
17+
let content = await readFile(build.initialOptions.outfile, 'utf8');
18+
19+
if (build.initialOptions.format === 'iife') {
20+
content = content.replace(
21+
new RegExp(`([^.]+\\.${build.initialOptions.globalName.split('.').pop()}=)(.+);`),
22+
`$1Object.assign(${build.initialOptions.globalName}??{},$2);`
23+
);
24+
}
25+
26+
await writeFile(build.initialOptions.outfile, content);
27+
});
28+
}
29+
};
230

331
await esbuild.build({
432
entryPoints: ['src/index.ts'],
533
format: 'esm',
634
logLevel: 'info',
35+
metafile: true,
736
minify: true,
837
outfile: 'dist/index.mjs',
38+
plugins: [OptimizePlugin]
939
});
1040

1141
await esbuild.build({
1242
entryPoints: ['src/index.ts'],
1343
format: 'iife',
1444
globalName: 'js4y.scrolling',
1545
logLevel: 'info',
46+
metafile: true,
1647
minify: true,
17-
outfile: 'dist/index.js'
48+
outfile: 'dist/index.js',
49+
plugins: [OptimizePlugin]
1850
});

0 commit comments

Comments
 (0)