Skip to content

Commit a10f67d

Browse files
committed
Fix distributable to be browserifiable, #12 (browserify/browserify#1151, browserify/browserify#670)
1 parent 836096e commit a10f67d

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

Gruntfile.js

+15-16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ module.exports = function (grunt) {
3232
singleRun: false
3333
}
3434
},
35+
exec: {
36+
// work around https://github.com/substack/node-browserify/pull/1151
37+
bundle: './node_modules/.bin/browserify --standalone <%= pkg.name %> --external url --external css-font-face-src --external cssom src/inline.js | ./node_modules/.bin/derequire > build/<%= pkg.name %>.bundled.js'
38+
},
3539
browserify: {
3640
xmlserializer: {
3741
src: [],
@@ -81,16 +85,6 @@ module.exports = function (grunt) {
8185
debug: true
8286
}
8387
}
84-
},
85-
browser: {
86-
src: 'src/inline.js',
87-
dest: 'build/<%= pkg.name %>.js',
88-
options: {
89-
browserifyOptions: {
90-
standalone: '<%= pkg.name %>',
91-
},
92-
external: ['cssom', 'url', 'css-font-face-src']
93-
}
9488
}
9589
},
9690
clean: {
@@ -100,6 +94,7 @@ module.exports = function (grunt) {
10094
concat: {
10195
dist: {
10296
options: {
97+
// Work around https://github.com/substack/node-browserify/issues/670
10398
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
10499
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
105100
'* <%= pkg.homepage %>\n' +
@@ -111,22 +106,25 @@ module.exports = function (grunt) {
111106
" define(['url', 'css-font-face-src', 'cssom'], function (a0,b1,c2) {",
112107
" return (root['<%= pkg.name %>'] = factory(a0,b1,c2));",
113108
" });",
114-
" } else if (typeof require === 'object') {",
109+
" } else if (typeof exports === 'object') { // browserify context",
115110
" var cssom;",
116111
" try { cssom = require('cssom'); } catch (e) {}",
117-
" module.exports = factory(require('url'), require('css-font-face-src'), cssom);",
112+
" var f = factory(require('url'), require('css-font-face-src'), cssom);",
113+
" for(var prop in f) exports[prop] = f[prop];",
118114
" } else {",
119115
" root['<%= pkg.name %>'] = factory(url,cssFontFaceSrc,window.cssom);",
120116
" }",
121117
"}(this, function (url, cssFontFaceSrc, cssom) {",
122118
" var modules = {url: url, 'css-font-face-src': cssFontFaceSrc, cssom: cssom};",
123119
" var require = function (name) { if (modules[name]) { return modules[name]; } else { throw new Error('Module not found: ' + name); }; };",
124120
" // cheat browserify module to leave the function reference for us",
125-
" var module = {}, exports={};\n"].join('\n'),
126-
footer: [" return module.exports;",
121+
" var module = {}, exports={};",
122+
" // from here on it's browserify all the way\n"].join('\n'),
123+
footer: ["\n // back to us",
124+
" return module.exports;",
127125
"}));\n"].join('\n')
128126
},
129-
src: ['build/<%= pkg.name %>.js'],
127+
src: ['build/<%= pkg.name %>.bundled.js'],
130128
dest: 'dist/<%= pkg.name %>.js'
131129
}
132130
},
@@ -232,6 +230,7 @@ module.exports = function (grunt) {
232230
grunt.loadNpmTasks('grunt-browserify');
233231
grunt.loadNpmTasks('grunt-umd');
234232
grunt.loadNpmTasks('grunt-karma');
233+
grunt.loadNpmTasks('grunt-exec');
235234

236235
grunt.registerTask('testDeps', [
237236
'browserify:xmlserializer',
@@ -258,7 +257,7 @@ module.exports = function (grunt) {
258257
]);
259258

260259
grunt.registerTask('build', [
261-
'browserify:browser',
260+
'exec:bundle',
262261
'concat:dist'
263262
]);
264263

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
},
3434
"devDependencies": {
3535
"babel-polyfill": "^6.16.0",
36+
"derequire": "^2.0.3",
3637
"grunt": "^1.0.1",
3738
"grunt-browserify": "^5.0.0",
3839
"grunt-cli": "^1.2.0",
@@ -41,6 +42,7 @@
4142
"grunt-contrib-jshint": "^1.0.0",
4243
"grunt-contrib-uglify": "^2.0.0",
4344
"grunt-contrib-watch": "^1.0.0",
45+
"grunt-exec": "^1.0.1",
4446
"grunt-karma": "^2.0.0",
4547
"grunt-regex-check": "~0.1.5",
4648
"grunt-umd": "^2.3.6",

0 commit comments

Comments
 (0)