From 5b97c8a88847c8326e4dc9fae2c7ee442e659f57 Mon Sep 17 00:00:00 2001 From: webmustang Date: Thu, 5 Dec 2019 17:50:07 +0200 Subject: [PATCH] Asset pipeline issue demo --- build.gradle | 6 ++- grails-app/assets/javascripts/application.es6 | 12 ++++++ grails-app/views/layouts/main.gsp | 2 +- .../groovy/demo/asset/es6/Es6AssetFile.groovy | 39 ++++++++++++++++++ .../es6/FastBabelJsProcessedContainer.groovy | 13 ++++++ .../asset/es6/FastBabelJsProcessor.groovy | 24 +++++++++++ .../es6/FastBabelJsProcessorCache.groovy | 40 +++++++++++++++++++ .../META-INF/asset-pipeline/asset.specs | 1 + 8 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 grails-app/assets/javascripts/application.es6 create mode 100644 src/main/groovy/demo/asset/es6/Es6AssetFile.groovy create mode 100644 src/main/groovy/demo/asset/es6/FastBabelJsProcessedContainer.groovy create mode 100644 src/main/groovy/demo/asset/es6/FastBabelJsProcessor.groovy create mode 100644 src/main/groovy/demo/asset/es6/FastBabelJsProcessorCache.groovy create mode 100644 src/main/resources/META-INF/asset-pipeline/asset.specs diff --git a/build.gradle b/build.gradle index 89b9a17..b28cb83 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,8 @@ buildscript { dependencies { classpath "org.grails:grails-gradle-plugin:$grailsVersion" classpath "org.grails.plugins:hibernate5:${gormVersion-".RELEASE"}" - classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.15.1" + classpath "com.bertramlabs.plugins:asset-pipeline-gradle:3.0.11" + classpath 'com.bertramlabs.plugins:sass-asset-pipeline:3.0.11' } } @@ -51,7 +52,8 @@ dependencies { runtime "org.glassfish.web:el-impl:2.1.2-b03" runtime "com.h2database:h2" runtime "org.apache.tomcat:tomcat-jdbc" - runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.15.1" + runtime "com.bertramlabs.plugins:asset-pipeline-grails:3.0.11" + assets 'com.bertramlabs.plugins:sass-asset-pipeline:3.0.11' testCompile "org.grails:grails-gorm-testing-support" testCompile "org.grails.plugins:geb" testCompile "org.grails:grails-web-testing-support" diff --git a/grails-app/assets/javascripts/application.es6 b/grails-app/assets/javascripts/application.es6 new file mode 100644 index 0000000..178dbd2 --- /dev/null +++ b/grails-app/assets/javascripts/application.es6 @@ -0,0 +1,12 @@ +// This is a manifest file that'll be compiled into application.js. +// +// Any JavaScript file within this directory can be referenced here using a relative path. +// +// You're free to add application-wide JavaScript to this file, but it's generally better +// to create separate JavaScript files as needed. +// +//= require jquery-3.3.1.min +import './bootstrap' +//= require popper.min +//= require_self + diff --git a/grails-app/views/layouts/main.gsp b/grails-app/views/layouts/main.gsp index 6f3d601..a771ef2 100644 --- a/grails-app/views/layouts/main.gsp +++ b/grails-app/views/layouts/main.gsp @@ -64,7 +64,7 @@ - + diff --git a/src/main/groovy/demo/asset/es6/Es6AssetFile.groovy b/src/main/groovy/demo/asset/es6/Es6AssetFile.groovy new file mode 100644 index 0000000..f65cc49 --- /dev/null +++ b/src/main/groovy/demo/asset/es6/Es6AssetFile.groovy @@ -0,0 +1,39 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package demo.asset.es6 + +import asset.pipeline.AbstractAssetFile +import asset.pipeline.processors.JsNodeInjectProcessor +import asset.pipeline.processors.JsProcessor +import asset.pipeline.processors.JsRequireProcessor +import groovy.transform.CompileStatic + +import java.util.regex.Pattern + +/** + * An {@link asset.pipeline.AssetFile} implementation for ES6 Javascript + * + * @author Roman Chychyna + */ +@CompileStatic +class Es6AssetFile extends AbstractAssetFile { + static final List contentType = ['application/javascript', 'application/x-javascript', 'text/javascript'] + static List extensions = ['es6', 'es7', 'es8', 'es'] + static String compiledExtension = 'js' + static processors = [JsProcessor, JsNodeInjectProcessor, FastBabelJsProcessor, JsRequireProcessor] + Pattern directivePattern = ~/(?m)^\/\/=(.*)/ +} diff --git a/src/main/groovy/demo/asset/es6/FastBabelJsProcessedContainer.groovy b/src/main/groovy/demo/asset/es6/FastBabelJsProcessedContainer.groovy new file mode 100644 index 0000000..bcd100f --- /dev/null +++ b/src/main/groovy/demo/asset/es6/FastBabelJsProcessedContainer.groovy @@ -0,0 +1,13 @@ +package demo.asset.es6 + +class FastBabelJsProcessedContainer { + + String value + + String input + + FastBabelJsProcessedContainer(String value, String input) { + this.value = value + this.input = input + } +} diff --git a/src/main/groovy/demo/asset/es6/FastBabelJsProcessor.groovy b/src/main/groovy/demo/asset/es6/FastBabelJsProcessor.groovy new file mode 100644 index 0000000..71d60ff --- /dev/null +++ b/src/main/groovy/demo/asset/es6/FastBabelJsProcessor.groovy @@ -0,0 +1,24 @@ +package demo.asset.es6 + +import asset.pipeline.AssetCompiler +import asset.pipeline.AssetFile +import asset.pipeline.processors.BabelJsProcessor +import groovy.util.logging.Commons + +@Commons +class FastBabelJsProcessor extends BabelJsProcessor { + FastBabelJsProcessor(AssetCompiler precompiler) { + super(precompiler) + } + + @Override + String process(String input, AssetFile assetFile) { + def start = System.currentTimeMillis() + def cache = FastBabelJsProcessorCache.getInstance() + if (!cache.has(assetFile.path) || cache.isChanged(assetFile.path, input)) { + cache.set(assetFile.path, input, super.process(input, assetFile)) + } + log.debug("Duration: " + (System.currentTimeMillis() - start) + "\tFile: " + assetFile.name + "") + return cache.get(assetFile.path) + } +} diff --git a/src/main/groovy/demo/asset/es6/FastBabelJsProcessorCache.groovy b/src/main/groovy/demo/asset/es6/FastBabelJsProcessorCache.groovy new file mode 100644 index 0000000..8d68e92 --- /dev/null +++ b/src/main/groovy/demo/asset/es6/FastBabelJsProcessorCache.groovy @@ -0,0 +1,40 @@ +package demo.asset.es6 + +class FastBabelJsProcessorCache { + + private Map cache + + static FastBabelJsProcessorCache instance + + static FastBabelJsProcessorCache getInstance() { + if (!instance) { + instance = new FastBabelJsProcessorCache() + } + + return instance + } + + boolean has(String key) { + return getCache().containsKey(key) + } + + boolean isChanged(String key, String input) { + getCache().get(key)?.input != input + } + + void set(String key, String input, String value) { + getCache().put(key, new FastBabelJsProcessedContainer(value, input)) + } + + String get(String key) { + getCache().get(key).value + } + + private Map getCache() { + if (!cache) { + cache = new HashMap() + } + + cache + } +} diff --git a/src/main/resources/META-INF/asset-pipeline/asset.specs b/src/main/resources/META-INF/asset-pipeline/asset.specs new file mode 100644 index 0000000..df912b3 --- /dev/null +++ b/src/main/resources/META-INF/asset-pipeline/asset.specs @@ -0,0 +1 @@ +demo.asset.es6.Es6AssetFile \ No newline at end of file