2
2
* This file is used to compile the assets from an UX package.
3
3
*/
4
4
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' ;
12
12
13
13
const args = parseArgs ( {
14
14
allowPositionals : true ,
@@ -34,7 +34,7 @@ async function main() {
34
34
process . exit ( 1 ) ;
35
35
}
36
36
37
- const packageData = require ( path . join ( packageRoot , 'package.json' ) ) ;
37
+ const packageData = await import ( path . join ( packageRoot , 'package.json' ) , { with : { type : 'json' } } ) ;
38
38
const packageName = packageData . name ;
39
39
const srcDir = path . join ( packageRoot , 'src' ) ;
40
40
const distDir = path . join ( packageRoot , 'dist' ) ;
@@ -51,7 +51,7 @@ async function main() {
51
51
}
52
52
53
53
const inputScriptFiles = [
54
- ...glob . sync ( path . join ( srcDir , '*controller.ts' ) ) ,
54
+ ...globSync ( path . join ( srcDir , '*controller.ts' ) ) ,
55
55
...( [ '@symfony/ux-react' , '@symfony/ux-vue' , '@symfony/ux-svelte' ] . includes ( packageName )
56
56
? [ path . join ( srcDir , 'loader.ts' ) , path . join ( srcDir , 'components.ts' ) ]
57
57
: [ ] ) ,
@@ -62,12 +62,10 @@ async function main() {
62
62
63
63
const inputStyleFile = packageData . config ?. css_source ;
64
64
const buildCss = async ( ) => {
65
- const inputStyleFileDist = inputStyleFile
66
- ? path . resolve ( distDir , `${ path . basename ( inputStyleFile , '.css' ) } .min.css` )
67
- : undefined ;
68
65
if ( ! inputStyleFile ) {
69
66
return ;
70
67
}
68
+ const inputStyleFileDist = path . resolve ( distDir , `${ path . basename ( inputStyleFile , '.css' ) } .min.css` ) ;
71
69
72
70
console . log ( 'Minifying CSS...' ) ;
73
71
const css = await fs . promises . readFile ( inputStyleFile , 'utf-8' ) ;
@@ -82,34 +80,41 @@ async function main() {
82
80
83
81
if ( inputScriptFiles . length === 0 ) {
84
82
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.`
86
84
) ;
87
85
process . exit ( 1 ) ;
88
86
}
89
87
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
+ } ) ;
91
105
92
106
if ( isWatch ) {
93
107
console . log (
94
108
`Watching for JavaScript${ inputStyleFile ? ' and CSS' : '' } files modifications in "${ srcDir } " directory...`
95
109
) ;
96
110
97
- if ( inputStyleFile ) {
98
- rollupConfig . plugins = ( rollupConfig . plugins || [ ] ) . concat ( {
99
- name : 'watcher' ,
100
- buildStart ( ) {
101
- this . addWatchFile ( inputStyleFile ) ;
102
- } ,
103
- } ) ;
104
- }
105
-
106
111
const watcher = rollup . watch ( rollupConfig ) ;
107
112
watcher . on ( 'event' , ( event ) => {
108
113
if ( event . code === 'ERROR' ) {
109
114
console . error ( 'Error during build:' , event . error ) ;
110
115
}
111
116
112
- if ( event . result ) {
117
+ if ( ( event . code === 'BUNDLE_END' || event . code === 'ERROR' ) && event . result ) {
113
118
event . result . close ( ) ;
114
119
}
115
120
} ) ;
@@ -126,6 +131,13 @@ async function main() {
126
131
console . log ( `Building JavaScript files from ${ packageName } package...` ) ;
127
132
const start = Date . now ( ) ;
128
133
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
+
129
141
const bundle = await rollup . rollup ( rollupConfig ) ;
130
142
await bundle . write ( rollupConfig . output ) ;
131
143
0 commit comments