Skip to content

Commit

Permalink
Overhaul of the AMD integration
Browse files Browse the repository at this point in the history
Now we do not have separate AMD builds, instead we use a UMD wrapper
to support both AMD and CommonJS environments along with the browser.
This has been tested with RequireJS and it correctly imports Select2
and hooks into jQuery.

This means that all builds will include the Almond AMD loader, but
this isn't that much of an issue when you consider the file sizes.
The loader is namespaced so RequireJS and other AMD builders don't
complain about `require` and `define` existing. This is also done
so AMD loaders don't conflict, as Select2's AMD loader should
operate independently of others.

There are now two layers of wrappers that are applied to files,
only one of which is used for the translation files. This allows
us to only load in the internal Select2 modules once, and in
general makes most things cleaner. These have also been pulled out
into individual JS files instead of being stored in JSON files,
allowing us to add comments and make things look readable.

Because we use a few hacks, these files are heavily commented as well.

This closes select2#2945.
This closes select2#2946.
  • Loading branch information
kevin-brown committed Mar 14, 2015
1 parent e8fd5a0 commit d1e8a6f
Show file tree
Hide file tree
Showing 56 changed files with 325 additions and 10,296 deletions.
2 changes: 2 additions & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
src/js/banner.*.js
src/js/wrapper.*.js
tests/vendor/*.js
tests/helpers.js
85 changes: 47 additions & 38 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
module.exports = function (grunt) {
// Full list of files that must be included by RequireJS
includes = [
'jquery.select2'
];

amdIncludes = [
'jquery.select2',
'almond'
];

Expand Down Expand Up @@ -56,7 +53,7 @@ module.exports = function (grunt) {
i18nPaths[name] = '../../' + name;
}

var minifiedBanner = '/!* Select2 <%= package.version %> | https://github.com/select2/select2/blob/master/LICENSE.md */';
var minifiedBanner = '/*! Select2 <%= package.version %> | https://github.com/select2/select2/blob/master/LICENSE.md */';

grunt.initConfig({
package: grunt.file.readJSON('package.json'),
Expand All @@ -65,6 +62,29 @@ module.exports = function (grunt) {
docs: ['docs/_site']
},

concat: {
'dist': {
options: {
banner: grunt.file.read('src/js/wrapper.start.js'),
},
src: [
'dist/js/select2.js',
'src/js/wrapper.end.js'
],
dest: 'dist/js/select2.js'
},
'dist.full': {
options: {
banner: grunt.file.read('src/js/wrapper.start.js'),
},
src: [
'dist/js/select2.full.js',
'src/js/wrapper.end.js'
],
dest: 'dist/js/select2.full.js'
}
},

connect: {
tests: {
options: {
Expand Down Expand Up @@ -227,12 +247,16 @@ module.exports = function (grunt) {
optimize: 'none',
name: 'select2/core',
out: 'dist/js/select2.js',
include: amdIncludes.concat(includes),
include: includes,
namespace: 'S2',
paths: {
almond: '../../vendor/almond-0.2.9',
jquery: 'jquery.shim'
},
wrap: grunt.file.readJSON('src/js/banner.json')
wrap: {
startFile: 'src/js/banner.start.js',
endFile: 'src/js/banner.end.js'
}
}
},
'dist.full': {
Expand All @@ -241,40 +265,17 @@ module.exports = function (grunt) {
optimize: 'none',
name: 'select2/core',
out: 'dist/js/select2.full.js',
include: amdIncludes.concat(fullIncludes),
include: fullIncludes,
namespace: 'S2',
paths: {
almond: '../../vendor/almond-0.2.9',
jquery: 'jquery.shim',
'jquery.mousewheel': '../../vendor/jquery.mousewheel'
},
wrap: grunt.file.readJSON('src/js/banner.json')
}
},
'amd': {
options: {
baseUrl: 'src/js',
optimize: 'none',
name: 'select2/core',
out: 'dist/js/select2.amd.js',
include: includes,
paths: {
jquery: 'empty:'
},
wrap: grunt.file.readJSON('src/js/banner.amd.json')
}
},
'amd.full': {
options: {
baseUrl: 'src/js',
optimize: 'none',
name: 'select2/core',
out: 'dist/js/select2.amd.full.js',
include: fullIncludes,
paths: {
jquery: 'empty:',
'jquery.mousewheel': '../../vendor/jquery.mousewheel'
},
wrap: grunt.file.readJSON('src/js/banner.amd.json')
wrap: {
startFile: 'src/js/banner.start.js',
endFile: 'src/js/banner.end.js'
}
}
},
'i18n': {
Expand All @@ -283,7 +284,11 @@ module.exports = function (grunt) {
dir: 'dist/js/i18n',
paths: i18nPaths,
modules: i18nModules,
wrap: grunt.file.readJSON('src/js/banner.basic.json')
namespace: 'S2',
wrap: {
start: minifiedBanner + grunt.file.read('src/js/banner.start.js'),
end: grunt.file.read('src/js/banner.end.js')
}
}
}
},
Expand Down Expand Up @@ -329,7 +334,11 @@ module.exports = function (grunt) {

grunt.registerTask('default', ['compile', 'test', 'minify']);

grunt.registerTask('compile', ['requirejs', 'sass:dev']);
grunt.registerTask('compile', [
'requirejs:dist', 'requirejs:dist.full', 'requirejs:i18n',
'concat:dist', 'concat:dist.full',
'sass:dev'
]);
grunt.registerTask('minify', ['uglify', 'sass:dist']);
grunt.registerTask('test', ['connect:tests', 'qunit', 'jshint']);

Expand Down
2 changes: 1 addition & 1 deletion dist/js/i18n/az.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/i18n/bg.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/i18n/ca.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/i18n/cs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/i18n/da.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/i18n/de.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/i18n/en.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/i18n/es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d1e8a6f

Please sign in to comment.