From d974ed43474a09fe1cae81998e85fc78fed7c7ab Mon Sep 17 00:00:00 2001 From: cueedee Date: Mon, 1 Aug 2016 16:50:35 +0200 Subject: [PATCH] feat(app generator): add option to let app load jQuery through a CDN --- generators/app/index.js | 6 ++ generators/app/templates/@Gruntfile.coffee | 66 ++++++++++++++----- generators/app/templates/@package.json | 6 +- generators/app/templates/src/app.coffee | 5 +- .../app/templates/src/index.template.html | 9 +-- .../app/templates/test/unit/init.coffee | 25 +++++++ 6 files changed, 94 insertions(+), 23 deletions(-) diff --git a/generators/app/index.js b/generators/app/index.js index 259f3135..99da5b56 100644 --- a/generators/app/index.js +++ b/generators/app/index.js @@ -234,6 +234,12 @@ var AppGenerator = generators.Base.extend( return answers.i18n || this.templateData.i18n; }.bind( this ) } + , { + type: 'confirm' + , name: 'jqueryCdn' + , message: 'Would you like your app to load jQuery from a CDN (googleapis.com) instead of bundling it?' + , default: false + } , { type: 'confirm' , name: 'ie8' diff --git a/generators/app/templates/@Gruntfile.coffee b/generators/app/templates/@Gruntfile.coffee index 79c6ceb5..faf0a1e7 100644 --- a/generators/app/templates/@Gruntfile.coffee +++ b/generators/app/templates/@Gruntfile.coffee @@ -135,8 +135,9 @@ ## ==== ## -path = require( 'path' ) -_ = require( 'underscore' ) +child_process = require( 'child_process' ) +path = require( 'path' ) +_ = require( 'underscore' ) module.exports = ( grunt ) -> @@ -147,11 +148,13 @@ module.exports = ( grunt ) -> ## ------------------------------------------------ ## - ## Contents of npm's 'package.json' file as '<%= npm.* %>' + ## Contents of npm's 'package.json' file as '<%= npm.pkg.* %>' + ## Installed dependencies of npm's 'package.json' file as '<%= npm.installed.* %>' ## npm: - grunt.file.readJSON( 'package.json' ) + pkg: grunt.file.readJSON( 'package.json' ) + installed: JSON.parse( child_process.execSync( 'npm ls --json --prod --depth 0 --silent' )).dependencies ## @@ -173,7 +176,7 @@ module.exports = ( grunt ) -> settings: 'settings/' test: 'test/' - artifactBase: '<%= build.dist %><%= npm.name %>-<%= npm.version %>' + artifactBase: '<%= build.dist %><%= npm.pkg.name %>-<%= npm.pkg.version %>' ## ## This is the default build environment but may be overridden by the 'environment' task @@ -196,9 +199,9 @@ module.exports = ( grunt ) -> lint: '<%= build.source %>**/*.coffee' - ## NOTE: <%= npm.main %> should have <%= build.dist %> as its prefix: + ## NOTE: <%= npm.pkg.main %> should have <%= build.dist %> as its prefix: ## - tgt: '<%= npm.main %>' + tgt: '<%= npm.pkg.main %>' brief: tgt: '<%= build.assembly.app %>build.json' @@ -334,7 +337,16 @@ module.exports = ( grunt ) -> noParse: [ 'jquery' ] - ) + )<@ if ( jqueryCdn ) { @> + + ## Do not include `jquery` in the output bundle. It is an `npm install`ed dependency, and `require()`d, chiefly, by `Backbone`. + ## Instead, a `<% }} %> -
+
<@ if ( jqueryCdn ) { @> + <@ } @> diff --git a/generators/app/templates/test/unit/init.coffee b/generators/app/templates/test/unit/init.coffee index a76550c9..bcde3368 100644 --- a/generators/app/templates/test/unit/init.coffee +++ b/generators/app/templates/test/unit/init.coffee @@ -1,4 +1,29 @@ +<% if ( jqueryCdn ) { %> +## ============================================================================ +## +## [jQuery] +## +## http://api.jquery.com/ +## + +$ = require( 'jquery' ) + +## ============================================================================ +## +## [Backbone] +## +## http://backbonejs.org/ +## + +Backbone = require( 'backbone' ) + +## Expose jQuery to Backbone. +## Needed because Backbone's jquery dependency will not be bundled with the build distribution artifact. +## +Backbone.$ = $ + +<% } %> ## ============================================================================ ## ## [madlib]