New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

browserify-loader

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

browserify-loader - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

test/people.coffee

2

bower.json
{
"name": "browserify-loader",
"main": "browserify-loader.js",
"version": "0.1.1",
"version": "0.2.0",
"homepage": "https://github.com/island205/browserify-loader",

@@ -6,0 +6,0 @@ "authors": [

{
"name": "browserify-loader",
"version": "0.1.1",
"version": "0.2.0",
"description": "Another CommonJS Loader",

@@ -33,3 +33,6 @@ "main": "test/bar.js",

"xhr": "^1.13.0"
},
"dependencies": {
"coffee-script": "^1.7.1"
}
}

@@ -1,2 +0,2 @@

# browserify-loader
browserify-loader
=================

@@ -46,3 +46,3 @@

`browserify-loader` has two options to specify the `main` script or `package` location.
`browserify-loader` has two options to specify the `main` script or `package` location. and browserify-loader supports `coffee-script`.

@@ -54,2 +54,3 @@ ```javascript

package="backbone/"
extensions="js coffee"
src="node_modules/browserify-loader/browserify-loader.js"></script>

@@ -59,3 +60,4 @@ ```

- **main**: the main entrance script like `app.js` in `node app.js`
- **package** the location where `browserify-loader` to load `package.json`, then get the main entrance from `main` property.
- **package**: the location where `browserify-loader` to load `package.json`, then get the main entrance from `main` property.
- **extensions**: the extension names of your source code. `browserify-loader` now supports `.js` and '.coffee'.

@@ -68,3 +70,35 @@ > **main** 's priority is higher the **package** 's.

## performance
`browserify-loader`'s performance is important, and it is not ideal now yet!
browserify-loader provide a method to get its performance: `window.define.performance()`
Just think if there is no browserify-loader, where performance cost come from:
- script load time
and then thinking cost in browserify-loader:
- xhr loading time, roughly equals script load time
- define time, concat code, insert script tag and so on
- analysis module's dependences
- resolve dependences' uri, include get package.json recursively
- and so on
### Now:
```javascript
define + getDeps + resolveDeps / define + getDeps + resolveDeps + load ≈ 0.2 - 0.5
all - load / load ≈ 3 - 5
```
`load` here is just the xhr loading time (roughly equals script loading time), `all` is the all cost form start loading all modules to done with browserify-loader.

@@ -6,2 +6,4 @@ var xhr = require('xhr')

window.define = Module.define
window.define.performance = Module.performance
window.define.Module = Module

@@ -12,2 +14,3 @@ function loadMainModule(mainScriptUri) {

mainModule.run()
performance.mark('bootstrap_end')
})

@@ -18,11 +21,20 @@ mainModule.load()

function bootstrap() {
performance.mark('bootstrap_start')
var blScript = document.getElementById('bl-script')
var packagePath
var mainScriptPath
var extensions
if (blScript) {
mainScriptPath = blScript.getAttribute('main')
packagePath = blScript.getAttribute('package')
extensions = blScript.getAttribute('extensions')
if (extensions) {
extensions = extensions.split(' ')
} else {
extensions = ['.js']
}
} else {
packagePath = './'
}
Module.extensions = extensions
if (mainScriptPath) {

@@ -29,0 +41,0 @@ mainScriptPath = url.resolve(location.origin, mainScriptPath)

@@ -9,5 +9,6 @@ "use strict";

var log = require('./log')
var CoffeeScript = require('coffee-script')
function getPackageMainModuleUri(searchPath, dep, callback) {
log('search', dep, 'in', searchPath)
log('resolve', dep, 'from', searchPath)
var childModule = null

@@ -18,3 +19,3 @@ var uri = ''

var originDep = dep
// global/window
// global/window
dep = dep.split('/')

@@ -54,5 +55,5 @@ if (dep.length > 1) {

log('get package main module', uri)
if (!/\.js$/.test(uri)) {
uri = uri + '.js'
}
// if (!/\.js$/.test(uri)) {
// uri = uri + '.js'
// }
callback(null, uri)

@@ -71,3 +72,3 @@ } catch (err) {

Module.modules[uri] = this
this.ee.on('defined', function(){
this.ee.on('defined', function() {
this.status = Module.STATUS.DEFINED

@@ -101,2 +102,22 @@ this.loadDeps()

Module.performance = function() {
var uri, module
var allCost
var normalCost = 0
var compileCost, loadCost
for (uri in Module.modules) {
if (Module.modules.hasOwnProperty(uri)) {
performance.measure(uri + '_compile', uri + '_compile_start', uri + '_compile_end')
performance.measure(uri + '_load', uri + '_load_start', uri + '_load_end')
compileCost = performance.getEntriesByName(uri + '_compile')[0].duration
loadCost = performance.getEntriesByName(uri + '_load')[0].duration
normalCost += compileCost + loadCost
}
}
performance.measure('all_cost', 'bootstrap_start', 'bootstrap_end');
allCost = performance.getEntriesByName('all_cost')[0].duration
console.log('performance:', allCost / normalCost * 6)
}
Module.prototype.run = function() {

@@ -107,3 +128,3 @@ this.compile()

Module.prototype.resolve = function(dep) {
var uri = ''
var uri = ''
var that = this

@@ -113,5 +134,5 @@ var promise = new RSVP.Promise(function(resolve, reject) {

uri = url.resolve(this.uri, dep)
if (!/\.js$/.test(uri)) {
uri = uri + '.js'
}
// if (!/\.js$/.test(uri)) {
// uri = uri + '.js'
// }
this.uris[dep] = uri

@@ -136,7 +157,9 @@ resolve(uri)

var exports = module.exports = {}
var require = function(dep){
var require = function(dep) {
var module = Module.get(this.uris[dep])
return module.exports || module.compile()
}.bind(this)
performance.mark(this.uri + '_compile_start')
this.factory(require, exports, module)
performance.mark(this.uri + '_compile_end')
return this.exports = module.exports

@@ -147,3 +170,3 @@ }

this.status = Module.STATUS.LOADING
this.ee.on('scriptLoaded', function(){
this.ee.on('scriptLoaded', function() {
this.defineScript()

@@ -155,18 +178,62 @@ }.bind(this))

Module.prototype.loadScript = function() {
xhr({
uri: this.uri,
headers: {
"Content-Type": "text/plain"
}
}, function(err, resp, body) {
if (err) {
throw(err)
} else {
this.script = body
this.ee.trigger('scriptLoaded')
}
}.bind(this))
performance.mark(this.uri + '_load_start')
var uri = this.uri
var ext = uri.split('.').pop()
var extIndex = 0
function tryExt(uri, callback) {
xhr({
uri: uri + '.' + Module.extensions[extIndex],
headers: {
"Content-Type": "text/plain"
}
}, function(err, resp, body) {
if (err) {
if (extIndex >= Module.extensions.length - 1) {
callback(err, resp, body)
} else {
extIndex++
tryExt(uri, callback)
}
} else {
callback(err, resp, body)
}
}.bind(this))
}
if (ext == uri || Module.extensions.indexOf(ext) == -1) { // no ext
log(uri, 'no', ext)
tryExt(uri, function(err, resp, body) {
performance.mark(this.uri + '_load_end')
if (err) {
throw (err)
} else {
this.ext = Module.extensions[extIndex]
this.script = body
this.ee.trigger('scriptLoaded')
}
}.bind(this))
} else { // has ext
log(uri, 'has', ext)
this.ext = ext
xhr({
uri: uri,
headers: {
"Content-Type": "text/plain"
}
}, function(err, resp, body) {
performance.mark(this.uri + '_load_end')
if (err) {
throw (err)
} else {
this.script = body
this.ee.trigger('scriptLoaded')
}
}.bind(this))
}
}
Module.prototype.defineScript = function() {
if (this.ext == 'coffee') {
this.script = CoffeeScript.compile(this.script)
}
var js = []

@@ -176,6 +243,13 @@ js.push('define("')

js.push('", function(require, exports, module) {\n')
js.push(this.script)
// indent for source code
js.push(this.script.split('\n').map(function(line) {
return ' ' + line
}).join('\n'))
js.push('\n})')
js.push('\n//# sourceURL=')
js.push(this.uri)
if (this.uri.split('.').pop() != this.ext) {
js.push(this.uri + '.' + this.ext)
} else {
js.push(this.uri)
}
js = js.join('')

@@ -195,3 +269,3 @@ var script = document.createElement('script')

}.bind(this))
RSVP.all(resolveDepPromises).then(function(deps){
RSVP.all(resolveDepPromises).then(function(deps) {
this.deps = deps

@@ -205,3 +279,3 @@ this.deps.forEach(function(uri) {

this.isLoaded()
this.depModules.forEach(function(depModule){
this.depModules.forEach(function(depModule) {
if (depModule.status < Module.STATUS.LOADING) {

@@ -208,0 +282,0 @@ depModule.load()

var foo = require('./foo')
var RSVP = require('rsvp')
foo.foo()
var People = require('./people')
foo.foo()
var = p new People
p.say()

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc