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 fs = require("fs");
const path = require("path");
const tempy = require("tempy");
const EMPTY = "export default {};";
const PREFIX = "\0shim:";
module.exports = function(dir) {
return {
resolveId(importee, importer) {
if (!importee || !importer || /\0/.test(importee)) {
resolveId(importee) {
if (importee.startsWith(PREFIX)) {
return importee;
}
if (/\0/.test(importee) || !builtins.includes(importee)) {
return null;
}
if (!~builtins.indexOf(importee)) {
return null;
const shim = path.resolve(dir, importee + ".js");
if (fs.existsSync(shim)) {
return shim;
}
return PREFIX + importee;
},
const newPath = path.resolve(dir, `${importee}.js`);
if (fs.existsSync(newPath)) {
return newPath;
load(id) {
if (id.startsWith(PREFIX)) {
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 function strictEqual() {}
export default {
ok() {},
strictEqual() {}
};

View File

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