I am building angular project with grunt and let's assume that i have follow structure:
-- app.js
-- modal
----createTemplate
------createTemplate.js
----modal.js
and createTemplate.js implements controller for module, declared in modal.js, for example:
modal.js
angular.module('modal', [])
.controller('ModalInstanceCtrl', function($scope) {
//some stuff
});
createTemplate.js
angular.module('modal')
.controller('CreateTemplateCtrl', function($scope) {
//some stuff
});
and i have Grunt task, that builds index.html:
index: {
build: {
dir: '<%= build_dir %>',
src: [
'<%= build_dir %>/src/**/*.js'
]
}
}
So, when i build index.html, i get follow order of files:
<script type="text/javascript" src="src/app/home/modal/createTemplate/createTemplate.js"></script>
<script type="text/javascript" src="src/app/home/modal/modal.js"></script>
What leads to error Module 'modal' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument., because declaration of module is happened after call to it.
So, how should i edit gruntfile to add files not in alphabetcal order, but in order of breadth-first-search?
It is always better to use separate files for module definition, and call it like 'modal-module.js'. After that you can easily inject module-files first, with this parameters:
index: {
build: {
dir: '<%= build_dir %>',
src: [
'<%= build_dir %>/app/app.js',
'<%= build_dir %>/app/**/*module.js',
'<%= build_dir %>/app/**/*constants.js',
'<%= build_dir %>/app/**/*provider.js',
'<%= build_dir %>/app/**/*enum.js',
'<%= build_dir %>/app/**/*model.js',
'<%= build_dir %>/app/**/*config.js',
'<%= build_dir %>/app/**/*filter.js',
'<%= build_dir %>/app/**/*directive.js',
'<%= build_dir %>/app/**/*decorator.js',
'<%= build_dir %>/app/**/*interceptor.js',
'<%= build_dir %>/app/**/*service.js',
'<%= build_dir %>/app/**/*workflow.js',
'<%= build_dir %>/app/**/*repository.js',
'<%= build_dir %>/app/**/*resolver.js',
'<%= build_dir %>/app/**/*controller.js'
]
}
}
Related
So I have started yo angular for a new project and it's great, the only issue is that I like to structure my views and have partials etc, for example a great structure for me is something like the following:
app/views/_partials/modals
app/views/_partials/menus
app/views/_partials/buttons
app/views/index.html
app/views/about.html
However, it appears that the angular generator doesn't look in sub directories when building into the dist folder and I get all sorts of missing includes and errors.
Can I change Grunt to look into these sub directories and process the views accordingly so that they appear within my app after building?
I located an interesting block that I think could be responsible in my Grunt file:
ngtemplates: {
dist: {
options: {
module: 'myApp',
htmlmin: '<%= htmlmin.dist.options %>',
usemin: 'scripts/scripts.js'
},
cwd: '<%= yeoman.app %>',
src: 'views/{,*/}*.html',
dest: '.tmp/templateCache.js'
}
},
Or is it something in the usemin block? I'm confused (relatively new to Angular):
usemin: {
html: ['<%= yeoman.dist %>/{,*/}*.html'],
css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
js: ['<%= yeoman.dist %>/scripts/{,*/}*.js'],
options: {
assetsDirs: [
'<%= yeoman.dist %>',
'<%= yeoman.dist %>/images',
'<%= yeoman.dist %>/styles'
],
patterns: {
js: [[/(images\/[^''""]*\.(png|jpg|jpeg|gif|webp|svg))/g, 'Replacing references to images']]
}
}
},
Ok, so I figured this one out, the issue was in the ngtemplates block and my code now looks like this:
ngtemplates: {
dist: {
options: {
module: 'myApp',
htmlmin: '<%= htmlmin.dist.options %>',
usemin: 'scripts/scripts.js'
},
cwd: '<%= yeoman.app %>',
src: 'views/**/**/*.html',
dest: '.tmp/templateCache.js'
}
},
Notice the src attribute has changed from:
src: 'views/{,*/}*.html',
to:
src: 'views/**/**/*.html',
This seems to capture all my templates within the _partials directories
I would like to be able to duplicate ng-boilerplate's script compilation process in my own app, but I am unable to get it working.
I am referring to having this line in index.html be compiled into a list of all the scripts in my app:
<!-- compiled JavaScript --><% scripts.forEach( function ( file ) { %>
<script type="text/javascript" src="<%= file %>"></script><% }); %>
I added the filterForJS function and registered the multi task in my gruntfile.js, but it is not working :(
Have you added the options for the index task?
```
/**
* The `index` task compiles the `index.html` file as a Grunt template. CSS
* and JS files co-exist here but they get split apart later.
*/
index: {
/**
* During development, we don't want to have wait for compilation,
* concatenation, minification, etc. So to avoid these steps, we simply
* add all script files directly to the `<head>` of `index.html`. The
* `src` property contains the list of included files.
*/
build: {
dir: '<%= build_dir %>',
src: [
'<%= vendor_files.js %>',
'<%= build_dir %>/src/**/*.js',
'<%= html2js.common.dest %>',
'<%= html2js.app.dest %>',
'<%= vendor_files.css %>',
'<%= recess.build.dest %>'
]
},
/**
* When it is time to have a completely compiled application, we can
* alter the above to include only a single JavaScript and a single CSS
* file. Now we're back!
*/
compile: {
dir: '<%= compile_dir %>',
src: [
'<%= concat.compile_js.dest %>',
'<%= vendor_files.css %>',
'<%= recess.compile.dest %>'
]
}
},
```
My Grunt file looks like:
"use strict"
module.exports = (grunt) ->
require("load-grunt-tasks") grunt
# Project configuration.
grunt.initConfig
appConfig:
# configurable paths
app: "app"
server: "server"
dist: "dist"
# ngMin
ngmin:
dist:
files: [
cwd: '<%= appConfig.app %>/scripts'
src: ['**/*.js']
dest: ['<%= appConfig.dist %>/scripts']
]
But when I run it, I get:
Running "ngmin:dist" (ngmin) task
Warning: Arguments to path.join must be strings Use --force to continue.
What am I doing wrong?
The destination dest should be a single folder, not a list:
dest: '<%= appConfig.dist() %>/scripts'
Not:
dest: [ '<%= appConfig.dist() %>/scripts' ]
I'm using a yeoman web app template that runs an AngularJS app.
usemin is great, but AngularJS files must be pre-processed with ngmin to be annotated before being uglified and minified by usemin.
I can produce a single, concatenated file of annotated AngularJS controllers, directives, services, etc. using ngmin.
ngmin: {
build: {
src: ['<%= yeoman.app %>/js/one.js','<%= yeoman.app %>/js/two.js'],
dest: '<%= yeoman.dist %>/ng/annotated.js'
}
}
Basically I run a build that successfully created annotated.js.
What I want is declare annotated.js file in index.html to inform usemin to use that file only for distribution, not the ones I mention for development.
<!-- build:js js/app.min.js --> //annotated.js only must be processed instead of those 2 files.
<script src="js/one.js" type="text/javascript"></script>
<script src="js/two.js" type="text/javascript"></script>
<!-- endbuild -->
For reference, my usemin config:
useminPrepare: {
options: {
dest: '<%= yeoman.dist %>'
},
html: '<%= yeoman.app %>/index.html'
},
usemin: {
options: {
dirs: ['<%= yeoman.dist %>']
},
html: ['<%= yeoman.dist %>/{,*/}*.html'],
css: ['<%= yeoman.dist %>/css/{,*/}*.css']
}
For reference, my build task:
grunt.registerTask('build', [
'clean:dist',
'useminPrepare',
'concat',
'cssmin',
'ngmin',
'uglify',
'copy:dist',
'rev',
'usemin'
]);
How can I achieve it?
So, you used yo webapp to generator an application that you then plugged Angular in to? For future Angular apps you create (or if it's not too late for this one), there's a generator for that! https://github.com/yeoman/generator-angular
Here is a link and some code that shows how the ngmin task is pre-configured with the Angular generator:
https://github.com/yeoman/generator-angular/blob/v0.4.0/templates/common/Gruntfile.js#L326-335
ngmin: {
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.dist %>/scripts',
src: '*.js',
dest: '<%= yeoman.dist %>/scripts'
}]
}
}
Plug that in, tweak as necessary, and let me know if it works!
When I grunt build my AngularJS app grunt-rev has a problem dealing with the fonts. It will create the font folders just fine but does not place any fonts in them. Here is what it shows me.
Running "rev:dist" (rev) task
dist/scripts/scripts.js >> a3e641ec.scripts.js
dist/styles/main.css >> 970b8797.main.css
Warning: Unable to read "dist/fonts/Aller" file (Error code: EISDIR). Use --force to continue.
grunt-rev in my Gruntfile.js
rev: {
dist: {
files: {
src: [
'<%= yeoman.dist %>/scripts/{,*/}*.js',
'<%= yeoman.dist %>/styles/{,*/}*.css',
'<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',
'<%= yeoman.dist %>/styles/fonts/*'
]
}
}
}
How to fix this?
I finally solved this problem by using the double asterisk ** method to match all the sub directories recursively. I don't know why that method was not used in my original Gruntfile.js. Instead it used the {,*/}* which did not behave recursively for some reason. So here is my new rev object in my Gruntfile.js
rev: {
dist: {
files: {
src: [
'<%= yeoman.dist %>/scripts/**/*.js',
'<%= yeoman.dist %>/styles/**/*.css',
'<%= yeoman.dist %>/images/**/*.{png,jpg,jpeg,gif,webp,svg}',
'<%= yeoman.dist %>/fonts/**/*.ttf'
]
}
}
},
I was having the same problem. They have a bug about it in version 0.1.0.
A workaround is to specify a file extension.
Before:
rev: {
dist: {
files: [{
expand: true,
cwd: '<%= config.dist %>/',
src: [
'images/**/*'
]
}]
}
}
After:
rev: {
dist: {
files: [{
expand: true,
cwd: '<%= config.dist %>/',
src: [
'images/**/*.{gif,jpeg,jpg,png}'
]
}]
}
}