Skip to content

Commit 7d053e0

Browse files
committed
Add UMD boilerplate to all compiled JS files
-Use glob patterns for Grunt tasks; don't loop through all files -Create separate copy task (this was previously bundled in with concat tasks and required making a separate task for every copied file)
1 parent 8226c66 commit 7d053e0

File tree

2 files changed

+76
-84
lines changed

2 files changed

+76
-84
lines changed

Gruntfile.js

Lines changed: 75 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//TODO: During next major version bump change to /dist. Leaving at ./form-validator for backwards
22
//compatibility
33
const DIST_DIR = './form-validator';
4-
const MAIN_PLUGIN_FILE = './form-validator/jquery.form-validator';
4+
const MAIN_PLUGIN_FILE = 'jquery.form-validator';
55
const SRC_DIR = './src';
66
const MAIN_DIR = '/main/';
7-
const MODULE_DIR = '/modules/';
8-
const LANG_DIR = '/lang/';
7+
const MODULE_DIR = '/modules';
8+
const LANG_DIR = '/lang';
99
const CSS_FILE = 'theme-default.css';
10+
const CORE_VALIDATORS = 'core-validators.js'; //must come at end of concatted file
11+
const coreValidatorsPath = SRC_DIR + MAIN_DIR + CORE_VALIDATORS;
1012

1113
var fs = require('fs'),
1214
readFile = function (file) {
@@ -16,7 +18,8 @@ var fs = require('fs'),
1618
fs.writeFileSync(path, readFile(path).replace(from, to));
1719
};
1820

19-
function initializeGruntConfig(grunt, filesToBuild) {
21+
function initializeGruntConfig(grunt) {
22+
2023
grunt.initConfig({
2124

2225
// Import package manifest
@@ -34,12 +37,36 @@ function initializeGruntConfig(grunt, filesToBuild) {
3437
" */\n"
3538
},
3639

37-
// Concat definitions.
38-
concat: filesToBuild.concat,
40+
concat: {
41+
main:{
42+
files: [
43+
//This concatenates the core validators file after the other files
44+
//Per: http://gruntjs.com/configuring-tasks
45+
{
46+
src: [SRC_DIR + MAIN_DIR+'*.js', '!' + coreValidatorsPath, coreValidatorsPath],
47+
dest: DIST_DIR + '/' + MAIN_PLUGIN_FILE + '.js'
48+
},
49+
{
50+
src: [SRC_DIR + MAIN_DIR+'*.js', '!' + coreValidatorsPath, coreValidatorsPath],
51+
dest: DIST_DIR + '/' + MAIN_PLUGIN_FILE + '.min.js'
52+
}]
53+
},
54+
options: {
55+
banner: "<%= meta.banner %>"
56+
}
57+
},
3958

4059
cssmin: {
4160
target: {
42-
files: filesToBuild.cssFiles
61+
files: [
62+
{
63+
dest: DIST_DIR,
64+
src: CSS_FILE,
65+
cwd: SRC_DIR,
66+
expand: true,
67+
ext: '.min.css'
68+
}
69+
]
4370
}
4471
},
4572
// Lint definitions
@@ -52,7 +79,21 @@ function initializeGruntConfig(grunt, filesToBuild) {
5279
},
5380

5481
// Minify definitions
55-
uglify: filesToBuild.uglify,
82+
uglify: {
83+
options: {
84+
banner: "<%= meta.banner %>"
85+
},
86+
main: {
87+
files: [
88+
{
89+
expand: true,
90+
cwd: DIST_DIR + '/',
91+
src: ['**/*.js', '!' + MAIN_PLUGIN_FILE +'.js'],
92+
dest: DIST_DIR + '/'
93+
}
94+
]
95+
}
96+
},
5697

5798
// watch for changes to source
5899
// Better than calling grunt a million times
@@ -78,98 +119,47 @@ function initializeGruntConfig(grunt, filesToBuild) {
78119
}
79120
}
80121
},
81-
122+
copy: {
123+
main: {
124+
files: [
125+
{
126+
src: SRC_DIR + '/' + CSS_FILE,
127+
dest: DIST_DIR + '/' + CSS_FILE
128+
},
129+
{
130+
cwd: SRC_DIR + '/' + MODULE_DIR,
131+
src: '**',
132+
dest: DIST_DIR + '/',
133+
expand: true
134+
},
135+
{
136+
cwd: SRC_DIR + '/' + LANG_DIR,
137+
src: '**',
138+
dest: DIST_DIR + LANG_DIR +'/',
139+
expand: true
140+
}]
141+
}
142+
},
82143
clean: [DIST_DIR + '/'],
83144
umd: {
84145
main: {
85146
options: {
86-
src: MAIN_PLUGIN_FILE + '.js',
147+
src: DIST_DIR + '/**/*.js',
148+
dest: './',
87149
deps: {
88150
default: ['jQuery'],
89151
amd: [{'jquery': 'jQuery'}],
90152
cjs: [{'jquery': 'jQuery'}]
91153
}
92154
}
93-
},
94-
minified: {
95-
options: {
96-
src: MAIN_PLUGIN_FILE + '.min.js',
97-
deps: {
98-
default: ['$'],
99-
amd: [{'jquery': 'jQuery'}],
100-
cjs: [{'jquery': 'jQuery'}]
101-
}
102-
}
103155
}
104156
}
105157
});
106158
}
107159

108160
module.exports = function (grunt) {
109-
var filesToBuild = {
110-
uglify: {},
111-
concat: {
112-
main:{
113-
src: [SRC_DIR + MAIN_DIR+'core-validators.js'],
114-
dest: MAIN_PLUGIN_FILE + '.js'
115-
}
116-
}
117-
};
118-
// Gather up all module and language files
119-
[MODULE_DIR, LANG_DIR].forEach(function (path) {
120-
var srcPath = SRC_DIR + path;
121-
var distPath = DIST_DIR + path;
122-
if (path === MODULE_DIR) {
123-
distPath = DIST_DIR + '/';
124-
}
125-
126-
fs.readdirSync(srcPath).forEach(function (fileName) {
127-
var fullPath = srcPath + fileName;
128-
filesToBuild.uglify[distPath + fileName] = fullPath;
129-
filesToBuild.concat[fullPath] = {
130-
src: [fullPath],
131-
dest: distPath + fileName
132-
};
133-
});
134-
});
135-
filesToBuild.concat[CSS_FILE] = {
136-
src: [SRC_DIR + '/' + CSS_FILE],
137-
dest: DIST_DIR + '/' + CSS_FILE
138-
};
139-
// Gather up all source files that will added to minified core library
140-
fs.readdirSync(SRC_DIR + MAIN_DIR).forEach(function (fileName) {
141-
var fullPath = SRC_DIR + MAIN_DIR + fileName;
142-
if (filesToBuild.concat.main.src.indexOf(fullPath) === -1) {
143-
filesToBuild.concat.main.src.unshift(fullPath);
144-
}
145-
});
146-
147-
filesToBuild.cssFiles = [];
148-
filesToBuild.cssFiles.push({
149-
dest: DIST_DIR,
150-
src: CSS_FILE,
151-
cwd: SRC_DIR,
152-
expand: true,
153-
ext: '.min.css'
154-
});
155-
156-
// Add options for concat and uglify
157-
filesToBuild.concat.options = {
158-
banner: "<%= meta.banner %>"
159-
};
160-
filesToBuild.uglify.options = {
161-
banner: "<%= meta.banner %>"
162-
};
163-
164-
// Add main script to uglify
165-
filesToBuild.uglify[MAIN_PLUGIN_FILE + '.js'] = {
166-
src: MAIN_PLUGIN_FILE + '.js',
167-
expand: true,
168-
extDot: 'last',
169-
ext: '.min.js'
170-
};
171161

172-
initializeGruntConfig(grunt, filesToBuild);
162+
initializeGruntConfig(grunt);
173163
/*
174164
* Change to new version or the next version number. The project must be built again after this task
175165
* in order for the version change to take effect.
@@ -199,6 +189,7 @@ module.exports = function (grunt) {
199189

200190
grunt.loadNpmTasks('grunt-contrib-clean');
201191
grunt.loadNpmTasks("grunt-contrib-concat");
192+
grunt.loadNpmTasks('grunt-contrib-copy');
202193
grunt.loadNpmTasks("grunt-contrib-jshint");
203194
grunt.loadNpmTasks("grunt-contrib-uglify");
204195
grunt.loadNpmTasks("grunt-contrib-watch");
@@ -208,7 +199,7 @@ module.exports = function (grunt) {
208199
grunt.loadNpmTasks('grunt-umd');
209200

210201
grunt.registerTask("build-production", ["version", "test", "uglify"]);
211-
grunt.registerTask('test', ['concat', 'umd', 'cssmin','jshint', 'qunit']);
202+
grunt.registerTask('test', ['concat', 'copy', 'umd', 'cssmin','jshint', 'qunit']);
212203
grunt.registerTask("default", ["test", "watch"]);
213204
grunt.registerTask("prepublish", ["test", "uglify"]);
214205
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"grunt-contrib-clean": "~1.0.0",
2929
"grunt-contrib-concat": "^0.5.1",
3030
"grunt-contrib-connect": "^0.11.2",
31+
"grunt-contrib-copy": "~1.0.0",
3132
"grunt-contrib-cssmin": "~0.14.0",
3233
"grunt-contrib-jshint": "~1.0.0",
3334
"grunt-contrib-qunit": "^0.7.0",

0 commit comments

Comments
 (0)