From 04a25fc527a19763887d2dfaae08465a2b9907a2 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 5 Nov 2020 22:56:40 +0100 Subject: [PATCH] feat(browser): inject script to js template --- packages/nitro/src/build.ts | 21 ++++++++++++--------- packages/nitro/src/index.ts | 10 +--------- packages/nitro/src/utils.ts | 11 +++++++---- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/packages/nitro/src/build.ts b/packages/nitro/src/build.ts index 58c5e9668b..0552f64a67 100644 --- a/packages/nitro/src/build.ts +++ b/packages/nitro/src/build.ts @@ -7,7 +7,7 @@ import gzipSize from 'gzip-size' import chalk from 'chalk' import { emptyDir } from 'fs-extra' import { getRollupConfig } from './rollup/config' -import { hl, prettyPath, renderTemplate, compileTemplateToJS } from './utils' +import { hl, prettyPath, renderTemplate, compileTemplateToJS, writeFileP } from './utils' import { SLSOptions } from './config' export async function build (options: SLSOptions) { @@ -17,7 +17,17 @@ export async function build (options: SLSOptions) { const hooks = new Hookable() hooks.addHooks(options.hooks) - await hooks.callHook('options', options) + // Compile html template + const htmlTemplate = { + src: resolve(options.buildDir, `views/${{ 2: 'app', 3: 'document' }[options.nuxt]}.template.html`), + dst: '', + compiled: '' + } + htmlTemplate.dst = htmlTemplate.src.replace(/.html$/, '.js').replace('app.', 'document.') + htmlTemplate.compiled = await compileTemplateToJS(htmlTemplate.src) + await hooks.callHook('template:document', htmlTemplate) + await writeFileP(htmlTemplate.dst, htmlTemplate.compiled) + consola.info('Generated', prettyPath(htmlTemplate.dst)) emptyDir(options.slsDir) @@ -44,10 +54,3 @@ export async function build (options: SLSOptions) { await hooks.callHook('done', options) } - -export async function compileHTMLTemplate (options: SLSOptions) { - const htmlTemplateFile = resolve(options.buildDir, `views/${{ 2: 'app', 3: 'document' }[options.nuxt]}.template.html`) - const htmlTemplateFileJS = htmlTemplateFile.replace(/.html$/, '.js').replace('app.', 'document.') - await compileTemplateToJS(htmlTemplateFile, htmlTemplateFileJS) - consola.info('Generated', prettyPath(htmlTemplateFileJS)) -} diff --git a/packages/nitro/src/index.ts b/packages/nitro/src/index.ts index 4f5ad6d391..0816693169 100644 --- a/packages/nitro/src/index.ts +++ b/packages/nitro/src/index.ts @@ -41,13 +41,5 @@ export default function slsModule () { } }) - nuxt.hook('generate:done', () => buildSLS(options)) -} - -async function buildSLS (options) { - // Compile html template - await compileHTMLTemplate(options) - - // Bundle target - await build(options) + nuxt.hook('generate:done', () => build(options)) } diff --git a/packages/nitro/src/utils.ts b/packages/nitro/src/utils.ts index d48414db53..584a670aa7 100644 --- a/packages/nitro/src/utils.ts +++ b/packages/nitro/src/utils.ts @@ -24,12 +24,15 @@ export async function renderTemplate (src: string, dst: string, params: any) { await writeFile(dst, rendered) } -export async function compileTemplateToJS (src: string, dst: string) { +export async function compileTemplateToJS (src: string) { const contents = await readFile(src, 'utf-8') // eslint-disable-next-line no-template-curly-in-string - const compiled = `export default (params) => \`${contents.replace(/{{ (\w+) }}/g, '${params.$1}')}\`` - await mkdirp(dirname(dst)) - await writeFile(dst, compiled) + return `export default (params) => \`${contents.replace(/{{ (\w+) }}/g, '${params.$1}')}\`` +} + +export async function writeFileP (path, contents) { + await mkdirp(dirname(path)) + await writeFile(path, contents) } export const jitiImport = (dir: string, path: string) => jiti(dir)(path)