Skip to content

Commit d2ac9ff

Browse files
committed
Migrate bin/*.js to TypeScript, use tsx
1 parent 7241dc4 commit d2ac9ff

File tree

26 files changed

+596
-220
lines changed

26 files changed

+596
-220
lines changed

bin/build_package.js renamed to bin/build_package.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* This file is used to compile the assets from an UX package.
33
*/
44

5-
const { parseArgs } = require('node:util');
6-
const path = require('node:path');
7-
const fs = require('node:fs');
8-
const glob = require('glob');
9-
const rollup = require('rollup');
10-
const LightningCSS = require('lightningcss');
11-
const { getRollupConfiguration } = require('./rollup');
5+
import * as fs from 'node:fs';
6+
import * as path from 'node:path';
7+
import { parseArgs } from 'node:util';
8+
import * as LightningCSS from 'lightningcss';
9+
import * as rollup from 'rollup';
10+
import { globSync } from 'tinyglobby';
11+
import { getRollupConfiguration } from './rollup.ts';
1212

1313
const args = parseArgs({
1414
allowPositionals: true,
@@ -34,7 +34,7 @@ async function main() {
3434
process.exit(1);
3535
}
3636

37-
const packageData = require(path.join(packageRoot, 'package.json'));
37+
const packageData = await import(path.join(packageRoot, 'package.json'), {with: { type: 'json'}});
3838
const packageName = packageData.name;
3939
const srcDir = path.join(packageRoot, 'src');
4040
const distDir = path.join(packageRoot, 'dist');
@@ -51,7 +51,7 @@ async function main() {
5151
}
5252

5353
const inputScriptFiles = [
54-
...glob.sync(path.join(srcDir, '*controller.ts')),
54+
...globSync(path.join(srcDir, '*controller.ts')),
5555
...(['@symfony/ux-react', '@symfony/ux-vue', '@symfony/ux-svelte'].includes(packageName)
5656
? [path.join(srcDir, 'loader.ts'), path.join(srcDir, 'components.ts')]
5757
: []),
@@ -62,12 +62,10 @@ async function main() {
6262

6363
const inputStyleFile = packageData.config?.css_source;
6464
const buildCss = async () => {
65-
const inputStyleFileDist = inputStyleFile
66-
? path.resolve(distDir, `${path.basename(inputStyleFile, '.css')}.min.css`)
67-
: undefined;
6865
if (!inputStyleFile) {
6966
return;
7067
}
68+
const inputStyleFileDist = path.resolve(distDir, `${path.basename(inputStyleFile, '.css')}.min.css`);
7169

7270
console.log('Minifying CSS...');
7371
const css = await fs.promises.readFile(inputStyleFile, 'utf-8');
@@ -82,34 +80,41 @@ async function main() {
8280

8381
if (inputScriptFiles.length === 0) {
8482
console.error(
85-
`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.`
83+
`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 "${import.meta.filename}" file.`
8684
);
8785
process.exit(1);
8886
}
8987

90-
const rollupConfig = getRollupConfiguration({ packageRoot, inputFiles: inputScriptFiles, isWatch });
88+
const rollupConfig = getRollupConfiguration({
89+
packageRoot,
90+
inputFiles: inputScriptFiles,
91+
isWatch,
92+
additionalPlugins: [
93+
...(isWatch && inputStyleFile
94+
? [
95+
{
96+
name: 'watcher',
97+
buildStart(this: rollup.PluginContext) {
98+
this.addWatchFile(inputStyleFile);
99+
},
100+
},
101+
]
102+
: []),
103+
],
104+
});
91105

92106
if (isWatch) {
93107
console.log(
94108
`Watching for JavaScript${inputStyleFile ? ' and CSS' : ''} files modifications in "${srcDir}" directory...`
95109
);
96110

97-
if (inputStyleFile) {
98-
rollupConfig.plugins = (rollupConfig.plugins || []).concat({
99-
name: 'watcher',
100-
buildStart() {
101-
this.addWatchFile(inputStyleFile);
102-
},
103-
});
104-
}
105-
106111
const watcher = rollup.watch(rollupConfig);
107112
watcher.on('event', (event) => {
108113
if (event.code === 'ERROR') {
109114
console.error('Error during build:', event.error);
110115
}
111116

112-
if (event.result) {
117+
if ((event.code === 'BUNDLE_END' || event.code === 'ERROR') && event.result) {
113118
event.result.close();
114119
}
115120
});
@@ -126,6 +131,13 @@ async function main() {
126131
console.log(`Building JavaScript files from ${packageName} package...`);
127132
const start = Date.now();
128133

134+
if (typeof rollupConfig.output === 'undefined' || Array.isArray(rollupConfig.output)) {
135+
console.error(
136+
`The rollup configuration for package "${packageName}" does not contain a valid output configuration.`
137+
);
138+
process.exit(1);
139+
}
140+
129141
const bundle = await rollup.rollup(rollupConfig);
130142
await bundle.write(rollupConfig.output);
131143

bin/rollup.js

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)