This repository was archived by the owner on Mar 10, 2026. It is now read-only.
forked from dkaoster/scrolly-video
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.js
More file actions
81 lines (74 loc) · 2.33 KB
/
Copy pathvite.config.js
File metadata and controls
81 lines (74 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* @fileOverview Vite configuration, used for both the library and the docs site.
* The docs site can be used to test and develop the library.
*
* - `npm run dev` or `npm run start`: start the preview of the docs site.
* - `npm run build`: build the library.
* - `npm run build-docs`: build the docs site.
*/
import { dirname, resolve } from 'node:path';
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import vue from '@vitejs/plugin-vue';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
/**
* Settings for building the library.
*
* React and Vue files are kept because they have been part of the original code,
* in case we want to merge these changes back upstream.
*/
const libraryBuild = {
lib: {
entry: {
'scrolly-video': resolve(__dirname, 'src/ScrollyVideo.js'),
'ScrollyVideo.react': resolve(__dirname, 'src/ScrollyVideo.jsx'),
'ScrollyVideo.vue': resolve(__dirname, 'src/ScrollyVideo.vue'),
},
name: 'ScrollyVideo',
},
rollupOptions: {
external: ['svelte', 'vue', 'mp4boxjs', 'react', 'ua-parser-js'],
},
emptyOutDir: false,
};
/**
* No special settings are needed for the docs site.
*/
const docsBuild = {};
const docsBuildPlugins = [svelte(), vue()];
const libraryBuildPlugins = [viteWrapCodeInIIFE()];
/**
* @see https://vite.dev/config/
*/
export default defineConfig(({ mode }) => ({
base: './',
plugins:
mode === 'library'
? libraryBuildPlugins.concat(docsBuildPlugins)
: docsBuildPlugins,
build: mode === 'library' ? libraryBuild : docsBuild,
}));
/**
* Solves the issue of vite not wrapping all the code in an IIFE, which can lead to bundles overriding each other.
* @see https://github.com/vitejs/vite/issues/16443
* @see https://github.com/vitejs/vite/issues/17608#issue-2388013526
*/
function viteWrapCodeInIIFE(options = {}) {
return {
name: 'vite-wrap-code-in-iife',
apply: 'build',
enforce: 'post',
generateBundle(outputOptions, bundle) {
for (const [fileName, chunkOrAsset] of Object.entries(bundle)) {
if (
chunkOrAsset.type === 'chunk' &&
options.files &&
options.files.includes(fileName)
) {
chunkOrAsset.code = `(function () {${chunkOrAsset.code}})();`;
}
}
},
};
}