Skip to content

Commit

Permalink
Allow global assets to live anywhere
Browse files Browse the repository at this point in the history
Global assets can live anywhere, they no longer have to live in the root of the src directory.
  • Loading branch information
bcomnes committed Nov 13, 2023
1 parent e97a422 commit 2c2447e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/build-pages/resolve-vars.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { resolveVars } from './resolve-vars.js'
const __dirname = desm(import.meta.url)

tap.test('resolve vars resolves vars', async (t) => {
const varsPath = resolve(__dirname, '../../test-cases/general-features/src/global.vars.js')
const varsPath = resolve(__dirname, '../../test-cases/general-features/src/globals/global.vars.js')

const vars = await resolveVars({ varsPath })

Expand Down
2 changes: 1 addition & 1 deletion lib/defaults/default.root.layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function defaultRootLayout ({
vars: {
title,
siteName = 'TopBun'
/* defaultStyle = true Set this to false in global or pageto disable the default style in the default layout */
/* defaultStyle = true Set this to false in global or page to disable the default style in the default layout */
},
scripts,
styles,
Expand Down
5 changes: 4 additions & 1 deletion lib/helpers/top-bun-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
* 'TOP_BUN_WARNING_DUPLICATE_LAYOUT_CLIENT' |
* 'TOP_BUN_WARNING_ORPHANED_LAYOUT_CLIENT' |
* 'TOP_BUN_WARNING_NO_ROOT_LAYOUT' |
* 'TOP_BUN_WARNING_UNKNOWN_PAGE_BUILDER'
* 'TOP_BUN_WARNING_UNKNOWN_PAGE_BUILDER' |
* 'TOP_BUN_WARNING_DUPLICATE_GLOBAL_STYLE' |
* 'TOP_BUN_WARNING_DUPLICATE_GLOBAL_CLIENT' |
* 'TOP_BUN_WARNING_DUPLICATE_GLOBAL_VARS'
* } TopBunWarningCode */

/**
Expand Down
59 changes: 50 additions & 9 deletions lib/identify-pages.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { asyncFolderWalker } from 'async-folder-walker'
import assert from 'node:assert'
import desm from 'desm'
import { resolve, relative, join } from 'path'
import { resolve, relative, join, basename } from 'path'
import { pageBuilders } from './build-pages/index.js'
import { TopBunDuplicatePageError } from './helpers/top-bun-error.js'

Expand All @@ -18,6 +18,9 @@ const layoutSuffix = '.layout.js'
const layoutClientSuffix = '.layout.client.js'
const layoutStyleSuffix = '.layout.css'
export const templateSuffix = '.template.js'
const globalStyleName = 'global.css'
const globalClientName = 'global.client.js'
const globalVarsName = 'global.vars.js'

/**
* Shape the file walker object
Expand Down Expand Up @@ -130,6 +133,15 @@ export async function identifyPages (src, opts = {}) {
/** @type {TemplateInfo[]} The array of discovered template files */
const templates = []

/** @type {PageFileAsset | undefined } */
let globalStyle

/** @type {PageFileAsset | undefined } */
let globalClient

/** @type {PageFileAsset | undefined } */
let globalVars

/** @type {TopBunWarning[]} */
const warnings = []

Expand Down Expand Up @@ -265,11 +277,43 @@ export async function identifyPages (src, opts = {}) {
outputName: temlateFileName
})
}

if (basename(fileName) === globalStyleName) {
if (globalStyle) {
warnings.push({
code: 'TOP_BUN_WARNING_DUPLICATE_GLOBAL_STYLE',
message: `Skipping ${fileInfo.relname}. Duplicate global style ${fileName} to ${globalStyle.filepath}`
})
} else {
globalStyle = fileInfo
}
}

if (basename(fileName) === globalClientName) {
if (globalClient) {
warnings.push({
code: 'TOP_BUN_WARNING_DUPLICATE_GLOBAL_CLIENT',
message: `Skipping ${fileInfo.relname}. Duplicate global client ${fileName} to ${globalClient.filepath}`
})
} else {
globalClient = fileInfo
}
}

if (basename(fileName) === globalVarsName) {
if (globalVars) {
warnings.push({
code: 'TOP_BUN_WARNING_DUPLICATE_GLOBAL_VARS',
message: `Skipping ${fileInfo.relname}. Duplicate global client ${fileName} to ${globalVars.filepath}`
})
} else {
globalVars = fileInfo
}
}
}
}

// Inject global lauouts and vars
const rootFiles = dirs[''] ?? {}
// const rootFiles = dirs[''] ?? {}

let defaultLayout = false
// eslint-disable-next-line dot-notation
Expand Down Expand Up @@ -306,12 +350,9 @@ export async function identifyPages (src, opts = {}) {
})

const results = {
/** @type {PageFileAsset | undefined } */
globalStyle: rootFiles['global.css'],
/** @type {PageFileAsset | undefined } */
globalClient: rootFiles['global.client.js'],
/** @type {PageFileAsset | undefined } */
globalVars: rootFiles['global.vars.js'],
globalStyle,
globalClient,
globalVars,
/** @type {string?} Path to a default style */
defaultStyle: null,
/** @type {string?} Path to a default client */
Expand Down

0 comments on commit 2c2447e

Please sign in to comment.