Skip to content

Commit

Permalink
Add global.client.js and global.css to injected scripts and styles
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Sep 2, 2021
1 parent 1d0605b commit 061459d
Show file tree
Hide file tree
Showing 31 changed files with 118 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lib/build-css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import postcss from 'postcss'
import autoprefixer from 'autoprefixer'
import postcssImport from 'postcss-import'
import postcssUrl from 'postcss-url'
import postcssNesting from 'postcss-nesting'

/**
* Build all of the CSS for every page and global CSS
Expand All @@ -30,6 +31,7 @@ export async function buildCss (src, dest, siteData) {
useHash: true,
assetsPath: 'assets'
}),
postcssNesting,
autoprefixer
]).process(css, { from: style.relname, to: targetPath })
await writeFile(targetPath, result.css)
Expand Down
2 changes: 2 additions & 0 deletions lib/build-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import esbuild from 'esbuild'
*/
export async function buildJs (src, dest, siteData) {
const entryPoints = []
if (siteData.globalClient) entryPoints.push(join(src, siteData.globalClient.relname))

for (const page of siteData.pages) {
if (page.clientBundle) entryPoints.push(join(src, page.clientBundle.relname))
Expand All @@ -21,6 +22,7 @@ export async function buildJs (src, dest, siteData) {
write: true,
format: 'esm',
splitting: true,
sourcemap: 'both',
outdir: dest,
target: [
'es2020'
Expand Down
3 changes: 3 additions & 0 deletions lib/build-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export async function buildPages (src, dest, siteData) {
// This should never happen
if (!builder) throw new Error(`Can't build ${page.path}. unimplemented type ${page.type}`)

globalVars.styles = siteData.globalStyle ? ['/global.css'] : []
globalVars.scripts = siteData.globalClient ? ['/global.client.js'] : []

const result = await builder({ src, dest, page, globalVars, rootLayout })
results.push(result)
}
Expand Down
5 changes: 2 additions & 3 deletions lib/build-pages/page-builders/create-page-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { join } from 'node:path'
import pretty from 'pretty'
import { writeFile, mkdir } from 'node:fs/promises'

import { fsPathToUrlPath } from './fs-path-to-url.js'
import { resolveVars } from '../resolve-vars.js'

export function createPageBuilder (builder) {
Expand All @@ -24,8 +23,8 @@ export function createPageBuilder (builder) {

const finalVars = Object.assign({}, globalVars, pageVars, vars)

if (page.pageStyle) finalVars.styles = [fsPathToUrlPath(join(page.path, 'style.css'))]
if (page.clientBundle) finalVars.scripts = [fsPathToUrlPath(join(page.path, 'client.js'))]
if (page.pageStyle) finalVars.styles.push('./style.css')
if (page.clientBundle) finalVars.scripts.push('./client.js')

const output = await pageLayout(finalVars)

Expand Down
1 change: 1 addition & 0 deletions lib/identify-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export async function identifyPages (src, pageBuilders) {

const results = {
globalStyle: rootFiles['global.css'],
globalClient: rootFiles['global.client.js'],
globalVars: rootFiles['global.vars.js'],
rootLayout: rootFiles['root.layout.js'],
pages,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"postcss": "^8.3.6",
"postcss-import": "^14.0.2",
"postcss-url": "^10.1.3",
"postcss-nesting": "^8.0.1",
"pretty": "^2.0.0",
"streamx": "^2.10.3",
"uhtml-ssr": "^0.7.4",
Expand Down
4 changes: 2 additions & 2 deletions test-project/public/a-page/client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
foo
} from "../chunk-OWLKZ7N7.js";
import "../chunk-OFR2H3GI.js";
} from "../chunk-WZ7JV6GS.js";
import "../chunk-HC4Q5QIB.js";

// test-project/src/a-page/client.js
console.log("a-page client bundle");
Expand Down
7 changes: 7 additions & 0 deletions test-project/public/a-page/client.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test-project/public/a-page/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, user-scalable=no" />
<script src="/a-page/client.js" type='module'></script>
<link rel="stylesheet" href="/a-page/style.css" />
<script src="./client.js" type='module'></script>
<link rel="stylesheet" href="/global.css" />
<link rel="stylesheet" href="./style.css" />
</head>

<body>
Expand Down
4 changes: 2 additions & 2 deletions test-project/public/a-page/nested-page/client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
a_lib_exports
} from "../../chunk-OWLKZ7N7.js";
import "../../chunk-OFR2H3GI.js";
} from "../../chunk-WZ7JV6GS.js";
import "../../chunk-HC4Q5QIB.js";

// test-project/src/a-page/nested-page/client.js
console.log(a_lib_exports);
Expand Down
7 changes: 7 additions & 0 deletions test-project/public/a-page/nested-page/client.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test-project/public/a-page/nested-page/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, user-scalable=no" />
<script src="/a-page/nested-page/client.js" type='module'></script>
<link rel="stylesheet" href="/a-page/nested-page/style.css" />
<script src="./client.js" type='module'></script>
<link rel="stylesheet" href="/global.css" />
<link rel="stylesheet" href="./style.css" />
</head>

<body>
Expand Down
11 changes: 11 additions & 0 deletions test-project/public/chunk-HC4Q5QIB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var __defProp = Object.defineProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
__markAsModule(target);
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};

export {
__export
};
7 changes: 7 additions & 0 deletions test-project/public/chunk-HC4Q5QIB.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions test-project/public/chunk-WZ7JV6GS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
__export
} from "./chunk-HC4Q5QIB.js";

// test-project/src/a-page/libs/a-lib.js
var a_lib_exports = {};
__export(a_lib_exports, {
foo: () => foo
});
var foo = { some: "data" };

export {
foo,
a_lib_exports
};
7 changes: 7 additions & 0 deletions test-project/public/chunk-WZ7JV6GS.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test-project/public/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "./chunk-OFR2H3GI.js";
import "./chunk-HC4Q5QIB.js";

// test-project/src/client.js
console.log("hello world root client bundle");
7 changes: 7 additions & 0 deletions test-project/public/client.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test-project/public/global.client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import "./chunk-HC4Q5QIB.js";

// test-project/src/global.client.js
console.log("Hello I run on every page!");
7 changes: 7 additions & 0 deletions test-project/public/global.client.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test-project/public/global.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test-project/public/global.css.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test-project/public/html-page/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "../chunk-OFR2H3GI.js";
import "../chunk-HC4Q5QIB.js";

// test-project/src/html-page/client.js
console.log("html page client bundle");
7 changes: 7 additions & 0 deletions test-project/public/html-page/client.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test-project/public/html-page/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, user-scalable=no" />
<script src="/html-page/client.js" type='module'></script>
<link rel="stylesheet" href="/html-page/style.css" />
<script src="./client.js" type='module'></script>
<link rel="stylesheet" href="/global.css" />
<link rel="stylesheet" href="./style.css" />
</head>

<body>
Expand Down
4 changes: 2 additions & 2 deletions test-project/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, user-scalable=no" />
<script src="/client.js" type='module'></script>
<link rel="stylesheet" href="/style.css" />
<script src="./client.js" type='module'></script>
<link rel="stylesheet" href="/global.css" />
<link rel="stylesheet" href="./style.css" />
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion test-project/public/md-page/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "../chunk-OFR2H3GI.js";
import "../chunk-HC4Q5QIB.js";

// test-project/src/md-page/client.js
console.log("markdown client bundle");
7 changes: 7 additions & 0 deletions test-project/public/md-page/client.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test-project/public/md-page/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, user-scalable=no" />
<script src="/md-page/client.js" type='module'></script>
<link rel="stylesheet" href="/md-page/style.css" />
<script src="./client.js" type='module'></script>
<link rel="stylesheet" href="/global.css" />
<link rel="stylesheet" href="./style.css" />
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions test-project/src/global.client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello I run on every page!')
1 change: 0 additions & 1 deletion test-project/src/root.layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export default async function RootLayout ({
${styles
? styles.map(style => html`<link rel="stylesheet" href=${style} />`)
: null}
<link rel="stylesheet" href="/global.css" />
</head>
<body>
${typeof children === 'string' ? html([children]) : children /* Support both uhtml and string children. Optional. */}
Expand Down

0 comments on commit 061459d

Please sign in to comment.