javascript - How to make Zepto compatible with Browserify? -
i made changes zepto , hope can use in browserify:
➤➤ git diff diff --git a/package.json b/package.json index 294af90..e4f8fd1 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ , "dist": "coffee make dist" , "start": "coffee test/server.coffee" } + , "main": "dist/zepto.js" , "repository": { "type": "git" , "url": "https://github.com/madrobby/zepto.git" diff --git a/src/zepto.js b/src/zepto.js index 93bfe18..cdf8929 100644 --- a/src/zepto.js +++ b/src/zepto.js @@ -787,6 +787,17 @@ var zepto = (function() { return $ })() -// if `$` not yet defined, point `zepto` -window.zepto = zepto -'$' in window || (window.$ = zepto) +// detect module loader jquery +// http://code.jquery.com/jquery-2.0.3.js +if ( typeof module === "object" && module && typeof module.exports === "object" ) { + module.exports = zepto; +} else { + if ( typeof define === "function" && define.amd ) { + define( "zepto", [], function () { return zepto; } ); + } +} +if ( typeof window === "object" && typeof window.document === "object" ) { + window.zepto = zepto + // if `$` not yet defined, point `zepto` + '$' in window || (window.$ = zepto) +}
but got errors:
/usr/lib/node_modules/watchify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:91 var dir = path.resolve(x, pkg.main); ^ typeerror: cannot read property 'main' of undefined @ /usr/lib/node_modules/watchify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:91:54 @ load (/usr/lib/node_modules/watchify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:54:43) @ /usr/lib/node_modules/watchify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:60:22 @ /usr/lib/node_modules/watchify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:16:47 @ object.oncomplete (fs.js:107:15)
is there solution?
you can manually add module.exports = window.$
bottom of zepto file or use browserify-shim in order modules adapted browserify on fly.
i recommend latter option since editing 3rd party modules problematic if plan upgrade them later.
browserify-shim tested work zepto in particular.
take moment study readme , examples learn how set things up. can of course shim pretty library attaches variable global context, i.e. jquery.
on note, errors got due bug in browserify has been fixed in meantime.
Comments
Post a Comment