-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex.js
90 lines (80 loc) · 2.09 KB
/
index.js
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
82
83
84
85
86
87
88
89
90
/**
* @module vite-plugin-glsl
* @author Ustym Ukhman <ustym.ukhman@gmail.com>
* @description Import, inline (and compress) GLSL shader files
* @version 1.3.1
* @license MIT
*/
import { createFilter } from '@rollup/pluginutils';
import { transformWithEsbuild } from 'vite';
import loadShader from './loadShader.js';
/**
* @const
* @default
* @readonly
* @type {string}
*/
const DEFAULT_EXTENSION = 'glsl';
/**
* @const
* @default
* @readonly
* @type {readonly RegExp[]}
*/
const DEFAULT_SHADERS = Object.freeze([
'**/*.glsl', '**/*.wgsl',
'**/*.vert', '**/*.frag',
'**/*.vs', '**/*.fs'
]);
/**
* @function
* @name glsl
* @description Plugin entry point to import,
* inline, (and compress) GLSL shader files
*
* @see {@link https://vitejs.dev/guide/api-plugin.html}
* @link https://github.com/UstymUkhman/vite-plugin-glsl
*
* @param {import('./types').PluginOptions} options Plugin config object
*
* @returns {import('vite').Plugin} Vite plugin that converts shader code
*/
export default function ({
include = DEFAULT_SHADERS,
exclude = undefined,
warnDuplicatedImports = true,
defaultExtension = DEFAULT_EXTENSION,
compress = false,
watch = true,
root = '/'
} = {}
) {
let sourcemap = false;
const filter = createFilter(include, exclude);
const prod = process.env.NODE_ENV === 'production';
return {
enforce: 'pre',
name: 'vite-plugin-glsl',
configResolved (resolvedConfig) {
sourcemap = resolvedConfig.build.sourcemap;
},
async transform (source, shader) {
if (!filter(shader)) return;
const { dependentChunks, outputShader } = loadShader(source, shader, {
warnDuplicatedImports,
defaultExtension,
compress,
root
});
if (watch && !prod) {
const chunks = Array.from(dependentChunks.values()).flat();
chunks.forEach(chunk => this.addWatchFile(chunk));
}
return await transformWithEsbuild(outputShader, shader, {
sourcemap: sourcemap && 'external',
loader: 'text', format: 'esm',
minifyWhitespace: prod
});
}
};
}