UNPKG

st-bundle

Version:

CLI for watching and bundling SpringType projects.

110 lines (109 loc) 4.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = require("path"); const Bundle_1 = require("../bundle/Bundle"); const env_1 = require("../env"); const utils_1 = require("../utils/utils"); const htmlStrings_1 = require("./htmlStrings"); function replaceWebIndexStrings(str, keys) { return str.replace(/\$([a-z_-]+)/gi, (_var, name) => { return keys[name] !== undefined ? (typeof keys[name] === 'object' ? JSON.stringify(keys[name]) : keys[name]) : ''; }); } exports.replaceWebIndexStrings = replaceWebIndexStrings; function getEssentialWebIndexParams(config, log) { let templatePath = path_1.join(env_1.env.FUSE_MODULES, 'web-index-default-template/template.html'); let publicPath = '/'; let distFileName = 'index.html'; let templateContent = utils_1.readFile(templatePath); if (typeof config === 'object') { if (config.template) { templatePath = utils_1.ensureAbsolutePath(config.template, env_1.env.SCRIPT_PATH); } if (config.publicPath) { publicPath = config.publicPath; } if (config.distFileName) { distFileName = config.distFileName; } } if (utils_1.fileExists(templatePath)) { templateContent = utils_1.readFile(templatePath); } else { log.warn('No webIndex template found, using default HTML template instead.'); } return { distFileName, publicPath, templateContent, templatePath, }; } exports.getEssentialWebIndexParams = getEssentialWebIndexParams; function createWebIndex(ctx) { const config = ctx.config.webIndex; const isDisabled = config.enabled === false; const logger = ctx.log; if (isDisabled) { return { isDisabled }; } const opts = getEssentialWebIndexParams(config, ctx.log); return { resolve: (userPath) => { return utils_1.joinFuseBoxPath(opts.publicPath, userPath); }, generate: async (bundles) => { const scriptTags = []; const cssTags = []; let ftlEnabled = false; if (ctx.cache && ctx.config.cache.FTL && ctx.devServer && !ctx.config.production) { ftlEnabled = true; } const sorted = bundles.sort((a, b) => a.bundle.props.priority - b.bundle.props.priority); sorted.forEach(item => { if (item.bundle.props.webIndexed) { if (ftlEnabled && item.bundle.props.type == Bundle_1.BundleType.PROJECT_ENTRY) { scriptTags.push(htmlStrings_1.htmlStrings.scriptTag('/__ftl')); } if (item.bundle.props.type !== Bundle_1.BundleType.CSS) { if (config.embedIndexedBundles && ctx.config.production) { scriptTags.push(htmlStrings_1.htmlStrings.embedScriptTag(item.bundle.contents.content.toString())); } else { scriptTags.push(htmlStrings_1.htmlStrings.scriptTag(utils_1.joinFuseBoxPath(opts.publicPath, item.stat.relBrowserPath))); } } else { if (config.embedIndexedBundles && ctx.config.production) { cssTags.push(htmlStrings_1.htmlStrings.cssTagScript(item.bundle.contents.content.toString())); } else { cssTags.push(htmlStrings_1.htmlStrings.cssTag(utils_1.joinFuseBoxPath(opts.publicPath, item.stat.relBrowserPath))); } } } }); const scriptOpts = { bundles: scriptTags.join('\n'), css: cssTags.join('\n'), }; let fileContents = opts.templateContent; const pluginResponse = await ctx.ict.send('before_webindex_write', { filePath: opts.templatePath, fileContents, bundles, scriptTags, cssTags, }); if (pluginResponse && pluginResponse.fileContents) { fileContents = pluginResponse.fileContents; } logger.info('webindex', 'writing to $name</yellow></bold></dim>', { name: opts.distFileName, }); await ctx.writer.write(opts.distFileName, replaceWebIndexStrings(fileContents, scriptOpts)); }, }; } exports.createWebIndex = createWebIndex;