Skip to content

Commit d1e8a6f

Browse files
committed
Overhaul of the AMD integration
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.
1 parent e8fd5a0 commit d1e8a6f

56 files changed

Lines changed: 325 additions & 10296 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.jshintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
src/js/banner.*.js
2+
src/js/wrapper.*.js
13
tests/vendor/*.js
24
tests/helpers.js

Gruntfile.js

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
module.exports = function (grunt) {
22
// Full list of files that must be included by RequireJS
33
includes = [
4-
'jquery.select2'
5-
];
6-
7-
amdIncludes = [
4+
'jquery.select2',
85
'almond'
96
];
107

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

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

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

65+
concat: {
66+
'dist': {
67+
options: {
68+
banner: grunt.file.read('src/js/wrapper.start.js'),
69+
},
70+
src: [
71+
'dist/js/select2.js',
72+
'src/js/wrapper.end.js'
73+
],
74+
dest: 'dist/js/select2.js'
75+
},
76+
'dist.full': {
77+
options: {
78+
banner: grunt.file.read('src/js/wrapper.start.js'),
79+
},
80+
src: [
81+
'dist/js/select2.full.js',
82+
'src/js/wrapper.end.js'
83+
],
84+
dest: 'dist/js/select2.full.js'
85+
}
86+
},
87+
6888
connect: {
6989
tests: {
7090
options: {
@@ -227,12 +247,16 @@ module.exports = function (grunt) {
227247
optimize: 'none',
228248
name: 'select2/core',
229249
out: 'dist/js/select2.js',
230-
include: amdIncludes.concat(includes),
250+
include: includes,
251+
namespace: 'S2',
231252
paths: {
232253
almond: '../../vendor/almond-0.2.9',
233254
jquery: 'jquery.shim'
234255
},
235-
wrap: grunt.file.readJSON('src/js/banner.json')
256+
wrap: {
257+
startFile: 'src/js/banner.start.js',
258+
endFile: 'src/js/banner.end.js'
259+
}
236260
}
237261
},
238262
'dist.full': {
@@ -241,40 +265,17 @@ module.exports = function (grunt) {
241265
optimize: 'none',
242266
name: 'select2/core',
243267
out: 'dist/js/select2.full.js',
244-
include: amdIncludes.concat(fullIncludes),
268+
include: fullIncludes,
269+
namespace: 'S2',
245270
paths: {
246271
almond: '../../vendor/almond-0.2.9',
247272
jquery: 'jquery.shim',
248273
'jquery.mousewheel': '../../vendor/jquery.mousewheel'
249274
},
250-
wrap: grunt.file.readJSON('src/js/banner.json')
251-
}
252-
},
253-
'amd': {
254-
options: {
255-
baseUrl: 'src/js',
256-
optimize: 'none',
257-
name: 'select2/core',
258-
out: 'dist/js/select2.amd.js',
259-
include: includes,
260-
paths: {
261-
jquery: 'empty:'
262-
},
263-
wrap: grunt.file.readJSON('src/js/banner.amd.json')
264-
}
265-
},
266-
'amd.full': {
267-
options: {
268-
baseUrl: 'src/js',
269-
optimize: 'none',
270-
name: 'select2/core',
271-
out: 'dist/js/select2.amd.full.js',
272-
include: fullIncludes,
273-
paths: {
274-
jquery: 'empty:',
275-
'jquery.mousewheel': '../../vendor/jquery.mousewheel'
276-
},
277-
wrap: grunt.file.readJSON('src/js/banner.amd.json')
275+
wrap: {
276+
startFile: 'src/js/banner.start.js',
277+
endFile: 'src/js/banner.end.js'
278+
}
278279
}
279280
},
280281
'i18n': {
@@ -283,7 +284,11 @@ module.exports = function (grunt) {
283284
dir: 'dist/js/i18n',
284285
paths: i18nPaths,
285286
modules: i18nModules,
286-
wrap: grunt.file.readJSON('src/js/banner.basic.json')
287+
namespace: 'S2',
288+
wrap: {
289+
start: minifiedBanner + grunt.file.read('src/js/banner.start.js'),
290+
end: grunt.file.read('src/js/banner.end.js')
291+
}
287292
}
288293
}
289294
},
@@ -329,7 +334,11 @@ module.exports = function (grunt) {
329334

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

332-
grunt.registerTask('compile', ['requirejs', 'sass:dev']);
337+
grunt.registerTask('compile', [
338+
'requirejs:dist', 'requirejs:dist.full', 'requirejs:i18n',
339+
'concat:dist', 'concat:dist.full',
340+
'sass:dev'
341+
]);
333342
grunt.registerTask('minify', ['uglify', 'sass:dist']);
334343
grunt.registerTask('test', ['connect:tests', 'qunit', 'jshint']);
335344

dist/js/i18n/az.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/i18n/bg.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/i18n/ca.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/i18n/cs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/i18n/da.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/i18n/de.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/i18n/en.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/i18n/es.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)