| 
									
										
										
										
											2020-05-06 02:20:44 +08:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-27 17:33:07 +08:00
										 |  |  | /*! | 
					
						
							|  |  |  |  * Script to build our plugins to use them separately. | 
					
						
							| 
									
										
										
										
											2025-01-15 19:43:41 +08:00
										 |  |  |  * Copyright 2020-2025 The Bootstrap Authors | 
					
						
							| 
									
										
										
										
											2020-06-17 02:41:47 +08:00
										 |  |  |  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
 | 
					
						
							| 
									
										
										
										
											2018-06-27 17:33:07 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 12:37:24 +08:00
										 |  |  | import path from 'node:path' | 
					
						
							|  |  |  | import { fileURLToPath } from 'node:url' | 
					
						
							|  |  |  | import { babel } from '@rollup/plugin-babel' | 
					
						
							| 
									
										
										
										
											2024-03-11 18:06:01 +08:00
										 |  |  | import { globby } from 'globby' | 
					
						
							| 
									
										
										
										
											2023-08-06 12:37:24 +08:00
										 |  |  | import { rollup } from 'rollup' | 
					
						
							|  |  |  | import banner from './banner.mjs' | 
					
						
							| 
									
										
										
										
											2018-11-28 19:03:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 12:37:24 +08:00
										 |  |  | const __filename = fileURLToPath(import.meta.url) | 
					
						
							|  |  |  | const __dirname = path.dirname(fileURLToPath(import.meta.url)) | 
					
						
							| 
									
										
										
										
											2018-06-27 17:33:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-29 15:38:35 +08:00
										 |  |  | const sourcePath = path.resolve(__dirname, '../js/src/').replace(/\\/g, '/') | 
					
						
							| 
									
										
										
										
											2024-03-11 18:06:01 +08:00
										 |  |  | const jsFiles = await globby(`${sourcePath}/**/*.js`) | 
					
						
							| 
									
										
										
										
											2018-07-25 17:29:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  | // Array which holds the resolved plugins
 | 
					
						
							|  |  |  | const resolvedPlugins = [] | 
					
						
							| 
									
										
										
										
											2018-06-27 17:33:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  | // Trims the "js" extension and uppercases => first letter, hyphens, backslashes & slashes
 | 
					
						
							|  |  |  | const filenameToEntity = filename => filename.replace('.js', '') | 
					
						
							|  |  |  |   .replace(/(?:^|-|\/|\\)[a-z]/g, str => str.slice(-1).toUpperCase()) | 
					
						
							| 
									
										
										
										
											2018-06-27 17:33:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  | for (const file of jsFiles) { | 
					
						
							|  |  |  |   resolvedPlugins.push({ | 
					
						
							| 
									
										
										
										
											2022-10-26 13:26:51 +08:00
										 |  |  |     src: file, | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  |     dist: file.replace('src', 'dist'), | 
					
						
							|  |  |  |     fileName: path.basename(file), | 
					
						
							|  |  |  |     className: filenameToEntity(path.basename(file)) | 
					
						
							| 
									
										
										
										
											2021-10-29 15:38:35 +08:00
										 |  |  |     // safeClassName: filenameToEntity(path.relative(sourcePath, file))
 | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  |   }) | 
					
						
							| 
									
										
										
										
											2018-07-25 17:29:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-06 12:52:06 +08:00
										 |  |  | const build = async plugin => { | 
					
						
							| 
									
										
										
										
											2024-03-11 18:06:01 +08:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * @type {import('rollup').GlobalsOption} | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  |   const globals = {} | 
					
						
							| 
									
										
										
										
											2018-07-25 17:29:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 12:37:24 +08:00
										 |  |  |   const bundle = await rollup({ | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  |     input: plugin.src, | 
					
						
							|  |  |  |     plugins: [ | 
					
						
							|  |  |  |       babel({ | 
					
						
							|  |  |  |         // Only transpile our source code
 | 
					
						
							|  |  |  |         exclude: 'node_modules/**', | 
					
						
							|  |  |  |         // Include the helpers in each file, at most one copy of each
 | 
					
						
							|  |  |  |         babelHelpers: 'bundled' | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     ], | 
					
						
							| 
									
										
										
										
											2022-02-15 14:50:37 +08:00
										 |  |  |     external(source) { | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  |       // Pattern to identify local files
 | 
					
						
							|  |  |  |       const pattern = /^(\.{1,2})\// | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // It's not a local file, e.g a Node.js package
 | 
					
						
							|  |  |  |       if (!pattern.test(source)) { | 
					
						
							|  |  |  |         globals[source] = source | 
					
						
							|  |  |  |         return true | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-02-23 06:37:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  |       const usedPlugin = resolvedPlugins.find(plugin => { | 
					
						
							|  |  |  |         return plugin.src.includes(source.replace(pattern, '')) | 
					
						
							|  |  |  |       }) | 
					
						
							| 
									
										
										
										
											2019-02-23 06:37:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  |       if (!usedPlugin) { | 
					
						
							|  |  |  |         throw new Error(`Source ${source} is not mapped!`) | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-06-27 17:33:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  |       // We can change `Index` with `UtilIndex` etc if we use
 | 
					
						
							|  |  |  |       // `safeClassName` instead of `className` everywhere
 | 
					
						
							|  |  |  |       globals[path.normalize(usedPlugin.src)] = usedPlugin.className | 
					
						
							|  |  |  |       return true | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-06-27 17:33:07 +08:00
										 |  |  |   }) | 
					
						
							| 
									
										
										
										
											2020-05-06 12:52:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   await bundle.write({ | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  |     banner: banner(plugin.fileName), | 
					
						
							| 
									
										
										
										
											2020-05-06 12:52:06 +08:00
										 |  |  |     format: 'umd', | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  |     name: plugin.className, | 
					
						
							| 
									
										
										
										
											2020-05-06 12:52:06 +08:00
										 |  |  |     sourcemap: true, | 
					
						
							|  |  |  |     globals, | 
					
						
							| 
									
										
										
										
											2021-10-05 00:46:07 +08:00
										 |  |  |     generatedCode: 'es2015', | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  |     file: plugin.dist | 
					
						
							| 
									
										
										
										
											2020-05-06 12:52:06 +08:00
										 |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  |   console.log(`Built ${plugin.className}`) | 
					
						
							| 
									
										
										
										
											2020-05-06 12:52:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  | (async () => { | 
					
						
							| 
									
										
										
										
											2020-05-06 12:52:06 +08:00
										 |  |  |   try { | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  |     const basename = path.basename(__filename) | 
					
						
							|  |  |  |     const timeLabel = `[${basename}] finished` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     console.log('Building individual plugins...') | 
					
						
							|  |  |  |     console.time(timeLabel) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await Promise.all(Object.values(resolvedPlugins).map(plugin => build(plugin))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     console.timeEnd(timeLabel) | 
					
						
							| 
									
										
										
										
											2020-05-06 12:52:06 +08:00
										 |  |  |   } catch (error) { | 
					
						
							|  |  |  |     console.error(error) | 
					
						
							|  |  |  |     process.exit(1) | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-11-23 23:37:14 +08:00
										 |  |  | })() |