11/**
2- * UI5 Custom Task: WebComponents Bundler (Standalone Plugin)
2+ * UI5 Custom Task: WebComponents & NPM Modules Bundler (Standalone Plugin)
33 *
4- * Uses the custom standalone rollup-plugin-ui5-webcomponents.js
5- * to bundle @ui5/webcomponents during the build process.
4+ * Bundles:
5+ * 1. @ui5/webcomponents using rollup-plugin-ui5-webcomponents.js
6+ * 2. NPM dependencies from package.json using shared Rollup config
7+ *
8+ * Output: /resources/thirdparty/<module-name>.js
69 */
710
11+ import { readFile } from "fs/promises" ;
12+ import { join , dirname } from "path" ;
13+ import { fileURLToPath } from "url" ;
14+
815import ui5WebComponentsPlugin from "../lib/plugins/rollup-plugin-ui5-webcomponents.js" ;
916import { WebComponentsBundler } from "../lib/core/webcomponents-bundler.js" ;
1017import { WorkspaceWriter } from "../lib/core/output-writer.js" ;
1118import { PathResolver } from "../lib/utils/path-resolver.js" ;
1219import { DEFAULT_CONFIG , createPluginOptions , createBundlerOptions } from "../lib/config/standalone-config.js" ;
20+ import { createRollupConfig , generateAMDBundle } from "../scenarios/shared-config.js" ;
21+
22+ const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
23+
24+ /**
25+ * NPM packages to exclude from bundling (UI5 internal, dev deps, tooling)
26+ */
27+ const EXCLUDED_PACKAGES = [
28+ "ui5-tooling-modules" ,
29+ "@ui5/webcomponents" , // Handled separately by WebComponents bundler
30+ "@ui5/webcomponents-base" ,
31+ "@ui5/webcomponents-theming" ,
32+ "@ui5/webcomponents-icons"
33+ ] ;
34+
35+ /**
36+ * Read and parse package.json dependencies
37+ *
38+ * @returns {Promise<string[]> } List of dependency names to bundle
39+ */
40+ async function getPackageDependencies ( ) {
41+ const packageJsonPath = join ( __dirname , "package.json" ) ;
42+ const packageJson = JSON . parse ( await readFile ( packageJsonPath , "utf-8" ) ) ;
43+
44+ const dependencies = Object . keys ( packageJson . dependencies || { } ) ;
45+
46+ // Filter out excluded packages
47+ return dependencies . filter ( ( dep ) => ! EXCLUDED_PACKAGES . includes ( dep ) ) ;
48+ }
1349
1450/**
1551 * Custom task entry point
@@ -22,36 +58,59 @@ import {DEFAULT_CONFIG, createPluginOptions, createBundlerOptions} from "../lib/
2258 * @returns {Promise<undefined> } Promise resolving when task is complete
2359 */
2460export default async function ( { workspace, log, taskUtil} ) {
25- log . info ( "Running WebComponents bundler task (standalone plugin) ..." ) ;
61+ log . info ( "Running WebComponents & NPM Modules bundler task ..." ) ;
2662
2763 const { resourceFactory} = taskUtil ;
2864 const $metadata = { } ;
2965
3066 try {
31- // Create plugin instance
32- const plugin = ui5WebComponentsPlugin ( createPluginOptions ( log , $metadata ) ) ;
67+ // ========== 1. Bundle WebComponents ==========
68+ log . info ( "📦 Phase 1: Bundling WebComponents..." ) ;
3369
34- // Create path resolver
70+ const plugin = ui5WebComponentsPlugin ( createPluginOptions ( log , $metadata ) ) ;
3571 const pathResolver = new PathResolver (
3672 DEFAULT_CONFIG . namespace ,
3773 DEFAULT_CONFIG . thirdpartyNamespace
3874 ) ;
39-
40- // Create workspace writer
4175 const workspaceWriter = new WorkspaceWriter ( pathResolver , workspace , resourceFactory ) ;
42-
43- // Create bundler instance
4476 const bundler = new WebComponentsBundler ( createBundlerOptions ( log , plugin ) ) ;
4577
46- // Bundle the MessageStrip webcomponent
4778 const output = await bundler . bundle ( "@ui5/webcomponents/dist/MessageStrip.js" ) ;
48-
49- // Process output and write to workspace
5079 await bundler . processOutput ( output , workspaceWriter , $metadata ) ;
5180
52- log . info ( "✅ WebComponents bundled successfully using standalone plugin" ) ;
81+ log . info ( "✅ WebComponents bundled successfully" ) ;
82+
83+ // ========== 2. Bundle NPM Packages ==========
84+ log . info ( "📦 Phase 2: Bundling NPM packages from package.json..." ) ;
85+
86+ const dependencies = await getPackageDependencies ( ) ;
87+ log . info ( ` Found ${ dependencies . length } dependencies to bundle: ${ dependencies . join ( ", " ) } ` ) ;
88+
89+ for ( const packageName of dependencies ) {
90+ try {
91+ // Reuse shared Rollup config and AMD generator (DRY)
92+ const config = createRollupConfig ( packageName ) ;
93+ const code = await generateAMDBundle ( config ) ;
94+
95+ // Sanitize package name for path (handle scoped packages)
96+ const sanitizedName = packageName . replace ( / ^ @ / , "" ) . replace ( / \/ / g, "-" ) ;
97+ const outputPath = `/resources/thirdparty/${ sanitizedName } .js` ;
98+
99+ const resource = resourceFactory . createResource ( {
100+ path : outputPath ,
101+ string : code
102+ } ) ;
103+
104+ await workspace . write ( resource ) ;
105+ log . info ( ` ✅ ${ packageName } → ${ outputPath } (${ ( code . length / 1024 ) . toFixed ( 1 ) } KB)` ) ;
106+ } catch ( error ) {
107+ log . error ( ` ❌ Failed to bundle ${ packageName } : ${ error . message } ` ) ;
108+ }
109+ }
110+
111+ log . info ( "✅ All NPM packages bundled successfully" ) ;
53112 } catch ( error ) {
54- log . error ( `❌ Error bundling WebComponents : ${ error . message } ` ) ;
113+ log . error ( `❌ Error in bundler task : ${ error . message } ` ) ;
55114 log . error ( error . stack ) ;
56115 throw error ;
57116 }
0 commit comments