Skip to content

Commit

Permalink
Inject style and hydrate ID into HTML.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Apr 24, 2020
1 parent 9c4e08e commit a915a50
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
4 changes: 0 additions & 4 deletions cmd/build/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,13 @@ func DataSource(buildPath string, siteConfig readers.SiteConfig) (string, string

// Check for path overrides from plenti.json config file.
for configContentType, slug := range siteConfig.Types {
fmt.Printf("cct is: %s\n", configContentType)
fmt.Printf("ct is: %s\n", contentType)
if configContentType == contentType {
slug = strings.Replace(slug, ":filename", fileName, -1)
slug = strings.Replace(slug, "_", "-", -1)
slug = strings.ToLower(slug)
slug = strings.TrimSuffix(slug, filepath.Ext(slug))
path = slug
}
fmt.Printf("slug is: %s\n", slug)
fmt.Printf("path is: %s\n", path)
}

destPath := buildPath + "/" + path + ".html"
Expand Down
33 changes: 26 additions & 7 deletions defaults/layout/ejected/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import fs from 'fs';
// Get the arguments from Go command execution.
const args = process.argv.slice(2)

// -----------------------
// Start client SPA build:
// -----------------------
// -----------------
// Helper Functions:
// -----------------

// Create any missing sub folders.
const ensureDirExists = filePath => {
Expand All @@ -21,6 +21,19 @@ const ensureDirExists = filePath => {
fs.mkdirSync(dirname);
}

// Concatenates HTML strings together.
const injectString = (order, content, element, html) => {
if (order == 'prepend') {
return html.replace(element, content + element);
} else if (order == 'append') {
return html.replace(element, element + content);
}
};

// -----------------------
// Start client SPA build:
// -----------------------

let clientBuildStr = JSON.parse(args[0]);

clientBuildStr.forEach(arg => {
Expand All @@ -44,10 +57,6 @@ clientBuildStr.forEach(arg => {
// Start static HTML build:
// ------------------------

//console.log(args[1]);
//console.log("\n\n");
//console.log(args[2]);

let staticBuildStr = JSON.parse(args[1]);
let allNodes = JSON.parse(args[2]);

Expand All @@ -70,6 +79,16 @@ staticBuildStr.forEach(arg => {
// Create the static HTML and CSS.
let { html, css } = component.render(props);

// Inject Style.
let style = "<style>" + css.code + "</style>";
html = injectString('prepend', style, '</head>', html);
// Inject SPA entry point.
let entryPoint = '<script type="module" src="https://unpkg.com/dimport?module" data-main="/spa/ejected/main.js"></script><script nomodule src="https://unpkg.com/dimport/nomodule" data-main="/spa/ejected/main.js"></script>';
html = injectString('prepend', entryPoint, '</head>', html);
// Inject ID used to hydrate SPA.
let hydrator = ' id="hydrate-plenti"';
html = injectString('append', hydrator, '<html', html);

// Write .html file to filesystem.
ensureDirExists(destPath);
fs.promises.writeFile(destPath, html);
Expand Down
33 changes: 26 additions & 7 deletions generated/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ import fs from 'fs';
// Get the arguments from Go command execution.
const args = process.argv.slice(2)
// -----------------------
// Start client SPA build:
// -----------------------
// -----------------
// Helper Functions:
// -----------------
// Create any missing sub folders.
const ensureDirExists = filePath => {
Expand All @@ -131,6 +131,19 @@ const ensureDirExists = filePath => {
fs.mkdirSync(dirname);
}
// Concatenates HTML strings together.
const injectString = (order, content, element, html) => {
if (order == 'prepend') {
return html.replace(element, content + element);
} else if (order == 'append') {
return html.replace(element, element + content);
}
};
// -----------------------
// Start client SPA build:
// -----------------------
let clientBuildStr = JSON.parse(args[0]);
clientBuildStr.forEach(arg => {
Expand All @@ -154,10 +167,6 @@ clientBuildStr.forEach(arg => {
// Start static HTML build:
// ------------------------
//console.log(args[1]);
//console.log("\n\n");
//console.log(args[2]);
let staticBuildStr = JSON.parse(args[1]);
let allNodes = JSON.parse(args[2]);
Expand All @@ -180,6 +189,16 @@ staticBuildStr.forEach(arg => {
// Create the static HTML and CSS.
let { html, css } = component.render(props);
// Inject Style.
let style = "<style>" + css.code + "</style>";
html = injectString('prepend', style, '</head>', html);
// Inject SPA entry point.
let entryPoint = '<script type="module" src="https://unpkg.com/dimport?module" data-main="/spa/ejected/main.js"></script><script nomodule src="https://unpkg.com/dimport/nomodule" data-main="/spa/ejected/main.js"></script>';
html = injectString('prepend', entryPoint, '</head>', html);
// Inject ID used to hydrate SPA.
let hydrator = ' id="hydrate-plenti"';
html = injectString('append', hydrator, '<html', html);
// Write .html file to filesystem.
ensureDirExists(destPath);
fs.promises.writeFile(destPath, html);
Expand Down

0 comments on commit a915a50

Please sign in to comment.