Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substituting <script> tags in HTML with merged values #4

Open
blutorange opened this issue Oct 14, 2018 · 9 comments
Open

Substituting <script> tags in HTML with merged values #4

blutorange opened this issue Oct 14, 2018 · 9 comments
Labels
enhancement New feature or request
Milestone

Comments

@blutorange
Copy link
Owner

Issue by Adhara3
Friday Feb 20, 2015 at 15:41 GMT
Originally opened as samaxes#88


I thing this plugin does a great job but this feature is, IMHO, missing and makes the merge feature less usable.
I saw that you created the bundle feature in 1.7.5 (great!) and that could be the starting point to substitute in the HTML files the old css/js script/link tags with the new ones.
In my scenario I have a single page app with only one HTML containing all the imports. I am using Angular JS which is best managed with several js files (50+).
It would be great to bundle all of them into a single file and change the script tag so that I only import that single file. That would improve performances a lot!

It is a nice to have, but nothing really critical since, while the HTTP request for 50+ files may be not ideal, for single page apps this is performed only once.

Thanks
Andrea

@blutorange
Copy link
Owner Author

Comment by vgalcenco
Friday Apr 17, 2015 at 07:23 GMT


+1 for this feature to be implemented as I'm facing exactly the same challenges Andrea mentioned, however in my scenario I don't have a single page app.

@blutorange blutorange added this to the 2.0.0 milestone Oct 14, 2018
@blutorange blutorange added the enhancement New feature or request label Oct 14, 2018
@blutorange
Copy link
Owner Author

Comment by stefan-niedermann
Thursday Apr 30, 2015 at 15:36 GMT


+1 from me, too. i like the plugin but if i use it, my complete project depends on maven because i have to link a script file which does not exist until maven creates it.

@blutorange
Copy link
Owner Author

Comment by woodgoblin
Wednesday Jul 01, 2015 at 13:30 GMT


+1 also.
If it is possilbe, it will be great to do so.
I have a single-page app and all I need is to have over 50 scripts or only 2, depending on profile

@grantsunny
Copy link

vote +1

@blutorange
Copy link
Owner Author

@grantsunny

Is there still any interest in this? I could imagine the following approach, what do you think?

For each execution, add a configuration to the plugin for the HTML and for selecting a script tag in the file, e.g. by ID. The plugin would then replace the src attribute of the script tag with the path to the minified JavaScript file, relative to the HTML file.

<!-- Configure resources so that target/generated-resources/frontend/js -->
<!-- is copied to target/classes -->
<configuration>
    <htmlRoot>src/main/resources/webapp</htmlRoot>
    <scriptRoot>target/generated-resources/frontend/js</scriptRoot>
    <baseTargetDir>target/generated-resources/frontend/js</baseTargetDir>
</configuration>
<executions>
  <execution>
    <configuration>
      <targetDir>public/resources/main</targetDir>
      <outputFilename>script.min.js</outputFilename>
      <htmlOutput>
        <htmlFile>src/main/resources/webapp/public/pages/profile/index.html</htmlFile>
        <scriptId>main</scriptId>
      </htmlOutput> 
    <configuration>
  </execution>
  <execution>
    <configuration>
      <targetDir>public/resources/special/other</targetDir>
      <outputFilename>script.min.js</outputFilename>
      <htmlOutput>
        <htmlFile>src/main/resources/webapp/public/pages/sub/dashboard/index.html</htmlFile>
        <scriptId>other</scriptId>
      </htmlOutput> 
    <configuration>
  </execution>
</executions>

This will create 2 minified files

  • target/generated-resources/frontend/js/public/resources/main/script.min.js
  • target/generated-resources/frontend/js/public/resources/special/other/script.min.js

These files are put in target/classes/public/resources/main/script.min.js and target/classes/public/resources/special/other/script.min.js, respectively.

src/main/resources/webapp/public/pages/profile/index.html might look like

<html>
  <body>
    <script id="main"></script>
  </body>
</html>

The src attribute of the script with the ID main will be set to the script file.

The path of src/main/resources/webapp/public/pages/profile/index.html relative to the HTML root src/main/resources/webapp is public/pages/profile/index.html.

The path of target/generated-resources/frontend/js/public/resources/main/script.min.js relative to the script root target/generated-resources/frontend/js is public/resources/main/script.min.js.

Therefore, the path public/resources/main/script.min.js relative to public/pages/profile/index.html is ../../resources/main/script.min.js. Finally, the HTML is changed to

<html>
  <body>
    <script id="main" src="../../resources/main/script.min.js"></script>
  </body>
</html>

Similarly, for the second script file, the HTML file becomes:

<html>
  <body>
    <script id="other" src="../../../resources/special/other/script.min.js"></script>
  </body>
</html>

When no htmlRoot or scriptRoot is given, the folder of the real location of the files on the file system is used.

@grantsunny
Copy link

Thanks @blutorange for following this up! To share my 2 cents.
Most cases, when I am playing it with development environment, I will simply use

<script src="script.js"></script>

And after the build, we expect it help me automatically update the html into

<script src="script.min.js"></script>

Above design will satisfy this use case good. It is also a wise idea to leverage the script id to locate to the right <script> element - it would satisfy the case when we have 3rd party <script> elements in same HTML file.

However, this design of configuration might worthy second consideration when we wanted to deal with use case as below :), I would say it can be a relevant rare case, perhaps we can consider that later on.

<html>
  <body>
    <script id="main" src="../../resources/main/script.min.js"></script>
    <script id="other" src="../../../resources/special/other/script.min.js"></script>
  </body>
</html>

blutorange added a commit that referenced this issue Sep 15, 2024
blutorange added a commit that referenced this issue Sep 15, 2024
blutorange added a commit that referenced this issue Sep 15, 2024
blutorange added a commit that referenced this issue Sep 15, 2024
blutorange added a commit that referenced this issue Sep 15, 2024
Add new options for updating HTML files (#4, #70).
@blutorange
Copy link
Owner Author

blutorange commented Sep 15, 2024

@grantsunny

You can try out version 2.32.0-SNAPSHOT if you'd like. See the site for documentation on the new options, as well as the JavaDocs for HtmlUpdate and the Mojo goal docs. Let me know what you think -- I can still change some settings if it helps.

However, this design of configuration might worthy second consideration when we wanted to deal with use case as below :), I would say it can be a relevant rare case, perhaps we can consider that later on.

I'm not quite sure what your setup is, but the configuration should be flexible enough to achieve this -- you can use multiple executions, multiple htmlUpdates per execution, as well as update multiple HTML files and script tags per htmlUpdate.

@blutorange
Copy link
Owner Author

Any opinions on the pre-release snapsoht, @grantsunny ?

@grantsunny
Copy link

Thank you @blutorange - recently were coupled by several other things, will try this week or next week and back to here. I appreciate your great effort man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants