Grails: excluding a JS file from minification - grails

I read tons of documentation on asset-pipeline Grails plugin, but found no definite answers and I can not exclude a single stupid JS file from being minified.
Grails is in ver. 3.3.6 and assetPipelineVersion 2.14.1.
Goal:
Stop minifying grails-app/assets/javascripts/json-tree/json-tree.js.
What I tried:
I changed the assets block in build.gradle:
assets {
minifyJs = true
minifyCss = true
includes = []
excludes = [ '**/json-tree.js' ] // tried also with 'json-tree/json-tree.js'
}
I also added a section to my application.yml:
grails:
assets:
excludes: ['json-tree/*.js'] # also tried with **
Problem: the file still gets minified.
How can I solve the problem?
Also, how the assets pipeline should be configured: per build.gradle or application.yml or both?

Please try with following :
grails.assets.minifyOptions.excludes = ["json-tree/json-tree.js"]
To exclude single file you can try above otherwise you can exclude whole folder from minify
grails.assets.minifyOptions.excludes = ["json-tree/*.js"]
For more information you check documentation
Hope this will help you

Related

Rails, ESBuild source map is always missing, but it is in the build

ActionController::RoutingError (No route matches [GET] "/assets/application.js-1246371c75312771378dc97fa604ef404c73f9b477b5eb8f4c6ebb2fd2e1e323.map"):
My build script in package.json is:
"build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds",
So I see under assets, under builds, my application.js, and my application.js.map
But developer console shows 404 on the source map, and the server logs show that resource as an error. What am I doing wrong?
Corresponding to this thread
Setting config.assets.debug = false will solve that error
The config.assets.debug = false workaround has undesirable side effects. Missing debug log messages etc.
A cleaner solution is to add the following route definition to config/routes.rb:
if Rails.env.development?
redirector = ->(params, _) { ApplicationController.helpers.asset_path("#{params[:name].split('-').first}.map") }
constraint = ->(request) { request.path.ends_with?(".map") }
get "assets/*name", to: redirect(redirector), constraints: constraint
end
This resolves the sourcemaps for JS and CSS files and keeps asset debugging intact.

Getting Cesium to work with Webpacker and Rails

I'm trying to follow these instructions to add cesium to my rails project with Webpack, but I can't figure out how to translate the instructions to work with the rails implementation of Webpack.
For example:
In webpack.config.js, we add the following above our configuration object:
// The path to the Cesium source code
const cesiumSource = 'node_modules/cesium/Source';
const cesiumWorkers = '../Build/Cesium/Workers';
I assume in a rails project we would do our file imports in app/javascript/packs/application.js like this:
import 'cesium/Source';
import 'cesium/Build/Cesium/Workers';
but that gives the error:
Failed to compile.
./app/javascript/packs/application.js
Module not found: Error: Can't resolve 'cesium/Build/Cesium/Workers' in '/Users/user/Developer/appName/app/javascript/packs'
# ./app/javascript/packs/application.js 15:0-37
# multi (webpack)-dev-server/client?http://localhost:3035 ./app/javascript/packs/application.js
I've double checked, and the path is correct.
The Cesium instructions also indicate that I need to add some configuration options, but I can't figure out where I would put those since the rails webpacker gem doesn't have a webpack.config.js file. Do I just add config to the same file as the imports?
You would rather add a new config file which resolve your libs custom paths.
You can get inspiration from Webpacker documentation here:
https://github.com/rails/webpacker/blob/master/docs/webpack.md#configuration
You could do this :
//config/webpack/cesium.js
module.exports = {
resolve: {
alias: {
cesiumSource: 'cesium/Source',
cesiumWorkers: 'cesium/Build/Cesium/Workers'
}
}
}
Then merge the config options either in environment.js (global) or in development|test|production.js if this is specific to a particular environment
// config/webpack/environment.js
const { environment } = require('#rails/webpacker')
const cesiumConfig = require('./cesium')
environment.config.merge(cesiumConfig)
Hope this help.
Regards

getting grails 2.5.4 asset pipeline excludes to work

I am unable to get either the sass asset pipeline or the less asset pipeline plugin to work, if I am using sass or less imports.
If I only have one sass or less file, grail war works fine.
In my scenario however, file1.scss imports file2.scss, and in the end I only want to end up with a file1.css created from both. This should be possible according to this
https://grails.org/plugin/sass-asset-pipeline
But I am getting this output
| Minifying File 345 of 398 - file2.
| Error WAR packaging error: Stream closed
from grails war --stacktrace --verbose
Both files are in the same directory and have teh following contents:
file1.scss
#import "file2";
.brand {background-color:$mainColor;}
file2.scss
$mainColor: #277D85;
My build config has this:
plugins = {
...
compile ':asset-pipeline:2.5.7'
provided ":sass-asset-pipeline:2.9.1"
}
grails.assets.minifyJs = false
grails.assets.minifyCss = false
grails.assets.minifyOptions = [
optimizationLevel: 'WHITESPACE_ONLY'
]
grails.assets.excludes = ["**/file2.scss"]
What am I doing wrong?
How can I get more insights into what's going on here.
Your plugin block should remain in BuildConfig.groovy. However, the rest should be placed in Config.groovy

How do disable log4j plugin in grails?

It appears the Grails 2.1 log4j plugin resets the log4j configuration during initialization of the grails application (see stack trace below).
at org.apache.log4j.LogManager.resetConfiguration(LogManager.java:233)
at org.apache.log4j.LogManager$resetConfiguration.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
at org.codehaus.groovy.grails.plugins.log4j.Log4jConfig.initialize(Log4jConfig.groovy:66)
at org.codehaus.groovy.grails.plugins.log4j.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:48)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3910)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4389)
at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeployInternal(TomcatDeployment.java:313)
at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeploy(TomcatDeployment.java:145)
Is there any way to disable this "feature" or to remove this plugin altogether?
My JBoss server is already configured through jboss-log4j.xml and I do not want grails to make any changes to the configuration. I have already tried removing the log4j section of Config.groovy, but doing so had no effect.
As Kelly suggested, I have already removed all logging-related jars from my war file. Log4j classes are provided by JBoss.
EDIT I also tried the trick described in https://stackoverflow.com/a/1190438/539048 but that didn't seem to make any difference.
The solution was to remove the following section from the generated web.xml file:
<listener>
<listener-class>org.codehaus.groovy.grails.plugins.log4j.web.util.Log4jConfigListener</tag0:listener-class>
</listener>
To do so, I edited the scripts/Events.groovy file according to this blog but changed the listener class name to org.codehaus.groovy.grails.plugins.log4j.web.util.Log4jConfigListener.
eventWebXmlEnd = {String tmpfile ->
def root = new XmlSlurper().parse(webXmlFile)
def log4j = root.listener.findAll {node ->
node.'listener-class'.text() == 'org.codehaus.groovy.grails.plugins.log4j.web.util.Log4jConfigListener'
}
log4j.replaceNode {}
webXmlFile.text = new StreamingMarkupBuilder().bind {
mkp.declareNamespace("": "http://java.sun.com/xml/ns/j2ee")
mkp.yield(root)
}
}
Modify your BuildConfig.groovy like this:
inherits("global") {
excludes 'log4j', 'jcl-over-slf4j', 'slf4j-api', 'slf4j-log4j12'
}
This should remove all the logging libraries.
I tried the above suggestion on this grails application, so that I could expect to exclude the log4j dependencies of grails. However, after applying the suggestion, the jar files expected to be removed are still there in the generated war file. These jar files are: ./lib/grails-plugin-log4j-2.4.4.jar and ./lib/log4j-1.2.17.jar

Grails 2.1 disables log4j config on JBoss

We recently upgraded our app from Grails 1.3.7 to 2.1, which builds under maven, and made a bunch of changes to the pom file and other config- and build-related files of grails to get things working.
Here's the issue: when our JBoss server deploys our grails war file, something appears to cause log4j to be reconfigured such that the appserver stops all logging from that point forward (for any class). The app server is still running fine, but there is no logging after this line:
INFO [org.jboss.web.tomcat.service.deployers.TomcatDeployment] (main) deploy, ctxPath=/our-app
Removing our war file and restarting the server shows things logging correctly.
Our appserver has a server/default/conf/jboss-log4j.xml file in where we specify our cross-app log4j config.
I've tried removing log4j and slf4j jar files from our war, as that has caused issues in the past, but it hasn't solved the problem this time.
I also commented out the entire log4j section of grails-app/Config.groovy thinking that might be the culprit, but that had no effect.
I also tried removing the grails log4j plugin (by marking it as 'provided' scope in the pom), but that causes the following error during deployment:
ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/out-app]] (main) Error configuring application listener of class org.codehaus.groovy.grails.plugins.log4j.web.util.Log4jConfigListener
java.lang.ClassNotFoundException: org.codehaus.groovy.grails.plugins.log4j.web.util.Log4jConfigListener
Any idea what I might have missed?
An abbreviated listing of my war file is below:
WEB-INF/applicationContext.xml
WEB-INF/classes/BootStrap.class
WEB-INF/classes/BuildConfig.class
WEB-INF/classes/Config.class
WEB-INF/classes/DataSource.class
WEB-INF/classes/HibernateGrailsPlugin.class
WEB-INF/classes/UrlMappings.class
WEB-INF/classes/application.properties
WEB-INF/classes/gsp/views.properties
WEB-INF/classes/resources.class
WEB-INF/grails.xml
WEB-INF/jboss-web.xml
WEB-INF/lib/antlr-2.7.7.jar
WEB-INF/lib/aopalliance-1.0.jar
WEB-INF/lib/aspectjrt-1.6.10.jar
WEB-INF/lib/aspectjweaver-1.6.10.jar
WEB-INF/lib/cglib-2.2.jar
WEB-INF/lib/commons-beanutils-1.8.3.jar
WEB-INF/lib/commons-codec-1.5.jar
WEB-INF/lib/commons-collections-3.2.1.jar
WEB-INF/lib/commons-dbcp-1.4.jar
WEB-INF/lib/commons-el-1.0.jar
WEB-INF/lib/commons-fileupload-1.2.2.jar
WEB-INF/lib/commons-io-2.0.1.jar
WEB-INF/lib/commons-lang-2.6.jar
WEB-INF/lib/commons-pool-1.5.6.jar
WEB-INF/lib/commons-validator-1.3.1.jar
WEB-INF/lib/concurrentlinkedhashmap-lru-1.2_jdk5.jar
WEB-INF/lib/dom4j-1.6.1.jar
WEB-INF/lib/ehcache-core-2.4.6.jar
WEB-INF/lib/encryption-1.0.jar
WEB-INF/lib/ezmorph-1.0.6.jar
WEB-INF/lib/grails-bootstrap-2.1.0.jar
WEB-INF/lib/grails-core-2.1.0.jar
WEB-INF/lib/grails-crud-2.1.0.jar
WEB-INF/lib/grails-datastore-core-1.0.9.RELEASE.jar
WEB-INF/lib/grails-datastore-gorm-1.0.9.RELEASE.jar
WEB-INF/lib/grails-datastore-simple-1.0.9.RELEASE.jar
WEB-INF/lib/grails-hibernate-2.1.0.jar
WEB-INF/lib/grails-logging-2.1.0.jar
WEB-INF/lib/grails-plugin-codecs-2.1.0.jar
WEB-INF/lib/grails-plugin-controllers-2.1.0.jar
WEB-INF/lib/grails-plugin-converters-2.1.0.jar
WEB-INF/lib/grails-plugin-datasource-2.1.0.jar
WEB-INF/lib/grails-plugin-domain-class-2.1.0.jar
WEB-INF/lib/grails-plugin-filters-2.1.0.jar
WEB-INF/lib/grails-plugin-gsp-2.1.0.jar
WEB-INF/lib/grails-plugin-i18n-2.1.0.jar
WEB-INF/lib/grails-plugin-log4j-2.1.0.jar
WEB-INF/lib/grails-plugin-mimetypes-2.1.0.jar
WEB-INF/lib/grails-plugin-scaffolding-2.1.0.jar
WEB-INF/lib/grails-plugin-services-2.1.0.jar
WEB-INF/lib/grails-plugin-servlets-2.1.0.jar
WEB-INF/lib/grails-plugin-url-mappings-2.1.0.jar
WEB-INF/lib/grails-plugin-validation-2.1.0.jar
WEB-INF/lib/grails-resources-2.1.0.jar
WEB-INF/lib/grails-spring-2.1.0.jar
WEB-INF/lib/grails-web-2.1.0.jar
WEB-INF/lib/grails-webflow-2.1.0.jar
WEB-INF/lib/groovy-all-1.8.6.jar
WEB-INF/lib/hibernate-ehcache-3.6.10.Final.jar
WEB-INF/lib/hibernate-jpa-2.0-api-1.0.1.Final.jar
WEB-INF/lib/hibernate-validator-4.1.0.Final.jar
WEB-INF/lib/http-builder-0.5.0-RC3.jar
WEB-INF/lib/httpclient-4.0.jar
WEB-INF/lib/httpcore-4.0.1.jar
WEB-INF/lib/json-lib-2.3-jdk15.jar
WEB-INF/lib/jstl-1.1.2.jar
WEB-INF/lib/nekohtml-1.9.9.jar
WEB-INF/lib/oro-2.0.8.jar
WEB-INF/lib/sitemesh-2.4.jar
WEB-INF/lib/spring-aop-3.1.0.RELEASE.jar
WEB-INF/lib/spring-asm-3.1.0.RELEASE.jar
WEB-INF/lib/spring-aspects-3.1.0.RELEASE.jar
WEB-INF/lib/spring-beans-3.1.0.RELEASE.jar
WEB-INF/lib/spring-context-3.1.0.RELEASE.jar
WEB-INF/lib/spring-context-support-3.1.0.RELEASE.jar
WEB-INF/lib/spring-core-3.1.0.RELEASE.jar
WEB-INF/lib/spring-expression-3.1.0.RELEASE.jar
WEB-INF/lib/spring-jdbc-3.1.0.RELEASE.jar
WEB-INF/lib/spring-jms-3.1.0.RELEASE.jar
WEB-INF/lib/spring-orm-3.1.0.RELEASE.jar
WEB-INF/lib/spring-tx-3.1.0.RELEASE.jar
WEB-INF/lib/spring-web-3.1.0.RELEASE.jar
WEB-INF/lib/spring-webmvc-3.1.0.RELEASE.jar
WEB-INF/lib/util-io-1.2-SNAPSHOT.jar
WEB-INF/lib/utils-1.07.00.jar
WEB-INF/lib/validation-api-1.0.0.GA.jar
WEB-INF/lib/xml-resolver-1.2.jar
WEB-INF/lib/xpp3_min-1.1.4c.jar
WEB-INF/lib/zdecimal-3.2.jar
WEB-INF/plugins/hibernate-2.1.0/grails-app/i18n/messages.properties
WEB-INF/plugins/hibernate-2.1.0/plugin.xml
WEB-INF/sitemesh.xml
WEB-INF/web.xml
And Config.groovy (with app-specific stuff removed):
grails.project.groupId = appName // change this to alter the default package name and Maven publishing destination
grails.mime.file.extensions = true // enables the parsing of file extensions from URLs into the request format
grails.mime.use.accept.header = false
grails.mime.types = [
all: '*/*',
atom: 'application/atom+xml',
css: 'text/css',
csv: 'text/csv',
form: 'application/x-www-form-urlencoded',
html: ['text/html','application/xhtml+xml'],
js: 'text/javascript',
json: ['application/json', 'text/json'],
multipartForm: 'multipart/form-data',
rss: 'application/rss+xml',
text: 'text/plain',
xml: ['text/xml', 'application/xml']
]
grails.resources.adhoc.patterns = ['/images/*', '/css/*', '/js/*', '/plugins/*']
grails.views.default.codec = "none" // none, html, base64
grails.views.gsp.encoding = "UTF-8"
grails.converters.encoding = "UTF-8"
grails.views.gsp.sitemesh.preprocess = true
grails.scaffolding.templates.domainSuffix = 'Instance'
grails.json.legacy.builder = false
grails.enable.native2ascii = true
grails.logging.jul.usebridge = true
grails.spring.bean.packages = []
grails.hibernate.cache.queries = false
environments {
production {
grails.logging.jul.usebridge = false
}
development {
grails.logging.jul.usebridge = true
}
test {
// shouldn't be using this environment
}
}
Through some debugging breakpoints, I was able to determine that the Grails log4j plugin is the culprit. It appears to be resetting the global log4j config on startup.
I'll ask a separate question about how to disable this.
see How do disable log4j plugin in grails?

Resources