doowb/unlazy-loader

Currently transforms non-lazy requires.

Closed this issue · 4 comments

With files that have both lazy and non-lazy requires, unlazy-loader transforms them all. One example is micromatch.

var path = require('path');
var fileRe = require('filename-regex');
var utils = require('lazy-cache')(require);

var fn = require;
require = utils;

require('arr-diff', 'diff');

...

require = fn;

Currently, the top lines are transformed into the following wrong code:

utils.path = require('path');
utils.fileRe = require('filename-regex');
var utils = {};

Finding the line number of require = utils; and the line number of require = fn; then only transforming the requires in between would fix this.

var firstLine = source.split('\n').findIndex(function(line) {
  return line == 'require = ' + first.variable + ';';
});

var lastLine = source.split('\n').findIndex(function(line) {
  return line == 'require = fn;';
});
if (lastLine == -1) lastLine = Infinity;

...

results.forEach(function(ele) {
  if (ele.line < firstLine || ele.line > lastLine) return;
  ...
});

this doesn't speak to the bug, but fwiw the latest micromatch no longer uses lazy-cache

doowb commented

@DeadHeadRussell just merged in @jonschlinkert's changes that should fix this (he even added tests around it).

I'll get this published soon.

doowb commented

Published to npm.

Awesome, thanks!