Skip to content

Commit d43ead3

Browse files
mreinsteinpatrickkettner
authored andcommitted
option to specify global var name to attach modernizr to Modernizr#2362 (Modernizr#2363)
* add option to specificy global variable name to attach modernizr to Modernizr#2362 * add tests for scriptGlobalName Modernizr#2362 * add command line option for script global name Modernizr#2362
1 parent 189838a commit d43ead3

4 files changed

Lines changed: 38 additions & 0 deletions

File tree

bin/modernizr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ var yargs = require('yargs')
3939
alias: 'uglify',
4040
describe: 'uglify/minify the output'
4141
})
42+
.options('s', {
43+
alias: 'scriptGlobalName',
44+
describe: 'Script global variable where Modernizr is attached. Defaults to window'
45+
})
4246
.options('q', {
4347
alias: 'quiet',
4448
describe: 'Silence all output'
@@ -178,6 +182,10 @@ if (argv.u) {
178182
config.minify = true;
179183
}
180184

185+
if (argv.scriptGlobalName) {
186+
config.scriptGlobalName = argv.scriptGlobalName;
187+
}
188+
181189
Modernizr.build(config, function(output) {
182190
fs.writeFileSync(dest, output);
183191
log('Modernizr build saved to ' + dest);

lib/build.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ function build(generate, generateBanner, pkg) {
8282
requireConfig.optimize = 'none';
8383
}
8484

85+
if(config.scriptGlobalName) {
86+
requireConfig.wrap.end = '})(' + config.scriptGlobalName + ', document);';
87+
}
88+
8589
requireConfig.out = function(output) {
8690
output = banner + output;
8791

lib/config-all.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"classPrefix": "",
3+
"scriptGlobalName": "window",
34
"options": [
45
"addTest",
56
"atRule",

test/node/lib/build.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,31 @@ describe('cli/build', function() {
113113

114114
});
115115

116+
describe('scriptGlobalName', function() {
117+
118+
it('should inject modernizr onto window by default', function(done) {
119+
var config = {
120+
'feature-detects': ['css/boxsizing']
121+
};
122+
build(config, function(file) {
123+
expect(file).to.contain('})(window, document);');
124+
done();
125+
});
126+
});
127+
128+
it('should inject modernizr onto custom window global when specified', function(done) {
129+
var config = {
130+
'scriptGlobalName': 'window.awesomeco',
131+
'feature-detects': ['css/boxsizing']
132+
};
133+
build(config, function(file) {
134+
expect(file).to.contain('})(window.awesomeco, document);');
135+
done();
136+
});
137+
});
138+
139+
});
140+
116141
describe('minified', function() {
117142
var output;
118143

0 commit comments

Comments
 (0)