diff --git a/CHANGES.rst b/CHANGES.rst index d4e9a8add5..d97c92ebc3 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -118,6 +118,9 @@ New features: - Resource Registry: If file system version is newer than ``last_compilation`` date of a bundle, use this as ``last_compilation`` date. [jensens] +- Simplify generated Gruntfile.js (DRY) + [jensens] + Bug fixes: diff --git a/Products/CMFPlone/_scripts/_generate_gruntfile.py b/Products/CMFPlone/_scripts/_generate_gruntfile.py index de55bed4dc..b9587cec6d 100644 --- a/Products/CMFPlone/_scripts/_generate_gruntfile.py +++ b/Products/CMFPlone/_scripts/_generate_gruntfile.py @@ -40,11 +40,14 @@ IResourceRegistry, prefix="plone.resources", check=False) # noqa lessvariables = registry.records['plone.lessvariables'].value -GRUNTFILE_TEMPLATE = """ +GRUNTFILE_TEMPLATE = """// This file is generated by "plone-compile-resources" + module.exports = function(grunt) {{ - 'use strict'; + "use strict"; + var lessPaths = {lesspaths}, + modifyVars = {modifyvars}; grunt.initConfig({{ - pkg: grunt.file.readJSON('package.json'), + pkg: grunt.file.readJSON("package.json"), less: {{ {less} }}, @@ -60,19 +63,19 @@ watch: {{ scripts: {{ files: {files}, - tasks: ['requirejs', 'less', 'sed', 'uglify'] + tasks: ["requirejs", "less", "sed", "uglify"] }} }} }}); - grunt.loadNpmTasks('grunt-contrib-watch'); - grunt.loadNpmTasks('grunt-contrib-requirejs'); - grunt.loadNpmTasks('grunt-contrib-less'); - grunt.loadNpmTasks('grunt-contrib-uglify'); - grunt.loadNpmTasks('grunt-sed'); + grunt.loadNpmTasks("grunt-contrib-watch"); + grunt.loadNpmTasks("grunt-contrib-requirejs"); + grunt.loadNpmTasks("grunt-contrib-less"); + grunt.loadNpmTasks("grunt-contrib-uglify"); + grunt.loadNpmTasks("grunt-sed"); - grunt.registerTask('default', ['watch']); - grunt.registerTask('compile-all', ['requirejs', 'less', 'sed', 'uglify']); + grunt.registerTask("default", ["watch"]); + grunt.registerTask("compile-all", ["requirejs", "less", "sed", "uglify"]); {bundleTasks} }} @@ -80,24 +83,24 @@ SED_CONFIG_TEMPLATE = """ {name}: {{ - path: '{path}', - pattern: '{pattern}', - replacement: '{destination}', + path: "{path}", + pattern: "{pattern}", + replacement: "{destination}", }}, """ REQUIREJS_CONFIG_TEMPLATE = """ "{bkey}": {{ options: {{ - baseUrl: '/', + baseUrl: "/", generateSourceMaps: false, preserveLicenseComments: false, paths: {paths}, shim: {shims}, wrapShim: true, - name: '{name}', - exclude: ['jquery'], - out: '{out}', + name: "{name}", + exclude: ["jquery"], + out: "{out}", optimize: "none" }} }}, @@ -106,11 +109,11 @@ "{bkey}": {{ options: {{ sourceMap: true, - sourceMapName: '{destination}.map', + sourceMapName: "{destination}.map", sourceMapIncludeSources: false }}, files: {{ - '{destination}': {files} + "{destination}": {files} }} }}, """ @@ -130,17 +133,17 @@ sourceMapBasepath: "{base_path}", relativeUrls: true, plugins: [ - new require('less-plugin-inline-urls'), + new require("less-plugin-inline-urls"), ], - paths: {less_paths}, - modifyVars: {{ -{globalVars} - }} + paths: lessPaths, + modifyVars: modifyVars }} - }} + }}\ """ -COMPILE_TASK_TEMPLATE = " grunt.registerTask('compile-{name}', {tasks})\n" +COMPILE_TASK_TEMPLATE = """\ + grunt.registerTask("compile-{name}", {tasks})\n\ +""" def resource_to_dir(resource, file_type='.js'): @@ -187,6 +190,7 @@ def resource_to_dir(resource, file_type='.js'): # REQUIRE JS CONFIGURATION + paths = {} shims = {} for requirejs, script in resources.items(): @@ -238,13 +242,13 @@ def resource_to_dir(resource, file_type='.js'): # LESS CONFIGURATION -globalVars = {} -globalVars["sitePath"] = "'/'" -globalVars["isPlone"] = "false" -globalVars["isMockup"] = "false" -globalVars['staticPath'] = "'" + os.path.join( +modify_vars = {} +modify_vars['sitePath'] = '/' +modify_vars['isPlone'] = 'false' +modify_vars['isMockup'] = 'false' +modify_vars['staticPath'] = '\'' + os.path.join( os.path.dirname(CMFPlone.__file__), - 'static') + "'" + 'static') + '\'' less_vars_params = { 'site_url': 'LOCAL', @@ -264,12 +268,12 @@ def resource_to_dir(resource, file_type='.js'): if t_object: t_file = resource_to_dir(t_object) t_file = t_file.replace(os.getcwd() + '/', '') - globalVars[name] = "'%s/'" % t_file + modify_vars[name] = "'%s/'" % t_file else: print "No file found: " + \ str(t.replace('LOCAL/', '').replace('\\"', '')) # else: - globalVars[name] = t + modify_vars[name] = t # Path to search for less less_paths = [] @@ -312,8 +316,8 @@ def resource_to_dir(resource, file_type='.js'): # relative = '' # for i in range(len(local_file.split('/'))): # relative += '../' - # globalVars[name.replace('.', '_')] = "'%s'" % local_file # noqa - globalVars[name.replace('.', '_')] = "'{0}'".format( + # modify_vars[name.replace('.', '_')] = "'%s'" % local_file # noqa + modify_vars[name.replace('.', '_')] = "'{0}'".format( local_file.split('/')[-1] ) if '/'.join(local_file.split('/')[:-1]) not in less_paths: @@ -321,21 +325,16 @@ def resource_to_dir(resource, file_type='.js'): else: print "No file found: " + css -globalVars_string = "" -for key, value in sorted(globalVars.items()): - globalVars_string += '{0}"{1}": "{2}",\n'.format(22 * ' ', key, value) - - # BUNDLE LOOP -require_configs = "" -uglify_cfgs_final = "" +require_configs = '' +uglify_cfgs_final = '' less_cfgs_final = [] -sourceMap_url = "" -sed_cfg_final = "" +sourceMap_url = '' +sed_cfg_final = '' watch_files = [] sed_count = 0 -bundle_grunt_tasks = "" +bundle_grunt_tasks = '' for bkey, bundle in bundles.items(): @@ -444,9 +443,7 @@ def resource_to_dir(resource, file_type='.js'): if less_files: less_cfgs_final.append(LESS_CONFIG_TEMPLATE.format( name=bkey, - globalVars=globalVars_string, files=json.dumps(less_files), - less_paths=json.dumps(sorted(less_paths), indent=22), sourcemap_url=sourceMap_url, base_path=os.getcwd())) @@ -487,6 +484,8 @@ def resource_to_dir(resource, file_type='.js'): gruntfile.write( GRUNTFILE_TEMPLATE.format( less=','.join(less_cfgs_final), + lesspaths=json.dumps(sorted(less_paths), indent=12), + modifyvars=json.dumps(modify_vars, indent=12, sort_keys=True), requirejs=require_configs, uglify=uglify_cfgs_final, sed=sed_cfg_final,