-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.js
99 lines (87 loc) · 2.79 KB
/
build.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
'use strict';
const fs = require('fs');
const { copySync } = require('fs-extra');
const { execSync, exec } = require('child_process');
const path = require('path');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config');
try {
fs.mkdirSync(path.join(__dirname, 'public', 'vendor'));
} catch (err) {}
try {
fs.mkdirSync(path.join(__dirname, 'public', 'vendor', 'vanillatoasts'));
} catch (err) {}
fs.copyFileSync(
path.join(__dirname, 'node_modules', 'vanillatoasts', 'vanillatoasts.css'),
path.join(__dirname, 'public', 'vendor', 'vanillatoasts', 'vanillatoasts.css')
);
copySync(
path.join(__dirname, 'node_modules', 'vue', 'dist'),
path.join(__dirname, 'public', 'vendor', 'vue')
);
copySync(
path.join(__dirname, 'node_modules', 'vue-router', 'dist'),
path.join(__dirname, 'public', 'vendor', 'vue-router')
);
copySync(
path.join(__dirname, 'node_modules', 'prismjs'),
path.join(__dirname, 'public', 'vendor', 'prismjs')
);
module.exports = async function build(watch) {
if (watch) {
const childProcess = exec('npm run tailwind:watch');
childProcess.stdout.on('data', data => console.log('[TAILWIND]', data));
childProcess.stderr.on('data', data => console.log('[TAILWIND]', data));
} else {
execSync('npm run tailwind');
}
generateComponentsDirectory();
const compiler = webpack(webpackConfig);
if (watch) {
compiler.watch({}, (err, res) => {
if (err) {
process.nextTick(() => { throw new Error('Error compiling bundle: ' + err.stack); });
}
console.log('Webpack compiled successfully');
});
} else {
await new Promise((resolve, reject) => {
compiler.run((err) => {
if (err) {
return reject(err);
}
console.log('Webpack compiled successfully');
resolve();
});
});
}
};
function generateComponentsDirectory() {
// Generate list of components
let components = `
'use strict';
// This file is auto-generated. Do not modify this file directly.
`.trim();
fs.readdirSync(path.join(__dirname, 'src')).forEach(folder => {
if (folder.startsWith('_')) {
return;
}
const stat = fs.statSync(path.join(__dirname, 'src', folder));
if (!stat.isDirectory()) {
return;
}
components += `\nexports['${folder}'] = require('./${folder}/${folder}.js');`;
const folderPath = path.join(__dirname, 'src', folder);
for (const subdir of fs.readdirSync(folderPath)) {
const stat = fs.statSync(path.join(folderPath, subdir));
if (!stat.isDirectory()) {
continue;
}
components += `\nexports['${subdir}'] = require('./${folder}/${subdir}/${subdir}.js');`;
}
});
fs.writeFileSync(path.join(__dirname, 'src', 'components.js'), components);
}
if (require.main === module) {
module.exports();
}