UNPKG

imod

Version:

A bundler for tiny modules, powered by Rollup.

155 lines 5.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs"); const path = require("path"); const rollup_1 = require("rollup"); const glob = require("glob"); const ora = require("ora"); const colors_1 = require("colors"); const rollup_config_1 = require("./rollup.config"); const getUserOptions_1 = require("./getUserOptions"); const utils_1 = require("./utils"); const standard = require("standard"); class IMod { constructor(config) { this.silent = false; this.iswatching = false; this.cwd = path.resolve(config.cwd); this.config = Object.assign(Object.assign(Object.assign({}, getUserOptions_1.default(this.cwd)), config), { cwd: this.cwd }); this.pkgJSON = require(`${this.cwd}/package.json`); this.console = console; } build() { this.iswatching = false; this._run(); } dev() { this.iswatching = true; this._run(); } async _run() { const start = Date.now(); process.chdir(this.cwd); this._clean(); if (!this.config.verbose) { try { global.console = {}; Object.keys(this.console).forEach(type => { try { global.console[type] = (...args) => { if (this.silent) return; this.console[type].apply(this.console, args); }; } catch (_a) { // TypeError: Cannot set property console of #<Object> which has only a getter } }); } catch (_a) { // TypeError: Cannot set property console of #<Object> which has only a getter } } let inputFiles = this.config.input; if (!Array.isArray(inputFiles)) inputFiles = [inputFiles]; inputFiles = inputFiles.filter(input => input && typeof input === 'string'); if (inputFiles.length) { inputFiles = inputFiles.map(input => path.resolve(this.cwd, input)); } else { inputFiles = glob.sync(path.resolve(this.cwd, `src/index*{${IMod.EXTENSIONS.join(',')}}`)); } let task = []; for (let inputFile of inputFiles) { const fileName = path.basename(inputFile).replace(path.extname(inputFile), ''); for (let { extName, format, target } of this.config.compilerOptions) { const outputFile = path.resolve(this.cwd, this.config.outDir, `${fileName}${extName}`); task.push(this._rollup({ inputFile, outputFile, format, target })); } } await utils_1.parallelize(task); this._clean(); console.log(colors_1.green(`total: ${Date.now() - start}ms`)); } async _rollup({ inputFile, outputFile, format, target }) { this.silent = true; const MSG = `compile ${this._relative(inputFile)} -> ${this._relative(outputFile)}`.padEnd(50, ' '); const spinner = ora(MSG); try { const { inputOptions, outputOptions } = rollup_config_1.default({ input: inputFile, output: outputFile, format, target, banner: this.config.banner, name: this.config.name || '', outDir: this.config.outDir, declarationDir: this.config.declarationDir }); if (!this.iswatching) { spinner.start(); const start = Date.now(); const bundle = await rollup_1.rollup(inputOptions); await bundle.write(outputOptions); await this._format(outputFile, format); spinner.succeed(`${MSG} ${colors_1.green(`${Date.now() - start}ms`)}`); this.silent = false; return; } const watchOptions = Object.assign(Object.assign({}, inputOptions), { output: outputOptions, watch: IMod.WATCH_OPTS }); const watcher = rollup_1.watch(watchOptions); let start; watcher.on('event', (e) => { switch (e.code) { case 'START': start = Date.now(); spinner.start(MSG); break; case 'BUNDLE_END': spinner.succeed(`${MSG} ${colors_1.green(`${Date.now() - start}ms`)}`); break; case 'ERROR': this.console.error(e.error.stack || e.error); spinner.fail(colors_1.red(`${MSG} ${Date.now() - start}ms`)); } }); } catch (e) { this.console.error(e.stack); spinner.fail(); // rimraf.sync(outputFile) } finally { if (!this.iswatching) { this.silent = false; } } } async _format(outputFile, format) { if (format !== 'esm') return; try { let code = fs.readFileSync(outputFile, 'utf8'); const standardLint = standard.lintTextSync(code, { fix: true }); code = standardLint.results[0].output; fs.writeFileSync(outputFile, code, 'utf8'); } catch ( /* noop */_a) { /* noop */ } } _relative(file) { return path.relative(this.cwd, file); } _clean() { utils_1.rimraf.sync(IMod.CACHE_PATH); } } exports.default = IMod; IMod.CACHE_PATH = './node_modules/.cache/imod'; IMod.EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.es6', '.es', '.mjs']; IMod.WATCH_OPTS = { clearScreen: true, exclude: 'node_modules/**' }; //# sourceMappingURL=index.js.map