Domanda

I made some changes to Zepto and hope I can use it 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 `$` is not yet defined, point it to `Zepto`
-window.Zepto = Zepto
-'$' in window || (window.$ = Zepto)
+// detect module loader like 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 `$` is not yet defined, point it to `Zepto`
+  '$' in window || (window.$ = Zepto)  
+}

But I 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
    at /usr/lib/node_modules/watchify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:91:54
    at load (/usr/lib/node_modules/watchify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:54:43)
    at /usr/lib/node_modules/watchify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:60:22
    at /usr/lib/node_modules/watchify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:16:47
    at Object.oncomplete (fs.js:107:15)

Is there any solution?

È stato utile?

Soluzione

You can manually add module.exports = window.$ to the bottom of the zepto file or use browserify-shim in order to get your modules adapted for browserify on the fly.

I would recommend the latter option since editing 3rd party modules is problematic especially if you plan to upgrade them later.

browserify-shim was tested to work with zepto in particular.

Take a moment to study the readme and examples to learn how to properly set things up. You can then of course shim pretty much any library that attaches a variable to the global context, i.e. jquery.

On another note, the errors you got where due to a bug in browserify that has been fixed in the meantime.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top