npm npm_folder gruntfile relative root path specification - javascript

I want ot ser my node operations in lib folder and my project structure is like this,
src
lib
node_modules
package.json
Gruntfile.js
doc
So I am using concat task,
module.exports = function(grunt){
grunt.initConfig({
concat: {
dist: {
src: ['../src/js/controller.js', '../src/js/service.js'],
dest: '../dist/js/built.js',
},
},
});
grunt.loadNpmTasks('grunt-contrib-concat');
}
This wokrs and creates built.js file in dist folder. But I should use "../" all paths and I will have so many configuration. Can I set it as a root path only one time in Gruntfile.js. Is there any settings like {root:"../"} this?

Related

Source map to files outside public directory

I have a file structure like this:
/server/
- package.json
- node_modules/
- Gruntfile.js
/public/
- index.html
- assets/
When in /server I run npm install, which downloads things like Angular into /server/node_modules.
I then run the Gruntfile.js which looks like this:
require('load-grunt-tasks')(grunt);
grunt.initConfig({
//concat js
concat: {
options: {
separator: ';',
sourceMap: true
},
lib: {
src: [
'./node_modules/angular/angular.js',
'./node_modules/angular-route/angular-route.js',
'./node_modules/angular-animate/angular-animate.js'
],
dest: '../public/assets/build/lib/lib.min.js'
}
},
//minify js
uglify: {
options: {
mange: true,
compress: true,
sourceMap: true
},
lib: {
src: '<%= concat.lib.dest %>',
dest: '<%= concat.lib.dest %>'
}
}
});
grunt.registerTask('build', ['concat', 'uglify']);
The grunt file works as expected to concat and minify the Angular modules with a source map. The problem is that the domain name points to the public directory, but the source map wants to point to the Angular source code in the server directory, which I don't think will work.
How do I get around this problem? Do I have to have the node_modules in the root of the public directory? I'd rather not have any precompiled code in the public directory if possible, the reason being that when I put the site live, I can simply FTP the public directory and ignore the server, reducing unnecessary bulk.

grunt-email-workflow Grunt tasks?

I'm using Lee Munroe's grunt-email-workflow to build out a set of email templates but I am not able to find where are the tasks configured.
When I run "grunt" from the terminal I can see that few tasks are executed:
-clean
-sass
-assemble pages
-juice
etc.
But the Gruntfile.js does not contain anything a part from:
module.exports = function(grunt) {
require('load-grunt-config')(grunt, {
// Pass data to tasks
data: {
// Re-usable filesystem path variables
paths: {
src: 'src',
src_img: 'src/img',
dist: 'dist',
dist_img: 'dist/img',
preview: 'preview'
},
// secrets.json is ignored in git because it contains sensitive data
// See the README for configuration settings
secrets: grunt.file.readJSON('secrets.json')
}
});
};
Am I missing something?
load-grunt-config is auto-loading the required grunt modules located in the package.json of the project. This is where clean (grunt-contrib-clean) sass (grunt-sass) etc are coming from.
This File determines which tasks are run by grunt

Configure grunt.js to minify files one by one in bower folder

I have the dependencies of the application in bower_components, some of the dependencies don't have a minified version so I'd like to create a task creates a minified copy of the file version in the same place where the file is located like:
bower_components
lib1
lib1.js
lib1.min.js <- create this file if doesn't exist
lib2
lib2.js
lib2.min.js <- create this file in it's own lib2 folder
lib3
lib3.js
lib3.min.js <- and so on...
This is my grunt Config so far:
uglify: {
dev: {
files:[
{
expand: true,
src: 'bower_components/modernizr/modernizr.js',
dest: '/',
ext:'.min.js'
}, {
expand: true,
src: 'bower_components/angular-facebook/lin/angular-facebook.js',
dest: '/',
ext: '.min.js'
}]
},
main: {
src: 'temp/app.min.js',
dest:'dist/app.min.js'
}
}
the Grunt task says that copied modernizr to it's own folder but when I look at it, the file is not there and after the first file Grunt passes to the next task and ignores the 'second' file in the array.
I was just testing this obviously I'd like to implement a way that grunt scan all the dependencies in bower_components automatically.
btw, I don't mind to change the task to any other library.
the / in your dest-option means the root path (were your gruntfile resides). just delete the dest-option or put an empty string there.
important: this just works with the expand-option set!
{
expand: true,
src: 'bower_components/modernizr/modernizr.js',
ext:'.min.js'
}
Edit:
for scanning all folders an minimizing all js files do it like this (note the second argument in src to not minify files which are already minified):
{
expand: true,
src: ['bower_components/**/*.js', '!bower_components/**/*.min.js'],
ext:'.min.js'
}

grunt copy moving build folder outside of project

Hello I have a file structure that looks like this,
|-App
|------|src
|----------|js
|----------|img
|----------|css
|----------|less
|----------|tpl
|----------|index.php
|- Gruntfile.js (withing parent app folder)
|- package.json (withing parent app folder)
What I am trying to do is move all the contents of the src folder into a build folder, the build folder gets created but outside of the App folder, and I don't really understand why, secondly when it does copy the src folder it copies the actual src folder, I want to copy all its children but no the parent src folder, and thirdly the copy ignores the index.php file why? Below is my current Gruntfile.js
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
copy: {
build: {
cwd: '.',
src: ['src/*/*'],
dest: '../build',
expand: true
}
},
clean: {
build: {
cwd: '.',
src: ['build']
}
}
});
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.registerTask('move', 'Moves the project to the build folder', ['copy', 'clean']);
};
Your paths are wrong. Change the build options to:
build: {
cwd: '.',
src: ['src/**/*'],
dest: './build',
expand: true
}
../build meant the build directory was created in the parent dir (.. is the parent dir)
src/**/* means recursively copy all files and files of descendent folders.

How can I customize this build script with Node?

I have a unique directory structure that I need help making a build script for.
Here is the link (slightly different) or directory structure:
client
/extensions
/sandbox
/widgets
/form
/collections
/models
/views
/styles
custom.css
/controllers
main.coffee
server
/views
/layouts
/errors
app.coffee
config.coffee
Couple things I need:
Compile coffeescript with a watch task into a server-dist +
client-dist
Copy over all other files into their nested folders, preferably with a watch task also
Problems:
If I just compile coffeescript it just copies over the .coffee files
to .js into their nested directories but that leaves behind .css /
imgs / etc loaded with require.js. I need a way to bring them as well
into the -dist directories
Main.coffee in the /client folder is a require.config and can be used with requirejs grunt build tool to optimize things.
Anyways the easiest solution is what I am looking for.
I ended up using grunt - with the following tasks:
clean: Clears the server / client build directories
watch: Monitors .coffee files and both build directories
copy: Copies over client / server files to build directories ignoring .coffee files which are managed by the coffee task
coffee: Compiles .coffee files to .js moving them to the build directories
Here is the grunt file in its current iteration:
grunt.initConfig({
clean: {
build: ['client-dist', 'server-dist'],
release: []
},
watch: {
coffee: {
files: ['client/**/*.coffee', 'server/**/*.coffee'],
tasks: 'coffee reload'
},
reload: {
files: ['client/**/*.!(coffee)', 'server/**/*.!(coffee)'],
tasks: 'copy reload'
}
},
copy: {
client: {
files: {
"client-dist/": "client/**/*.!(coffee)"
},
options: {
basePath: "client"
}
},
server: {
files: {
"server-dist/": "server/**/*.!(coffee)"
},
options: {
basePath: "server"
}
}
},
coffee: {
compile: {
files: {
'server-dist/*.js': 'server/**/*.coffee',
'client-dist/*.js': 'client/**/*.coffee'
}
}
}
});
grunt.loadNpmTasks('grunt-contrib');
grunt.loadNpmTasks('grunt-reload');
grunt.registerTask('default', '');
grunt.registerTask('build', 'clean:build copy coffee watch');

Categories