baset-reader-babel
Version:
Babel reader plugin for BaseT project.
47 lines • 2.35 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const babel_core_1 = require("babel-core");
const baset_core_1 = require("baset-core");
const path_1 = __importDefault(require("path"));
class BabelReader extends baset_core_1.AbstractReader {
constructor(options = { config: {} }) {
super(options);
this.options = options;
this.exts = [...babel_core_1.util.canCompile.EXTENSIONS];
this.read = (filePath, spec) => __awaiter(this, void 0, void 0, function* () {
const ext = path_1.default.extname(filePath);
const sources = yield spec;
return (sources instanceof Array)
? sources.map((src, index) => this.compile(src, `${filePath}.${index}${ext}`))
: this.compile(sources, filePath);
});
this.registerHook = (addHook, addFileResolver) => {
addHook(this.compile, {
exts: this.exts,
matcher: filename => (this.options && this.options.includeNodeModules) ||
!filename.includes('node_modules'),
});
};
this.compile = (code, filename) => {
const opts = new babel_core_1.OptionManager().init(Object.assign({ sourceRoot: path_1.default.dirname(filename) }, this.config, { filename }));
if (opts === null)
return code;
const result = babel_core_1.transform(code, opts);
return result.code || '';
};
this.config = options.config;
}
}
exports.default = BabelReader;
//# sourceMappingURL=index.js.map