-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.plugins.js
130 lines (85 loc) · 2.27 KB
/
vite.plugins.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import path from "path";
import fs from "fs";
/**
* Copy examples folder to dist
* and change html content
*/
export function copyExamples( from, to, overwrite = false ){
let bundleName = '';
return {
name: 'copy-files',
config( config ){
bundleName = config.build.lib.fileName;
},
closeBundle(){
const log = msg => console.log( '\x1b[36m%s\x1b[0m', msg );
log( `copy files: ${ from } → ${ to }` );
fs.readdirSync( from ).forEach( file => {
/**
* Do not copy css files
* vite already builds and adds to dist
*/
if ( /.\.css$/.test( file ) ) return;
const fromFile = `${ from }/${ file }`;
const toFile = `${ to }/${ file }`;
if ( fs.existsSync( toFile ) && ! overwrite ) return;
log( `• ${ fromFile } → ${ toFile }` );
fs.copyFileSync(
path.resolve( fromFile ),
path.resolve( toFile )
);
const htmlContent = fs.readFileSync( toFile, "UTF-8" );
let newhtmlContent = '';
newhtmlContent = htmlContent.replace( '{{style}}', `style.css` );
newhtmlContent = newhtmlContent.replace(
/<script id="main" type="module">[\s\S]*?<\/script>/,
`<script src="./${ bundleName }.umd.cjs"></script>`
);
fs.writeFileSync( toFile, newhtmlContent, 'UTF-8' );
});
}
}
}
/**
* Basic glsl mangler collect things start with "_" and
* converts to a0, z0, c24 ...
*/
export function glslMangler(){
const glslPicker = /\.(glsl)$/;
return {
enforce: 'pre',
name: 'glsl-mangler',
transform( code, id ){
if ( glslPicker.test( id ) === false ) return;
let i = 0;
const letters = "abcdefghijklmnoprstuvwyz";
const data = {};
code = code.replace( /\b_\w+/g, function( match, p1 ){
if ( ! data[ match ] ){
const char = i % letters.length;
const order = Math.floor( i / letters.length );
const id = `${ letters.charAt( char ) }${ order }`;
data[ match ] = id;
i++;
}
return data[ match ];
});
return {
code: code,
map: null
};
}
};
}
export function header(){
return {
renderChunk( code ){
return `/**
* @license
* SPDX-License-Identifier: MIT
* https://github.com/yunusbayraktaroglu/parallax-scene-js
*/
${ code }`;
}
};
}