Skip to content

Commit

Permalink
Fixes #57 Allow requiring non-JS dependencies to work on the server
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-steele-idem committed Jun 24, 2015
1 parent e4edd65 commit ff3ac6c
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
31 changes: 24 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,17 +484,34 @@ Lasso.js walks a dependency graph to find all of the resources that need to be b

It's also possible to register your own [custom dependency types](#custom-dependency-types). With custom dependency types, you can control how resources are compiled or a custom dependency type can be used to resolve additional dependencies during optimization.

A dependency can be described using a simple `String` path as shown in the following code:
A dependency can be described using a simple `String` path as shown in the following sample `browser.json` file:

```json
[
"./style.less",
"../third-party/jquery.js",
"**/*.css"
]
{
"dependencies": [
"./style.less",
"../third-party/jquery.js",
"**/*.css"
]
}
```

In the examples, the dependency type is inferred from the filename extension. Alternatively, the dependency type can be made explicit using either one of the following formats:
Alternatively, dependencies can be "required" inside a JavaScript module as shown in the following sample JavaScript code:

```javascript
require('./style.less');

// ...
```

The only caveat to using a `require()` call to add a non-JavaScript module dependency is that by default Node.js will try to load the required file as a JavaScript module if the code runs on the server. To prevent Node.js from trying to load a Less file or other non-JavaScript files as JavaScript modules you can add the following code to your main script:

```javascript
require('lasso/node-require-no-op').enable('.less', '.css');
```


For simple paths, the dependency type is inferred from the filename extension. Alternatively, the dependency type can be made explicit using either one of the following formats:

```json
[
Expand Down
28 changes: 28 additions & 0 deletions node-require-no-op.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function requireNoOp(module, filename) { /* no-op */ }

function enableForExtension(extension) {
if (extension == null) {
return;
}

if (Array.isArray(extension)) {
extension.forEach(enableForExtension);
return;
}

if (typeof extension !== 'string') {
throw new Error('Expected extension to be a string. Actual: ' + extension);
}

if (extension.charAt(0) !== '.') {
extension = '.' + extension;
}

require.extensions[extension] = requireNoOp;
}

exports.enable = function(extensions) {
for (var i=0; i<arguments.length; i++) {
enableForExtension(arguments[i]);
}
};
Empty file added test/fixtures/noop/test.xbar
Empty file.
Empty file added test/fixtures/noop/test.xfoo
Empty file.
Empty file added test/fixtures/noop/test.ybar
Empty file.
Empty file added test/fixtures/noop/test.yfoo
Empty file.
19 changes: 19 additions & 0 deletions test/require-no-op-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
var chai = require('chai');
chai.Assertion.includeStack = true;
var expect = chai.expect;

describe('node-require-no-op', function() {

it('should allow for optimizing a page with flags', function() {
require('../node-require-no-op').enable('.xfoo');
require('../node-require-no-op').enable('xbar');
require('../node-require-no-op').enable(['yfoo', '.ybar']);

var test = require('./fixtures/noop/test.xfoo');
expect(Object.keys(test).length).to.equal(0);
require('./fixtures/noop/test.xbar');
require('./fixtures/noop/test.yfoo');
require('./fixtures/noop/test.yfoo');
});
});

0 comments on commit ff3ac6c

Please sign in to comment.