├── .gitignore ├── .jshintrc ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── examples ├── dependency-with-global │ ├── .bin │ │ └── browserify │ ├── Readme.md │ ├── build-diag.js │ ├── build.js │ ├── cli-diag.sh │ ├── cli.sh │ ├── index.html │ ├── js │ │ └── entry.js │ └── package.json ├── expose-jquery │ ├── .bin │ │ └── browserify │ ├── Readme.md │ ├── build-diag.js │ ├── build.js │ ├── cli-diag.sh │ ├── cli.sh │ ├── index.html │ ├── js │ │ └── entry.js │ ├── node_modules │ │ ├── .bin │ │ ├── browserify │ │ └── browserify-shim │ └── package.json ├── shim-jquery-external │ ├── Readme.md │ ├── build-diag.js │ ├── config │ │ └── shim.js │ ├── index.html │ ├── js │ │ ├── entry.js │ │ └── vendor │ │ │ └── .gitignore │ └── package.json ├── shim-jquery-ui │ ├── cli-diag.sh │ ├── index.html │ ├── js │ │ └── entry.js │ └── package.json └── shim-jquery │ ├── Readme.md │ ├── build-diag.js │ ├── build.js │ ├── cli-diag.sh │ ├── cli.sh │ ├── index.html │ ├── js │ ├── entry.js │ └── vendor │ │ └── .gitignore │ └── package.json ├── index.js ├── lib ├── debug.js ├── parse-inline-shims.js └── resolve-shims.js ├── package.json └── test ├── bower ├── components │ ├── jquery-ui │ │ ├── package.json │ │ └── ui │ │ │ └── jquery.ui.position.js │ └── jquery │ │ ├── jquery.js │ │ └── package.json ├── index.js ├── node_modules │ └── browserify-shim └── package.json ├── bundle-deps.js ├── bundle-ember-bower.js ├── bundle-expose-globals.js ├── bundle-invalid-require.js ├── bundle-multideps.js ├── bundle-nodeps.js ├── bundle-pack-bower.js ├── bundle-packs.js ├── deps ├── extshim │ ├── config │ │ └── shim.js │ ├── main.js │ ├── package.json │ └── vendor │ │ ├── non-cjs-dep.js │ │ └── non-cjs.js ├── inlineshim │ ├── main.js │ ├── package.json │ └── vendor │ │ ├── non-cjs-dep.js │ │ └── non-cjs.js └── node_modules │ └── browserify-shim ├── ember-bower ├── bower_components │ ├── ember │ │ ├── ember.js │ │ └── package.json │ ├── handlebars │ │ └── handlebars.js │ └── jquery │ │ ├── jquery.js │ │ └── package.json ├── main.js ├── node_modules │ └── browserify-shim └── package.json ├── exposify ├── extshim │ ├── config │ │ └── shim.js │ ├── main.js │ ├── package.json │ └── vendor │ │ └── non-cjs.js ├── inlineshim │ ├── main.js │ ├── package.json │ └── vendor │ │ └── non-cjs.js └── node_modules │ └── browserify-shim ├── invalid-require ├── main.js ├── node_modules │ └── browserify-shim ├── non-cjs.js └── package.json ├── multideps ├── extshim │ ├── config │ │ └── shim.js │ ├── main.js │ ├── package.json │ └── vendor │ │ ├── non-cjs-core.js │ │ ├── non-cjs-dep.js │ │ └── non-cjs.js ├── inlineshim │ ├── main.js │ ├── package.json │ └── vendor │ │ ├── non-cjs-core.js │ │ ├── non-cjs-dep.js │ │ └── non-cjs.js └── node_modules │ └── browserify-shim ├── nodeps ├── extshim-exposed │ ├── config │ │ └── shim.js │ ├── main.js │ ├── package.json │ └── vendor │ │ └── non-cjs.js ├── extshim │ ├── config │ │ └── shim.js │ ├── main.js │ ├── package.json │ └── vendor │ │ └── non-cjs.js ├── inlineshim-exposed │ ├── main.js │ ├── package.json │ └── vendor │ │ └── non-cjs.js ├── inlineshim │ ├── main.js │ ├── package.json │ └── vendor │ │ └── non-cjs.js └── node_modules │ └── browserify-shim ├── packs ├── main.js ├── node_modules │ ├── browserify-shim │ ├── sub1 │ │ ├── main.js │ │ ├── package.json │ │ └── vendor │ │ │ ├── non-cjs-dep.js │ │ │ └── non-cjs.js │ └── sub2 │ │ ├── config │ │ └── shim.js │ │ ├── main.js │ │ ├── package.json │ │ └── vendor │ │ ├── non-cjs-dep.js │ │ └── non-cjs.js ├── package.json └── vendor │ └── non-cjs.js ├── parse-inline-shim.js ├── resolve-shims-bower.js ├── resolve-shims.js └── shim ├── fixtures ├── entry-requires-depend-on-jquery-and-_.js ├── entry-requires-depend-on-jquery.js ├── entry-requires-jquery.js ├── entry-requires-lib-with-global-problem.js ├── entry-requires-root-level-var.js ├── entry-requires-this-iife.js ├── foo.js └── shims │ ├── crippled-jquery.js │ ├── lib-depending-on-jquery-and-_.js │ ├── lib-depending-on-jquery.js │ ├── lib-exporting-_.js │ ├── lib-with-exports-define-global-problem.js │ ├── root-level-var.js │ └── this-iife.js ├── shim-depends.js ├── shim-exports-define-window.js ├── shim-impress.js ├── shim-jquery.js ├── shim-root-level-var.js ├── shim-underscore.js ├── shim-zepto.js ├── shim.js └── utils ├── browserify-version.js └── test-lib.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | tmp 15 | 16 | npm-debug.log 17 | node_modules 18 | 19 | bundle.js 20 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "curly": false, 3 | "noempty": true, 4 | "newcap": true, 5 | "eqeqeq": true, 6 | "eqnull": true, 7 | "undef": true, 8 | "devel": true, 9 | "node": true, 10 | "browser": true, 11 | "evil": false, 12 | "latedef": false, 13 | "nonew": true, 14 | "immed": true, 15 | "smarttabs": true, 16 | "strict": true, 17 | "laxcomma": true, 18 | "laxbreak": true, 19 | "asi": true 20 | } 21 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | tmp 15 | 16 | npm-debug.log 17 | node_modules 18 | 19 | bundle.js 20 | 21 | test/ 22 | examples/ 23 | .npmignore 24 | .jshintrc 25 | .travis.yml 26 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - iojs 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 Thorsten Lorenz. 2 | All rights reserved. 3 | 4 | Permission is hereby granted, free of charge, to any person 5 | obtaining a copy of this software and associated documentation 6 | files (the "Software"), to deal in the Software without 7 | restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the 10 | Software is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # browserify-shim [![build status](https://secure.travis-ci.org/thlorenz/browserify-shim.svg?branch=master)](http://travis-ci.org/thlorenz/browserify-shim) 2 | 3 | ### Make CommonJS-Incompatible Files Browserifyable 4 | 5 | #### package.json 6 | 7 | ```json 8 | { 9 | "main": "./js/entry.js", 10 | "browser": { 11 | "jquery": "./js/vendor/jquery.js" 12 | }, 13 | "browserify-shim": { 14 | "jquery": "$", 15 | "three": "global:THREE" 16 | }, 17 | "browserify": { 18 | "transform": [ "browserify-shim" ] 19 | }, 20 | "dependencies": { 21 | "browserify-shim": "~3.2.0" 22 | } 23 | } 24 | ``` 25 | 26 | browserify . -d -o bundle.js 27 | 28 | 29 | 30 | **Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)* 31 | 32 | - [Installation](#installation) 33 | - [Features](#features) 34 | - [API](#api) 35 | - [You Will Always](#you-will-always) 36 | - [1. Install browserify-shim dependency](#1-install-browserify-shim-dependency) 37 | - [2. Register browserify-shim as a transform with browserify](#2-register-browserify-shim-as-a-transform-with-browserify) 38 | - [3. Provide browserify-shim config](#3-provide-browserify-shim-config) 39 | - [Short Form vs. Long Form config](#short-form-vs-long-form-config) 40 | - [You will sometimes](#you-will-sometimes) 41 | - [a) Expose global variables via `global:*`](#a-expose-global-variables-via-global) 42 | - [1. add script tag for library you want to expose](#1-add-script-tag-for-library-you-want-to-expose) 43 | - [2. Add expose global config to `package.json`](#2-add-expose-global-config-to-packagejson) 44 | - [2.a. Add expose global config to external shim config](#2a-add-expose-global-config-to-external-shim-config) 45 | - [3. Require library by the name it was exposed as](#3-require-library-by-the-name-it-was-exposed-as) 46 | - [Why not just `var THREE = window.THREE`?](#why-not-just-var-three-=-windowthree) 47 | - [b) Use aliases](#b-use-aliases) 48 | - [c) Provide an external shim config](#c-provide-an-external-shim-config) 49 | - [d) Diagnose what browserify-shim is doing](#d-diagnose-what-browserify-shim-is-doing) 50 | - [Multi Shim Example including dependencies](#multi-shim-example-including-dependencies) 51 | - [a) Config inside `package.json` without aliases](#a-config-inside-packagejson-without-aliases) 52 | - [b) Config inside `package.json` with aliases](#b-config-inside-packagejson-with-aliases) 53 | - [c) Config inside `./config/shim.js` without aliases](#c-config-inside-configshimjs-without-aliases) 54 | - [`package.json`](#packagejson) 55 | - [`shim.js`](#shimjs) 56 | - [More Examples](#more-examples) 57 | 58 | 59 | 60 | ## Installation 61 | 62 | npm install browserify browserify-shim 63 | 64 | *For a version compatible with browserify@1.x run `npm install browserify-shim@1.x` instead.* 65 | 66 | *For a version compatible with the [v2 API](https://github.com/thlorenz/browserify-shim/tree/v2#api) `npm install browserify-shim@2.x` instead.* 67 | 68 | ## Features 69 | 70 | The core features of browserify-shim are: 71 | 72 | - Shims **non-CommonJS** modules in order for them to be **browserified** by specifying an alias, the path to the file, 73 | and the identifier under which the module attaches itself to the global `window` object. 74 | - Includes `depends` for shimming libraries that depend on other libraries being in the global namespace. 75 | - applies shims configured inside the dependencies of your package 76 | 77 | Additionally, it handles the following real-world edge cases: 78 | 79 | - Modules that just declare a `var foo = ...` on the script level and assume it gets attached to the `window` object. 80 | Since the only way they will ever be run is in the global context — "ahem, … NO?!" 81 | - Makes `define` and also `module` be `undefined`, in order to fix [improperly-authored 82 | libraries](https://github.com/mhemesath/r2d3/blob/918bd076e4f980722438b2594d1eba53a522ce75/r2d3.v2.js#L222) that need 83 | shimming but try anyway to use AMD or CommonJS. For more info read the comment inside [this 84 | fixture](https://github.com/thlorenz/browserify-shim/blob/master/test/shim/fixtures/shims/lib-with-exports-define-global-problem.js) 85 | - removes invalid requires, i.e. `require('jquery')` although `'jquery'` isn't installed due to the library being 86 | improperly published or *installed* incorrectly via a downloader like [bower](http://bower.io/) 87 | 88 | Since `browserify-shim` is a proper `browserify` transform you can publish packages with files that need to be shimmed, 89 | granted that you specify the shim config inside the `package.json`. 90 | 91 | When `browserify` resolves your package it will run the `browserify-shim` transform and thus shim what's necessary 92 | when generating the bundle. 93 | 94 | `browserify-shim` walks upwards from each source file and uses the first `"browserify-shim"` configuration it finds in a `package.json` file. You **can't** shim files outside your project from your project's package. You **can** add multiple `package.json` files as long as browserify-shim can always find a package above each source file with the right configuration. 95 | 96 | ## API 97 | 98 | ### You Will Always 99 | 100 | #### 1. Install browserify-shim dependency 101 | 102 | In most cases you want to install it as a [devDependency](https://npmjs.org/doc/json.html#devDependencies) via: 103 | 104 | npm install -D browserify-shim 105 | 106 | #### 2. Register browserify-shim as a transform with browserify 107 | 108 | Inside `package.json` add: 109 | 110 | ```json 111 | { 112 | "browserify": { 113 | "transform": [ "browserify-shim" ] 114 | } 115 | } 116 | ``` 117 | 118 | Browserify transforms are run in order and may modify your source code along the way. You'll typically want to include browserify-shim last. 119 | 120 | #### 3. Provide browserify-shim config 121 | 122 | Inside `package.json` add: 123 | 124 | ```json 125 | { 126 | "browserify-shim": { 127 | "./js/vendor/jquery.js": "$", 128 | "three": "global:THREE" 129 | } 130 | } 131 | ``` 132 | 133 | The above includes `./js/vendor/jquery.js` (relative to the `package.json`) in the bundle and exports `window.$`. 134 | 135 | Additionally it exposes `window.THREE` as `three`, so you can `var three = require('three')`. More info 136 | [below](#a-expose-global-variables-via-global). 137 | 138 | ##### Short Form vs. Long Form config 139 | 140 | Since `jquery` does not depend on other shimmed modules and thus has no `depends` field, we used the short form to 141 | specify its exports, however the example above is equivalent to: 142 | 143 | ```json 144 | { 145 | "browserify-shim": { 146 | "./js/vendor/jquery.js": { "exports": "$" } 147 | } 148 | } 149 | ``` 150 | 151 | ### You will sometimes 152 | 153 | #### a) Expose global variables via `global:*` 154 | 155 | In some cases the libraries you are using are very large and you'd prefer to add them via a script tag instead to get 156 | the following benefits: 157 | 158 | - faster bundling times since the library is not included in the bundle 159 | - pull libraries from a [CDN](http://en.wikipedia.org/wiki/Content_delivery_network) which allows it to be pulled 160 | straight from the user's browser cache in case it was downloaded before 161 | 162 | We'll show how this works by taking the rather huge yet awesome `THREE.js` library as an example: 163 | 164 | ##### 1. add script tag for library you want to expose 165 | 166 | ```html 167 | 168 | 169 | 170 | 171 | 172 | ``` 173 | 174 | ##### 2. Add expose global config to `package.json` 175 | 176 | ```json 177 | { 178 | "browserify-shim": { 179 | "three": "global:THREE" 180 | } 181 | } 182 | ``` 183 | 184 | ##### 2.a. Add expose global config to external shim config 185 | 186 | In case you are using an external shim config, you may achieve the same by specifying the global via an `exports`. 187 | 188 | ```js 189 | module.exports = { 190 | 'three': { exports: 'global:THREE' } 191 | } 192 | ``` 193 | 194 | [more about external configs here](#c-config-inside-configshimjs-without-aliases) 195 | 196 | **Note:** `THREE.js` attaches `window.THREE`. 197 | 198 | ##### 3. Require library by the name it was exposed as 199 | 200 | ```js 201 | var THREE = require('three'); 202 | ``` 203 | 204 | ##### Why not just `var THREE = window.THREE`? 205 | 206 | You want to avoid spreading the knowledge that `THREE` is a global and stay consistent in how you resolve dependencies. 207 | Additionally if `THREE` would ever be published to [npm](https://npmjs.org/) and you decide to install it from there, 208 | you don't have to change any of your code since it already is `require`ing it properly. 209 | 210 | 211 | #### b) Use aliases 212 | 213 | You may expose files under a different name via the [`browser` field](https://gist.github.com/defunctzombie/4339901#replace-specific-files---advanced) and refer to them under that alias in the shim config: 214 | 215 | ```json 216 | { 217 | "browser": { 218 | "jquery": "./js/vendor/jquery.js" 219 | }, 220 | "browserify-shim": { 221 | "jquery": "$" 222 | } 223 | } 224 | ``` 225 | 226 | This also allows you to require this module under the alias, i.e.: `var $ = require('jquery')`. 227 | 228 | #### c) Provide an external shim config 229 | 230 | ```json 231 | { 232 | "browserify-shim": "./config/shim.js" 233 | } 234 | ``` 235 | 236 | The external shim format is very similar to the way in which the shim is specified inside the `package.json`. See 237 | [below](#c-config-inside-configshimjs-without-aliases) for more details. 238 | 239 | #### d) Diagnose what browserify-shim is doing 240 | 241 | You may encounter problems when your shim config isn't properly setup. In that case you can diagnose them via the 242 | `BROWSERIFYSHIM_DIAGNOSTICS` flag. 243 | 244 | Simply set the flag when building your bundle, i.e.: 245 | 246 | BROWSERIFYSHIM_DIAGNOSTICS=1 browserify -d . -o js/bundle.js 247 | 248 | or in a `build.js` script add: `process.env.BROWSERIFYSHIM_DIAGNOSTICS=1` to the top. 249 | 250 | ## Multi Shim Example including dependencies 251 | 252 | Some libraries depend on other libraries to have attached their exports to the window for historical reasons :(. 253 | (Hopefully soon we can truly say that this bad design is history.) 254 | 255 | In this contrived example we are shimming four libraries since none of them are commonJS compatible: 256 | 257 | - **x** exports **window.$** 258 | - **x-ui** exports nothing since it just **attaches itself to x**. Therefore x-ui depends on x. 259 | - **y** exports **window.Y** and also **depends on x** expecting to find it on the window as $. 260 | - **z** exports **window.zorro** and **depends on x and y**. It expects to find x on the window as $, but y on the window as YNOT, 261 | which is actually different than the name under which y exports itself. 262 | 263 | We will be using the `depends` field in order to ensure that a dependency is included and initialized before a library 264 | that depends on it is initialized. 265 | 266 | Below are three examples, each showing a way to properly shim the above mentioned modules. 267 | 268 | ### a) Config inside `package.json` without aliases 269 | 270 | ```json 271 | { 272 | "browserify": { 273 | "transform": [ "browserify-shim" ] 274 | }, 275 | "browserify-shim": { 276 | "./vendor/x.js" : "$", 277 | "./vendor/x-ui.js" : { "depends": [ "./vendor/x.js" ] }, 278 | "./vendor/y.js" : { "exports": "Y", "depends": [ "./vendor/x.js:$" ] }, 279 | "./vendor/z.js" : { "exports": "zorro", "depends": [ "./vendor/x.js:$", "./vendor/y.js:YNOT" ] } 280 | } 281 | } 282 | ``` 283 | 284 | **Note:** the `depends` array consists of entries of the format `path-to-file:export` 285 | 286 | ### b) Config inside `package.json` with aliases 287 | 288 | ```json 289 | { 290 | "browserify": { 291 | "transform": [ "browserify-shim" ] 292 | }, 293 | "browser": { 294 | "x" : "./vendor/x.js", 295 | "x-ui" : "./vendor/x-ui.js", 296 | "y" : "./vendor/y.js", 297 | "z" : "./vendor/z.js" 298 | }, 299 | "browserify-shim": { 300 | "x" : "$", 301 | "x-ui" : { "depends": [ "x" ] }, 302 | "y" : { "exports": "Y", "depends": [ "x:$" ] }, 303 | "z" : { "exports": "zorro", "depends": [ "x:$", "y:YNOT" ] } 304 | } 305 | } 306 | ``` 307 | 308 | **Note:** the `depends` entries make use of the aliases as well `alias:export` 309 | 310 | ### c) Config inside `./config/shim.js` without aliases 311 | 312 | #### `package.json` 313 | 314 | ```json 315 | { 316 | "browserify": { 317 | "transform": [ "browserify-shim" ] 318 | }, 319 | "browserify-shim": "./config/shim.js" 320 | } 321 | ``` 322 | 323 | #### `shim.js` 324 | 325 | ```js 326 | module.exports = { 327 | '../vendor/x.js' : { 'exports': '$' }, 328 | '../vendor/x-ui.js' : { 'depends': { '../vendor/x.js': null } }, 329 | '../vendor/y.js' : { 'exports': 'Y', 'depends': { '../vendor/x.js': '$' } }, 330 | '../vendor/z.js' : { 'exports': 'zorro', 'depends': { '../vendor/x.js': '$', '../vendor/y.js': 'YNOT' } } 331 | } 332 | ``` 333 | 334 | **Note:** all paths are relative to `./config/shim.js` instead of the `package.json`. 335 | 336 | The main difference to `a)` is the `depends` field specification. Instead it being an array of strings it expresses its dependencies as a hashmap: 337 | 338 | - **key:** `path-to-file` 339 | - **value:** the name under which it is expected to be attached on the window 340 | 341 | ## More Examples 342 | 343 | - [shim-jquery](https://github.com/thlorenz/browserify-shim/tree/master/examples/shim-jquery) 344 | - [expose-jquery](https://github.com/thlorenz/browserify-shim/tree/master/examples/expose-jquery) 345 | - [shim-jquery-external](https://github.com/thlorenz/browserify-shim/tree/master/examples/shim-jquery-external) 346 | - the [tests](https://github.com/thlorenz/browserify-shim/tree/master/test) are a great resource to investigate the 347 | different ways to configure shims and to understand how shims are applied to packages found inside the `node_modules` 348 | of your package 349 | -------------------------------------------------------------------------------- /examples/dependency-with-global/.bin/browserify: -------------------------------------------------------------------------------- 1 | ../browserify/bin/cmd.js -------------------------------------------------------------------------------- /examples/dependency-with-global/Readme.md: -------------------------------------------------------------------------------- 1 | # Browserify-Shim 2 | 3 | This example demonstrates how to create a browserify-aware dependency with a dependency on a global. 4 | 5 | The important part is in `node_modules/my-3d-library/package.json`: 6 | 7 | ```json 8 | "browserify-shim": { 9 | "three": "global:THREE" 10 | }, 11 | "browserify": { 12 | "transform": [ 13 | "browserify-shim" 14 | ] 15 | } 16 | ``` 17 | 18 | ### Bundling via the command line 19 | 20 | Given this config you can build the bundle via: 21 | 22 | browserify -d . > js/bundle.js 23 | 24 | demonstrated [here](https://github.com/thlorenz/browserify-shim/blob/master/examples/expose-jquery/cli.sh). 25 | 26 | If you want to turn on browserify shim diagnostics messages set the `BROWSERIFYSHIM_DIAGNOSTICS` environment variable 27 | when bundling i.e.: 28 | 29 | BROWSERIFYSHIM_DIAGNOSTICS=1 browserify -d . > js/bundle.js 30 | 31 | demonstrated [here](https://github.com/thlorenz/browserify-shim/blob/master/examples/expose-jquery/cli-diag.sh). 32 | 33 | **Note** that in both cases the `-d` flag is added as well in order to turn on browserify sourcemaps. 34 | 35 | **Note** `.` tells browserify to use the current dir as the root of the bundling chain. As a result it finds `"main": 36 | "./js/entry.js"` in the `package.json` and thus uses that as the entry point. 37 | 38 | ### Bundling via a `.js` script 39 | 40 | Alternatively you can write a short `build.js` to perform the bundling step: 41 | 42 | ```js 43 | browserify() 44 | .require(require.resolve('./'), { entry: true }) 45 | .bundle({ debug: true }) 46 | .on('error', console.error.bind(console)) 47 | .pipe(fs.createWriteStream(path.join(__dirname, 'js', 'bundle.js'), 'utf8')) 48 | ``` 49 | 50 | demonstrated [here](https://github.com/thlorenz/browserify-shim/blob/master/examples/expose-jquery/build.js). 51 | 52 | To run this example: 53 | 54 | npm install browserify-shim 55 | npm explore browserify-shim 56 | 57 | Then: 58 | 59 | npm run dependency-with-global 60 | 61 | Or to see diagnostic messages: 62 | 63 | npm run dependency-with-global-diag 64 | -------------------------------------------------------------------------------- /examples/dependency-with-global/build-diag.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | process.env.BROWSERIFYSHIM_DIAGNOSTICS = 1; 4 | 5 | var path = require('path') 6 | , fs = require('fs') 7 | , browserify = require('browserify') 8 | ; 9 | 10 | (function bundle() { 11 | // This is function is the important part and should be similar to what you would use for your project 12 | var builtFile = path.join(__dirname, 'js', 'bundle.js'); 13 | 14 | browserify() 15 | // this resolves main file of our package 16 | .require(require.resolve('./'), { entry: true }) 17 | .bundle({ debug: true }) 18 | .on('end', function () { 19 | console.log('Build succeeded, open index.html to see the result.'); 20 | }) 21 | .on('error', console.error.bind(console)) 22 | .pipe(fs.createWriteStream(builtFile, 'utf8')) 23 | })(); 24 | -------------------------------------------------------------------------------- /examples/dependency-with-global/build.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var path = require('path') 4 | , fs = require('fs') 5 | , browserify = require('browserify') 6 | ; 7 | 8 | (function bundle() { 9 | // This is function is the important part and should be similar to what you would use for your project 10 | var builtFile = path.join(__dirname, 'js', 'bundle.js'); 11 | 12 | browserify() 13 | // this resolves main file of our package 14 | .require(require.resolve('./'), { entry: true }) 15 | .bundle({ debug: true }) 16 | .on('end', function () { 17 | console.log('Build succeeded, open index.html to see the result.'); 18 | }) 19 | .on('error', console.error.bind(console)) 20 | .pipe(fs.createWriteStream(builtFile, 'utf8')) 21 | })(); 22 | -------------------------------------------------------------------------------- /examples/dependency-with-global/cli-diag.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | BROWSERIFYSHIM_DIAGNOSTICS=1 ./node_modules/.bin/browserify -d . > js/bundle.js 4 | 5 | open index.html 6 | -------------------------------------------------------------------------------- /examples/dependency-with-global/cli.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ./node_modules/.bin/browserify -d . > js/bundle.js 4 | 5 | open index.html 6 | -------------------------------------------------------------------------------- /examples/dependency-with-global/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | dependency with global example 6 | 7 | 8 | 9 | 10 |

Welcome to the browserify-shim dependency with global Example

11 |

You are using Three.js version:

12 | 13 |

14 | If you open your devtools and find entry.js or bundle.js you will see that require('three') was replaced with (window.THREE). 15 |

16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /examples/dependency-with-global/js/entry.js: -------------------------------------------------------------------------------- 1 | var my3dLibrary = require('my-3d-library') 2 | , $ = require('jquery'); 3 | 4 | $('#three-version').text('r' + my3dLibrary.threeVersion); 5 | -------------------------------------------------------------------------------- /examples/dependency-with-global/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dependency-with-global", 3 | "version": "0.0.0", 4 | "description": "Example of using browserify-shim with a dependency that requires a separately loaded global.", 5 | "main": "./js/entry.js", 6 | "browserify-shim": { 7 | "jquery": "global:$" 8 | }, 9 | "browserify": { 10 | "transform": [ 11 | "browserify-shim" 12 | ] 13 | }, 14 | "repository": "", 15 | "author": "Paul Melnikow", 16 | "license": "BSD", 17 | "dependencies": { 18 | "request": "~2.88.0" 19 | }, 20 | "devDependencies": { 21 | "browserify": "~2.36.1", 22 | "browserify-shim": "~3.2.0" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /examples/expose-jquery/.bin/browserify: -------------------------------------------------------------------------------- 1 | ../browserify/bin/cmd.js -------------------------------------------------------------------------------- /examples/expose-jquery/Readme.md: -------------------------------------------------------------------------------- 1 | # Browserify-Shim expose jquery 2 | 3 | This example demonstrates expsing jquery by adding expose information to the `package.json`. 4 | 5 | The following config included in the 6 | [`package.json`](https://github.com/thlorenz/browserify-shim/blob/master/examples/expose-jquery/package.json) of this 7 | example project is needed to ensure that browserify runs the `browerify-shim` transform when bundling this project. 8 | 9 | ```json 10 | { 11 | "main": "./js/entry.js", 12 | "browserify-shim": { 13 | "jquery": "global:$" 14 | }, 15 | "browserify": { 16 | "transform": [ "browserify-shim" ] 17 | } 18 | } 19 | ``` 20 | 21 | **Note:** the `global:` prefix tells browserify-shim that it should expose the `$` attached to the `window` as `jquery`. 22 | 23 | ### Bundling via the command line 24 | 25 | Given this config you can build the bundle via: 26 | 27 | browserify -d . > js/bundle.js 28 | 29 | demonstrated [here](https://github.com/thlorenz/browserify-shim/blob/master/examples/expose-jquery/cli.sh). 30 | 31 | If you want to turn on browserify shim diagnostics messages set the `BROWSERIFYSHIM_DIAGNOSTICS` environment variable 32 | when bundling i.e.: 33 | 34 | BROWSERIFYSHIM_DIAGNOSTICS=1 browserify -d . > js/bundle.js 35 | 36 | demonstrated [here](https://github.com/thlorenz/browserify-shim/blob/master/examples/expose-jquery/cli-diag.sh). 37 | 38 | **Note** that in both cases the `-d` flag is added as well in order to turn on browserify sourcemaps. 39 | 40 | **Note** `.` tells browserify to use the current dir as the root of the bundling chain. As a result it finds `"main": 41 | "./js/entry.js"` in the `package.json` and thus uses that as the entry point. 42 | 43 | ### Bundling via a `.js` script 44 | 45 | Alternatively you can write a short `build.js` to perform the bundling step: 46 | 47 | ```js 48 | browserify() 49 | .require(require.resolve('./'), { entry: true }) 50 | .bundle({ debug: true }) 51 | .on('error', console.error.bind(console)) 52 | .pipe(fs.createWriteStream(path.join(__dirname, 'js', 'bundle.js'), 'utf8')) 53 | ``` 54 | 55 | demonstrated [here](https://github.com/thlorenz/browserify-shim/blob/master/examples/expose-jquery/build.js). 56 | 57 | To run this example: 58 | 59 | npm install browserify-shim 60 | npm explore browserify-shim 61 | 62 | Then: 63 | 64 | npm run expose-jquery 65 | 66 | Or to see diagnostic messages: 67 | 68 | 69 | npm run expose-jquery-diag 70 | -------------------------------------------------------------------------------- /examples/expose-jquery/build-diag.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | process.env.BROWSERIFYSHIM_DIAGNOSTICS = 1; 4 | 5 | var path = require('path') 6 | , fs = require('fs') 7 | , browserify = require('browserify') 8 | ; 9 | 10 | (function bundle() { 11 | // This is function is the important part and should be similar to what you would use for your project 12 | var builtFile = path.join(__dirname, 'js', 'bundle.js'); 13 | 14 | browserify() 15 | // this resolves main file of our package 16 | .require(require.resolve('./'), { entry: true }) 17 | .bundle({ debug: true }) 18 | .on('end', function () { 19 | console.log('Build succeeded, open index.html to see the result.'); 20 | }) 21 | .on('error', console.error.bind(console)) 22 | .pipe(fs.createWriteStream(builtFile, 'utf8')) 23 | })(); 24 | -------------------------------------------------------------------------------- /examples/expose-jquery/build.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var path = require('path') 4 | , fs = require('fs') 5 | , browserify = require('browserify') 6 | ; 7 | 8 | (function bundle() { 9 | // This is function is the important part and should be similar to what you would use for your project 10 | var builtFile = path.join(__dirname, 'js', 'bundle.js'); 11 | 12 | browserify() 13 | // this resolves main file of our package 14 | .require(require.resolve('./'), { entry: true }) 15 | .bundle({ debug: true }) 16 | .on('end', function () { 17 | console.log('Build succeeded, open index.html to see the result.'); 18 | }) 19 | .on('error', console.error.bind(console)) 20 | .pipe(fs.createWriteStream(builtFile, 'utf8')) 21 | })(); 22 | -------------------------------------------------------------------------------- /examples/expose-jquery/cli-diag.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | BROWSERIFYSHIM_DIAGNOSTICS=1 ./node_modules/.bin/browserify -d . > js/bundle.js 4 | 5 | open index.html 6 | -------------------------------------------------------------------------------- /examples/expose-jquery/cli.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ./node_modules/.bin/browserify -d . > js/bundle.js 4 | 5 | open index.html 6 | -------------------------------------------------------------------------------- /examples/expose-jquery/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | jquery shim example 6 | 7 | 8 | 9 |

Welcome to the browserify-shim expose jquery Example

10 |

You are using jquery version:

11 | 12 |

13 | If you open your devtools and find entry.js or bundle.js you will see that require('jquery') was replaced with (window.$). 14 |

15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /examples/expose-jquery/js/entry.js: -------------------------------------------------------------------------------- 1 | var $ = require('jquery') 2 | , jqVersion = $().jquery; 3 | 4 | $('#jq-version').text(jqVersion); 5 | -------------------------------------------------------------------------------- /examples/expose-jquery/node_modules/.bin: -------------------------------------------------------------------------------- 1 | ../../../node_modules/.bin -------------------------------------------------------------------------------- /examples/expose-jquery/node_modules/browserify: -------------------------------------------------------------------------------- 1 | ../../../node_modules/browserify -------------------------------------------------------------------------------- /examples/expose-jquery/node_modules/browserify-shim: -------------------------------------------------------------------------------- 1 | ../../../ -------------------------------------------------------------------------------- /examples/expose-jquery/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shim-jquery", 3 | "version": "0.0.0", 4 | "description": "Example of using browserify-shim to expose jquery for use with browserify", 5 | "main": "./js/entry.js", 6 | "browserify-shim": { 7 | "jquery": "global:$" 8 | }, 9 | "browserify": { 10 | "transform": [ 11 | "browserify-shim" 12 | ] 13 | }, 14 | "repository": "", 15 | "author": "Thorsten Lorenz", 16 | "license": "BSD", 17 | "dependencies": { 18 | "request": "~2.88.0" 19 | }, 20 | "devDependencies": { 21 | "browserify": "~2.36.1", 22 | "browserify-shim": "~3.2.0" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /examples/shim-jquery-external/Readme.md: -------------------------------------------------------------------------------- 1 | # Browserify-Shim jquery with external shim file 2 | 3 | This example demonstrates shimming jquery by adding the shim information to an external `shim.js`. 4 | 5 | The following config included in the 6 | [`package.json`](https://github.com/thlorenz/browserify-shim/blob/master/examples/shim-jquery-external/package.json) of this 7 | example project is needed to ensure that browserify runs the `browerify-shim` transform when bundling this project, 8 | however in this case it **does not include the shim config**: 9 | 10 | ```json 11 | { 12 | "main": "./js/entry.js", 13 | "browserify-shim": "./config/shim.js", 14 | "browserify": { 15 | "transform": [ 16 | "browserify-shim" 17 | ] 18 | } 19 | } 20 | ``` 21 | 22 | As you can see the `browserify-shim` field points to the [external shim file](https://github.com/thlorenz/browserify-shim/blob/master/examples/shim-jquery-external/config/shim.js): 23 | 24 | ```js 25 | module.exports = { 26 | '../js/vendor/jquery.js': { exports: '$' } 27 | } 28 | ``` 29 | 30 | **Note**: we didn't expose `./js/vendor/jquery.js` as `jquery` like we did in the [other jquery 31 | example](https://github.com/thlorenz/browserify-shim/tree/master/examples/shim-jquery). Instead our config spells out the 32 | path to `./js/vendor/jquery.js` relative to the `shim.js` file. 33 | 34 | As a result we also cannot just `require('jquery')` in our [entry.js](https://github.com/thlorenz/browserify-shim/blob/master/examples/shim-jquery-external/js/entry.js#L1), but have to spell out the relative path here as well, i.e. 35 | `var $ = require('./vendor/jquery')`. 36 | 37 | ### Bundling 38 | 39 | As explained in the [shim-jquery example](https://github.com/thlorenz/browserify-shim/tree/master/examples/shim-jquery) 40 | there are multiple ways to produce the bundle. 41 | 42 | browserify -d . > js/bundle.js 43 | 44 | Or with diagnostics: 45 | 46 | BROWSERIFYSHIM_DIAGNOSTICS=1 browserify -d . > js/bundle.js 47 | 48 | Alternatively you can write a short `build.js` to perform the bundling step: 49 | 50 | ```js 51 | browserify() 52 | .require(require.resolve('./'), { entry: true }) 53 | .bundle({ debug: true }) 54 | .on('error', console.error.bind(console)) 55 | .pipe(fs.createWriteStream(path.join(__dirname, 'js', 'bundle.js'), 'utf8')) 56 | ``` 57 | -------------------------------------------------------------------------------- /examples/shim-jquery-external/build-diag.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | process.env.BROWSERIFYSHIM_DIAGNOSTICS = 1; 4 | 5 | var request = require('request') 6 | , fs = require('fs') 7 | , path = require('path') 8 | , browserify = require('browserify') 9 | ; 10 | 11 | function bundle() { 12 | // This is function is the important part and should be similar to what you would use for your project 13 | var builtFile = path.join(__dirname, 'js', 'bundle.js'); 14 | 15 | browserify() 16 | // this resolves main file of our package 17 | .require(require.resolve('./'), { entry: true }) 18 | .bundle({ debug: true }) 19 | .on('end', function () { 20 | console.log('Build succeeded, open index.html to see the result.'); 21 | }) 22 | .on('error', console.error.bind(console)) 23 | .pipe(fs.createWriteStream(builtFile, 'utf8')) 24 | } 25 | 26 | // Normally jquery.js would be in vendor folder already, but I wanted to avoid spreading jquerys all over github ;) 27 | // So lets download jquery and then run the bundler. 28 | request('http://code.jquery.com/jquery-1.8.3.min.js', function(err, resp, body) { 29 | var jqueryFile = path.join(__dirname, 'js/vendor/jquery.js'); 30 | 31 | fs.writeFileSync(jqueryFile, body); 32 | 33 | bundle(); 34 | }); 35 | -------------------------------------------------------------------------------- /examples/shim-jquery-external/config/shim.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | '../js/vendor/jquery.js': { exports: '$' } 3 | } 4 | -------------------------------------------------------------------------------- /examples/shim-jquery-external/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | jquery shim example 6 | 7 | 8 |

Welcome to the browserify-shim jquery Example

9 |

You are using jquery version:

10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /examples/shim-jquery-external/js/entry.js: -------------------------------------------------------------------------------- 1 | var $ = require('./vendor/jquery') 2 | , jqVersion = $().jquery; 3 | 4 | $('#jq-version').text(jqVersion); 5 | -------------------------------------------------------------------------------- /examples/shim-jquery-external/js/vendor/.gitignore: -------------------------------------------------------------------------------- 1 | jquery.js 2 | -------------------------------------------------------------------------------- /examples/shim-jquery-external/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shim-jquery-external", 3 | "version": "0.0.0", 4 | "description": "Example of using browserify-shim to shim jquery for use with browserify, using an external shim file", 5 | "main": "./js/entry.js", 6 | "browserify-shim": "./config/shim.js", 7 | "browserify": { 8 | "transform": [ 9 | "browserify-shim" 10 | ] 11 | }, 12 | "scripts": { 13 | "test": "echo \"Error: no test specified\" && exit 1" 14 | }, 15 | "repository": "", 16 | "author": "Thorsten Lorenz", 17 | "license": "BSD", 18 | "dependencies": { 19 | "request": "~2.12.0" 20 | }, 21 | "devDependencies": { 22 | "browserify": "~2.36.1", 23 | "browserify-shim": "~3.0.0" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /examples/shim-jquery-ui/cli-diag.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | BROWSERIFYSHIM_DIAGNOSTICS=1 ../../node_modules/.bin/browserify -d ./js/entry.js > ./js/bundle.js 4 | 5 | open index.html 6 | -------------------------------------------------------------------------------- /examples/shim-jquery-ui/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | jquery-ui shim example 6 | 7 | 8 | 9 | 10 |

Welcome to the browserify-shim jquery-ui Example

11 |

You are using jquery version:

12 | 13 | 14 |
15 | 16 |

17 | If you open your devtools and find entry.js or bundle.js you will see that require('jquery') was replaced with (window.$). 18 |

19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /examples/shim-jquery-ui/js/entry.js: -------------------------------------------------------------------------------- 1 | var $ = require('jquery') 2 | , jqVersion = $().jquery; 3 | require('jquery-ui'); 4 | 5 | $('#jq-version').text(jqVersion); 6 | $( "#slider" ).slider(); 7 | -------------------------------------------------------------------------------- /examples/shim-jquery-ui/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shim-jquery-ui", 3 | "version": "0.0.0", 4 | "description": "Example of using browserify-shim to shim jquery-ui for use with browserify", 5 | "main": "./js/entry.js", 6 | "browser": { 7 | "jquery": "./node_modules/jquery/dist/jquery.js" 8 | }, 9 | "browserify-shim": { 10 | "jquery": "$", 11 | "jquery-ui": {"depends": "jquery:$"} 12 | }, 13 | "browserify": { 14 | "transform": [ 15 | "browserify-shim" 16 | ] 17 | }, 18 | "scripts": { 19 | "test": "echo \"Error: no test specified\" && exit 1" 20 | }, 21 | "repository": "", 22 | "author": "Thorsten Lorenz", 23 | "license": "BSD", 24 | "dependencies": { 25 | "request": "~2.88.0" 26 | }, 27 | "devDependencies": { 28 | "browserify": "~2.36.1", 29 | "browserify-shim": "~3.0.0", 30 | "jquery": "^3.5.0", 31 | "jquery-ui": "^1.10.5" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /examples/shim-jquery/Readme.md: -------------------------------------------------------------------------------- 1 | # Browserify-Shim jquery 2 | 3 | This example demonstrates shimming jquery by adding all shim information to the `package.json`. 4 | 5 | The following config included in the 6 | [`package.json`](https://github.com/thlorenz/browserify-shim/blob/master/examples/shim-jquery/package.json) of this 7 | example project is needed to ensure that browserify runs the `browerify-shim` transform when bundling this project. 8 | 9 | ```json 10 | { 11 | "main": "./js/entry.js", 12 | "browser": { 13 | "jquery": "./js/vendor/jquery.js" 14 | }, 15 | "browserify-shim": { 16 | "jquery": "$" 17 | }, 18 | "browserify": { 19 | "transform": [ "browserify-shim" ] 20 | } 21 | } 22 | ``` 23 | 24 | ### Bundling via the command line 25 | 26 | Given this config you can build the bundle via: 27 | 28 | browserify -d . > js/bundle.js 29 | 30 | demonstrated [here](https://github.com/thlorenz/browserify-shim/blob/master/examples/shim-jquery/cli.sh). 31 | 32 | If you want to turn on browserify shim diagnostics messages set the `BROWSERIFYSHIM_DIAGNOSTICS` environment variable 33 | when bundling i.e.: 34 | 35 | BROWSERIFYSHIM_DIAGNOSTICS=1 browserify -d . > js/bundle.js 36 | 37 | demonstrated [here](https://github.com/thlorenz/browserify-shim/blob/master/examples/shim-jquery/cli-diag.sh). 38 | 39 | **Note** that in both cases the `-d` flag is added as well in order to turn on browserify sourcemaps. 40 | 41 | **Note** `.` tells browserify to use the current dir as the root of the bundling chain. As a result it finds `"main": 42 | "./js/entry.js"` in the `package.json` and thus uses that as the entry point. 43 | 44 | ### Bundling via a `.js` script 45 | 46 | Alternatively you can write a short `build.js` to perform the bundling step: 47 | 48 | ```js 49 | browserify() 50 | .require(require.resolve('./'), { entry: true }) 51 | .bundle({ debug: true }) 52 | .on('error', console.error.bind(console)) 53 | .pipe(fs.createWriteStream(path.join(__dirname, 'js', 'bundle.js'), 'utf8')) 54 | ``` 55 | 56 | demonstrated [here](https://github.com/thlorenz/browserify-shim/blob/master/examples/shim-jquery/build.js). 57 | 58 | To run this example: 59 | 60 | npm install browserify-shim 61 | npm explore browserify-shim 62 | 63 | Then: 64 | 65 | npm run shim-jquery 66 | 67 | Or to see diagnostic messages: 68 | 69 | 70 | npm run shim-jquery-diag 71 | -------------------------------------------------------------------------------- /examples/shim-jquery/build-diag.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | process.env.BROWSERIFYSHIM_DIAGNOSTICS = 1; 4 | 5 | var request = require('request') 6 | , fs = require('fs') 7 | , path = require('path') 8 | , browserify = require('browserify') 9 | ; 10 | 11 | function bundle() { 12 | // This is function is the important part and should be similar to what you would use for your project 13 | var builtFile = path.join(__dirname, 'js', 'bundle.js'); 14 | 15 | browserify() 16 | // this resolves main file of our package 17 | .require(require.resolve('./'), { entry: true }) 18 | .bundle({ debug: true }) 19 | .on('end', function () { 20 | console.log('Build succeeded, open index.html to see the result.'); 21 | }) 22 | .on('error', console.error.bind(console)) 23 | .pipe(fs.createWriteStream(builtFile, 'utf8')) 24 | } 25 | 26 | // Normally jquery.js would be in vendor folder already, but I wanted to avoid spreading jquerys all over github ;) 27 | // So lets download jquery and then run the bundler. 28 | request('http://code.jquery.com/jquery-1.8.3.min.js', function(err, resp, body) { 29 | var jqueryFile = path.join(__dirname, 'js/vendor/jquery.js'); 30 | 31 | fs.writeFileSync(jqueryFile, body); 32 | 33 | bundle(); 34 | }); 35 | -------------------------------------------------------------------------------- /examples/shim-jquery/build.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var request = require('request') 4 | , fs = require('fs') 5 | , path = require('path') 6 | , browserify = require('browserify') 7 | ; 8 | 9 | function bundle() { 10 | // This is function is the important part and should be similar to what you would use for your project 11 | var builtFile = path.join(__dirname, 'js', 'bundle.js'); 12 | 13 | browserify() 14 | // this resolves main file of our package 15 | .require(require.resolve('./'), { entry: true }) 16 | .bundle({ debug: true }) 17 | .on('end', function () { 18 | console.log('Build succeeded, open index.html to see the result.'); 19 | }) 20 | .on('error', console.error.bind(console)) 21 | .pipe(fs.createWriteStream(builtFile, 'utf8')) 22 | } 23 | 24 | // Normally jquery.js would be in vendor folder already, but I wanted to avoid spreading jquerys all over github ;) 25 | // So lets download jquery and then run the bundler. 26 | request('http://code.jquery.com/jquery-1.8.3.min.js', function(err, resp, body) { 27 | var jqueryFile = path.join(__dirname, 'js/vendor/jquery.js'); 28 | 29 | fs.writeFileSync(jqueryFile, body); 30 | 31 | bundle(); 32 | }); 33 | -------------------------------------------------------------------------------- /examples/shim-jquery/cli-diag.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | BROWSERIFYSHIM_DIAGNOSTICS=1 ../../node_modules/.bin/browserify -d . > js/bundle.js 4 | 5 | open index.html 6 | -------------------------------------------------------------------------------- /examples/shim-jquery/cli.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ../../node_modules/.bin/browserify -d . > js/bundle.js 4 | 5 | open index.html 6 | -------------------------------------------------------------------------------- /examples/shim-jquery/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | jquery shim example 6 | 7 | 8 |

Welcome to the browserify-shim jquery Example

9 |

You are using jquery version:

10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /examples/shim-jquery/js/entry.js: -------------------------------------------------------------------------------- 1 | var $ = require('jquery') 2 | , jqVersion = $().jquery; 3 | 4 | $('#jq-version').text(jqVersion); 5 | -------------------------------------------------------------------------------- /examples/shim-jquery/js/vendor/.gitignore: -------------------------------------------------------------------------------- 1 | jquery.js 2 | -------------------------------------------------------------------------------- /examples/shim-jquery/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shim-jquery", 3 | "version": "0.0.0", 4 | "description": "Example of using browserify-shim to shim jquery for use with browserify", 5 | "main": "./js/entry.js", 6 | "browser": { 7 | "jquery": "./js/vendor/jquery.js" 8 | }, 9 | "browserify-shim": { 10 | "jquery": "$" 11 | }, 12 | "browserify": { 13 | "transform": [ 14 | "browserify-shim" 15 | ] 16 | }, 17 | "scripts": { 18 | "test": "echo \"Error: no test specified\" && exit 1" 19 | }, 20 | "repository": "", 21 | "author": "Thorsten Lorenz", 22 | "license": "BSD", 23 | "dependencies": { 24 | "request": "~2.88.0" 25 | }, 26 | "devDependencies": { 27 | "browserify": "~2.36.1", 28 | "browserify-shim": "~3.0.0" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var util = require('util') 4 | , resolve = require('resolve') 5 | , exposify = require('exposify') 6 | , format = require('util').format 7 | , path = require('path') 8 | , through = require('through') 9 | , resolveShims = require('./lib/resolve-shims') 10 | , rename = require('rename-function-calls') 11 | , debug = require('./lib/debug') 12 | 13 | var shimRequire = '__browserify_shim_require__'; 14 | 15 | function requireDependencies(depends, packageRoot, browserAliases, dependencies) { 16 | if (!depends) return ''; 17 | 18 | function customResolve (k) { 19 | // resolve aliases to full paths to avoid conflicts when require is injected into a file 20 | // inside another package, i.e. the it's shim was defined in a package.json one level higher 21 | // aliases don't get resolved by browserify in that case, since it only looks in the package.json next to it 22 | var browserAlias = browserAliases && browserAliases[k] 23 | , dependency = dependencies && dependencies[k] 24 | , alias; 25 | 26 | try { 27 | // prefer browser aliases defined explicitly 28 | alias = browserAlias 29 | ? path.resolve(packageRoot, browserAlias) 30 | 31 | // but also consider dependencies installed in the package in which shims were defined 32 | : dependency 33 | ? resolve.sync(k, { basedir: packageRoot }) 34 | 35 | // lets hope for the best that browserify will be able to resolve this, cause we can't 36 | : k; 37 | } catch (err) { 38 | // resolve.sync may fail, in which case we give up and hope browserify can figure it out 39 | alias = k; 40 | } 41 | 42 | return { alias: alias, exports: depends[k] || null }; 43 | } 44 | 45 | function noResolve(k) { 46 | return { alias: k, exports: depends[k] || null }; 47 | } 48 | 49 | return Object.keys(depends) 50 | 51 | // if the package was looked up from the parent of its enclosing package we need to pre-resolve the depends 52 | .map(customResolve) 53 | .reduce( 54 | function (acc, dep) { 55 | var alias = dep.alias.replace(/\\/g, "\\\\"); 56 | return dep.exports 57 | // Example: jQuery = global.jQuery = require("jquery"); 58 | // the global dangling variable is needed cause some libs reference it as such and it breaks outside of the browser, 59 | // i.e.: (function ($) { ... })( jQuery ) 60 | // This little extra makes it work everywhere and since it's on top, it will be shadowed by any other definitions 61 | // so it doesn't conflict with anything. 62 | ? acc + dep.exports + ' = global.' + dep.exports + ' = require("' + alias + '");\n' 63 | : acc + 'require("' + alias + '");\n'; 64 | } 65 | , '\n; ' 66 | ); 67 | } 68 | 69 | function bindWindowWithExports(s, dependencies) { 70 | // purposely make module, exports, require and define be 'undefined', 71 | // but pass a function that allows exporting our dependency from the window or the context 72 | 73 | // This results in code similarly to this example which shims ember which depends on jquery: 74 | 75 | /** 76 | * -- browserify wrapper 77 | * function(require,module,exports){ 78 | * 79 | * -- our deps (which still have access to require) 80 | * jquery = global.jquery = require("/full/path/to/jquery.js"); 81 | * 82 | * -- assigning shimmed require to actual require 83 | * -- this shouldn't matter, but would fix cases where libraries reach __browserify_shim_require__(x) as long 84 | * -- as x was included in the bundle 85 | * 86 | * __browserify_shim_require__=require; 87 | * 88 | * -- also it won't hurt anything 89 | * 90 | * -- browserify-shim wrapper 91 | * (function browserifyShim(module, exports, require, define, browserify_shim__define__module__export__) { 92 | * -- inside this function neither module, exports, require, or define are defined 93 | * 94 | * -- browserify_shim__define__module__export__ allows exporting (since module and exports aren't available) 95 | * 96 | * [..] -- code that needs shimming 97 | * 98 | * -- exporting whatever ember attached to the window 99 | * ; browserify_shim__define__module__export__(typeof ember != "undefined" ? ember : window.ember); 100 | * 101 | * }).call(global, undefined, undefined, undefined, undefined, function defineExport(ex) { module.exports = ex; }); 102 | * -- browserify-shim wrapper closed 103 | * } 104 | * -- browserify wrapper closed 105 | */ 106 | 107 | // Shadowing require is necessary to fix code that tries to do common-js, but ends up requiring deps that cannot be resolved 108 | // In the case below we want the below condition to be false at run time. 109 | /** 110 | * if (!jQuery && typeof require === 'function') { 111 | * jQuery = require('jquery'); 112 | * } 113 | */ 114 | 115 | // Additionally `require('jquery')` needs to be refactored to prevent browserify from looking for 'jquery' at bundle time. 116 | // The rewriting step happens inside the main @see shim function. 117 | // Thus it gets rewritten via rename-function-calls: 118 | /** 119 | * if (!jQuery && typeof require === 'function') { 120 | * jQuery = __browserify_shim_removed_require__('jquery'); 121 | * } 122 | */ 123 | // The fact that __browserify_shim_removed_require__ is not defined doesn't matter since we never enter that block. 124 | 125 | return dependencies 126 | + '; var ' + shimRequire + '=require;' 127 | + '(function browserifyShim(module, exports, require, define, browserify_shim__define__module__export__) {\n' 128 | + s 129 | + '\n}).call(global, undefined, undefined, undefined, undefined, function defineExport(ex) { module.exports = ex; });\n'; 130 | } 131 | 132 | function bindWindowWithoutExports(s, dependencies) { 133 | // if a module doesn't need anything to be exported, it is likely, that it exports itself properly 134 | // therefore it is not a good idea to override the module here, however we need to still disable require 135 | // all else is similar to @see bindWindowWithExports 136 | return dependencies 137 | + '; var ' + shimRequire + '=require;' 138 | + '(function browserifyShim(module, define, require) {\n' 139 | + s 140 | + '\n}).call(global, module, undefined, undefined);\n'; 141 | } 142 | 143 | function moduleExport(exp) { 144 | return format('\n; browserify_shim__define__module__export__(typeof %s != "undefined" ? %s : window.%s);\n', exp, exp, exp); 145 | } 146 | 147 | function wrap(content, config, packageRoot, browserAliases) { 148 | var exported = config.exports 149 | ? content + moduleExport(config.exports) 150 | : content 151 | , dependencies = requireDependencies(config.depends, packageRoot, browserAliases) 152 | , boundWindow = config.exports 153 | ? bindWindowWithExports(exported, dependencies) 154 | : bindWindowWithoutExports(exported, dependencies); 155 | 156 | return boundWindow; 157 | } 158 | 159 | module.exports = function shim(file) { 160 | var content = ''; 161 | var stream = through(write, end); 162 | return stream; 163 | 164 | function write(buf) { content += buf; } 165 | function end() { 166 | var messages = []; 167 | resolveShims(file, messages, function (err, info) { 168 | if (err) { 169 | stream.emit('error', err); 170 | return stream.queue(null); 171 | } 172 | 173 | debug(''); 174 | debug.inspect({ file: file, info: info, messages: messages }); 175 | 176 | var eg = info.exposeGlobals; 177 | if(eg && Object.keys(eg)) { 178 | content = exposify.expose(eg, content); 179 | } 180 | 181 | if (info.shim) { 182 | 183 | // at this point we consider all remaining (not exposified) require statements to be invalid (why else are we shimming this) 184 | content = rename('require', shimRequire, content); 185 | 186 | var transformed = wrap(content, info.shim, info.packageDir, info.browser) 187 | stream.queue(transformed); 188 | } else { 189 | stream.queue(content); 190 | } 191 | 192 | stream.queue(null); 193 | }); 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /lib/debug.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var diagnostics = process.env.BROWSERIFYSHIM_DIAGNOSTICS; 4 | 5 | function inspect(obj, depth) { 6 | return require('util').inspect(obj, false, depth || 5, true); 7 | } 8 | 9 | exports = module.exports = function debug() { 10 | if (diagnostics) console.error.apply(console, arguments); 11 | } 12 | 13 | exports.inspect = function(obj, depth) { 14 | if (diagnostics) console.error(inspect(obj, depth)); 15 | } 16 | -------------------------------------------------------------------------------- /lib/parse-inline-shims.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function parseDepends(deps) { 4 | if (!deps) return undefined; 5 | // allow depends: [ '..' ] and depends: '..' 6 | deps = Array.isArray(deps) ? deps : [ deps ]; 7 | 8 | return deps 9 | .reduce(function (acc, d) { 10 | var parts = d.split(':'); 11 | if (!parts 12 | || parts.length > 2 13 | || parts.length < 1 14 | || !parts[0]) 15 | throw new Error('Invalid depends specification: "' + d + '". Needs to have format: "nameORpath:export"'); 16 | 17 | parts = parts.map(function (p) { 18 | return typeof p === 'string' ? p.trim() : p 19 | }); 20 | 21 | // if parts[1] is not defined that means that we depend on module named in parts[0] 22 | // but we don't need it to be attached to the window under a certain name 23 | acc[parts[0]] = parts[1] || null; 24 | return acc; 25 | }, {}); 26 | } 27 | 28 | /** 29 | * Parses inlined shims-config and returns a config in the same format that is used by external shims 30 | * 31 | * Example: 32 | * 33 | * Given: 34 | * { jquery: '$', 35 | * 'non-cjs': 'noncjs', 36 | * 'non-cjs-dep': { exports: 'noncjsdep', depends: 'non-cjs:noncjs' }, 37 | * 'just-dep': { exports: 'justdep', depends: [ 'non-cjs:noncjs', 'jquery:$' ] } 38 | * } 39 | * 40 | * returns: 41 | * { jquery: { exports: '$', depends: undefined }, 42 | * 'non-cjs': { exports: 'noncjs', depends: undefined }, 43 | * 'non-cjs-dep': { exports: 'noncjsdep', depends: { 'non-cjs': 'noncjs' } }, 44 | * 'just-dep': { exports: 'justdep', depends: { 'non-cjs': 'noncjs', jquery: '$' } } 45 | * } 46 | * 47 | * @name parseInlineShims 48 | * @function 49 | * @param {Object} config inlined shims config 50 | * @return {Object} parsed config 51 | */ 52 | var go = module.exports = function (config) { 53 | // all errors thrown are caught inside resolve-shims and passed back to browserify-shim 54 | return Object.keys(config) 55 | .reduce(function (acc, field) { 56 | var conf = config[field]; 57 | 58 | // normalize two possible formats: 59 | // "key": "export, 60 | // "key": { "exports": "export" .. } 61 | if (typeof conf === 'string') conf = { exports: conf }; 62 | 63 | var exps = conf.exports && conf.exports.length ? conf.exports.trim() : null; 64 | 65 | acc[field.trim()] = { 66 | exports: exps 67 | , depends: parseDepends(conf.depends) 68 | } 69 | 70 | return acc; 71 | }, {}); 72 | } 73 | -------------------------------------------------------------------------------- /lib/resolve-shims.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var path = require('path') 4 | , fs = require('fs') 5 | , util = require('util') 6 | , parseInlineShims = require('./parse-inline-shims') 7 | , mothership = require('mothership') 8 | , format = require('util').format 9 | 10 | var shimsCache = {} 11 | , shimsByPath = {}; 12 | 13 | function inspect(obj, depth) { 14 | return util.inspect(obj, false, depth || 5, true); 15 | } 16 | 17 | function isPath(s) { 18 | return (/^[.]{0,2}[/\\]/).test(s); 19 | } 20 | 21 | function validate(key, config, dir) { 22 | var msg 23 | , details = 'When evaluating shim "' + key + '": ' + inspect(config) + '\ninside ' + dir + '\n'; 24 | 25 | if (!config.hasOwnProperty('exports')) { 26 | msg = 'browserify-shim needs at least a path and exports to do its job, you are missing the exports. ' + 27 | '\nIf this module has no exports, specify exports as null.' 28 | throw new Error(details + msg); 29 | } 30 | } 31 | 32 | function updateCache(packageDir, pack, resolvedShims, exposeGlobals) { 33 | shimsCache[packageDir] = { pack: pack, shims: resolvedShims, exposeGlobals: exposeGlobals }; 34 | Object.keys(resolvedShims).forEach(function(fullPath) { 35 | var shim = resolvedShims[fullPath]; 36 | validate(fullPath, shim, packageDir); 37 | shimsByPath[fullPath] = shim; 38 | }); 39 | } 40 | 41 | function resolveDependsRelativeTo(dir, browser, depends, packDeps, messages) { 42 | var resolved; 43 | 44 | if (!depends) return undefined; 45 | 46 | return Object.keys(depends).reduce(function (acc, k) { 47 | if (browser[k]){ 48 | acc[k] = depends[k]; 49 | messages.push(format('Found depends "%s" exposed in browser field', k)); 50 | } else if (!isPath(k)) { 51 | acc[k] = depends[k]; 52 | if (packDeps[k]) { 53 | messages.push(format('Found depends "%s" as an installed dependency of the package', k)); 54 | } else { 55 | messages.push(format('WARNING, depends "%s" is not a path, nor is it exposed in the browser field, nor was it found in package dependencies.', k)); 56 | } 57 | } else { 58 | // otherwise resolve the path 59 | resolved = path.resolve(dir, k); 60 | acc[resolved] = depends[k]; 61 | messages.push(format('Depends "%s" was resolved to be at [%s]', k, resolved)); 62 | } 63 | 64 | return acc; 65 | }, {}) 66 | } 67 | 68 | function resolvePaths (packageDir, shimFileDir, browser, shims, packDeps, messages) { 69 | return Object.keys(shims) 70 | .reduce(function (acc, relPath) { 71 | var shim = shims[relPath]; 72 | var exposed = browser[relPath]; 73 | var shimPath; 74 | 75 | if (exposed) { 76 | // lib exposed under different name/path in package.json's browser field 77 | // and it is referred to by this alias in the shims (either external or in package.json) 78 | // i.e.: 'non-cjs': { ... } -> browser: { 'non-cjs': './vendor/non-cjs.js } 79 | shimPath = path.resolve(packageDir, exposed); 80 | messages.push(format('Found "%s" in browser field referencing "%s" and resolved it to "%s"', relPath, exposed, shimPath)); 81 | } else if (shimFileDir) { 82 | // specified via relative path to shim file inside shim file 83 | // i.e. './vendor/non-cjs': { exports: .. } 84 | shimPath = path.resolve(shimFileDir, relPath); 85 | messages.push(format('Resolved "%s" found in shim file to "%s"', relPath, shimPath)); 86 | } else { 87 | // specified via relative path in package.json browserify-shim config 88 | // i.e. 'browserify-shim': { './vendor/non-cjs': 'noncjs' } 89 | shimPath = path.resolve(packageDir, relPath); 90 | messages.push(format('Resolved "%s" found in package.json to "%s"', relPath, shimPath)); 91 | } 92 | var depends = resolveDependsRelativeTo(shimFileDir || packageDir, browser, shim.depends, packDeps, messages); 93 | 94 | acc[shimPath] = { exports: shim.exports, depends: depends }; 95 | return acc; 96 | }, {}); 97 | } 98 | 99 | function mapifyExposeGlobals(exposeGlobals) { 100 | return Object.keys(exposeGlobals) 101 | .reduce(function (acc, k) { 102 | 103 | var val = exposeGlobals[k]; 104 | var parts = val.split(':'); 105 | 106 | if (parts.length < 2 || !parts[1].length) { 107 | throw new Error( 108 | 'Expose Globals need to have the format "global:expression.\n"' 109 | + inspect({ key: k, value: val }) + 'does not.' 110 | ); 111 | } 112 | 113 | // this also handle unlikely cases of 'global:_.someFunc(':')' with a `:` in the actual global expression 114 | parts.shift(); 115 | acc[k] = parts.join(':'); 116 | 117 | return acc; 118 | }, {}); 119 | } 120 | 121 | function separateExposeGlobals(shims) { 122 | var onlyShims = {} 123 | , exposeGlobals = {}; 124 | 125 | Object.keys(shims).forEach(function (k) { 126 | // https://github.com/thlorenz/browserify-shim/issues/245 127 | if (k === '__proto__' || k === 'constructor') { 128 | return; 129 | } 130 | 131 | var val = shims[k] 132 | , exp = val && val.exports; 133 | 134 | if (exp && /^global\:/.test(exp)) { 135 | exposeGlobals[k] = exp; 136 | } else { 137 | onlyShims[k] = val; 138 | } 139 | }); 140 | 141 | return { shims: onlyShims, exposeGlobals: mapifyExposeGlobals(exposeGlobals) }; 142 | } 143 | 144 | function resolveFromShimFile(packageDir, pack, shimField, messages) { 145 | var shimFile = path.join(packageDir, shimField) 146 | , shimFileDir = path.dirname(shimFile); 147 | 148 | var allShims = require(shimFile); 149 | var separated = separateExposeGlobals(allShims); 150 | 151 | var resolvedShims = resolvePaths(packageDir, shimFileDir, pack.browser || {}, separated.shims, pack.dependencies || {}, messages); 152 | return { shims: resolvedShims, exposeGlobals: separated.exposeGlobals }; 153 | } 154 | 155 | function resolveInlineShims(packageDir, pack, shimField, messages) { 156 | var allShims = parseInlineShims(shimField); 157 | var separated = separateExposeGlobals(allShims); 158 | 159 | var resolvedShims = resolvePaths(packageDir, null, pack.browser || {}, separated.shims, pack.dependencies || {}, messages); 160 | return { shims: resolvedShims, exposeGlobals: separated.exposeGlobals }; 161 | } 162 | 163 | var resolve = module.exports = function resolveShims (file, messages, cb) { 164 | // find the package.json that defines browserify-shim config for this file 165 | mothership(file, function (pack) { return !! pack['browserify-shim'] }, function (err, res) { 166 | if (err) return cb(err); 167 | 168 | if (!res || !res.pack) return cb(new Error('Unable to find a browserify-shim config section in the package.json for ' + file)); 169 | 170 | var pack = res.pack; 171 | var packFile = res.path; 172 | var packageDir = path.dirname(packFile); 173 | 174 | // we cached this before which means it was also grouped by file 175 | var cached = shimsCache[packageDir]; 176 | // if it was cached, that means any package fixes were applied as well 177 | if (cached) { 178 | return cb(null, { 179 | package_json : packFile 180 | , packageDir : packageDir 181 | , resolvedPreviously : true 182 | , shim : shimsByPath[file] 183 | , exposeGlobals : cached.exposeGlobals 184 | , browser : pack.browser 185 | , 'browserify-shim' : pack['browserify-shim'] 186 | , dependencies : pack.dependencies 187 | }); 188 | } 189 | 190 | try { 191 | pack = require(packFile); 192 | 193 | var shimField = pack['browserify-shim']; 194 | if (!shimField) return cb(null, { package_json: packFile, shim: undefined }); 195 | 196 | var resolved = typeof shimField === 'string' 197 | ? resolveFromShimFile(packageDir, pack, shimField, messages) 198 | : resolveInlineShims(packageDir, pack, shimField, messages); 199 | 200 | messages.push({ resolved: resolved.shims }); 201 | updateCache(packageDir, pack, resolved.shims, resolved.exposeGlobals); 202 | 203 | cb(null, { 204 | package_json : packFile 205 | , packageDir : packageDir 206 | , shim : shimsByPath[file] 207 | , exposeGlobals : resolved.exposeGlobals 208 | , browser : pack.browser 209 | , 'browserify-shim' : pack['browserify-shim'] 210 | , dependencies : pack.dependencies 211 | }); 212 | 213 | } catch (err) { 214 | console.trace(); 215 | return cb(err); 216 | } 217 | }); 218 | } 219 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "browserify-shim", 3 | "version": "3.8.16", 4 | "description": "Makes CommonJS-incompatible modules browserifyable.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test-main": "tap test/*.js && tap test/shim/*.js", 8 | "test": "test-peer-range browserify", 9 | "shim-jquery": "npm install opener && cd ./examples/shim-jquery && npm install && node build.js && opener index.html", 10 | "shim-jquery-diag": "npm install opener && cd ./examples/shim-jquery && npm install && node build-diag.js && opener index.html", 11 | "expose-jquery": "npm install opener && cd ./examples/expose-jquery && npm install && node build.js && opener index.html", 12 | "expose-jquery-diag": "npm install opener && cd ./examples/expose-jquery && npm install && node build-diag.js && opener index.html", 13 | "dependency-with-global": "npm install opener && cd ./examples/dependency-with-global && npm install && node build.js && opener index.html", 14 | "dependency-with-global-diag": "npm install opener && cd ./examples/dependency-with-global && npm install && node build-diag.js && opener index.html" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git://github.com/thlorenz/browserify-shim.git" 19 | }, 20 | "keywords": [ 21 | "browserify", 22 | "browserify-transform", 23 | "shim", 24 | "global", 25 | "globals", 26 | "transform", 27 | "window", 28 | "commonjs" 29 | ], 30 | "author": "Thorsten Lorenz (thlorenz.com)", 31 | "license": "MIT", 32 | "readmeFilename": "README.md", 33 | "devDependencies": { 34 | "browserify": ">= 13", 35 | "jsdom": "^5.4.2", 36 | "ncp": "~0.5.0", 37 | "proxyquire": "~0.5.1", 38 | "request": "~2.88.0", 39 | "rimraf": "~2.2.6", 40 | "tap": "^1.1.0", 41 | "test-peer-range": "1.0.1" 42 | }, 43 | "dependencies": { 44 | "exposify": "~0.5.0", 45 | "mothership": "~0.3.0", 46 | "rename-function-calls": "~0.1.0", 47 | "resolve": "~0.6.1", 48 | "through": "~2.3.4" 49 | }, 50 | "peerDependencies": { 51 | "browserify": ">= 2.3" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /test/bower/components/jquery-ui/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery-ui", 3 | "title": "jQuery UI", 4 | "description": "A curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library.", 5 | "version": "1.10.3", 6 | "homepage": "http://jqueryui.com", 7 | "author": { 8 | "name": "jQuery Foundation and other contributors", 9 | "url": "https://github.com/jquery/jquery-ui/blob/1.10.0/AUTHORS.txt" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git://github.com/jquery/jquery-ui.git" 14 | }, 15 | "bugs": "http://bugs.jqueryui.com/", 16 | "licenses": [ 17 | { 18 | "type": "MIT", 19 | "url": "https://github.com/jquery/jquery-ui/blob/1.10.0/MIT-LICENSE.txt" 20 | } 21 | ], 22 | "dependencies": {}, 23 | "devDependencies": { 24 | }, 25 | "keywords": [] 26 | } 27 | -------------------------------------------------------------------------------- /test/bower/components/jquery-ui/ui/jquery.ui.position.js: -------------------------------------------------------------------------------- 1 | (function( $, undefined ) { 2 | 3 | $.ui = $.ui || {}; 4 | 5 | $.position = function () { 6 | return '$.position'; 7 | }; 8 | 9 | $.fn.position = function( options ) { 10 | return '$.fn.position'; 11 | }; 12 | 13 | $.ui.position = function () { 14 | return '$.ui.position'; 15 | }; 16 | 17 | }( jQuery ) ); 18 | -------------------------------------------------------------------------------- /test/bower/components/jquery/jquery.js: -------------------------------------------------------------------------------- 1 | // The stripped down version just for testing. Don't want to overload github and npm with jquery copies ;) 2 | (function( window, undefined ) { 3 | var 4 | // A central reference to the root jQuery(document) 5 | rootjQuery, 6 | 7 | // The deferred used on DOM ready 8 | readyList, 9 | 10 | // Map over jQuery in case of overwrite 11 | _jQuery = window.jQuery, 12 | 13 | // Map over the $ in case of overwrite 14 | _$ = window.$, 15 | 16 | jQuery = function( selector, context ) { 17 | return jQuery.fn; 18 | }; 19 | 20 | jQuery.fn = jQuery.prototype = { 21 | // The current version of jQuery being used 22 | jquery: "1.8.3", 23 | }; 24 | 25 | // help with testing 26 | window.require = require; 27 | 28 | // Expose jQuery to the global object 29 | window.jQuery = window.$ = jQuery; 30 | 31 | if ( typeof define === "function" && define.amd && define.amd.jQuery ) { 32 | define( "jquery", [], function () { return jQuery; } ); 33 | } 34 | 35 | })( window ); 36 | 37 | if(this !== window) throw new Error('this should be window'); 38 | -------------------------------------------------------------------------------- /test/bower/components/jquery/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "components-jquery", 3 | "version": "1.10.2", 4 | "description": "jQuery component", 5 | "keywords": ["jquery"], 6 | "main": "./jquery.js" 7 | } 8 | -------------------------------------------------------------------------------- /test/bower/index.js: -------------------------------------------------------------------------------- 1 | require('ui-position'); 2 | 3 | // at this point the global $ was loaded since jquery has been automatically required 4 | module.exports = { 5 | root: window.$.position() 6 | , fn: window.$.fn.position() 7 | , ui: window.$.ui.position() 8 | }; 9 | -------------------------------------------------------------------------------- /test/bower/node_modules/browserify-shim: -------------------------------------------------------------------------------- 1 | ../../../ -------------------------------------------------------------------------------- /test/bower/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bower-project", 3 | "description": "Requires bower components and is used to repro problem with bower due to it's own package.json", 4 | "main": "index.js", 5 | "browserify": { 6 | "transform": [ 7 | "browserify-shim" 8 | ] 9 | }, 10 | "browser": { 11 | "jquery": "./components/jquery/jquery.js", 12 | "ui-position": "./components/jquery-ui/ui/jquery.ui.position.js" 13 | }, 14 | "browserify-shim": { 15 | "jquery": "$", 16 | "ui-position": { "depends": [ "jquery:jQuery" ] } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /test/bundle-deps.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | var browserify = require('browserify') 5 | , test = require('tap').test 6 | , vm = require('vm') 7 | 8 | function bundleNcheck(relPath, t) { 9 | browserify( { ignoreGlobals: true }) 10 | .require(require.resolve(relPath)) 11 | .bundle(function (err, src) { 12 | if (err) { t.fail(err); return t.end(); } 13 | 14 | var ctx = { window: {}, console: console }; 15 | ctx.self = ctx.window; 16 | var require_ = vm.runInNewContext(src, ctx); 17 | 18 | var main = require_(require.resolve(relPath)); 19 | 20 | t.deepEqual(main(), { noncjs: { name: 'non-cjs', version: 1.1 } }, 'shims non-cjs, requireing non-cjs-dep '); 21 | t.end() 22 | }); 23 | } 24 | 25 | test('\nshimming deps with external shim, with depends all exposed' 26 | , bundleNcheck.bind(null, './deps/extshim/main')) 27 | 28 | test('\nshimming nodeps with inlined shim, with depends all exposed' 29 | , bundleNcheck.bind(null, './deps/inlineshim/main')) 30 | -------------------------------------------------------------------------------- /test/bundle-ember-bower.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | process.env.BROWSERIFYSHIM_DIAGNOSTICS=1; 5 | 6 | // reproduces: https://github.com/thlorenz/browserify-shim/issues/30 7 | var browserify = require('browserify') 8 | , test = require('tap').test 9 | , path = require('path') 10 | , vm = require('vm') 11 | 12 | function inspect(obj, depth) { 13 | console.error(require('util').inspect(obj, false, depth || 5, true)); 14 | } 15 | function bundleNcheck(relPath, t) { 16 | browserify( { ignoreGlobals: true }) 17 | .require(require.resolve(relPath)) 18 | .bundle(function (err, src) { 19 | if (err) { t.fail(err); return t.end(); } 20 | 21 | var ctx = { window: {}, console: console }; 22 | ctx.self = ctx.window; 23 | var require_ = vm.runInNewContext(src, ctx); 24 | 25 | var main = require_(require.resolve(relPath)); 26 | 27 | t.equal(main.ENV, 'this is ember env', 'shims ember correctly and fixes requires') 28 | t.deepEqual(main.$(), { jquery: '1.8.3' }, 'resolves jquery') 29 | t.equal(main.Handlebars, 'this is handlebars', 'resolves handlebars') 30 | t.end() 31 | }); 32 | } 33 | 34 | 35 | test('\nbundling pack that has ember with invalid requires, installed via bower' 36 | , bundleNcheck.bind(null, './ember-bower')) 37 | -------------------------------------------------------------------------------- /test/bundle-expose-globals.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | var browserify = require('browserify') 5 | , test = require('tap').test 6 | , vm = require('vm') 7 | 8 | function inspect(obj, depth) { 9 | console.error(require('util').inspect(obj, false, depth || 5, true)); 10 | } 11 | 12 | var jquery = 'the jquery'; 13 | function bundleNcheck(relPath, t) { 14 | browserify( { ignoreGlobals: true }) 15 | .require(require.resolve(relPath)) 16 | .bundle(function (err, src) { 17 | if (err) { t.fail(err); return t.end(); } 18 | 19 | var ctx = { window: { $: jquery }, console: console }; 20 | ctx.self = ctx.window; 21 | var require_ = vm.runInNewContext(src, ctx); 22 | 23 | var main = require_(require.resolve(relPath)); 24 | t.deepEqual( 25 | main() 26 | , { noncjs: { name: 'non-cjs', version: 1.1 }, $: 'the jquery' } 27 | , 'shims non-cjs and exposes jquery' 28 | ); 29 | t.end() 30 | }); 31 | } 32 | 33 | test('\nshimming nodeps with inlined shim, no depends, but exposing jquery' 34 | , bundleNcheck.bind(null, './exposify/inlineshim/main')) 35 | 36 | test('\nshimming nodeps with external shim, no depends, but exposing jquery' 37 | , bundleNcheck.bind(null, './exposify/extshim/main')) 38 | -------------------------------------------------------------------------------- /test/bundle-invalid-require.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | process.env.BROWSERIFYSHIM_DIAGNOSTICS=1; 5 | 6 | var browserify = require('browserify') 7 | , test = require('tap').test 8 | , vm = require('vm') 9 | 10 | function inspect(obj, depth) { 11 | console.error(require('util').inspect(obj, false, depth || 5, true)); 12 | } 13 | 14 | function bundleNcheck(relPath, t) { 15 | browserify( { ignoreGlobals: true }) 16 | .require(require.resolve(relPath)) 17 | .bundle(function (err, src) { 18 | if (err) { t.fail(err); return t.end(); } 19 | 20 | var ctx = { window: {}, console: console }; 21 | ctx.self = ctx.window; 22 | var require_ = vm.runInNewContext(src, ctx); 23 | 24 | var main = require_(require.resolve(relPath)); 25 | t.equal(main(), 'I survived the horror of humans messing with my brain', 'shims that module just fine'); 26 | t.end() 27 | }); 28 | } 29 | 30 | test('\nshimming a module that has an invalid require that needs to be renamed' 31 | , bundleNcheck.bind(null, './invalid-require/main')) 32 | -------------------------------------------------------------------------------- /test/bundle-multideps.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | var browserify = require('browserify') 5 | , test = require('tap').test 6 | , vm = require('vm') 7 | 8 | function inspect(obj, depth) { 9 | console.error(require('util').inspect(obj, false, depth || 5, true)); 10 | } 11 | 12 | function bundleNcheck(relPath, t) { 13 | browserify( { ignoreGlobals: true }) 14 | .require(require.resolve(relPath)) 15 | .bundle(function (err, src) { 16 | if (err) { t.fail(err); return t.end(); } 17 | 18 | var ctx = { window: {}, console: console }; 19 | ctx.self = ctx.window; 20 | var require_ = vm.runInNewContext(src, ctx); 21 | 22 | var main = require_(require.resolve(relPath)); 23 | 24 | t.deepEqual( 25 | main() 26 | , { noncjs: { name: 'non-cjs', version: 1.1 }, 27 | noncjscore: { name: 'core', version: 1 } 28 | } 29 | , 'shims non-cjs, requireing non-cjs-dep ' 30 | ); 31 | t.end() 32 | }); 33 | } 34 | 35 | test('\nshimming deps with external shim, with one depends exposed, one referenced by relative path' 36 | , bundleNcheck.bind(null, './multideps/extshim/main')) 37 | 38 | test('\nshimming deps with inlined shim, with one depends exposed, one referenced by relative path' 39 | , bundleNcheck.bind(null, './multideps/inlineshim/main')) 40 | -------------------------------------------------------------------------------- /test/bundle-nodeps.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | var browserify = require('browserify') 5 | , test = require('tap').test 6 | , vm = require('vm') 7 | 8 | function inspect(obj, depth) { 9 | console.error(require('util').inspect(obj, false, depth || 5, true)); 10 | } 11 | 12 | function bundleNcheck(relPath, t) { 13 | browserify( { ignoreGlobals: true }) 14 | .require(require.resolve(relPath)) 15 | .bundle(function (err, src) { 16 | if (err) { t.fail(err); return t.end(); } 17 | 18 | var ctx = { window: {}, console: console }; 19 | ctx.self = ctx.window; 20 | var require_ = vm.runInNewContext(src, ctx); 21 | 22 | var main = require_(require.resolve(relPath)); 23 | t.deepEqual(main(), { name: 'non-cjs', version: 1.1 }, 'shims non-cjs'); 24 | t.end() 25 | }); 26 | } 27 | 28 | test('\nshimming nodeps with external shim, no depends' 29 | , bundleNcheck.bind(null, './nodeps/extshim/main')) 30 | 31 | test('\nshimming nodeps with external shim, no depends, exposed non-cjs' 32 | , bundleNcheck.bind(null, './nodeps/extshim-exposed/main')) 33 | 34 | test('\nshimming nodeps with inlined shim, no depends' 35 | , bundleNcheck.bind(null, './nodeps/inlineshim/main')) 36 | 37 | test('\nshimming nodeps with inlined shim, no depends, exposed non-cjs' 38 | , bundleNcheck.bind(null, './nodeps/inlineshim-exposed/main')) 39 | -------------------------------------------------------------------------------- /test/bundle-pack-bower.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | process.env.BROWSERIFYSHIM_DIAGNOSTICS=1; 5 | 6 | var browserify = require('browserify') 7 | , test = require('tap').test 8 | , path = require('path') 9 | , vm = require('vm') 10 | 11 | function bundleNcheck(relPath, t) { 12 | browserify( { ignoreGlobals: true }) 13 | .require(require.resolve(relPath)) 14 | .bundle(function (err, src) { 15 | if (err) { t.fail(err); return t.end(); } 16 | 17 | var ctx = { window: {}, console: console }; 18 | ctx.self = ctx.window; 19 | var require_ = vm.runInNewContext(src, ctx); 20 | 21 | var main = require_(require.resolve(relPath)); 22 | 23 | t.deepEqual( 24 | main 25 | , { root: '$.position', fn: '$.fn.position', ui: '$.ui.position' } 26 | , 'shims it correctly including requiring its dependends' 27 | ); 28 | t.end() 29 | }); 30 | } 31 | 32 | 33 | test('\nbundling pack that has shimmed bower component dependency' 34 | , bundleNcheck.bind(null, './bower')) 35 | -------------------------------------------------------------------------------- /test/bundle-packs.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | var browserify = require('browserify') 5 | , test = require('tap').test 6 | , vm = require('vm') 7 | 8 | function inspect(obj, depth) { 9 | console.error(require('util').inspect(obj, false, depth || 5, true)); 10 | } 11 | 12 | function bundleNcheck(relPath, t) { 13 | browserify( { ignoreGlobals: true }) 14 | .require(require.resolve(relPath)) 15 | .bundle(function (err, src) { 16 | if (err) { t.fail(err); return t.end(); } 17 | 18 | var ctx = { window: {}, console: console }; 19 | ctx.self = ctx.window; 20 | var require_ = vm.runInNewContext(src, ctx); 21 | 22 | var main = require_(require.resolve(relPath)); 23 | 24 | t.deepEqual( 25 | main() 26 | , { sub1: 27 | { name: 'sub1noncjsdep', 28 | sub1noncjs: { name: 'sub1-non-cjs', version: 0.1 } }, 29 | sub1noncjsdep: 30 | { name: 'sub1noncjsdep', 31 | sub1noncjs: { name: 'sub1-non-cjs', version: 0.1 } }, 32 | sub2: 33 | { name: 'sub2noncjsdep', 34 | sub2noncjs: { name: 'sub2-non-cjs', version: 0.2 } }, 35 | sub2noncjsdep: 36 | { name: 'sub2noncjsdep', 37 | sub2noncjs: { name: 'sub2-non-cjs', version: 0.2 } } 38 | } 39 | , 'shims all non-cjs modules, attaches them to the window and allows pulling them all together' 40 | ); 41 | t.end() 42 | }); 43 | } 44 | 45 | test('\nshimming pack with sub1 and sub2 dependency' 46 | , bundleNcheck.bind(null, './packs/main')) 47 | -------------------------------------------------------------------------------- /test/deps/extshim/config/shim.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | 'non-cjs': { exports : 'noncjs' }, 5 | 'non-cjs-dep': { exports: 'noncjsdep', depends: { 'non-cjs': 'noncjs' } } 6 | }; 7 | -------------------------------------------------------------------------------- /test/deps/extshim/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var noncjsdep = require('non-cjs-dep'); 4 | 5 | var go = module.exports = function () { 6 | console.log('main', noncjsdep.noncjs.main); 7 | console.log('version', noncjsdep.noncjs.version); 8 | return noncjsdep; 9 | }; 10 | -------------------------------------------------------------------------------- /test/deps/extshim/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./main.js", 3 | "browser": { 4 | "non-cjs": "./vendor/non-cjs.js", 5 | "non-cjs-dep": "./vendor/non-cjs-dep.js" 6 | }, 7 | "browserify": { 8 | "transform": [ 9 | "browserify-shim" 10 | ] 11 | }, 12 | "browserify-shim": "./config/shim.js" 13 | } 14 | -------------------------------------------------------------------------------- /test/deps/extshim/vendor/non-cjs-dep.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // expecting nodeps to have been required before me and attached itself to the window 4 | window.noncjsdep = { noncjs: window.noncjs }; 5 | -------------------------------------------------------------------------------- /test/deps/extshim/vendor/non-cjs.js: -------------------------------------------------------------------------------- 1 | window.noncjs = { 2 | name: 'non-cjs' 3 | , version: 1.1 4 | }; 5 | -------------------------------------------------------------------------------- /test/deps/inlineshim/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var noncjsdep = require('non-cjs-dep'); 4 | 5 | var go = module.exports = function () { 6 | console.log('main', noncjsdep.noncjs.main); 7 | console.log('version', noncjsdep.noncjs.version); 8 | return noncjsdep; 9 | }; 10 | -------------------------------------------------------------------------------- /test/deps/inlineshim/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./main.js", 3 | "browser": { 4 | "non-cjs": "./vendor/non-cjs.js", 5 | "non-cjs-dep": "./vendor/non-cjs-dep.js" 6 | }, 7 | "browserify-shim": { 8 | "non-cjs": "noncjs", 9 | "non-cjs-dep": { "exports": "noncjsdep", "depends": "non-cjs:noncjs" } 10 | }, 11 | "browserify": { 12 | "transform": [ 13 | "browserify-shim" 14 | ] 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /test/deps/inlineshim/vendor/non-cjs-dep.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // expecting nodeps to have been required before me and attached itself to the window 4 | window.noncjsdep = { noncjs: window.noncjs }; 5 | -------------------------------------------------------------------------------- /test/deps/inlineshim/vendor/non-cjs.js: -------------------------------------------------------------------------------- 1 | window.noncjs = { 2 | name: 'non-cjs' 3 | , version: 1.1 4 | }; 5 | -------------------------------------------------------------------------------- /test/deps/node_modules/browserify-shim: -------------------------------------------------------------------------------- 1 | ../../../ -------------------------------------------------------------------------------- /test/ember-bower/bower_components/ember/ember.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | 3 | if ('undefined' === typeof Ember) { 4 | Ember = {}; 5 | 6 | if ('undefined' !== typeof window) { 7 | window.Em = window.Ember = Em = Ember; 8 | } 9 | } 10 | Ember.ENV = 'this is ember env'; 11 | 12 | var jQuery = this.jQuery || (Ember.imports && Ember.imports.jQuery); 13 | if (!jQuery && typeof require === 'function') { 14 | jQuery = require('jquery'); 15 | } 16 | Ember.$ = jQuery; 17 | 18 | var Handlebars = (Ember.imports && Ember.imports.Handlebars) || (this && this.Handlebars); 19 | if (!Handlebars && typeof require === 'function') { 20 | Handlebars = require('handlebars'); 21 | } 22 | Ember.Handlebars = Handlebars; 23 | 24 | })(); 25 | -------------------------------------------------------------------------------- /test/ember-bower/bower_components/ember/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "components-ember", 3 | "version": "1.3.2", 4 | "description": "Ember Application Framework", 5 | "keywords": ["ember"], 6 | "main": "./ember.js", 7 | "dependencies": { 8 | "jquery": "*", 9 | "handlebars": ">= 1.0.0 < 2.0" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/ember-bower/bower_components/handlebars/handlebars.js: -------------------------------------------------------------------------------- 1 | var Handlebars = (function() { 2 | return 'this is handlebars'; 3 | })(); 4 | -------------------------------------------------------------------------------- /test/ember-bower/bower_components/jquery/jquery.js: -------------------------------------------------------------------------------- 1 | // The stripped down version just for testing. Don't want to overload github and npm with jquery copies ;) 2 | (function( window, undefined ) { 3 | var 4 | // A central reference to the root jQuery(document) 5 | rootjQuery, 6 | 7 | // The deferred used on DOM ready 8 | readyList, 9 | 10 | // Map over jQuery in case of overwrite 11 | _jQuery = window.jQuery, 12 | 13 | // Map over the $ in case of overwrite 14 | _$ = window.$, 15 | 16 | jQuery = function( selector, context ) { 17 | return jQuery.fn; 18 | }; 19 | 20 | jQuery.fn = jQuery.prototype = { 21 | // The current version of jQuery being used 22 | jquery: "1.8.3", 23 | }; 24 | 25 | // Expose jQuery to the global object 26 | window.jQuery = window.$ = jQuery; 27 | 28 | if ( typeof define === "function" && define.amd && define.amd.jQuery ) { 29 | define( "jquery", [], function () { return jQuery; } ); 30 | } 31 | 32 | })( window ); 33 | -------------------------------------------------------------------------------- /test/ember-bower/bower_components/jquery/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery", 3 | "title": "jQuery", 4 | "description": "JavaScript library for DOM operations", 5 | "version": "2.0.3", 6 | "homepage": "http://jquery.com", 7 | "author": { 8 | "name": "jQuery Foundation and other contributors", 9 | "url": "https://github.com/jquery/jquery/blob/master/AUTHORS.txt" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/jquery/jquery.git" 14 | }, 15 | "bugs": { 16 | "url": "http://bugs.jquery.com" 17 | }, 18 | "licenses": [ 19 | { 20 | "type": "MIT", 21 | "url": "https://github.com/jquery/jquery/blob/master/MIT-LICENSE.txt" 22 | } 23 | ], 24 | "dependencies": {}, 25 | "devDependencies": { 26 | "grunt-compare-size": "~0.4.0", 27 | "grunt-git-authors": "1.2.0", 28 | "grunt-update-submodules": "0.2.0", 29 | "grunt-contrib-watch": "0.3.1", 30 | "grunt-contrib-jshint": "0.3.0", 31 | "grunt-contrib-uglify": "0.2.0", 32 | "grunt": "0.4.1", 33 | "gzip-js": "0.3.1", 34 | "testswarm": "~1.1.0", 35 | "archiver": "~0.4.2" 36 | }, 37 | "keywords": [] 38 | } 39 | -------------------------------------------------------------------------------- /test/ember-bower/main.js: -------------------------------------------------------------------------------- 1 | var ember = require('ember'); 2 | 3 | module.exports = ember; 4 | -------------------------------------------------------------------------------- /test/ember-bower/node_modules/browserify-shim: -------------------------------------------------------------------------------- 1 | ../../../ -------------------------------------------------------------------------------- /test/ember-bower/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "browserify-ember", 3 | "description": "Reproduces: https://github.com/thlorenz/browserify-shim/issues/30", 4 | "version": "0.0.1", 5 | "dependencies": { 6 | "browserify-shim": "~3.2.2" 7 | }, 8 | "scripts": { 9 | "start": "browserify main.js -d > out.js && open index.html", 10 | "test": "BROWSERIFYSHIM_DIAGNOSTICS=1 browserify main.js -d > out.js && open index.html" 11 | }, 12 | "main": "./main.js", 13 | "browser": { 14 | "jquery": "./bower_components/jquery/jquery.js", 15 | "handlebars": "./bower_components/handlebars/handlebars.js", 16 | "ember": "./bower_components/ember/ember.js" 17 | }, 18 | "browserify-shim": { 19 | "jquery": "$", 20 | "handlebars": "Handlebars", 21 | "ember": { 22 | "exports": "Ember", 23 | "depends": [ 24 | "jquery:jQuery", 25 | "handlebars:Handlebars" 26 | ] 27 | } 28 | }, 29 | "browserify": { 30 | "transform": [ 31 | "browserify-shim" 32 | ] 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /test/exposify/extshim/config/shim.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | 'non-cjs': { exports : 'noncjs' }, 5 | 'jquery': { exports: 'global:$' } 6 | }; 7 | -------------------------------------------------------------------------------- /test/exposify/extshim/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var noncjs = require('non-cjs'); 4 | 5 | var go = module.exports = function () { 6 | console.log('version', noncjs.version); 7 | return { noncjs: noncjs, $: require('jquery') }; 8 | }; 9 | -------------------------------------------------------------------------------- /test/exposify/extshim/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./main.js", 3 | "browser": { 4 | "non-cjs": "./vendor/non-cjs.js" 5 | }, 6 | "browserify": { 7 | "transform": [ 8 | "browserify-shim" 9 | ] 10 | }, 11 | "browserify-shim": "./config/shim.js" 12 | } 13 | -------------------------------------------------------------------------------- /test/exposify/extshim/vendor/non-cjs.js: -------------------------------------------------------------------------------- 1 | window.noncjs = { 2 | name: 'non-cjs' 3 | , version: 1.1 4 | }; 5 | -------------------------------------------------------------------------------- /test/exposify/inlineshim/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var noncjs = require('./vendor/non-cjs'); 4 | 5 | var go = module.exports = function () { 6 | console.log('version', noncjs.version); 7 | return { noncjs: noncjs, $: require('jquery') }; 8 | }; 9 | -------------------------------------------------------------------------------- /test/exposify/inlineshim/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./main.js", 3 | "browserify": { 4 | "transform": [ 5 | "browserify-shim" 6 | ] 7 | }, 8 | "browserify-shim": { 9 | "./vendor/non-cjs.js": "noncjs", 10 | "jquery": "global:$" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test/exposify/inlineshim/vendor/non-cjs.js: -------------------------------------------------------------------------------- 1 | window.noncjs = { 2 | name: 'non-cjs' 3 | , version: 1.1 4 | }; 5 | -------------------------------------------------------------------------------- /test/exposify/node_modules/browserify-shim: -------------------------------------------------------------------------------- 1 | ../../../ -------------------------------------------------------------------------------- /test/invalid-require/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var noncjs = require('non-cjs'); 3 | 4 | module.exports = function () { return noncjs; } 5 | -------------------------------------------------------------------------------- /test/invalid-require/node_modules/browserify-shim: -------------------------------------------------------------------------------- 1 | ../../../ -------------------------------------------------------------------------------- /test/invalid-require/non-cjs.js: -------------------------------------------------------------------------------- 1 | // Some modules require other modules that they haven't even installed (trying to be commonJS compliant the wrong way) 2 | // A more detailed case can be found inside bower-ember 3 | 4 | var jQuery; 5 | 6 | // we will need to reassign the require (after we did our require calls to get depends) 7 | // therefore we'll add `require = undefined` above the code of this module 8 | if (!jQuery && typeof require === 'function') { 9 | // browserify will try to find 'jQuery' now, so we have to derequire it 10 | jQuery = require('jquery'); 11 | } 12 | window.Ember = 'I survived the horror of humans messing with my brain'; 13 | -------------------------------------------------------------------------------- /test/invalid-require/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./main.js", 3 | "browser": { 4 | "non-cjs": "./non-cjs.js" 5 | }, 6 | "browserify": { 7 | "transform": [ 8 | "browserify-shim" 9 | ] 10 | }, 11 | "browserify-shim": { 12 | "non-cjs": "Ember" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /test/multideps/extshim/config/shim.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | 'non-cjs': { exports : 'noncjs' }, 5 | '../vendor/non-cjs-core.js': { exports : 'noncjscore' }, 6 | 'non-cjs-dep': { 7 | exports: 'noncjsdep' 8 | // non-cjs-core is not exposed so we need to refer to it under its relative path 9 | , depends: { 'non-cjs': 'noncjs', '../vendor/non-cjs-core.js': 'core' } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /test/multideps/extshim/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var noncjsdep = require('non-cjs-dep'); 4 | 5 | var go = module.exports = function () { 6 | console.log('main', noncjsdep.noncjs.main); 7 | console.log('version', noncjsdep.noncjs.version); 8 | return noncjsdep; 9 | }; 10 | -------------------------------------------------------------------------------- /test/multideps/extshim/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./main.js", 3 | "browser": { 4 | "non-cjs": "./vendor/non-cjs.js", 5 | "non-cjs-dep": "./vendor/non-cjs-dep.js" 6 | }, 7 | "browserify": { 8 | "transform": [ 9 | "browserify-shim" 10 | ] 11 | }, 12 | "browserify-shim": "./config/shim.js" 13 | } 14 | -------------------------------------------------------------------------------- /test/multideps/extshim/vendor/non-cjs-core.js: -------------------------------------------------------------------------------- 1 | window.noncjscore = { 2 | name: 'core' 3 | , version: 1.0 4 | }; 5 | -------------------------------------------------------------------------------- /test/multideps/extshim/vendor/non-cjs-dep.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // expecting noncjs and noncjscore to have been required before me and attached itself to the window 4 | // it expectes non-cjs-core to be attached as 'core' instead of 'noncjscore' as which it attaches 5 | // itself to the window 6 | window.noncjsdep = { noncjs: window.noncjs, noncjscore : window.core }; 7 | -------------------------------------------------------------------------------- /test/multideps/extshim/vendor/non-cjs.js: -------------------------------------------------------------------------------- 1 | window.noncjs = { 2 | name: 'non-cjs' 3 | , version: 1.1 4 | }; 5 | -------------------------------------------------------------------------------- /test/multideps/inlineshim/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var noncjsdep = require('non-cjs-dep'); 4 | 5 | var go = module.exports = function () { 6 | console.log('main', noncjsdep.noncjs.main); 7 | console.log('version', noncjsdep.noncjs.version); 8 | return noncjsdep; 9 | }; 10 | -------------------------------------------------------------------------------- /test/multideps/inlineshim/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./main.js", 3 | "browser": { 4 | "non-cjs": "./vendor/non-cjs.js", 5 | "non-cjs-dep": "./vendor/non-cjs-dep.js" 6 | }, 7 | "browserify-shim": { 8 | "non-cjs": "noncjs", 9 | "./vendor/non-cjs-core.js": "noncjscore", 10 | "non-cjs-dep": { 11 | "exports": "noncjsdep", 12 | "depends": [ "non-cjs:noncjs", "./vendor/non-cjs-core.js:core" ] 13 | } 14 | }, 15 | "browserify": { 16 | "transform": [ 17 | "browserify-shim" 18 | ] 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /test/multideps/inlineshim/vendor/non-cjs-core.js: -------------------------------------------------------------------------------- 1 | window.noncjscore = { 2 | name: 'core' 3 | , version: 1.0 4 | }; 5 | -------------------------------------------------------------------------------- /test/multideps/inlineshim/vendor/non-cjs-dep.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // expecting noncjs and noncjscore to have been required before me and attached itself to the window 4 | // it expectes non-cjs-core to be attached as 'core' instead of 'noncjscore' as which it attaches 5 | // itself to the window 6 | window.noncjsdep = { noncjs: window.noncjs, noncjscore : window.core }; 7 | -------------------------------------------------------------------------------- /test/multideps/inlineshim/vendor/non-cjs.js: -------------------------------------------------------------------------------- 1 | window.noncjs = { 2 | name: 'non-cjs' 3 | , version: 1.1 4 | }; 5 | -------------------------------------------------------------------------------- /test/multideps/node_modules/browserify-shim: -------------------------------------------------------------------------------- 1 | ../../../ -------------------------------------------------------------------------------- /test/nodeps/extshim-exposed/config/shim.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | 'non-cjs': { exports : 'noncjs' } 5 | }; 6 | -------------------------------------------------------------------------------- /test/nodeps/extshim-exposed/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var noncjs = require('non-cjs'); 4 | 5 | var go = module.exports = function () { 6 | console.log('main', noncjs.main); 7 | console.log('version', noncjs.version); 8 | return noncjs; 9 | }; 10 | -------------------------------------------------------------------------------- /test/nodeps/extshim-exposed/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./main.js", 3 | "browser": { 4 | "non-cjs": "./vendor/non-cjs.js" 5 | }, 6 | "browserify": { 7 | "transform": [ 8 | "browserify-shim" 9 | ] 10 | }, 11 | "browserify-shim": "./config/shim.js" 12 | } 13 | -------------------------------------------------------------------------------- /test/nodeps/extshim-exposed/vendor/non-cjs.js: -------------------------------------------------------------------------------- 1 | window.noncjs = { 2 | name: 'non-cjs' 3 | , version: 1.1 4 | }; 5 | -------------------------------------------------------------------------------- /test/nodeps/extshim/config/shim.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | '../vendor/non-cjs.js': { exports : 'noncjs' } 5 | }; 6 | -------------------------------------------------------------------------------- /test/nodeps/extshim/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var noncjs = require('./vendor/non-cjs'); 4 | 5 | var go = module.exports = function () { 6 | console.log('main', noncjs.main); 7 | console.log('version', noncjs.version); 8 | return noncjs; 9 | }; 10 | -------------------------------------------------------------------------------- /test/nodeps/extshim/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./main.js", 3 | "browserify": { 4 | "transform": [ 5 | "browserify-shim" 6 | ] 7 | }, 8 | "browserify-shim": "./config/shim.js" 9 | } 10 | -------------------------------------------------------------------------------- /test/nodeps/extshim/vendor/non-cjs.js: -------------------------------------------------------------------------------- 1 | window.noncjs = { 2 | name: 'non-cjs' 3 | , version: 1.1 4 | }; 5 | -------------------------------------------------------------------------------- /test/nodeps/inlineshim-exposed/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var noncjs = require('./vendor/non-cjs'); 4 | 5 | var go = module.exports = function () { 6 | console.log('main', noncjs.main); 7 | console.log('version', noncjs.version); 8 | return noncjs; 9 | }; 10 | -------------------------------------------------------------------------------- /test/nodeps/inlineshim-exposed/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./main.js", 3 | "browser": { 4 | "non-cjs": "./vendor/non-cjs.js" 5 | }, 6 | "browserify-shim": { 7 | "non-cjs": "noncjs" 8 | }, 9 | "browserify": { 10 | "transform": [ 11 | "browserify-shim" 12 | ] 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /test/nodeps/inlineshim-exposed/vendor/non-cjs.js: -------------------------------------------------------------------------------- 1 | window.noncjs = { 2 | name: 'non-cjs' 3 | , version: 1.1 4 | }; 5 | -------------------------------------------------------------------------------- /test/nodeps/inlineshim/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var noncjs = require('./vendor/non-cjs'); 4 | 5 | var go = module.exports = function () { 6 | console.log('main', noncjs.main); 7 | console.log('version', noncjs.version); 8 | return noncjs; 9 | }; 10 | -------------------------------------------------------------------------------- /test/nodeps/inlineshim/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./main.js", 3 | "browserify": { 4 | "transform": [ 5 | "browserify-shim" 6 | ] 7 | }, 8 | "browserify-shim": { 9 | "./vendor/non-cjs.js": "noncjs" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/nodeps/inlineshim/vendor/non-cjs.js: -------------------------------------------------------------------------------- 1 | window.noncjs = { 2 | name: 'non-cjs' 3 | , version: 1.1 4 | }; 5 | -------------------------------------------------------------------------------- /test/nodeps/node_modules/browserify-shim: -------------------------------------------------------------------------------- 1 | ../../../ -------------------------------------------------------------------------------- /test/packs/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var sub1 = require('sub1'); 4 | var sub2 = require('sub2'); 5 | 6 | var go = module.exports = function () { 7 | return { 8 | sub1: sub1(), 9 | // since sub1 main requires this module it will be attached to the window 10 | sub1noncjsdep: window.sub1noncjsdep, 11 | 12 | sub2: sub2(), 13 | // since sub2 main requires this module it will be attached to the window 14 | sub2noncjsdep: window.sub2noncjsdep 15 | } 16 | return require('root-non-cjs'); 17 | }; 18 | -------------------------------------------------------------------------------- /test/packs/node_modules/browserify-shim: -------------------------------------------------------------------------------- 1 | ../../../ -------------------------------------------------------------------------------- /test/packs/node_modules/sub1/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var noncjsdep = require('./vendor/non-cjs-dep'); 4 | 5 | var go = module.exports = function () { 6 | return noncjsdep; 7 | }; 8 | -------------------------------------------------------------------------------- /test/packs/node_modules/sub1/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sub1", 3 | "main": "./main.js", 4 | "browser": { 5 | "sub1-non-cjs": "./vendor/non-cjs.js" 6 | }, 7 | "browserify-shim": { 8 | "sub1-non-cjs": "sub1noncjs", 9 | "./vendor/non-cjs-dep.js": { "exports": "sub1noncjsdep", "depends": "sub1-non-cjs:sub1noncjs" } 10 | }, 11 | "browserify": { 12 | "transform": [ 13 | "browserify-shim" 14 | ] 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /test/packs/node_modules/sub1/vendor/non-cjs-dep.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // expecting nodeps to have been required before me and attached itself to the window 4 | window.sub1noncjsdep = { name: 'sub1noncjsdep', sub1noncjs: window.sub1noncjs }; 5 | -------------------------------------------------------------------------------- /test/packs/node_modules/sub1/vendor/non-cjs.js: -------------------------------------------------------------------------------- 1 | window.sub1noncjs = { 2 | name: 'sub1-non-cjs' 3 | , version: 0.1 4 | }; 5 | -------------------------------------------------------------------------------- /test/packs/node_modules/sub2/config/shim.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | 'sub2-non-cjs': { exports : 'sub2noncjs' }, 5 | 'sub2-non-cjs-dep': { exports: 'sub2noncjsdep', depends: { 'sub2-non-cjs': 'sub2noncjs' } } 6 | }; 7 | -------------------------------------------------------------------------------- /test/packs/node_modules/sub2/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var sub2noncjsdep = require('sub2-non-cjs-dep'); 4 | 5 | var go = module.exports = function () { 6 | return sub2noncjsdep; 7 | }; 8 | -------------------------------------------------------------------------------- /test/packs/node_modules/sub2/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./main.js", 3 | "browser": { 4 | "main": "./main.js", 5 | "sub2-non-cjs": "./vendor/non-cjs.js", 6 | "sub2-non-cjs-dep": "./vendor/non-cjs-dep.js" 7 | }, 8 | "browserify": { 9 | "transform": [ 10 | "browserify-shim" 11 | ] 12 | }, 13 | "browserify-shim": "./config/shim.js" 14 | } 15 | -------------------------------------------------------------------------------- /test/packs/node_modules/sub2/vendor/non-cjs-dep.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // expecting nodeps to have been required before me and attached itself to the window 4 | window.sub2noncjsdep = { name: 'sub2noncjsdep', sub2noncjs: window.sub2noncjs }; 5 | -------------------------------------------------------------------------------- /test/packs/node_modules/sub2/vendor/non-cjs.js: -------------------------------------------------------------------------------- 1 | window.sub2noncjs = { 2 | name: 'sub2-non-cjs' 3 | , version: 0.2 4 | }; 5 | -------------------------------------------------------------------------------- /test/packs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "root", 3 | "main": "./main.js", 4 | "browser": { 5 | "root-non-cjs": "./vendor/non-cjs.js" 6 | }, 7 | "browserify-shim": { 8 | "root-non-cjs": { 9 | "exports": "rootnoncjs", 10 | "depends": [ "sub1", "sub2" ] 11 | } 12 | }, 13 | "browserify": { 14 | "transform": [ 15 | "browserify-shim" 16 | ] 17 | }, 18 | "dependencies": { 19 | "sub1": "*", 20 | "sub2": "*" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /test/packs/vendor/non-cjs.js: -------------------------------------------------------------------------------- 1 | // expects browserify-shim to require and attach sub1noncjs and sub2noncjsdep 2 | window.rootnoncjs = { 3 | sub1noncjs: window.sub1noncjs, 4 | sub2noncjsdep: window.sub2noncjsdep 5 | }; 6 | -------------------------------------------------------------------------------- /test/parse-inline-shim.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | var debug = false; 5 | var test = debug ? function () {} : require('tap').test 6 | var test_ = !debug ? function () {} : require('tap').test 7 | 8 | var parse = require('../lib/parse-inline-shims') 9 | 10 | function inspect(obj, depth) { 11 | console.error(require('util').inspect(obj, false, depth || 5, true)); 12 | } 13 | 14 | test('\nparsing shims with pure string spec, single depends as string and multi-depends as array', function (t) { 15 | var shims = { 16 | jquery: '$', 17 | 'non-cjs' : 'noncjs', 18 | 'non-cjs-dep' : { exports: 'noncjsdep' , depends: 'non-cjs:noncjs' }, 19 | 'noexport-dep' : { exports: 'noexportdep' , depends: 'non-cjs' }, 20 | 'just-dep' : { exports: 'justdep' , depends: [ 'non-cjs:noncjs', 'jquery:$' ] }, 21 | } 22 | t.deepEqual( 23 | parse(shims) 24 | , { jquery : { exports : '$' , depends: undefined }, 25 | 'non-cjs' : { exports : 'noncjs' , depends: undefined }, 26 | 'non-cjs-dep' : { exports : 'noncjsdep' , depends: { 'non-cjs': 'noncjs' } }, 27 | 'noexport-dep': { exports : 'noexportdep' , depends: { 'non-cjs': null } }, 28 | 'just-dep' : { exports : 'justdep' , depends: { 'non-cjs': 'noncjs', jquery : '$' } } 29 | } 30 | ) 31 | t.end() 32 | }) 33 | 34 | test('\nparsing shims with invalid depends', function (t) { 35 | function shouldFail(shims, msg) { 36 | try { 37 | parse(shims); 38 | t.fail('should have failed'); 39 | } catch(e) { 40 | t.similar(e.message, /Invalid depends specification/, msg); 41 | } 42 | } 43 | 44 | shouldFail({ 45 | 'non-cjs': 'noncjs', 46 | 'non-cjs-dep': { exports: 'noncjsdep', depends: 'non-cjs:noncjs:jquery:$' }, 47 | } 48 | , 'more than one depends in one declaration' 49 | ) 50 | 51 | shouldFail({ 52 | 'non-cjs': 'noncjs', 53 | 'non-cjs-dep': { exports: 'noncjsdep', depends: ':noncjs' }, 54 | } 55 | , 'no export name' 56 | ) 57 | 58 | t.end(); 59 | }) 60 | 61 | test('\nparsing shims with whitespaces', function (t) { 62 | var shims = { 63 | jquery: '$', 64 | 'non-cjs' : 'noncjs', 65 | 'non-cjs-dep' : { exports: 'noncjsdep ', depends: 'non-cjs :noncjs' }, 66 | 'just-dep ' : { exports: ' justdep' , depends: [ ' non-cjs:noncjs', 'jquery: $ ' ] }, 67 | } 68 | var res = parse(shims); 69 | var jd = res['just-dep'] 70 | t.equal(res['non-cjs-dep'].exports, 'noncjsdep', 'removes trailing space from export') 71 | t.equal(jd.exports, 'justdep', 'removes leading space from exports') 72 | t.equal(jd.depends.jquery, '$', 'removes spaces around depends export') 73 | 74 | t.end() 75 | }) 76 | -------------------------------------------------------------------------------- /test/resolve-shims-bower.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | var test = require('tap').test 5 | var path = require('path') 6 | 7 | var resolve = require('../lib/resolve-shims'); 8 | 9 | var msgs = []; 10 | 11 | function inspect(obj, depth) { 12 | console.error(require('util').inspect(obj, false, depth || 5, true)); 13 | } 14 | 15 | test('\nresolving a bower component that is shimmed', function (t) { 16 | resolve(require.resolve('./bower/components/jquery-ui/ui/jquery.ui.position.js'), msgs, function (err, res) { 17 | 18 | if (err) { t.fail(err); return t.end(); } 19 | t.deepEqual(res.shim, { exports: null, depends: { jquery: 'jQuery' } }, 'resolves shim correctly') 20 | t.end(); 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/resolve-shims.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | process.env.BROWSERIFYSHIM_DIAGNOSTICS=1 5 | 6 | var test = require('tap').test 7 | var path = require('path'); 8 | var resolve = require('../lib/resolve-shims'); 9 | var msgs = []; 10 | 11 | function inspect(obj, depth) { 12 | console.error(require('util').inspect(obj, false, depth || 5, true)); 13 | } 14 | 15 | test('\nno dependencies, external shim file, no expose', function (t) { 16 | resolve(require.resolve('./nodeps/extshim/vendor/non-cjs'), msgs, function (err, res) { 17 | if (err) { t.fail(err); return t.end(); } 18 | t.deepEqual(res.shim, { exports: 'noncjs', depends: undefined }, 'resolves noncjs shim correctly') 19 | t.end(); 20 | }); 21 | }) 22 | 23 | test('\nno dependencies, external shim, exposed as non-cjs', function (t) { 24 | resolve(require.resolve('./nodeps/extshim-exposed/vendor/non-cjs'), msgs, function (err, res) { 25 | if (err) { t.fail(err); return t.end(); } 26 | t.deepEqual(res.shim, { exports: 'noncjs', depends: undefined }, 'resolves noncjs shim correctly') 27 | t.end(); 28 | }); 29 | }) 30 | 31 | test('\nno dependencies, inline shims, no expose', function (t) { 32 | resolve(require.resolve('./nodeps/inlineshim/vendor/non-cjs'), msgs, function (err, res) { 33 | if (err) { t.fail(err); return t.end(); } 34 | t.deepEqual(res.shim, { exports: 'noncjs', depends: undefined }, 'resolves noncjs shim correctly') 35 | t.end(); 36 | }); 37 | }) 38 | 39 | test('\nno dependencies, inline shims, exposed as non-cjs', function (t) { 40 | resolve(require.resolve('./nodeps/inlineshim-exposed/vendor/non-cjs'), msgs, function (err, res) { 41 | if (err) { t.fail(err); return t.end(); } 42 | t.deepEqual(res.shim, { exports: 'noncjs', depends: undefined }, 'resolves noncjs shim correctly') 43 | t.end(); 44 | }); 45 | }) 46 | 47 | test('\nnon-cjs-dep depends on non-cjs, external shim file, all exposed', function (t) { 48 | resolve(require.resolve('./deps/extshim/vendor/non-cjs-dep'), msgs, function (err, res) { 49 | if (err) { t.fail(err); return t.end(); } 50 | t.deepEqual(res.shim 51 | , { exports: 'noncjsdep', depends: { 'non-cjs': 'noncjs' } } 52 | , 'resolves noncjsdep shim correctly' 53 | ) 54 | t.end(); 55 | }); 56 | }) 57 | 58 | test('\nnon-cjs-dep depends on non-cjs, inline shims, all exposed', function (t) { 59 | resolve(require.resolve('./deps/inlineshim/vendor/non-cjs-dep'), msgs, function (err, res) { 60 | if (err) { t.fail(err); return t.end(); } 61 | t.deepEqual(res.shim 62 | , { exports: 'noncjsdep', depends: { 'non-cjs': 'noncjs' } } 63 | , 'resolves noncjsdep shim correctly' 64 | ) 65 | t.end(); 66 | }); 67 | }) 68 | 69 | test('\nnon-cjs-dep depends on non-cjs and non-cjs-core, external shim file, all exposed except non-cjs-core', function (t) { 70 | resolve(require.resolve('./multideps/extshim/vendor/non-cjs-dep'), msgs, function (err, res) { 71 | if (err) { t.fail(err); return t.end(); } 72 | var corePath = path.join(__dirname, 'multideps/extshim/vendor/non-cjs-core.js'); 73 | var depends = { 'non-cjs': 'noncjs' } 74 | depends[corePath] = 'core'; 75 | t.deepEqual( 76 | res.shim 77 | , { exports: 'noncjsdep', depends: depends } 78 | , 'resolves noncjsdep shim correctly' 79 | ) 80 | t.end(); 81 | }); 82 | }) 83 | 84 | test('\nnon-cjs-dep depends on non-cjs and non-cjs-core, inline shims, all exposed except non-cjs-core', function (t) { 85 | resolve(require.resolve('./multideps/inlineshim/vendor/non-cjs-dep'), msgs, function (err, res) { 86 | if (err) { t.fail(err); return t.end(); } 87 | var corePath = path.join(__dirname, 'multideps/inlineshim/vendor/non-cjs-core.js'); 88 | var depends = { 'non-cjs': 'noncjs' } 89 | depends[corePath] = 'core'; 90 | t.deepEqual( 91 | res.shim 92 | , { exports: 'noncjsdep', depends: depends } 93 | , 'resolves noncjsdep shim correctly' 94 | ) 95 | t.end(); 96 | }); 97 | }) 98 | 99 | test('\nno dependencies, inline shims, no expose, $ exposified as jquery', function (t) { 100 | resolve(require.resolve('./exposify/inlineshim/vendor/non-cjs'), msgs, function (err, res) { 101 | if (err) { t.fail(err); return t.end(); } 102 | t.deepEqual(res.shim, { exports: 'noncjs', depends: undefined }, 'resolves noncjs shim correctly') 103 | t.deepEqual(res.exposeGlobals, { jquery: '$' }, 'resolves expose globals correctly') 104 | t.end(); 105 | }); 106 | }) 107 | 108 | test('\nno dependencies, external shims, no expose, $ exposified as jquery', function (t) { 109 | resolve(require.resolve('./exposify/extshim/vendor/non-cjs'), msgs, function (err, res) { 110 | if (err) { t.fail(err); return t.end(); } 111 | t.deepEqual(res.shim, { exports: 'noncjs', depends: undefined }, 'resolves noncjs shim correctly') 112 | t.deepEqual(res.exposeGlobals, { jquery: '$' }, 'resolves expose globals correctly') 113 | t.end(); 114 | }); 115 | }) 116 | -------------------------------------------------------------------------------- /test/shim/fixtures/entry-requires-depend-on-jquery-and-_.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var $ = require('./shims/lib-depending-on-jquery-and-_') 4 | 5 | // expose require in order to support testing 6 | window.require = require; 7 | 8 | // depended on libs are exposed as dep inside lib-depending-on-jquery 9 | module.exports = window.dep; 10 | -------------------------------------------------------------------------------- /test/shim/fixtures/entry-requires-depend-on-jquery.js: -------------------------------------------------------------------------------- 1 | 2 | 'use strict'; 3 | 4 | var $ = require('./shims/lib-depending-on-jquery') 5 | 6 | // expose require in order to support testing 7 | window.require = require; 8 | 9 | // depended on libs are exposed as dep inside lib-depending-on-jquery 10 | module.exports = window.dep; 11 | -------------------------------------------------------------------------------- /test/shim/fixtures/entry-requires-jquery.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var $ = require('./shims/crippled-jquery') 4 | , foo = require('./foo'); 5 | 6 | console.log('jquery version', $().jquery); 7 | console.log('foo', foo()); 8 | 9 | // expose require in order to support testing 10 | window.require = require; 11 | 12 | module.exports = { 13 | getJqueryVersion: function () { return $().jquery; } 14 | , foo: foo() 15 | }; 16 | -------------------------------------------------------------------------------- /test/shim/fixtures/entry-requires-lib-with-global-problem.js: -------------------------------------------------------------------------------- 1 | var eve = require('./shims/lib-with-exports-define-global-problem'); 2 | 3 | // expose require to tests 4 | window.require = require; 5 | 6 | exports.exportedEve = eve; 7 | exports.attachedEve = window.eve; 8 | -------------------------------------------------------------------------------- /test/shim/fixtures/entry-requires-root-level-var.js: -------------------------------------------------------------------------------- 1 | var rootvar = require('./shims/root-level-var'); 2 | 3 | // expose require to tests 4 | window.require = require; 5 | 6 | module.exports = rootvar; 7 | -------------------------------------------------------------------------------- /test/shim/fixtures/entry-requires-this-iife.js: -------------------------------------------------------------------------------- 1 | var respond = require('./shims/this-iife'); 2 | 3 | // expose require to tests 4 | window.require = require; 5 | 6 | module.exports = respond; 7 | -------------------------------------------------------------------------------- /test/shim/fixtures/foo.js: -------------------------------------------------------------------------------- 1 | module.exports = function () { return 'foo'; }; 2 | -------------------------------------------------------------------------------- /test/shim/fixtures/shims/crippled-jquery.js: -------------------------------------------------------------------------------- 1 | // The stripped down version just for testing. Don't want to overload github and npm with jquery copies ;) 2 | (function( window, undefined ) { 3 | var 4 | // A central reference to the root jQuery(document) 5 | rootjQuery, 6 | 7 | // The deferred used on DOM ready 8 | readyList, 9 | 10 | // Map over jQuery in case of overwrite 11 | _jQuery = window.jQuery, 12 | 13 | // Map over the $ in case of overwrite 14 | _$ = window.$, 15 | 16 | jQuery = function( selector, context ) { 17 | return jQuery.fn; 18 | }; 19 | 20 | jQuery.fn = jQuery.prototype = { 21 | // The current version of jQuery being used 22 | jquery: "1.8.3", 23 | }; 24 | 25 | // help with testing 26 | window.require = require; 27 | 28 | // Expose jQuery to the global object 29 | window.jQuery = window.$ = jQuery; 30 | 31 | if ( typeof define === "function" && define.amd && define.amd.jQuery ) { 32 | define( "jquery", [], function () { return jQuery; } ); 33 | } 34 | 35 | })( window ); 36 | 37 | if(this !== window) throw new Error('this should be window'); 38 | -------------------------------------------------------------------------------- /test/shim/fixtures/shims/lib-depending-on-jquery-and-_.js: -------------------------------------------------------------------------------- 1 | var dep = {}; 2 | window.dep = dep; 3 | dep.jqVersion = window.$().jquery; 4 | dep._ = window._; 5 | -------------------------------------------------------------------------------- /test/shim/fixtures/shims/lib-depending-on-jquery.js: -------------------------------------------------------------------------------- 1 | var dep = {}; 2 | window.dep = dep; 3 | dep.jqVersion = window.$().jquery; 4 | -------------------------------------------------------------------------------- /test/shim/fixtures/shims/lib-exporting-_.js: -------------------------------------------------------------------------------- 1 | var _ = function () { return 'super underscore' } 2 | module.exports = _; 3 | -------------------------------------------------------------------------------- /test/shim/fixtures/shims/lib-with-exports-define-global-problem.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Reproducing problem here: https://github.com/mhemesath/r2d3/blob/master/r2d3.v2.js 3 | * If module.exports or define is present, it never attaches itself to the window. 4 | * This is a problem since browserify defines module.exports. 5 | * If it is not a clean separate module like the one mentioned this becomes a problem. 6 | * 7 | * In this specific case eve is not attached to the window since module.exports is present. 8 | * However the included module, Raphael, expects it to be attached. 9 | * Obviously a bad case of concetenating libs together, but we need to deal with this since, 10 | * its out there. 11 | */ 12 | 13 | (function init(glob) { 14 | 15 | var eve = 'loves adam'; 16 | 17 | // this horrible case of nested ternaries came straight from: https://github.com/mhemesath/r2d3/blob/master/r2d3.v2.js#L222 18 | (typeof module != "undefined" && module.exports) 19 | ? (module.exports = eve) 20 | : (typeof define != "undefined" 21 | ? (define("eve", [], function() { return eve; })) 22 | : (window.eve = eve)); 23 | })(this); 24 | -------------------------------------------------------------------------------- /test/shim/fixtures/shims/root-level-var.js: -------------------------------------------------------------------------------- 1 | var nineties = {}; 2 | nineties.message = "I declare vars on the script level and expect them to get attached to the window because I'm doin' it 90s style baby!"; 3 | -------------------------------------------------------------------------------- /test/shim/fixtures/shims/this-iife.js: -------------------------------------------------------------------------------- 1 | (function (window) { 2 | window.respond = { 3 | update: function () {} 4 | }; 5 | })(this); 6 | -------------------------------------------------------------------------------- /test/shim/shim-depends.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | var browserify_version = require('./utils/browserify-version'); 4 | 5 | var browserify = require('browserify') 6 | , test = require('tap').test 7 | , vm = require('vm') 8 | , proxyquire = require('proxyquire') 9 | 10 | var jquery = { exports: '$' }; 11 | var jqueryPath = require.resolve('./fixtures/shims/crippled-jquery'); 12 | 13 | var underscore = { exports: null }; 14 | var underscorePath = require.resolve('./fixtures/shims/lib-exporting-_'); 15 | 16 | var dependent = { 17 | exports: 'dep' 18 | , depends: { } 19 | }; 20 | dependent.depends[jqueryPath] = '$'; 21 | var dependentPath = require.resolve('./fixtures/shims/lib-depending-on-jquery'); 22 | 23 | var multidependentPath = require.resolve('./fixtures/shims/lib-depending-on-jquery-and-_'); 24 | var multidependent = { 25 | exports: 'dep' 26 | , depends: { } 27 | }; 28 | 29 | multidependent.depends[jqueryPath] = '$'; 30 | multidependent.depends[underscorePath] = '_'; 31 | 32 | 33 | function inspect(obj, depth) { 34 | console.error(require('util').inspect(obj, false, depth || 5, true)); 35 | } 36 | 37 | function runFirstBundle(entry, fullPaths, cb) { 38 | function resolveShims (file_, msgs, cb) { 39 | var res; 40 | if (file_ === jqueryPath) res = jquery; 41 | if (file_ === dependentPath) res = dependent; 42 | 43 | setTimeout(cb.bind(null, null, { shim: res }), 0) 44 | } 45 | 46 | var shim = proxyquire('../../', { 47 | './lib/resolve-shims': resolveShims 48 | }) 49 | 50 | browserify(entry, { fullPaths: fullPaths }) 51 | .transform(shim) 52 | .bundle(function (err, src) { 53 | if (err) return cb(err); 54 | 55 | var ctx = { window: {}, console: console }; 56 | ctx.self = ctx.window; 57 | 58 | var require_ = vm.runInNewContext(src, ctx); 59 | cb(null, require_) 60 | }) 61 | } 62 | 63 | test('\nwhen I shim "jquery" and shim a lib that depends on it', function (t) { 64 | 65 | var entry = require.resolve('./fixtures/entry-requires-depend-on-jquery'); 66 | 67 | runFirstBundle(entry, false, function (err, require_) { 68 | if (err) { t.fail(err); return t.end(); } 69 | 70 | var dep = require_(1); 71 | t.equal(dep.jqVersion, '1.8.3', 'when dependent gets required, $ is attached to the window'); 72 | t.end() 73 | }) 74 | }) 75 | 76 | if (browserify_version >= 5) 77 | test('\nwhen I shim "jquery" and shim a lib that depends on it, using fullPaths', function (t) { 78 | 79 | var entry = require.resolve('./fixtures/entry-requires-depend-on-jquery'); 80 | 81 | runFirstBundle(entry, true, function (err, require_) { 82 | if (err) { t.fail(err); return t.end(); } 83 | 84 | var dep = require_(entry); 85 | t.equal(dep.jqVersion, '1.8.3', 'when dependent gets required, $ is attached to the window'); 86 | t.end() 87 | }) 88 | }) 89 | 90 | function runSecondBundle(entry, fullPaths, cb) { 91 | function resolveShims (file_, msgs, cb) { 92 | var res; 93 | if (file_ === jqueryPath) res = jquery; 94 | if (file_ === underscorePath) res = underscore; 95 | if (file_ === multidependentPath) res = multidependent; 96 | 97 | setTimeout(cb.bind(null, null, { shim: res }), 0) 98 | } 99 | 100 | var shim = proxyquire('../../', { 101 | './lib/resolve-shims': resolveShims 102 | }) 103 | 104 | browserify(entry, { fullPaths: fullPaths }) 105 | .transform(shim) 106 | .bundle(function (err, src) { 107 | if (err) return cb(err); 108 | 109 | var ctx = { window: {}, console: console }; 110 | ctx.self = ctx.window; 111 | var require_ = vm.runInNewContext(src, ctx); 112 | cb(null, require_) 113 | }) 114 | } 115 | 116 | test('\nwhen I shim "jquery" and _ lib in debug mode and shim a lib that depends on both', function (t) { 117 | 118 | var entry = require.resolve('./fixtures/entry-requires-depend-on-jquery-and-_'); 119 | runSecondBundle(entry, false, function (err, require_) { 120 | if (err) { t.fail(err); return t.end(); } 121 | 122 | var dep = require_(1); 123 | 124 | t.equal(dep.jqVersion, '1.8.3', 'when multidependent gets required, $ is attached to the window'); 125 | t.equal(dep._(), 'super underscore', 'and _ is attached to the window'); 126 | t.end() 127 | }) 128 | }) 129 | 130 | if (browserify_version >= 5) 131 | test('\nwhen I shim "jquery" and _ lib in debug mode and shim a lib that depends on both, using fullPaths', function (t) { 132 | 133 | var entry = require.resolve('./fixtures/entry-requires-depend-on-jquery-and-_'); 134 | runSecondBundle(entry, true, function (err, require_) { 135 | if (err) { t.fail(err); return t.end(); } 136 | 137 | var dep = require_(entry); 138 | 139 | t.equal(dep.jqVersion, '1.8.3', 'when multidependent gets required, $ is attached to the window'); 140 | t.equal(dep._(), 'super underscore', 'and _ is attached to the window'); 141 | t.end() 142 | }) 143 | }) 144 | -------------------------------------------------------------------------------- /test/shim/shim-exports-define-window.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | var browserify_version = require('./utils/browserify-version'); 5 | var browserify = require('browserify') 6 | , test = require('tap').test 7 | , vm = require('vm') 8 | , proxyquire = require('proxyquire') 9 | ; 10 | 11 | var file = require.resolve('./fixtures/shims/lib-with-exports-define-global-problem.js'); 12 | var entry = require.resolve('./fixtures/entry-requires-lib-with-global-problem.js'); 13 | 14 | // More explanation about the issue reproduced by this test inside the fixture itself 15 | function runBundle(fullPaths, cb) { 16 | function resolveShims (file_, msgs, cb) { 17 | var res = file_ === file 18 | ? { exports: 'eve' } 19 | : null; 20 | 21 | setTimeout(cb.bind(null, null, { shim: res }), 0) 22 | } 23 | 24 | var shim = proxyquire('../../', { 25 | './lib/resolve-shims': resolveShims 26 | }) 27 | 28 | browserify(entry, { fullPaths: fullPaths }) 29 | .transform(shim) 30 | .bundle(function (err, src) { 31 | if (err) return cb(err); 32 | 33 | var ctx = { window: { name: 'the window' }, console: console }; 34 | ctx.self = ctx.window; 35 | var require_ = vm.runInNewContext(src, ctx) 36 | cb(null, require_); 37 | }) 38 | } 39 | 40 | test('when a module only attaches to the window after checking for module.exports and define and we browserify it', function (t) { 41 | runBundle(false, function (err, require_) { 42 | if (err) { t.fail(err); return t.end() } 43 | 44 | var exp = require_(1); 45 | 46 | t.equal(exp.attachedEve, 'loves adam', 'attaches it to window') 47 | t.equal(exp.exportedEve, 'loves adam', 'exports it as well') 48 | t.end() 49 | }) 50 | }) 51 | 52 | if (browserify_version >= 5) 53 | test('when a module only attaches to the window after checking for module.exports and define and we browserify it, using fullPaths', function (t) { 54 | runBundle(true, function (err, require_) { 55 | if (err) { t.fail(err); return t.end() } 56 | 57 | var exp = require_(entry); 58 | 59 | t.equal(exp.attachedEve, 'loves adam', 'attaches it to window') 60 | t.equal(exp.exportedEve, 'loves adam', 'exports it as well') 61 | t.end() 62 | }) 63 | }) 64 | -------------------------------------------------------------------------------- /test/shim/shim-impress.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true*/ 3 | 4 | var testLib = require('./utils/test-lib') 5 | var test = require('tap').test 6 | , baseUrl = 'https://raw.github.com/bartaz/impress.js/' 7 | 8 | test('impressjs 0.5.3', function (t) { 9 | var shimConfig = { exports: 'impress' } 10 | testLib(t, { 11 | name: 'impress.js' 12 | , test: function (t, resolved) { t.equals(typeof resolved().init, 'function', 'shims impressjs 0.5.3') } 13 | , asserts: 1 14 | , shimConfig: shimConfig 15 | , baseUrl: baseUrl + '0.5.3/js/' 16 | }) 17 | }); 18 | -------------------------------------------------------------------------------- /test/shim/shim-jquery.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true*/ 3 | 4 | var testLib = require('./utils/test-lib') 5 | , test = require('tap').test 6 | , baseUrl = 'http://code.jquery.com/' 7 | , shimConfig = { exports: '$' } 8 | 9 | test('jquery version 1.6.4', function (t) { 10 | var jq = { 11 | name: 'jquery-1.6.4.min.js' 12 | , test: function (t, resolved) { t.equals(resolved().jquery, '1.6.4', 'shims jquery 1.6.4') } 13 | , asserts: 1 14 | , shimConfig: shimConfig 15 | , baseUrl: baseUrl 16 | } 17 | 18 | testLib(t, jq); 19 | }) 20 | 21 | test('jquery version 1.7.2', function (t) { 22 | var jq = { 23 | name: 'jquery-1.7.2.min.js' 24 | , test: function (t, resolved) { t.equals(resolved().jquery, '1.7.2', 'shims jquery 1.7.2') } 25 | , asserts: 1 26 | , shimConfig: shimConfig 27 | , baseUrl: baseUrl 28 | } 29 | 30 | testLib(t, jq); 31 | }) 32 | 33 | test('jquery version 1.8.3', function (t) { 34 | var jq = { 35 | name: 'jquery-1.8.3.min.js' 36 | , test: function (t, resolved) { t.equals(resolved().jquery, '1.8.3', 'shims jquery 1.8.3') } 37 | , asserts: 1 38 | , shimConfig: shimConfig 39 | , baseUrl: baseUrl 40 | } 41 | 42 | testLib(t, jq); 43 | }) 44 | 45 | test('jquery version 2.0.3', function (t) { 46 | var jq = { 47 | name: 'jquery-2.0.3.min.js' 48 | , test: function (t, resolved) { t.equals(resolved().jquery, '2.0.3', 'shims jquery 2.0.3') } 49 | , asserts: 1 50 | , shimConfig: shimConfig 51 | , baseUrl: baseUrl 52 | } 53 | 54 | testLib(t, jq); 55 | }) 56 | -------------------------------------------------------------------------------- /test/shim/shim-root-level-var.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | var browserify_version = require('./utils/browserify-version'); 4 | 5 | var browserify = require('browserify') 6 | , test = require('tap').test 7 | , vm = require('vm') 8 | , path = require('path') 9 | , proxyquire = require('proxyquire') 10 | 11 | var entry = require.resolve('./fixtures/entry-requires-root-level-var.js'); 12 | var file = require.resolve('./fixtures/shims/root-level-var'); 13 | 14 | function runBundle(fullPaths, cb) { 15 | 16 | function resolveShims (file_, msgs, cb) { 17 | var res = file_ === file 18 | ? { exports: 'nineties' } 19 | : null; 20 | 21 | setTimeout(cb.bind(null, null, { shim: res }), 0) 22 | } 23 | 24 | var shim = proxyquire('../../', { 25 | './lib/resolve-shims': resolveShims 26 | }) 27 | 28 | browserify(entry, { fullPaths: fullPaths }) 29 | .transform(shim) 30 | .bundle(function (err, src) { 31 | if (err) return cb(err); 32 | 33 | var ctx = { window: {}, console: console }; 34 | ctx.self = ctx.window; 35 | var require_ = vm.runInNewContext(src, ctx); 36 | 37 | cb(null, require_) 38 | }) 39 | } 40 | 41 | test('when I shim a module that declares its export as a var on the root level instead of attaching it to the window', function (t) { 42 | runBundle(false, function (err, require_) { 43 | if (err) { t.fail(err); return t.end() } 44 | t.equal( 45 | require_(1).message 46 | , "I declare vars on the script level and expect them to get attached to the window because I'm doin' it 90s style baby!" 47 | , 'shims that sucker' 48 | ); 49 | t.end() 50 | }); 51 | }) 52 | 53 | if (browserify_version >= 5) 54 | test('when I shim a module that declares its export as a var on the root level instead of attaching it to the window, using fullPaths', function (t) { 55 | runBundle(true, function (err, require_) { 56 | if (err) { t.fail(err); return t.end() } 57 | t.equal( 58 | require_(entry).message 59 | , "I declare vars on the script level and expect them to get attached to the window because I'm doin' it 90s style baby!" 60 | , 'shims that sucker' 61 | ); 62 | t.end() 63 | }); 64 | }) 65 | -------------------------------------------------------------------------------- /test/shim/shim-underscore.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true*/ 3 | 4 | var testLib = require('./utils/test-lib') 5 | , test = require('tap').test 6 | , baseUrl = 'https://raw.github.com/documentcloud/underscore/master/' 7 | 8 | // Not necessary to shim underscore, but serves as a good test case since it tries very hard to interface with commonJS 9 | test('underscore master', function (t) { 10 | var shimConfig = { exports: '_' } 11 | testLib(t, { 12 | name: 'underscore.js' 13 | , test: function (t, resolved) { t.equals(typeof resolved().each, 'function', 'shims underscore master') } 14 | , asserts: 1 15 | , shimConfig: shimConfig 16 | , baseUrl: baseUrl 17 | }) 18 | }); 19 | -------------------------------------------------------------------------------- /test/shim/shim-zepto.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true*/ 3 | 4 | var testLib = require('./utils/test-lib') 5 | , test = require('tap').test 6 | , baseUrl = 'http://zeptojs.com/' 7 | 8 | test('zepto', function (t) { 9 | var shimConfig = { exports: '$' } 10 | testLib(t, { 11 | name: 'zepto.min.js' 12 | , test: function (t, resolved) { t.equals(resolved.param({ one: 1, two: 2}), 'one=1&two=2', 'shims zepto') } 13 | , asserts: 1 14 | , shimConfig: shimConfig 15 | , baseUrl: baseUrl 16 | }) 17 | }); 18 | -------------------------------------------------------------------------------- /test/shim/shim.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | var browserify_version = require('./utils/browserify-version'); 4 | 5 | var browserify = require('browserify') 6 | , test = require('tap').test 7 | , vm = require('vm') 8 | , proxyquire = require('proxyquire') 9 | 10 | function inspect(obj, depth) { 11 | console.error(require('util').inspect(obj, false, depth || 5, true)); 12 | } 13 | 14 | var entry = require.resolve('./fixtures/entry-requires-jquery.js'); 15 | var file = require.resolve('./fixtures/shims/crippled-jquery') 16 | 17 | function runBundle(fullPaths, cb) { 18 | function resolveShims (file_, msgs, cb) { 19 | var res = file_ === file 20 | ? { exports: '$' } 21 | : null; 22 | 23 | setTimeout(cb.bind(null, null, { shim: res }), 0) 24 | } 25 | 26 | var shim = proxyquire('../../', { 27 | './lib/resolve-shims': resolveShims 28 | }) 29 | 30 | browserify(entry, { fullPaths: fullPaths }) 31 | .transform(shim) 32 | .bundle(function (err, src) { 33 | if (err) return cb(err); 34 | 35 | var ctx = { window: {}, console: console }; 36 | ctx.self = ctx.window; 37 | var require_ = vm.runInNewContext(src, ctx); 38 | cb(null, require_) 39 | }) 40 | } 41 | 42 | test('when I shim "jquery" to a crippled jquery filerequire it inside the entry file', function (t) { 43 | runBundle(false, function (err, require_) { 44 | if (err) { t.fail(err); return t.end() } 45 | 46 | t.equal(require_(1).getJqueryVersion(), '1.8.3', 'requires crippled jquery and gets version'); 47 | t.end() 48 | }) 49 | }) 50 | 51 | if (browserify_version >= 5) 52 | test('when I shim "jquery" to a crippled jquery filerequire it inside the entry file, using fullPaths', function (t) { 53 | runBundle(true, function (err, require_) { 54 | if (err) { t.fail(err); return t.end() } 55 | 56 | t.equal(require_(entry).getJqueryVersion(), '1.8.3', 'requires crippled jquery and gets version'); 57 | t.end() 58 | }) 59 | }) 60 | 61 | test('when I shim a library expecting this === window', function (t) { 62 | entry = require.resolve('./fixtures/entry-requires-this-iife.js') 63 | file = require.resolve('./fixtures/shims/this-iife.js') 64 | 65 | function resolveShims (file_, msgs, cb) { 66 | var res = file_ === file 67 | ? { exports: 'respond' } 68 | : null; 69 | 70 | setTimeout(cb.bind(null, null, { shim: res }), 0) 71 | } 72 | 73 | var shim = proxyquire('../../', { 74 | './lib/resolve-shims': resolveShims 75 | }); 76 | 77 | browserify(entry) 78 | .transform(shim) 79 | .bundle(function (err, src) { 80 | if (err) { 81 | t.fail(err); 82 | return t.end() 83 | } 84 | 85 | var ctx = { window: {}, console: console }; 86 | ctx.self = ctx.window; 87 | var require_ = vm.runInNewContext(src, ctx); 88 | t.equal(typeof require_(1).update, 'function', 'passes window as this') 89 | t.end() 90 | }) 91 | }) 92 | -------------------------------------------------------------------------------- /test/shim/utils/browserify-version.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var path = require('path'); 4 | 5 | module.exports = parseInt(require(path.join(path.dirname(require.resolve('browserify')), 'package.json')).version.slice(0, 1)); 6 | -------------------------------------------------------------------------------- /test/shim/utils/test-lib.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true*/ 3 | 4 | var browserify_version = require('../utils/browserify-version'); 5 | 6 | var request = require('request') 7 | , fs = require('fs') 8 | , vm = require('vm') 9 | , path = require('path') 10 | , jsdom = require('jsdom').jsdom 11 | , proxyquire = require('proxyquire') 12 | , browserify = require('browserify') 13 | 14 | , shimsdir = path.join(__dirname, '..', 'fixtures', 'shims') 15 | , entryFile = path.join(__dirname, '..', 'fixtures', 'entry-straight-export.js') 16 | 17 | var html = 18 | '' 19 | + '' 20 | + ' ' 21 | + ' Some empty page' 22 | + ' ' 23 | + ' ' 24 | + ' ' 25 | + '' 26 | 27 | function generateEntry(alias) { 28 | // just pass in exported shim in order to ensure it can be required 29 | return ('module.exports = require("' + alias + '");\n') 30 | } 31 | 32 | require('tap').on('end', function () { 33 | fs.unlinkSync(entryFile); 34 | }) 35 | 36 | module.exports = function testLib(t, opts) { 37 | var baseUrl = opts.baseUrl 38 | , name = opts.name 39 | , shimConfig = opts.shimConfig 40 | , runTest = opts.test 41 | 42 | request( baseUrl + name, function(err, res, body) { 43 | if (err) { 44 | console.error(err); 45 | t.fail(err); 46 | return t.end() 47 | } 48 | 49 | var file = path.join(shimsdir, name) 50 | 51 | fs.writeFileSync(file, body, 'utf-8'); 52 | fs.writeFileSync(entryFile, generateEntry(file), 'utf-8'); 53 | 54 | var entry = require.resolve(entryFile); 55 | 56 | function resolveShims (file_, msgs, cb) { 57 | var res = file_ === file 58 | ? shimConfig 59 | : null; 60 | 61 | setTimeout(cb.bind(null, null, { shim: res }), 0) 62 | } 63 | 64 | var shim = proxyquire('../../../', { 65 | './lib/resolve-shims': resolveShims 66 | }) 67 | 68 | // testing with and without fullPaths option (introduced in browserify version 5) 69 | if (browserify_version >= 5) 70 | t.plan(opts.asserts * 2) 71 | else 72 | t.plan(opts.asserts) 73 | 74 | 75 | t.on('end', function () { 76 | fs.unlinkSync(file); 77 | }) 78 | 79 | function runBundle(fullPaths, cb) { 80 | browserify(entryFile, { fullPaths: fullPaths }) 81 | .transform(shim) 82 | .bundle(function (err, src) { 83 | if (err) return cb(err); 84 | 85 | var window = jsdom(html).defaultView 86 | , context = vm.createContext(window) 87 | 88 | Object.keys(window).forEach(function (k) { context[k] = window[k] }) 89 | var require_ = vm.runInContext(src, context); 90 | cb(null, require_) 91 | }) 92 | } 93 | 94 | runBundle(false, function (err, require_) { 95 | if (err) { t.fail(err); return t.end() } 96 | runTest(t, require_(1)); 97 | }) 98 | 99 | if (browserify_version >= 5) 100 | runBundle(true, function (err, require_) { 101 | if (err) { t.fail(err); return t.end() } 102 | runTest(t, require_(entryFile)); 103 | }) 104 | }) 105 | 106 | } 107 | --------------------------------------------------------------------------------