Skip to content

Commit

Permalink
Fallback to detective if konan fails
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Mar 7, 2021
1 parent c80a7a2 commit f78550a
Show file tree
Hide file tree
Showing 9 changed files with 3,817 additions and 53 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
@@ -0,0 +1,10 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
109 changes: 58 additions & 51 deletions index.js
Expand Up @@ -4,6 +4,7 @@ var relativePath = require('cached-path-relative')

var browserResolve = require('browser-resolve');
var nodeResolve = require('resolve');
var detective = require('detective');
var konan = require('konan');
var through = require('through2');
var concat = require('concat-stream');
Expand Down Expand Up @@ -82,9 +83,9 @@ function Deps (opts) {
var self = this;
if (!(this instanceof Deps)) return new Deps(opts);
Transform.call(this, { objectMode: true });

if (!opts) opts = {};

this.basedir = opts.basedir || process.cwd();
this.cache = opts.cache;
this.fileCache = opts.fileCache;
Expand All @@ -96,7 +97,7 @@ function Deps (opts) {
this.walking = {};
this.entries = [];
this._input = [];

this.paths = opts.paths || process.env.NODE_PATH || '';
if (typeof this.paths === 'string') {
var delimiter = path.delimiter || (process.platform === 'win32' ? ';' : ':');
Expand All @@ -107,7 +108,7 @@ function Deps (opts) {
.map(function (p) {
return path.resolve(self.basedir, p);
});

this.transforms = [].concat(opts.transform).filter(Boolean);
this.globalTransforms = [].concat(opts.globalTransform).filter(Boolean);
this.resolver = opts.resolve || browserResolve;
Expand All @@ -119,7 +120,7 @@ function Deps (opts) {
if (!this.options.expose) this.options.expose = {};
this.pending = 0;
this.inputPending = 0;

var topfile = path.join(this.basedir, '__fake.js');
this.top = {
id: topfile,
Expand Down Expand Up @@ -154,14 +155,14 @@ Deps.prototype._transform = function (row, enc, next) {
this.transforms.push([ row.transform, row.options ]);
return next();
}

self.pending ++;
var basedir = defined(row.basedir, self.basedir);

if (row.entry !== false) {
self.entries.push(path.resolve(basedir, row.file || row.id));
}

self.lookupPackage(row.file, function (err, pkg) {
if (err && self.options.ignoreMissing) {
self.emit('missing', row.file, self.top);
Expand Down Expand Up @@ -190,7 +191,7 @@ Deps.prototype._flush = function () {
}
else files[w.file || w.id] = r;
});

Object.keys(files).forEach(function (key) {
var r = files[key];
var pkg = r.pkg || {};
Expand All @@ -207,7 +208,7 @@ Deps.prototype._flush = function () {
Deps.prototype.resolve = function (id, parent, cb) {
var self = this;
var opts = self.options;

if (xhas(self.cache, parent.id, 'deps', id)
&& self.cache[parent.id].deps[id]) {
var file = self.cache[parent.id].deps[id];
Expand All @@ -217,25 +218,25 @@ Deps.prototype.resolve = function (id, parent, cb) {
cb(null, file, pkg);
});
}

parent.packageFilter = function (p, x) {
var pkgdir = dirname(x);
if (opts.packageFilter) p = opts.packageFilter(p, x);
p.__dirname = pkgdir;

return p;
};

if (opts.extensions) parent.extensions = opts.extensions;
if (opts.modules) parent.modules = opts.modules;

self.resolver(id, parent, function onresolve (err, file, pkg, fakePath) {
if (err) return cb(err);
if (!file) return cb(new Error(
'module not found: "' + id + '" from file '
+ parent.filename
));

if (!pkg || !pkg.__dirname) {
self.lookupPackage(file, function (err, p) {
if (err) return cb(err);
Expand Down Expand Up @@ -271,25 +272,25 @@ Deps.prototype.readFile = function (file, id, pkg) {
Deps.prototype.getTransforms = function (file, pkg, opts) {
if (!opts) opts = {};
var self = this;

var isTopLevel;
if (opts.builtin || opts.inNodeModules) isTopLevel = false;
else isTopLevel = this._isTopLevel(file);

var transforms = [].concat(isTopLevel ? this.transforms : [])
.concat(getTransforms(pkg, {
globalTransform: this.globalTransforms,
transformKey: this.options.transformKey
}))
;
if (transforms.length === 0) return through();

var pending = transforms.length;
var streams = [];
var input = through();
var output = through();
var dup = duplexer(input, output);

for (var i = 0; i < transforms.length; i++) (function (i) {
makeTransform(transforms[i], function (err, trs) {
if (err) return self.emit('error', err)
Expand All @@ -298,7 +299,7 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
});
})(i);
return dup;

function done () {
var middle = combine.apply(null, streams);
middle.on('error', function (err) {
Expand All @@ -308,7 +309,7 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
});
input.pipe(middle).pipe(output);
}

function makeTransform (tr, cb) {
var trOpts = {};
if (Array.isArray(tr)) {
Expand All @@ -327,24 +328,24 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
});
}
}

function loadTransform (id, trOpts, cb) {
var params = { basedir: dirname(file) };
nodeResolve(id, params, function nr (err, res, again) {
if (err && again) return cb && cb(err);

if (err) {
params.basedir = pkg.__dirname;
return nodeResolve(id, params, function (e, r) {
nr(e, r, true)
});
}

if (!res) return cb(new Error(
'cannot find transform module ' + tr
+ ' while transforming ' + file
));

var r = require(res);
if (typeof r !== 'function') {
return cb(new Error(
Expand All @@ -353,7 +354,7 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
+ 'Expected a transform function.'
));
}

var trs = r(file, trOpts);
self.emit('transform', trs, file);
cb(null, trs);
Expand All @@ -366,7 +367,7 @@ Deps.prototype.walk = function (id, parent, cb) {
var opts = self.options;
var sortKey = parent.sortKey || '';
this.pending ++;

var rec = {};
var input;
if (typeof id === 'object') {
Expand All @@ -376,7 +377,7 @@ Deps.prototype.walk = function (id, parent, cb) {
input = true;
this.inputPending ++;
}

self.resolve(id, parent, function (err, file, pkg, fakePath) {
// this is checked early because parent.modules is also modified
// by this function.
Expand All @@ -393,15 +394,15 @@ Deps.prototype.walk = function (id, parent, cb) {
self._emittedPkg[pkg.__dirname] = true;
self.emit('package', pkg);
}

if (opts.postFilter && !opts.postFilter(id, file, pkg)) {
if (--self.pending === 0) self.push(null);
if (input) --self.inputPending;
return cb && cb(null, undefined);
}
if (err && rec.source) {
file = rec.file;

var ts = self.getTransforms(file, pkg);
ts.pipe(concat(function (body) {
rec.source = body.toString('utf8');
Expand All @@ -422,7 +423,7 @@ Deps.prototype.walk = function (id, parent, cb) {
return cb && cb(null, file);
}
self.visited[file] = true;

if (rec.source) {
var ts = self.getTransforms(file, pkg);
ts.pipe(concat(function (body) {
Expand All @@ -431,10 +432,10 @@ Deps.prototype.walk = function (id, parent, cb) {
}));
return ts.end(rec.source);
}

var c = self.cache && self.cache[file];
if (c) return fromDeps(file, c.source, c.package, fakePath, Object.keys(c.deps), sortKey);

self.readFile(file, id, pkg)
.pipe(self.getTransforms(fakePath || file, pkg, {
builtin: builtin,
Expand All @@ -450,13 +451,13 @@ Deps.prototype.walk = function (id, parent, cb) {
var deps = rec.noparse ? [] : self.parseDeps(file, src);
if (deps) fromDeps(file, src, pkg, fakePath, deps, sortKey);
}

function fromDeps (file, src, pkg, fakePath, deps, sortKey) {
var p = deps.length;
var resolved = {};

if (input) --self.inputPending;

(function resolve () {
if (self.inputPending > 0) return setTimeout(resolve);
deps.forEach(function (id, i) {
Expand All @@ -481,19 +482,19 @@ Deps.prototype.walk = function (id, parent, cb) {
});
if (deps.length === 0) done();
})();

function done () {
if (!rec.id) rec.id = file;
if (!rec.source) rec.source = src;
if (!rec.deps) rec.deps = resolved;
if (!rec.file) rec.file = file;
rec.sortKey = sortKey + '!' + file;

if (self.entries.indexOf(file) >= 0) {
rec.entry = true;
}
self.push(rec);

if (cb) cb(null, file);
if (-- self.pending === 0) self.push(null);
}
Expand All @@ -503,13 +504,19 @@ Deps.prototype.walk = function (id, parent, cb) {
Deps.prototype.parseDeps = function (file, src, cb) {
if (this.options.noParse === true) return [];
if (/\.json$/.test(file)) return [];

if (Array.isArray(this.options.noParse)
&& this.options.noParse.indexOf(file) >= 0) {
return [];
}

try { var deps = konan(src).strings }

try {
try { var deps = konan(src).strings }
catch (ex) {
// konan does not support Vue (component) file, try to parse using detective (as a fallback)
deps = detective(src)
}
}
catch (ex) {
var message = ex && ex.message ? ex.message : ex;
this.emit('error', new Error(
Expand All @@ -522,14 +529,14 @@ Deps.prototype.parseDeps = function (file, src, cb) {

Deps.prototype.lookupPackage = function (file, cb) {
var self = this;

var cached = this.pkgCache[file];
if (cached) return nextTick(cb, null, cached);
if (cached === false) return nextTick(cb, null, undefined);


var dirs = parents(path.dirname(file || ''));

(function next () {
if (dirs.length === 0) {
self.pkgCache[file] = false;
Expand All @@ -539,17 +546,17 @@ Deps.prototype.lookupPackage = function (file, cb) {
if (dir.split(/[\\\/]/).slice(-1)[0] === 'node_modules') {
return cb(null, undefined);
}

var pkgfile = path.join(dir, 'package.json');

var cached = self.pkgCache[pkgfile];
if (cached) return nextTick(cb, null, cached);
else if (cached === false) return next();

var pcached = self.pkgFileCachePending[pkgfile];
if (pcached) return pcached.push(onpkg);
pcached = self.pkgFileCachePending[pkgfile] = [];

fs.readFile(pkgfile, function (err, src) {
if (err) return onpkg();
try { var pkg = JSON.parse(src) }
Expand All @@ -559,12 +566,12 @@ Deps.prototype.lookupPackage = function (file, cb) {
].join('')))
}
pkg.__dirname = dir;

self.pkgCache[pkgfile] = pkg;
self.pkgCache[file] = pkg;
onpkg(null, pkg);
});

function onpkg (err, pkg) {
if (self.pkgFileCachePending[pkgfile]) {
var fns = self.pkgFileCachePending[pkgfile];
Expand All @@ -580,7 +587,7 @@ Deps.prototype.lookupPackage = function (file, cb) {
}
})();
};

function getTransforms (pkg, opts) {
var trx = [];
if (opts.transformKey) {
Expand Down

0 comments on commit f78550a

Please sign in to comment.