UNPKG

st-bundle

Version:

CLI for watching and bundling SpringType projects.

149 lines (148 loc) 4.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fastAnalysis_1 = require("../analysis/fastAnalysis"); const pluginUtils_1 = require("../plugins/pluginUtils"); const utils_1 = require("../utils/utils"); const EXECUTABLE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.mjs']; class Module { constructor(props, pkg) { this.props = props; this.pkg = pkg; this.assembled = false; this.header = []; this.moduleDependencies = []; this.externalDependencies = []; } isEntry() { return this.pkg.entry === this; } isTypescriptModule() { return ['.ts', '.tsx'].includes(this.props.extension); } isJavascriptModule() { if (this._isJavascriptModule !== undefined) return this._isJavascriptModule; this._isJavascriptModule = ['.js', '.jsx', '.mjs'].indexOf(this.props.extension) > -1; return this._isJavascriptModule; } fastAnalyse() { this.fastAnalysis = fastAnalysis_1.fastAnalysis({ packageName: this.pkg.props.meta.name, input: this.contents, locations: this.isSourceMapRequired(), parseUsingAst: this.props.extension === '.js', }); return this.fastAnalysis; } setMeta(key, value) { this.meta = this.meta || {}; this.meta[key] = value; } getMeta(key) { if (this.meta && this.meta[key]) { return this.meta[key]; } } addWeakReference(url) { if (url === this.props.absPath) return; if (!this.weakReferences) this.weakReferences = []; if (!this.weakReferences.includes(url)) { this.weakReferences.push(url); this.props.ctx.weakReferences.add(url, this.props.absPath); } } testPath(path) { return pluginUtils_1.testPath(this.props.absPath, path); } setCache(basics) { this.isCached = true; this.cache = basics; } getSourceMapPath() { const config = this.props.ctx.config; if (this.props.ctx.config.isServer()) { return this.props.absPath; } if (this.pkg.isDefaultPackage) { return utils_1.joinFuseBoxPath(config.sourceMap.sourceRoot, utils_1.extractFuseBoxPath(this.props.ctx.config.homeDir, this.props.absPath)); } else { return utils_1.joinFuseBoxPath(config.defaultSourceMapModulesRoot, this.pkg.getPublicName(), utils_1.extractFuseBoxPath(this.pkg.props.meta.packageRoot, this.props.absPath)); } } read(forceReload) { if (this.contents === undefined || forceReload) { this.contents = utils_1.readFile(this.props.absPath); } return this.contents; } isExecutable() { if (this._isExecutable === undefined) { this._isExecutable = EXECUTABLE_EXTENSIONS.includes(this.props.extension); } return this._isExecutable; } isStylesheet() { if (this._isStylesheet === undefined) { this._isStylesheet = ['.css', '.scss', '.sass', '.less', '.styl'].indexOf(this.props.extension) > -1; } return this._isStylesheet; } notStylesheet() { this._isStylesheet = false; } getShortPath() { return `${this.pkg.getPublicName()}/${this.props.fuseBoxPath}`; } getHasedPath() { return utils_1.fastHash(`${this.pkg.getPublicName()}/${this.props.fuseBoxPath}`); } isCSSSourceMapRequired() { let requireSourceMap = true; const config = this.props.ctx.config; if (config.sourceMap.css === false) { requireSourceMap = false; } if (!this.pkg.isDefaultPackage && !config.sourceMap.vendor) { requireSourceMap = false; } return requireSourceMap; } isSourceMapRequired() { let requireSourceMaps = true; const config = this.props.ctx.config; if (this.pkg.isDefaultPackage) { if (!config.sourceMap.project) { requireSourceMaps = false; } } else { if (!config.sourceMap.vendor) { requireSourceMaps = false; } } return requireSourceMaps; } generate() { if (this.header) { const concat = utils_1.createConcat(true, '', '\n'); this.header.forEach(h => { concat.add(null, h); }); concat.add(null, this.contents, this.sourceMap); this.contents = concat.content.toString(); this.sourceMap = concat.sourceMap; } return { contents: this.contents, sourceMap: this.sourceMap, }; } } exports.Module = Module; function createModule(props, pkg) { return new Module(props, pkg); } exports.createModule = createModule;