Skip to content

Commit 8d875e6

Browse files
committed
init commit
0 parents  commit 8d875e6

File tree

10 files changed

+218
-0
lines changed

10 files changed

+218
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[{package.json,*.yml}]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.nyc_output
3+
coverage

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
3+
node_js:
4+
- 'stable'
5+
- '0.12'
6+
- '0.10'
7+
8+
after_script:
9+
- 'cat ./coverage/lcov.info | ./node_modules/.bin/coveralls'

fixture.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error('should never be loaded');

index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
module.exports = install;
3+
4+
function install(precompile, ext, extensions) {
5+
ext = ext || '.js';
6+
extensions = extensions || require.extensions;
7+
8+
var oldExtension = extensions[ext];
9+
10+
extensions[ext] = function (module, filename) {
11+
var source = precompile(filename);
12+
if (source) {
13+
module._compile(source, filename);
14+
return;
15+
}
16+
oldExtension(module, filename);
17+
};
18+
}

license

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) James Talmage <[email protected]> (github.com/jamestalmage)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "require-precompiled",
3+
"version": "0.0.0",
4+
"description": "Require extension that allows for caching/precompiling",
5+
"license": "MIT",
6+
"repository": "jamestalmage/require-precompiled",
7+
"author": {
8+
"name": "James Talmage",
9+
"email": "[email protected]",
10+
"url": "github.com/jamestalmage"
11+
},
12+
"engines": {
13+
"node": ">=0.10.0"
14+
},
15+
"scripts": {
16+
"test": "xo && nyc --reporter=lcov --reporter=text ava"
17+
},
18+
"files": [
19+
"index.js"
20+
],
21+
"keywords": [
22+
"require",
23+
"extension",
24+
"cache",
25+
"precompile"
26+
],
27+
"dependencies": {},
28+
"devDependencies": {
29+
"ava": "^0.8.0",
30+
"coveralls": "^2.11.6",
31+
"fake-module-system": "^0.3.0",
32+
"nyc": "^5.0.0",
33+
"xo": "^0.12.0"
34+
},
35+
"xo": {
36+
"ignores": [
37+
"test.js"
38+
]
39+
}
40+
}

readme.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# require-precompiled [![Build Status](https://travis-ci.org/jamestalmage/require-precompiled.svg?branch=master)](https://travis-ci.org/jamestalmage/require-precompiled)
2+
3+
> Require extension that allows for caching/precompiling
4+
5+
6+
## Install
7+
8+
```
9+
$ npm install --save require-precompiled
10+
```
11+
12+
13+
## Usage
14+
15+
```js
16+
const installPrecompiler = require('require-precompiled');
17+
const cache = require('my-cache-implementation');
18+
19+
installPrecompiler(filename => {
20+
if (cache.hasEntryFor(filename)) {
21+
return cache.getPrecompiledCode(filename);
22+
}
23+
// fall through to underlying extension chain;
24+
return null;
25+
});
26+
27+
// Any module required from this point on will be checked against the cache.
28+
const foo = require('some-module');
29+
```
30+
31+
32+
## API
33+
34+
### requirePrecompiled(callback)
35+
36+
#### callback
37+
38+
Type: `Function(string: filename)`
39+
40+
Return `string` contents for a cache hit, or `null` for a miss.
41+
42+
43+
## License
44+
45+
MIT © [James Talmage](http://github.com/jamestalmage)

test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import test from 'ava';
2+
import install from './';
3+
import System from 'fake-module-system';
4+
import path from 'path';
5+
6+
test('module is compiled with source returned from precompiler', t => {
7+
const system = new System({
8+
'/foo.js': 'normal foo'
9+
});
10+
11+
install(filename => {
12+
t.is(filename, '/foo.js');
13+
return 'precompiled foo';
14+
}, '.js', system.extensions);
15+
16+
const module = system.load('/foo.js');
17+
18+
t.is(module.code, 'precompiled foo');
19+
t.is(module.file, '/foo.js');
20+
});
21+
22+
test('passes through to underlying extension precompiler returns undefined', t => {
23+
const system = new System({
24+
'/foo.js': 'normal foo'
25+
});
26+
27+
install(filename => {
28+
t.is(filename, '/foo.js');
29+
return;
30+
}, '.js', system.extensions);
31+
32+
const module = system.load('/foo.js');
33+
34+
t.is(module.code, 'normal foo');
35+
t.is(module.file, '/foo.js');
36+
});
37+
38+
test('allows extensions beyond ".js"', t => {
39+
const system = new System({
40+
'/foo.coffee': 'coffee foo'
41+
});
42+
43+
install(filename => {
44+
t.is(filename, '/foo.coffee');
45+
return 'precompiled coffee';
46+
}, '.coffee', system.extensions);
47+
48+
const module = system.load('/foo.coffee');
49+
50+
t.is(module.code, 'precompiled coffee');
51+
t.is(module.file, '/foo.coffee');
52+
});
53+
54+
test('test actual require', t => {
55+
var fixtureFile = path.join(__dirname, 'fixture.js');
56+
57+
install(filename => {
58+
if (filename === fixtureFile) {
59+
return 'module.exports = "foobar"';
60+
}
61+
return null;
62+
});
63+
64+
t.is(require('./fixture'), 'foobar');
65+
});

0 commit comments

Comments
 (0)