Skip to content

Commit

Permalink
Build js and css assets from bower and grunt
Browse files Browse the repository at this point in the history
Heavily based on @robvdl improvement
  • Loading branch information
claudyus committed Dec 23, 2014
1 parent 348630d commit 6dea09f
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 15 deletions.
4 changes: 0 additions & 4 deletions .bowerrc

This file was deleted.

11 changes: 9 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@ gh-pages
dist
build

#Pycharm conf
.idea/
# Pycharm conf
.idea/

# bower
jsbuild/node_modules/*
jsbuild/bower_components/*
lwp/static/css/*.css
lwp/static/js/*.js
lwp/static/img/*
33 changes: 33 additions & 0 deletions fabfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,41 @@ def clone():
local('git checkout origin/gh-pages -b gh-pages || true')


@task(alias='assets')
def build_assets():
"""
Runs the assets pipeline, Grunt, bower, sass, etc.
"""
# only run npm install when needed
if not os.path.exists('jsbuild/node_modules'):
with lcd('jsbuild'):
local('npm install')

# run Bower, then Grunt
with lcd('jsbuild'):
local('node_modules/.bin/bower install')
local('node_modules/.bin/grunt')


@task
def site():
clone()
build_assets()
debian()
local('make -C gh-pages/')


@task
def clean_assets():
local('rm -rf lwp/static/js/vendor')
local('rm -f lwp/static/css/bootstrap.*')

@task
def clean_jsbuild():
local('rm -rf jsbuild/node_modules')
local('rm -rf jsbuild/bower_components')

@task
def clean():
clean_jsbuild()
clean_assets()
64 changes: 64 additions & 0 deletions jsbuild/Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

module.exports = function(grunt) {

grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),

concat: {
js: {
src: [
'bower_components/jquery/jquery.js',
'bower_components/bootstrap/bootstrap/js/bootstrap.js',
'bower_components/bootstrap-switch/build/js/bootstrap-switch.js',
'bower_components/jqBootstrapValidation/dist/jqBootstrapValidation-1.3.7.js'
],
dest: '../lwp/static/js/bootstrap.js'
},
css: {
src: [
'bower_components/bootstrap/bootstrap/css/bootstrap.css',
'bower_components/bootstrap/bootstrap/css/bootstrap-responsive.css',
'bower_components/bootstrap-switch/build/css/bootstrap2/bootstrap-switch.css'
],
dest: '../lwp/static/css/bootstrap.css'
}
},

uglify: {
compress: {
files: {
'../lwp/static/js/bootstrap.min.js': ['../lwp/static/js/bootstrap.js']
},
options: {
mangle: true
}
}
},

cssmin: {
build: {
src: '../lwp/static/css/bootstrap.css',
dest: '../lwp/static/css/bootstrap.min.css'
}
},

copy: {
main: {
expand: true,
cwd: 'bower_components/bootstrap/',
src: 'img/*',
dest: '../lwp/static/img/',
flatten: true,
filter: 'isFile',
},
}
});

grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-cssmin');

grunt.registerTask('default', ['concat', 'uglify', 'cssmin', 'copy']);
};
14 changes: 14 additions & 0 deletions jsbuild/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
About this directory
====================

In order to not pollute the root directory of this Python project with JS
build tool files, we use a directory "jsbuild" for this.

This just keeps the project a bit tidier and puts all frontend tools
in one place.

The npm node_modules and bower_components folders get created here.

npm, bower, and grunt are all executed from the fabfile.py

The fabfile.py is then invoked by the debian packaging process.
22 changes: 22 additions & 0 deletions jsbuild/bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "lwp-bower",
"version": "0.1",
"authors": [
"Rob van der Linde <robvdl@gmail.com>",
"Claudio Mignanti <c.mignanti@gmail.com>"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"jquery": "~2.0.3",
"bootstrap": "git@github.com:alonisser/bower-bootstrap-2.3.2-legacy.git",
"bootstrap-switch": "~2.0.1",
"jqBootstrapValidation": "git@github.com:ReactiveRaven/jqBootstrapValidation.git"
}
}
20 changes: 20 additions & 0 deletions jsbuild/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "lwp-jsbuild",
"description": "JavaScript build environment",
"author": "Rob van der Linde",
"tags": [],
"repository": "https://github.com/robvdl/LXC-Web-Panel.git",
"version": "0.1.1",
"engines": {
"node": ">=0.10.11"
},
"devDependencies": {
"bower": "~1.3.9",
"grunt": "~0.4.5",
"grunt-cli": "~0.1.13",
"grunt-contrib-concat": "~0.5.0",
"grunt-contrib-copy": "~0.7.0",
"grunt-contrib-uglify": "~0.5.1",
"grunt-contrib-cssmin": "~0.10.0"
}
}
2 changes: 0 additions & 2 deletions lwp/static/bower/.gitignore

This file was deleted.

9 changes: 2 additions & 7 deletions lwp/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">

<link href="{{ url_for('static', filename='bower/bootstrap/bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='bower/bootstrap/bootstrap/css/bootstrap-responsive.min.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='bower/bootstrap-switch/build/css/bootstrap2/bootstrap-switch.min.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='css/site.css') }}" rel="stylesheet">

<!--[if lt IE 9]>
Expand Down Expand Up @@ -58,10 +56,7 @@
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="{{ url_for('static', filename='bower/jquery/jquery.min.js') }}"></script>
<script src="{{ url_for('static', filename='bower/bootstrap/bootstrap/js/bootstrap.min.js') }}"></script>
<script src="{{ url_for('static', filename='bower/jqBootstrapValidation/dist/jqBootstrapValidation-1.3.7.js') }}"></script>
<script src="{{ url_for('static', filename='bower/bootstrap-switch/build/js/bootstrap-switch.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
<script>
$(function() {
$(".alert").not('.dont-hide').alert().delay(5000).slideUp();
Expand Down

3 comments on commit 6dea09f

@robvdl
Copy link
Contributor

@robvdl robvdl commented on 6dea09f Dec 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool

@robvdl
Copy link
Contributor

@robvdl robvdl commented on 6dea09f Dec 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check the jsbuild/package.json the url to repo is wrong, it still points to my fork rather than your own repo.

@robvdl
Copy link
Contributor

@robvdl robvdl commented on 6dea09f Dec 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this is more just for information, as I'm not expecting you to switch away from Grunt or anything, but in other projects I have started using Gulp + Browserify + Watchify as an assets chain and renamed the "jsbuild" folder just to "frontend".

Though Grunt itself is fine, doing this with Gulp and using Watchify is very fast for doing constant rebuilds. I also started using React.js and React-Bootstrap, which changes the JS code style completely.

Browserify basically means you can import JS modules using require() statements, then browserify packages everything up in one huge .js file, this includes all the dependencies: Bootstrap, React, React-Bootstrap and your own code all packaged into one .js file for the entire site.

I haven't done any of this to my LXC Web Panel fork however.

Please sign in to comment.