forked from symfony/ux
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_package.js
128 lines (107 loc) · 4.26 KB
/
build_package.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
/**
* This file is used to compile the assets from an UX package.
*/
const { parseArgs } = require('node:util');
const path = require('node:path');
const fs = require('node:fs');
const glob = require('glob');
const rollup = require('rollup');
const CleanCSS = require('clean-css');
const { getRollupConfiguration } = require('./rollup');
const args = parseArgs({
allowPositionals: true,
options: {
watch: {
type: 'boolean',
description: 'Watch the source files for changes and rebuild when necessary.',
},
},
});
async function main() {
const packageRoot = path.resolve(process.cwd(), args.positionals[0]);
if (!fs.existsSync(packageRoot)) {
console.error(`The package directory "${packageRoot}" does not exist.`);
process.exit(1);
}
if (!fs.existsSync(path.join(packageRoot, 'package.json'))) {
console.error(`The package directory "${packageRoot}" does not contain a package.json file.`);
process.exit(1);
}
const packageData = require(path.join(packageRoot, 'package.json'));
const packageName = packageData.name;
const srcDir = path.join(packageRoot, 'src');
const distDir = path.join(packageRoot, 'dist');
if (!fs.existsSync(srcDir)) {
console.error(`The package directory "${packageRoot}" does not contain a "src" directory.`);
process.exit(1);
}
if (fs.existsSync(distDir)) {
console.log(`Cleaning up the "${distDir}" directory...`);
await fs.promises.rm(distDir, { recursive: true });
await fs.promises.mkdir(distDir);
}
const inputScriptFiles = [
...glob.sync(path.join(srcDir, '*controller.ts')),
...(['@symfony/ux-react', '@symfony/ux-vue', '@symfony/ux-svelte'].includes(packageName)
? [path.join(srcDir, 'loader.ts'), path.join(srcDir, 'components.ts')]
: []),
...(packageName === '@symfony/stimulus-bundle'
? [path.join(srcDir, 'loader.ts'), path.join(srcDir, 'controllers.ts')]
: []),
];
const inputStyleFile = packageData.config?.css_source;
const buildCss = async () => {
const inputStyleFileDist = inputStyleFile
? path.resolve(distDir, `${path.basename(inputStyleFile, '.css')}.min.css`)
: undefined;
if (!inputStyleFile) {
return;
}
console.log('Minifying CSS...');
const css = await fs.promises.readFile(inputStyleFile, 'utf-8');
const minified = new CleanCSS().minify(css).styles;
await fs.promises.writeFile(inputStyleFileDist, minified);
};
if (inputScriptFiles.length === 0) {
console.error(
`No input files found for package "${packageName}" (directory "${packageRoot}").\nEnsure you have at least a file matching the pattern "src/*_controller.ts", or manually specify input files in "${__filename}" file.`
);
process.exit(1);
}
const rollupConfig = getRollupConfiguration({ packageRoot, inputFiles: inputScriptFiles });
if (args.values.watch) {
console.log(
`Watching for JavaScript${inputStyleFile ? ' and CSS' : ''} files modifications in "${srcDir}" directory...`
);
if (inputStyleFile) {
rollupConfig.plugins = (rollupConfig.plugins || []).concat({
name: 'watcher',
buildStart() {
this.addWatchFile(inputStyleFile);
},
});
}
const watcher = rollup.watch(rollupConfig);
watcher.on('event', ({ result }) => {
if (result) {
result.close();
}
});
watcher.on('change', async (id, { event }) => {
if (event === 'update') {
console.log('Files were modified, rebuilding...');
}
if (inputStyleFile && id === inputStyleFile) {
await buildCss();
}
});
} else {
console.log(`Building JavaScript files from ${packageName} package...`);
const start = Date.now();
const bundle = await rollup.rollup(rollupConfig);
await bundle.write(rollupConfig.output);
await buildCss();
console.log(`Done in ${((Date.now() - start) / 1000).toFixed(3)} seconds.`);
}
}
main();