-
Notifications
You must be signed in to change notification settings - Fork 556
Expand file tree
/
Copy pathdist.mjs
More file actions
210 lines (178 loc) · 5.43 KB
/
dist.mjs
File metadata and controls
210 lines (178 loc) · 5.43 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import fs from 'fs';
import path from 'path';
import { globSync } from 'glob';
import * as sass from 'sass';
import pkg from '../package.json' assert { type: 'json' };
const DEV = process.argv[2] === '--dev';
const DIST = 'dist/';
const CORE_JS = [
'src/main.js',
'src/defaults.js',
'src/plugins.js',
'src/core.js',
'src/public.js',
'src/data.js',
'src/template.js',
'src/utils.js',
'src/model.js',
'src/jquery.js',
];
const CORE_SASS = [
'src/scss/dark.scss',
'src/scss/default.scss',
];
const STANDALONE_JS = {
'jquery-extendext': 'node_modules/jquery-extendext/jquery-extendext.js',
'query-builder': `${DIST}js/query-builder.js`,
};
const BANNER = () => `/*!
* jQuery QueryBuilder ${pkg.version}
* Copyright 2014-${new Date().getFullYear()} Damien "Mistic" Sorel (http://www.strangeplanet.fr)
* Licensed under MIT (https://opensource.org/licenses/MIT)
*/`;
const LANG_BANNER = (locale, author) => `/*!
* jQuery QueryBuilder ${pkg.version}
* Locale: ${locale}
* Author: ${author}
* Licensed under MIT (https://opensource.org/licenses/MIT)
*/`;
const ALL_PLUGINS_JS = glob('src/plugins/*/plugin.js')
.sort()
.reduce((all, p) => {
const n = p.split('/')[2];
all[n] = p;
return all;
}, {});
const ALL_PLUGINS_SASS = glob('src/plugins/*/plugin.scss')
.sort()
.reduce((all, p) => {
const n = p.split('/')[2];
all[n] = p;
return all;
}, {});
const ALL_LANGS = glob('src/i18n/*.json')
.map(p => p.split(/[\/\.]/)[2])
.sort();
function glob(pattern) {
return globSync(pattern)
.map(p => p.split(path.sep).join('/'));
}
/**
* Build lang files
*/
function buildLangs() {
const wrapper = fs.readFileSync('src/i18n/.wrapper.js', { encoding: 'utf8' })
.split('@@js\n');
ALL_LANGS.forEach(lang => {
const outpath = `${DIST}i18n/query-builder.${lang}.js`;
console.log(`LANG ${lang} (${outpath})`);
fs.writeFileSync(outpath, getLang(lang, wrapper));
});
}
/**
* Get the content of a single lang
*/
function getLang(lang, wrapper = ['', '']) {
const corepath = `src/i18n/${lang}.json`;
const content = JSON.parse(fs.readFileSync(corepath, { encoding: 'utf8' }));
Object.keys(ALL_PLUGINS_JS).forEach(plugin => {
const pluginpath = `src/plugins/${plugin}/i18n/${lang}.json`;
try {
const plugincontent = JSON.parse(fs.readFileSync(pluginpath, { encoding: 'utf8' }));
Object.assign(content, plugincontent);
} catch { }
});
return LANG_BANNER(content.__locale || lang, content.__author || '')
+ '\n\n'
+ wrapper[0]
+ `QueryBuilder.regional['${lang}'] = `
+ JSON.stringify(content, null, 2)
+ ';\n\n'
+ `QueryBuilder.defaults({ lang_code: '${lang}' });`
+ wrapper[1];
}
/**
* Build main JS file
*/
function buildMain() {
const wrapper = fs.readFileSync('src/.wrapper.js', { encoding: 'utf8' })
.split('@@js\n');
const files_to_load = [
...CORE_JS,
...Object.values(ALL_PLUGINS_JS),
];
const output = BANNER()
+ '\n\n'
+ wrapper[0]
+ files_to_load.map(f => fs.readFileSync(f, { encoding: 'utf8' })).join('\n\n')
+ '\n\n'
+ getLang('en')
+ wrapper[1];
const outpath = `${DIST}js/query-builder.js`;
console.log(`MAIN (${outpath})`);
fs.writeFileSync(outpath, output);
}
/**
* Build standalone JS file
*/
function buildStandalone() {
const output = Object.entries(STANDALONE_JS)
.map(([name, file]) => {
return fs.readFileSync(file, { encoding: 'utf8' })
.replace(/define\((.*?)\);/, `define('${name}', $1);`);
})
.join('\n\n');
const outpath = `${DIST}js/query-builder.standalone.js`;
console.log(`STANDALONE (${outpath})`);
fs.writeFileSync(outpath, output);
}
/**
* Copy SASS files
*/
function copySass() {
Object.entries(ALL_PLUGINS_SASS).forEach(([plugin, path]) => {
const outpath = `${DIST}scss/plugins/${plugin}.scss`;
console.log(`SASS ${plugin} (${path})`);
fs.copyFileSync(path, outpath);
});
CORE_SASS.forEach(path => {
const name = path.split('/').pop();
const content = fs.readFileSync(path, { encoding: 'utf8' });
let output = BANNER()
+ '\n'
+ content;
if (name === 'default.scss') {
output += '\n'
+ Object.keys(ALL_PLUGINS_SASS).map(p => `@import "plugins/${p}";`).join('\n');
}
const outpath = `${DIST}scss/${name}`;
console.log(`SASS (${path})`);
fs.writeFileSync(outpath, output);
});
}
/**
* Build CSS files
*/
function buildCss() {
CORE_SASS.forEach(p => {
const path = p.replace('src/', DIST);
const name = path.split('/').pop();
const output = sass.compile(path);
const outpath = `${DIST}css/query-builder.${name.split('.').shift()}.css`;
console.log(`CSS (${path})`);
fs.writeFileSync(outpath, output.css);
});
}
if (!DEV) {
fs.rmSync(DIST, { recursive: true, force: true });
}
fs.mkdirSync(DIST + 'css', { recursive: true });
fs.mkdirSync(DIST + 'i18n', { recursive: true });
fs.mkdirSync(DIST + 'js', { recursive: true });
fs.mkdirSync(DIST + 'scss', { recursive: true });
fs.mkdirSync(DIST + 'scss/plugins', { recursive: true });
buildLangs();
buildMain();
buildStandalone();
copySass();
buildCss();