This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement static html export (#414)
- Loading branch information
Showing
16 changed files
with
641 additions
and
346 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { createCompiler } from '../preview/create-compiler'; | ||
import { Exporter, ExportResult } from './exporter'; | ||
import * as Fs from 'fs'; | ||
import * as Path from 'path'; | ||
import { previewDocument, PreviewDocumentMode } from '../preview/preview-document'; | ||
import { Store } from '../store/store'; | ||
import * as Util from 'util'; | ||
import * as uuid from 'uuid'; | ||
|
||
const SCRIPTS = ['vendor', 'renderer', 'components', 'preview']; | ||
|
||
const createScript = (name: string, content: string) => | ||
`<script data-script="${name}">${content}<\/script>`; | ||
|
||
export class HtmlExporter extends Exporter { | ||
public async createExport(): Promise<ExportResult> { | ||
const store = Store.getInstance(); | ||
const project = store.getCurrentProject(); | ||
const currentPage = store.getCurrentPage(); | ||
const styleguide = store.getStyleguide(); | ||
|
||
// TODO: Come up with good user-facing errors | ||
if (!project || !currentPage || !styleguide) { | ||
return {}; | ||
} | ||
|
||
const data = { | ||
id: uuid.v4(), | ||
type: 'state', | ||
payload: { | ||
mode: PreviewDocumentMode.Static, | ||
pageId: currentPage.getId(), | ||
pages: project.getPages().map(page => page.toJsonObject({ forRendering: true })) | ||
} | ||
}; | ||
|
||
const compiler = createCompiler(styleguide); | ||
await Util.promisify(compiler.run).bind(compiler)(); | ||
|
||
const fs = (await compiler.outputFileSystem) as typeof Fs; | ||
|
||
const scripts = SCRIPTS.map(name => [ | ||
name, | ||
fs.readFileSync(Path.posix.join('/', `${name}.js`)).toString() | ||
]) | ||
.map(([name, content]) => createScript(name, content)) | ||
.join('\n'); | ||
|
||
const document = previewDocument({ | ||
data, | ||
mode: PreviewDocumentMode.Static, | ||
scripts | ||
}); | ||
|
||
this.contents = Buffer.from(document); | ||
|
||
return { | ||
result: this.contents | ||
}; | ||
} | ||
} |
Oops, something went wrong.