chore(rollup): Refactor native shims plugin

master
Lucas Duailibe 2018-06-13 12:49:35 -03:00
parent 889b7dab79
commit f0907a049d
3 changed files with 24 additions and 16 deletions

View File

@ -3,28 +3,32 @@
const builtins = require("builtin-modules"); const builtins = require("builtin-modules");
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const tempy = require("tempy");
const EMPTY = "export default {};";
const PREFIX = "\0shim:";
module.exports = function(dir) { module.exports = function(dir) {
return { return {
resolveId(importee, importer) { resolveId(importee) {
if (!importee || !importer || /\0/.test(importee)) { if (importee.startsWith(PREFIX)) {
return importee;
}
if (/\0/.test(importee) || !builtins.includes(importee)) {
return null; return null;
} }
if (!~builtins.indexOf(importee)) { const shim = path.resolve(dir, importee + ".js");
return null; if (fs.existsSync(shim)) {
return shim;
} }
return PREFIX + importee;
},
const newPath = path.resolve(dir, `${importee}.js`); load(id) {
if (fs.existsSync(newPath)) { if (id.startsWith(PREFIX)) {
return newPath; return EMPTY;
} }
// This helps debugging when a stub in the module is needed
const fallback = tempy.file({ name: `${importee}.js` });
fs.writeFileSync(fallback, "module.exports = {};");
return fallback;
} }
}; };
}; };

View File

@ -1,2 +1,4 @@
export function ok() {} export default {
export function strictEqual() {} ok() {},
strictEqual() {}
};

View File

@ -1 +1,3 @@
export const EOL = "\n"; export default {
EOL: "\n"
};