Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLJS-1960: Require CommonJS modules directly from a ClojureScript nam…
…espace This patch addresses the first part of the solution outlined in the following design doc: https://github.com/clojure/clojurescript/wiki/Enhanced-Node.js-Modules-Support It makes possible to specify, install and require Node.js dependencies directly from ClojureScript namespaces. Future work can make it possible to support specifying these dependencies in `deps.cljs` files and handling conflict resolution between upstream foreign dependencies and foreign dependencies specified directly in the compiler options.
- Loading branch information
1 parent
1d38f73
commit 777d41b
Showing
4 changed files
with
201 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,71 @@ | ||
var path = require('path'); | ||
var mdeps = require('module-deps'); | ||
var nodeResolve = require('resolve'); | ||
var browserResolve = require('browser-resolve'); | ||
|
||
var md = mdeps({}); | ||
var deps_files = []; | ||
var target = 'CLJS_TARGET'; | ||
var filename = path.resolve(__dirname, 'JS_FILE'); | ||
var resolver = target === 'nodejs' ? nodeResolve : browserResolve; | ||
|
||
var md = mdeps({ | ||
resolve: function(id, parent, cb) { | ||
// set the basedir properly so we don't try to resolve requires in the Closure | ||
// Compiler processed `node_modules` folder. | ||
parent.basedir = parent.filename === filename ? __dirname: path.dirname(parent.filename); | ||
|
||
resolver(id, parent, cb); | ||
}, | ||
filter: function(id) { | ||
return !nodeResolve.isCore(id); | ||
}}); | ||
|
||
var pkgJsons = []; | ||
var deps_files = {}; | ||
|
||
md.on('package', function (pkg) { | ||
// we don't want to include the package.json for users' projects | ||
if (/node_modules/.test(pkg.__dirname)) { | ||
deps_files.push({file: path.join(pkg.__dirname, 'package.json')}); | ||
var pkgJson = { | ||
file: path.join(pkg.__dirname, 'package.json'), | ||
}; | ||
|
||
if (pkg.name != null) { | ||
pkgJson.provided = [ pkg.name ]; | ||
} | ||
|
||
if (pkg.main != null) { | ||
pkgJson.main = path.join(pkg.__dirname, pkg.main); | ||
} | ||
|
||
pkgJsons.push(pkgJson); | ||
} | ||
}); | ||
|
||
md.on('file', function(file) { | ||
deps_files.push({file: file}); | ||
deps_files[file] = { file: file }; | ||
}); | ||
|
||
md.on('end', function() { | ||
process.stdout.write(JSON.stringify(deps_files)); | ||
for (var i = 0; i < pkgJsons.length; i++) { | ||
var pkgJson = pkgJsons[i]; | ||
|
||
if (deps_files[pkgJson.main] != null && pkgJson.provided != null) { | ||
deps_files[pkgJson.main].provides = pkgJson.provided; | ||
} | ||
|
||
deps_files[pkgJson.file] = { file: pkgJson.file }; | ||
} | ||
|
||
var values = []; | ||
for (var key in deps_files) { | ||
values.push(deps_files[key]); | ||
} | ||
|
||
process.stdout.write(JSON.stringify(values)); | ||
}); | ||
|
||
md.end({ | ||
file: path.resolve(path.join(__dirname, 'JS_FILE')) | ||
file: filename, | ||
}); | ||
|
||
md.resume(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.