|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as _ from 'lodash'; |
| 4 | +import * as MemoryFS from 'memory-fs'; |
| 5 | +import { CliConfig } from '../models/config'; |
| 6 | + |
| 7 | +export class HtmlCliPlugin { |
| 8 | + private cachedTemplate: string; |
| 9 | + |
| 10 | + apply(compiler) { |
| 11 | + if (compiler.options.name === 'main' && CliConfig.fromProject().apps[0].mobile) { |
| 12 | + compiler.plugin('emit', this.cacheTemplate); |
| 13 | + } |
| 14 | + compiler.plugin('done', this.generateIndexHtml); |
| 15 | + } |
| 16 | + |
| 17 | + cacheTemplate(c, callback) { |
| 18 | + const sourceDir = CliConfig.fromProject().defaults.sourceDir; |
| 19 | + const projectRoot = path.resolve(sourceDir, '..'); |
| 20 | + const appShellPath = path.resolve(projectRoot, `./${sourceDir}/main-app-shell.ts`); |
| 21 | + const appShell = require(appShellPath); |
| 22 | + const bootloader = appShell.getBootloader(); |
| 23 | + const indexHtml = path.resolve(sourceDir, 'index.html'); |
| 24 | + const assetsPath = path.resolve(projectRoot, 'cli.assets.json'); |
| 25 | + |
| 26 | + appShell.serialize(bootloader, fs.readFileSync(indexHtml, 'utf8')).then(html => { |
| 27 | + console.log(html) |
| 28 | + this.cachedTemplate = html; |
| 29 | + callback(); |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + generateIndexHtml(stats) { |
| 34 | + const isMobile = CliConfig.fromProject().apps[0].mobile; |
| 35 | + const sourceDir = CliConfig.fromProject().defaults.sourceDir; |
| 36 | + const projectRoot = path.resolve(sourceDir, '..'); |
| 37 | + const assetsPath = path.resolve(projectRoot, 'cli.assets.json'); |
| 38 | + const contents = JSON.parse(fs.readFileSync(assetsPath, 'utf8')); |
| 39 | + const indexHtml = path.resolve(sourceDir, 'index.html'); |
| 40 | + let tpl; |
| 41 | + let mfs; |
| 42 | + let files = { js: [], css: [] }; |
| 43 | + |
| 44 | + Object.keys(contents).forEach(key => { |
| 45 | + let type = Object.keys(contents[key])[0]; |
| 46 | + files[type].unshift(contents[key][type]); |
| 47 | + }); |
| 48 | + |
| 49 | + if (this.cachedTemplate) { |
| 50 | + tpl = this.cachedTemplate; |
| 51 | + tpl = _.template(tpl.replace(/</g, '<').replace(/>/g, '>').replace(/##/g, '%')); |
| 52 | + writeTemplate(stats, tpl, files); |
| 53 | + } else { |
| 54 | + tpl = _.template(fs.readFileSync(indexHtml, 'utf8').replace(/##/g, '%')); |
| 55 | + writeTemplate(stats, tpl, files); |
| 56 | + } |
| 57 | + |
| 58 | + function writeTemplate(stats, tpl, files) { |
| 59 | + const destHtml = path.resolve(stats.compilation.compiler.outputPath, 'index.html'); |
| 60 | + |
| 61 | + if (stats.compilation.compiler.outputFileSystem instanceof MemoryFS) { |
| 62 | + if (!mfs) { mfs = stats.compilation.compiler.outputFileSystem; } |
| 63 | + mfs.writeFileSync(destHtml, tpl({ files: files }), 'utf8'); |
| 64 | + } else { |
| 65 | + fs.writeFileSync(destHtml, tpl({ files: files }), 'utf8'); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments