Skip to content

Commit

Permalink
New: Add support for legacy modules and dynamic config
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jan 3, 2019
1 parent da4fd86 commit e944fe7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ language: node_js
node_js:
- "0.10"
- "0.11"
env:
- COFFEE=coffee-script@~1.3
- COFFEE=coffee-script@^1.7
matrix:
fast_finish: true
allow_failures:
- node_js: 0.11
before_script: npm install $COFFEE # install a specific version of coffee-script
42 changes: 34 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ const interpret = require('interpret');

const EXTRE = /^[.]?[^.]+([.].*)$/;

function req (moduleName, cwd) {
return require(resolve.sync(moduleName, {basedir: cwd}));
}

function handleLegacy (moduleName, legacyModuleName, cwd) {
try {
return req(moduleName, cwd);
} catch (err) {
try {
return req(legacyModuleName, cwd);
} catch (__) {
// nice error messages
err.message = err.message.replace(moduleName, moduleName + '\' or \'' + legacyModuleName);
throw err;
}
}
}

exports.registerFor = function (filepath, cwd) {
var match = EXTRE.exec(path.basename(filepath));
if (!match) {
Expand All @@ -13,18 +31,26 @@ exports.registerFor = function (filepath, cwd) {
if (Object.keys(require.extensions).indexOf(ext) !== -1) {
return;
}
var moduleName = interpret.extensions[ext];
if (!moduleName) {
return;
}
if (!cwd) {
cwd = path.dirname(path.resolve(filepath));
}
var moduleName = interpret.extensions[ext];
if (moduleName) {
var compiler = require(resolve.sync(moduleName, {basedir: cwd}));
var register = interpret.register[moduleName];
if (register) {
register(compiler);
}
var legacyModuleName = interpret.legacy[ext];
var config = interpret.configurations[moduleName];
var compiler;
if (legacyModuleName) {
compiler = handleLegacy(moduleName, legacyModuleName, cwd);
} else {
compiler = req(moduleName, cwd);
}
}
var register = interpret.register[moduleName];
if (register) {
register(compiler, config);
}
};

exports.load = function (filepath) {
exports.registerFor(filepath);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"dependencies": {
"resolve": "^0.6.1",
"interpret": "^0.3.0"
"interpret": "^0.4.0"
},
"devDependencies": {
"LiveScript": "^1.2.0",
Expand Down

0 comments on commit e944fe7

Please sign in to comment.