Permalink
Browse files

CLJS-2318: module-deps.js doesn't respect the package.json `module` f…

…ield
  • Loading branch information...
anmonteiro authored and swannodette committed Aug 13, 2017
1 parent 5ee5f3f commit 1163381724c0e8ab11e998e9ab02d7a752848acc
View
@@ -1,3 +1,5 @@
image: Visual Studio 2017
environment:
nodejs_version: "6"
@@ -1,22 +1,35 @@
var fs = require('fs');
var path = require('path');
var mdeps = require('@cljs-oss/module-deps');
var nodeResolve = require('resolve');
var browserResolve = require('browser-resolve');
var konan = require('konan');
var enhancedResolve = require('enhanced-resolve');
var target = 'CLJS_TARGET';
var filename = path.resolve(__dirname, 'JS_FILE');
var resolver = target === 'nodejs' ? nodeResolve : browserResolve;
var filename = fs.realpathSync(path.resolve(__dirname, 'JS_FILE'));
var mainFields =
target === 'nodejs' ? ['module', 'main'] : ['module', 'browser', 'main'];
var resolver = enhancedResolve.create({
fileSystem: new enhancedResolve.CachedInputFileSystem(
new enhancedResolve.NodeJsInputFileSystem(),
4000
),
extensions: ['.js', '.json'],
mainFields: mainFields,
moduleExtensions: ['.js', '.json'],
});
var md = mdeps({
resolve: function(id, parentOpts, cb) {
// set the basedir properly so we don't try to resolve requires in the Closure
// Compiler processed `node_modules` folder.
parentOpts.basedir =
parentOpts.filename === filename ? __dirname : path.dirname(parentOpts.filename);
parentOpts.extensions = ['.js', '.json'];
parentOpts.filename === filename
? path.resolve(__dirname)
: path.dirname(parentOpts.filename);
resolver(id, parentOpts, cb);
resolver(parentOpts.basedir, id, cb);
},
filter: function(id) {
return !(target === 'nodejs' && nodeResolve.isCore(id));
@@ -28,6 +41,17 @@ var md = mdeps({
},
});
function getPackageJsonMainEntry(pkgJson) {
for (var i = 0; i < mainFields.length; i++) {
var entry = mainFields[i];
if (pkgJson[entry] != null) {
return pkgJson[entry];
}
}
return null;
}
var pkgJsons = [];
var deps_files = {};
@@ -42,8 +66,9 @@ md.on('package', function(pkg) {
pkgJson.provides = [pkg.name];
}
if (pkg.main != null) {
pkgJson.main = path.join(pkg.__dirname, pkg.main);
var pkgJsonMainEntry = getPackageJsonMainEntry(pkg);
if (pkgJsonMainEntry != null) {
pkgJson.mainEntry = path.join(pkg.__dirname, pkgJsonMainEntry);
}
pkgJsons.push(pkgJson);
@@ -58,8 +83,8 @@ md.on('end', function() {
for (var i = 0; i < pkgJsons.length; i++) {
var pkgJson = pkgJsons[i];
if (deps_files[pkgJson.main] != null && pkgJson.provides != null) {
deps_files[pkgJson.main].provides = pkgJson.provides;
if (deps_files[pkgJson.mainEntry] != null && pkgJson.provides != null) {
deps_files[pkgJson.mainEntry].provides = pkgJson.provides;
}
deps_files[pkgJson.file] = { file: pkgJson.file };
@@ -275,8 +275,8 @@
(defn node-inputs
"EXPERIMENTAL: return the foreign libs entries as computed by running
the module-deps package on the supplied JavaScript entry points. Assumes
that the `@cljs-oss/module-deps` and `konan` NPM packages are either
locally or globally installed."
that the `@cljs-oss/module-deps`, `enhanced-resolve` and `konan` NPM packages
are either locally or globally installed."
[entries]
(closure/node-inputs entries))
@@ -2117,7 +2117,7 @@
(spit pkg-json "{}"))
(let [proc (-> (ProcessBuilder.
(into (cond->> ["npm" "install" "@cljs-oss/module-deps"
"resolve" "browser-resolve" "konan"]
"enhanced-resolve" "resolve" "konan"]
util/windows? (into ["cmd" "/c"]))
(map (fn [[dep version]] (str (name dep) "@" version)))
npm-deps))
@@ -2141,8 +2141,8 @@
(defn node-module-deps
"EXPERIMENTAL: return the foreign libs entries as computed by running
the module-deps package on the supplied JavaScript entry point. Assumes
that the `@cljs-oss/module-deps` and `konan` NPM packages are either
locally or globally installed."
that the `@cljs-oss/module-deps`, `enhanced-resolve`, `enhanced-resolve` and
`konan` NPM packages are either locally or globally installed."
([entry]
(node-module-deps entry
(when env/*compiler*
@@ -2181,8 +2181,8 @@
(defn node-inputs
"EXPERIMENTAL: return the foreign libs entries as computed by running
the module-deps package on the supplied JavaScript entry points. Assumes
that the `@cljs-oss/module-deps` and `konan` NPM packages are either
locally or globally installed."
that the `@cljs-oss/module-deps`, `enhanced-resolve` and `konan` NPM packages
are either locally or globally installed."
([entries]
(node-inputs entries
(when env/*compiler*
@@ -405,8 +405,7 @@
(spit (io/file "package.json") (json/json-str {:dependencies {:left-pad "1.1.3"}
:devDependencies {"@cljs-oss/module-deps" "*"
:konan "*"
:resolve "*"
:browser-resolve "*"}}))
:enhanced-resolve "*"}}))
(apply sh/sh (cond->> ["npm" "install"]
util/windows? (into ["cmd" "/c"])))
(let [ws (atom [])
@@ -247,7 +247,7 @@
(spit (io/file "package.json") (json/json-str {:devDependencies {"@cljs-oss/module-deps" "*"
:konan "*"
:resolve "*"
:browser-resolve "*"}}))
:enhanced-resolve "*"}}))
(apply sh/sh (cond->> ["npm" "install"]
util/windows? (into ["cmd" "/c"])))
(let [file (io/file (test/tmp-dir) "cljs-2315-inputs.js")
@@ -260,3 +260,23 @@
:module-type :es6}])))
(.delete (io/file "package.json"))
(test/delete-node-modules))
(deftest test-cljs-2318
(spit (io/file "package.json") "{}")
(let [opts {:npm-deps {:react "15.6.1"
:react-dom "15.6.1"
:react-addons-css-transition-group "15.5.1"
"@blueprintjs/core" "1.24.0"}}
out (util/output-directory opts)]
(test/delete-node-modules)
(test/delete-out-files out)
(closure/maybe-install-node-deps! opts)
(is (true? (some (fn [module]
(= module {:module-type :es6
:file (.getAbsolutePath (io/file "node_modules/tslib/tslib.es6.js"))
:provides ["tslib"
"tslib/tslib.es6.js"
"tslib/tslib.es6"]}))
(closure/index-node-modules ["tslib"] opts))))
(test/delete-node-modules)
(test/delete-out-files out)))

0 comments on commit 1163381

Please sign in to comment.