Customising Bundling

Defining Fragments

By default the plugin will bundle all front-end dependencies into a single html import that contains all dependencies and their transitive dependencies. To split this potentially large file up into several smaller files that can be served only as needed, the plugin can be configured to produce fragment files.

Fragments are html imports containing dependencies that have been split out of the main bundle file.

Basic principle is the same: you specify a files that should go into each fragment by giving their paths. The paths listed in a fragment should be given relative to the transpileEs6SourceDirectory you have configured, which is by default ${project.build.directory}/frontend/.

Note that any shared dependencies between multiple fragments will be detected and added to the main bundle file. Thus to effectively split your bundle into fragments, shared dependencies should be taken into account when defining the fragment split to use.

As a starting point for splitting a bundle into fragments it is recommended that you first run the plugin without any fragments defined and inspect the produced vaadin-flow-bundle.html file, which contains all the front-end dependencies found in your projects run time classpath. The produced bundle can be found in the location defined by the transpileWorkingDirectory parameter, which defaults to ${project.build.directory}/, i.e. target/ if the project build directory has not been configured separately.

When running a bundle enabled flow application in production mode a DependencyFilter is automatically registered during startup to serve the bundle and fragment files instead of their individual dependencies whenever requested.

Example bundle configurations will be shown for Maven and a Json configuration file.

The defined fragments are:

  • A fragment containing only vaadin-icons

  • A fragment containing several components

  • A fragment containing vaadin-grid and its flow integration dependencies

Defining Fragments with Maven

Fragments are configured by adding <fragments> to the configuration of the plugin. Each fragment should have its name and at least one file specified.

pom.xml

  1. <plugin>
  2. <groupId>com.vaadin</groupId>
  3. <artifactId>vaadin-maven-plugin</artifactId>
  4. <version>${vaadin.version}</version>
  5. <executions>
  6. <execution>
  7. <goals>
  8. <goal>package-for-production</goal>
  9. </goals>
  10. <configuration>
  11. <bundle>true</bundle>
  12. <fragments>
  13. <fragment>
  14. <name>icons-fragment</name>
  15. <files>
  16. <file>bower_components/vaadin-icons/vaadin-icons.html</file>
  17. </files>
  18. </fragment>
  19. <fragment>
  20. <name>important-components</name>
  21. <files>
  22. <file>bower_components/vaadin-form-layout/vaadin-form-layout.html</file>
  23. <file>bower_components/vaadin-form-layout/vaadin-form-item.html</file>
  24. <file>bower_components/vaadin-text-field/vaadin-text-field.html</file>
  25. <file>bower_components/vaadin-text-field/vaadin-password-field.html</file>
  26. <file>bower_components/vaadin-combo-box/vaadin-combo-box.html</file>
  27. </files>
  28. </fragment>
  29. <fragment>
  30. <name>grid-fragment</name>
  31. <files>
  32. <file>gridConnector.js</file>
  33. <file>vaadin-grid-flow-selection-column.html</file>
  34. <file>bower_components/vaadin-grid/vaadin-grid.html</file>
  35. <file>bower_components/vaadin-grid/vaadin-grid-column-group.html</file>
  36. <file>bower_components/vaadin-grid/vaadin-grid-sorter.html</file>
  37. </files>
  38. </fragment>
  39. </fragments>
  40. </configuration>
  41. </execution>
  42. </executions>
  43. </plugin>

Defining Fragments with a configuration file

Same result may be achieved with a custom configuration file instead. Fragments are configured by adding a fragments object to your bundle configuration file. The fragments object should be an array of objects, where each object defines a fragment name and files that belong to the fragment.

fragments.json

  1. {
  2. "fragments": [
  3. {
  4. "name": "icons-fragment",
  5. "files": ["bower_components/vaadin-icons/vaadin-icons.html"]
  6. },
  7. {
  8. "name": "important-components",
  9. "files": [
  10. "bower_components/vaadin-form-layout/vaadin-form-layout.html",
  11. "bower_components/vaadin-form-layout/vaadin-form-item.html",
  12. "bower_components/vaadin-text-field/vaadin-text-field.html",
  13. "bower_components/vaadin-text-field/vaadin-password-field.html",
  14. "bower_components/vaadin-combo-box/vaadin-combo-box.html"
  15. ]
  16. },
  17. {
  18. "name": "grid-fragment",
  19. "files": [
  20. "gridConnector.js",
  21. "vaadin-grid-flow-selection-column.html",
  22. "bower_components/vaadin-grid/vaadin-grid.html",
  23. "bower_components/vaadin-grid/vaadin-grid-column-group.html",
  24. "bower_components/vaadin-grid/vaadin-grid-sorter.html"
  25. ]
  26. }
  27. ]
  28. }

You still have to configure Maven plugin if the json file is not in the default path (see corresponding parameter description for details):

pom.xml

  1. <plugin>
  2. <groupId>com.vaadin</groupId>
  3. <artifactId>vaadin-maven-plugin</artifactId>
  4. <version>${vaadin.version}</version>
  5. <executions>
  6. <execution>
  7. <goals>
  8. <goal>package-for-production</goal>
  9. </goals>
  10. <configuration>
  11. <bundle>true</bundle>
  12. <bundleConfiguration>${path.to.json.file.declared.above}</bundleConfiguration>
  13. </configuration>
  14. </execution>
  15. </executions>
  16. </plugin>