Allow server access from outside

This commit is contained in:
Andrew Abogado 2015-01-28 09:43:10 +08:00
parent 2a49046ff0
commit 5e035395b5

View file

@ -8,419 +8,429 @@
// If you want to recursively match all subfolders, use: // If you want to recursively match all subfolders, use:
// 'test/spec/**/*.js' // 'test/spec/**/*.js'
module.exports = function (grunt) { module.exports = function(grunt) {
// Time how long tasks take. Can help when optimizing build times // Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt); require('time-grunt')(grunt);
// Load grunt tasks automatically // Load grunt tasks automatically
require('load-grunt-tasks')(grunt); require('load-grunt-tasks')(grunt);
// Configurable paths // Configurable paths
var config = { var config = {
app: 'app', app: 'app',
dist: 'dist' dist: 'dist'
}; };
// Define the configuration for all the tasks // Define the configuration for all the tasks
grunt.initConfig({ grunt.initConfig({
// Project settings // Project settings
config: config, config: config,
// Watches files for changes and runs tasks based on the changed files // Watches files for changes and runs tasks based on the changed files
watch: { watch: {
bower: { bower: {
files: ['bower.json'], files: ['bower.json'],
tasks: ['wiredep'] tasks: ['wiredep']
}, },
js: { js: {
files: ['<%= config.app %>/scripts/{,*/}*.js'], files: ['<%= config.app %>/scripts/{,*/}*.js'],
tasks: ['jshint'], tasks: ['jshint'],
options: { options: {
livereload: true livereload: true
} }
}, },
jstest: { jstest: {
files: ['test/spec/{,*/}*.js'], files: ['test/spec/{,*/}*.js'],
tasks: ['test:watch'] tasks: ['test:watch']
}, },
gruntfile: { gruntfile: {
files: ['Gruntfile.js'] files: ['Gruntfile.js']
}, },
sass: { sass: {
files: ['<%= config.app %>/styles/{,*/}*.{scss,sass}'], files: ['<%= config.app %>/styles/{,*/}*.{scss,sass}'],
tasks: ['sass:server', 'autoprefixer'] tasks: ['sass:server', 'autoprefixer']
}, },
styles: { styles: {
files: ['<%= config.app %>/styles/{,*/}*.css'], files: ['<%= config.app %>/styles/{,*/}*.css'],
tasks: ['newer:copy:styles', 'autoprefixer'] tasks: ['newer:copy:styles', 'autoprefixer']
}, },
livereload: { livereload: {
options: { options: {
livereload: '<%= connect.options.livereload %>' livereload: '<%= connect.options.livereload %>'
}, },
files: [ files: [
'<%= config.app %>/{,*/}*.html', '<%= config.app %>/{,*/}*.html',
'.tmp/styles/{,*/}*.css', '.tmp/styles/{,*/}*.css',
'<%= config.app %>/images/{,*/}*' '<%= config.app %>/images/{,*/}*'
] ]
} }
}, },
// The actual grunt server settings // The actual grunt server settings
connect: { connect: {
options: { options: {
port: 9000, port: 9000,
open: true, open: true,
livereload: 35729, livereload: 35729,
// Change this to '0.0.0.0' to access the server from outside // Change this to '0.0.0.0' to access the server from outside
hostname: 'localhost' hostname: '0.0.0.0'
}, },
livereload: { livereload: {
options: { options: {
middleware: function(connect) { middleware: function(connect) {
return [ return [
connect.static('.tmp'), connect.static('.tmp'),
connect().use('/bower_components', connect.static('./bower_components')), connect().use('/bower_components', connect.static(
connect.static(config.app) './bower_components')),
]; connect.static(config.app)
} ];
} }
}, }
test: { },
options: { test: {
open: false, options: {
port: 9001, open: false,
middleware: function(connect) { port: 9001,
return [ middleware: function(connect) {
connect.static('.tmp'), return [
connect.static('test'), connect.static('.tmp'),
connect().use('/bower_components', connect.static('./bower_components')), connect.static('test'),
connect.static(config.app) connect().use('/bower_components', connect.static(
]; './bower_components')),
} connect.static(config.app)
} ];
}, }
dist: { }
options: { },
base: '<%= config.dist %>', dist: {
livereload: false options: {
} base: '<%= config.dist %>',
} livereload: false
}, }
}
},
// Empties folders to start fresh // Empties folders to start fresh
clean: { clean: {
dist: { dist: {
files: [{ files: [{
dot: true, dot: true,
src: [ src: [
'.tmp', '.tmp',
'<%= config.dist %>/*', '<%= config.dist %>/*',
'!<%= config.dist %>/.git*' '!<%= config.dist %>/.git*'
] ]
}] }]
}, },
server: '.tmp' server: '.tmp'
}, },
// Make sure code styles are up to par and there are no obvious mistakes // Make sure code styles are up to par and there are no obvious mistakes
jshint: { jshint: {
options: { options: {
jshintrc: '.jshintrc', jshintrc: '.jshintrc',
reporter: require('jshint-stylish') reporter: require('jshint-stylish')
}, },
all: [ all: [
'Gruntfile.js', 'Gruntfile.js',
'<%= config.app %>/scripts/{,*/}*.js', '<%= config.app %>/scripts/{,*/}*.js',
'!<%= config.app %>/scripts/vendor/*', '!<%= config.app %>/scripts/vendor/*',
'test/spec/{,*/}*.js' 'test/spec/{,*/}*.js'
] ]
}, },
// Mocha testing framework configuration options // Mocha testing framework configuration options
mocha: { mocha: {
all: { all: {
options: { options: {
run: true, run: true,
urls: ['http://<%= connect.test.options.hostname %>:<%= connect.test.options.port %>/index.html'] urls: [
} 'http://<%= connect.test.options.hostname %>:<%= connect.test.options.port %>/index.html'
} ]
}, }
}
},
// Compiles Sass to CSS and generates necessary files if requested // Compiles Sass to CSS and generates necessary files if requested
sass: { sass: {
options: { options: {
sourceMap: true, sourceMap: true,
includePaths: ['bower_components'] includePaths: ['bower_components']
}, },
dist: { dist: {
files: [{ files: [{
expand: true, expand: true,
cwd: '<%= config.app %>/styles', cwd: '<%= config.app %>/styles',
src: ['*.{scss,sass}'], src: ['*.{scss,sass}'],
dest: '.tmp/styles', dest: '.tmp/styles',
ext: '.css' ext: '.css'
}] }]
}, },
server: { server: {
files: [{ files: [{
expand: true, expand: true,
cwd: '<%= config.app %>/styles', cwd: '<%= config.app %>/styles',
src: ['*.{scss,sass}'], src: ['*.{scss,sass}'],
dest: '.tmp/styles', dest: '.tmp/styles',
ext: '.css' ext: '.css'
}] }]
} }
}, },
// Add vendor prefixed styles // Add vendor prefixed styles
autoprefixer: { autoprefixer: {
options: { options: {
browsers: ['> 1%', 'last 2 versions', 'Firefox ESR', 'Opera 12.1'] browsers: ['> 1%', 'last 2 versions', 'Firefox ESR', 'Opera 12.1']
}, },
dist: { dist: {
files: [{ files: [{
expand: true, expand: true,
cwd: '.tmp/styles/', cwd: '.tmp/styles/',
src: '{,*/}*.css', src: '{,*/}*.css',
dest: '.tmp/styles/' dest: '.tmp/styles/'
}] }]
} }
}, },
// Automatically inject Bower components into the HTML file // Automatically inject Bower components into the HTML file
wiredep: { wiredep: {
app: { app: {
ignorePath: /^\/|\.\.\//, ignorePath: /^\/|\.\.\//,
src: ['<%= config.app %>/index.html'], src: ['<%= config.app %>/index.html'],
exclude: ['bower_components/bootstrap-sass-official/assets/javascripts/bootstrap.js'] exclude: [
}, 'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap.js'
sass: { ]
src: ['<%= config.app %>/styles/{,*/}*.{scss,sass}'], },
ignorePath: /(\.\.\/){1,2}bower_components\// sass: {
} src: ['<%= config.app %>/styles/{,*/}*.{scss,sass}'],
}, ignorePath: /(\.\.\/){1,2}bower_components\//
}
},
// Renames files for browser caching purposes // Renames files for browser caching purposes
rev: { rev: {
dist: { dist: {
files: { files: {
src: [ src: [
'<%= config.dist %>/scripts/{,*/}*.js', '<%= config.dist %>/scripts/{,*/}*.js',
'<%= config.dist %>/styles/{,*/}*.css', '<%= config.dist %>/styles/{,*/}*.css',
'<%= config.dist %>/images/{,*/}*.*', '<%= config.dist %>/images/{,*/}*.*',
'<%= config.dist %>/styles/fonts/{,*/}*.*', '<%= config.dist %>/styles/fonts/{,*/}*.*',
'<%= config.dist %>/*.{ico,png}' '<%= config.dist %>/*.{ico,png}'
] ]
} }
} }
}, },
// Reads HTML for usemin blocks to enable smart builds that automatically // Reads HTML for usemin blocks to enable smart builds that automatically
// concat, minify and revision files. Creates configurations in memory so // concat, minify and revision files. Creates configurations in memory so
// additional tasks can operate on them // additional tasks can operate on them
useminPrepare: { useminPrepare: {
options: { options: {
dest: '<%= config.dist %>' dest: '<%= config.dist %>'
}, },
html: '<%= config.app %>/index.html' html: '<%= config.app %>/index.html'
}, },
// Performs rewrites based on rev and the useminPrepare configuration // Performs rewrites based on rev and the useminPrepare configuration
usemin: { usemin: {
options: { options: {
assetsDirs: [ assetsDirs: [
'<%= config.dist %>', '<%= config.dist %>',
'<%= config.dist %>/images', '<%= config.dist %>/images',
'<%= config.dist %>/styles' '<%= config.dist %>/styles'
] ]
}, },
html: ['<%= config.dist %>/{,*/}*.html'], html: ['<%= config.dist %>/{,*/}*.html'],
css: ['<%= config.dist %>/styles/{,*/}*.css'] css: ['<%= config.dist %>/styles/{,*/}*.css']
}, },
// The following *-min tasks produce minified files in the dist folder // The following *-min tasks produce minified files in the dist folder
imagemin: { imagemin: {
dist: { dist: {
files: [{ files: [{
expand: true, expand: true,
cwd: '<%= config.app %>/images', cwd: '<%= config.app %>/images',
src: '{,*/}*.{gif,jpeg,jpg,png}', src: '{,*/}*.{gif,jpeg,jpg,png}',
dest: '<%= config.dist %>/images' dest: '<%= config.dist %>/images'
}] }]
} }
}, },
svgmin: { svgmin: {
dist: { dist: {
files: [{ files: [{
expand: true, expand: true,
cwd: '<%= config.app %>/images', cwd: '<%= config.app %>/images',
src: '{,*/}*.svg', src: '{,*/}*.svg',
dest: '<%= config.dist %>/images' dest: '<%= config.dist %>/images'
}] }]
} }
}, },
htmlmin: { htmlmin: {
dist: { dist: {
options: { options: {
collapseBooleanAttributes: true, collapseBooleanAttributes: true,
collapseWhitespace: true, collapseWhitespace: true,
conservativeCollapse: true, conservativeCollapse: true,
removeAttributeQuotes: true, removeAttributeQuotes: true,
removeCommentsFromCDATA: true, removeCommentsFromCDATA: true,
removeEmptyAttributes: true, removeEmptyAttributes: true,
removeOptionalTags: true, removeOptionalTags: true,
removeRedundantAttributes: true, removeRedundantAttributes: true,
useShortDoctype: true useShortDoctype: true
}, },
files: [{ files: [{
expand: true, expand: true,
cwd: '<%= config.dist %>', cwd: '<%= config.dist %>',
src: '{,*/}*.html', src: '{,*/}*.html',
dest: '<%= config.dist %>' dest: '<%= config.dist %>'
}] }]
} }
}, },
// By default, your `index.html`'s <!-- Usemin block --> will take care // By default, your `index.html`'s <!-- Usemin block --> will take care
// of minification. These next options are pre-configured if you do not // of minification. These next options are pre-configured if you do not
// wish to use the Usemin blocks. // wish to use the Usemin blocks.
// cssmin: { // cssmin: {
// dist: { // dist: {
// files: { // files: {
// '<%= config.dist %>/styles/main.css': [ // '<%= config.dist %>/styles/main.css': [
// '.tmp/styles/{,*/}*.css', // '.tmp/styles/{,*/}*.css',
// '<%= config.app %>/styles/{,*/}*.css' // '<%= config.app %>/styles/{,*/}*.css'
// ] // ]
// } // }
// } // }
// }, // },
// uglify: { // uglify: {
// dist: { // dist: {
// files: { // files: {
// '<%= config.dist %>/scripts/scripts.js': [ // '<%= config.dist %>/scripts/scripts.js': [
// '<%= config.dist %>/scripts/scripts.js' // '<%= config.dist %>/scripts/scripts.js'
// ] // ]
// } // }
// } // }
// }, // },
// concat: { // concat: {
// dist: {} // dist: {}
// }, // },
// Copies remaining files to places other tasks can use // Copies remaining files to places other tasks can use
copy: { copy: {
dist: { dist: {
files: [{ files: [{
expand: true, expand: true,
dot: true, dot: true,
cwd: '<%= config.app %>', cwd: '<%= config.app %>',
dest: '<%= config.dist %>', dest: '<%= config.dist %>',
src: [ src: [
'*.{ico,png,txt}', '*.{ico,png,txt}',
'images/{,*/}*.webp', 'images/{,*/}*.webp',
'{,*/}*.html', '{,*/}*.html',
'styles/fonts/{,*/}*.*' 'styles/fonts/{,*/}*.*'
] ]
}, { }, {
src: 'node_modules/apache-server-configs/dist/.htaccess', src: 'node_modules/apache-server-configs/dist/.htaccess',
dest: '<%= config.dist %>/.htaccess' dest: '<%= config.dist %>/.htaccess'
}, { }, {
expand: true, expand: true,
dot: true, dot: true,
cwd: '.', cwd: '.',
src: 'bower_components/bootstrap-sass-official/assets/fonts/bootstrap/*', src: 'bower_components/bootstrap-sass-official/assets/fonts/bootstrap/*',
dest: '<%= config.dist %>' dest: '<%= config.dist %>'
}] }]
}, },
styles: { styles: {
expand: true, expand: true,
dot: true, dot: true,
cwd: '<%= config.app %>/styles', cwd: '<%= config.app %>/styles',
dest: '.tmp/styles/', dest: '.tmp/styles/',
src: '{,*/}*.css' src: '{,*/}*.css'
} }
}, },
// Run some tasks in parallel to speed up build process // Run some tasks in parallel to speed up build process
concurrent: { concurrent: {
server: [ server: [
'sass:server', 'sass:server',
'copy:styles' 'copy:styles'
], ],
test: [ test: [
'copy:styles' 'copy:styles'
], ],
dist: [ dist: [
'sass', 'sass',
'copy:styles', 'copy:styles',
'imagemin', 'imagemin',
'svgmin' 'svgmin'
] ]
} }
}); });
grunt.registerTask('serve', 'start the server and preview your app, --allow-remote for remote access', function (target) { grunt.registerTask('serve',
if (grunt.option('allow-remote')) { 'start the server and preview your app, --allow-remote for remote access',
grunt.config.set('connect.options.hostname', '0.0.0.0'); function(target) {
} if (grunt.option('allow-remote')) {
if (target === 'dist') { grunt.config.set('connect.options.hostname', '0.0.0.0');
return grunt.task.run(['build', 'connect:dist:keepalive']); }
} if (target === 'dist') {
return grunt.task.run(['build', 'connect:dist:keepalive']);
}
grunt.task.run([ grunt.task.run([
'clean:server', 'clean:server',
'wiredep', 'wiredep',
'concurrent:server', 'concurrent:server',
'autoprefixer', 'autoprefixer',
'connect:livereload', 'connect:livereload',
'watch' 'watch'
]); ]);
}); });
grunt.registerTask('server', function (target) { grunt.registerTask('server', function(target) {
grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.'); grunt.log.warn(
grunt.task.run([target ? ('serve:' + target) : 'serve']); 'The `server` task has been deprecated. Use `grunt serve` to start a server.'
}); );
grunt.task.run([target ? ('serve:' + target) : 'serve']);
});
grunt.registerTask('test', function (target) { grunt.registerTask('test', function(target) {
if (target !== 'watch') { if (target !== 'watch') {
grunt.task.run([ grunt.task.run([
'clean:server', 'clean:server',
'concurrent:test', 'concurrent:test',
'autoprefixer' 'autoprefixer'
]); ]);
} }
grunt.task.run([ grunt.task.run([
'connect:test', 'connect:test',
'mocha' 'mocha'
]); ]);
}); });
grunt.registerTask('build', [ grunt.registerTask('build', [
'clean:dist', 'clean:dist',
'wiredep', 'wiredep',
'useminPrepare', 'useminPrepare',
'concurrent:dist', 'concurrent:dist',
'autoprefixer', 'autoprefixer',
'concat', 'concat',
'cssmin', 'cssmin',
'uglify', 'uglify',
'copy:dist', 'copy:dist',
'rev', 'rev',
'usemin', 'usemin',
'htmlmin' 'htmlmin'
]); ]);
grunt.registerTask('default', [ grunt.registerTask('default', [
'newer:jshint', 'newer:jshint',
'test', 'test',
'build' 'build'
]); ]);
}; };