Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/build-esbuild/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
DEFAULT_DOMSTACK_MANIFEST_FILENAME,
isDomstackManifestEnabled,
} from '../domstack-manifest/index.js'
import { importFile } from '#helpers/import-file.js'
import { toPosix } from '../helpers/path.js'
import { createDomStackLogger } from '../logger.js'

Expand Down Expand Up @@ -514,7 +515,7 @@ async function createBrowserBuildOpts (src, dest, siteData, opts, modeOpts = {})
}

const esbuildSettingsExtends = siteData.esbuildSettings
? (await import(siteData.esbuildSettings.filepath)).default
? (await importFile(siteData.esbuildSettings.filepath)).default
: (/** @type {typeof buildOpts} */ esbuildOpts) => esbuildOpts

const extendedBuildOpts = await esbuildSettingsExtends(buildOpts)
Expand Down
3 changes: 2 additions & 1 deletion lib/build-pages/generated-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { basename, dirname, isAbsolute, join, normalize, resolve } from 'path'
import { computePageUrl } from '../../helpers/compute-page-url.js'
import { DomStackOutputConflictError } from '../../helpers/domstack-error.js'
import { importFile } from '#helpers/import-file.js'
import { isAsyncIterable, isPlainObject } from '../../helpers/type-guards.js'
import { createSubscribedData, resolveDataDeps } from '../global-data/data-deps.js'

Expand Down Expand Up @@ -146,7 +147,7 @@ export async function * resolveGeneratedPageInfos ({ siteData, factoryVars, glob
if (pagesFileFilterSet && !pagesFileFilterSet.has(pagesFile.pagesFile.filepath)) continue

try {
const importResults = await import(pagesFile.pagesFile.filepath)
const importResults = await importFile(pagesFile.pagesFile.filepath)
if (!('default' in importResults)) throw new Error(`Missing default export from pages file: ${pagesFile.pagesFile.relname}`)

const pagesExport = importResults.default
Expand Down
3 changes: 2 additions & 1 deletion lib/build-pages/global-data/resolve-global-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @import { GlobalDataFunctionParams } from '../index.js'
*/

import { importFile } from '#helpers/import-file.js'
import { isFunction, isObject } from '../../helpers/type-guards.js'

/**
Expand All @@ -20,7 +21,7 @@ import { isFunction, isObject } from '../../helpers/type-guards.js'
export async function resolveGlobalData ({ globalDataPath, context }) {
if (!globalDataPath) return {}

const imported = await import(globalDataPath)
const imported = await importFile(globalDataPath)
const maybeGlobalData = imported.default

if (isFunction(maybeGlobalData)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/build-pages/layouts/resolve-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @import { PageOutputsFunction } from '../outputs/page-outputs.js'
*/

import { pathToFileURL } from 'node:url'
import { importFile } from '#helpers/import-file.js'
import { resolveVarsExport } from '../vars/resolve-vars.js'
import { validatePageOutputsHook } from '../outputs/page-outputs.js'

Expand All @@ -20,7 +20,7 @@ import { validatePageOutputsHook } from '../outputs/page-outputs.js'
* @returns {Promise<{ render: LayoutFunction<T, U, V, D>, vars: Partial<T>, parentLayout: string | undefined, pageOutputs: PageOutputsFunction<T, D> | undefined, source: string }>} The resolved layout module exports.
*/
export async function resolveLayout (layoutPath) {
const { default: layout, vars, parentLayout, pageOutputs } = await import(pathToFileURL(layoutPath).href)
const { default: layout, vars, parentLayout, pageOutputs } = await importFile(layoutPath)
if (typeof layout !== 'function') throw new TypeError(`Layout "${layoutPath}" must export a default render function`)
if (parentLayout !== undefined && (typeof parentLayout !== 'string' || !parentLayout.trim())) {
throw new TypeError(`Layout "${layoutPath}" parentLayout must be a non-empty string`)
Expand Down
3 changes: 2 additions & 1 deletion lib/build-pages/page-builders/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import assert from 'node:assert'
import { importFile } from '#helpers/import-file.js'
import { validatePageOutputsHook } from '../../outputs/page-outputs.js'

/**
Expand All @@ -27,7 +28,7 @@ export async function jsBuilder ({ pageInfo }) {
}
}

const { default: pageLayout, vars, pageOutputs } = await import(pageInfo.pageFile.filepath)
const { default: pageLayout, vars, pageOutputs } = await importFile(pageInfo.pageFile.filepath)

assert(pageLayout, 'js pages must export a page layout default export')
assert(typeof pageLayout === 'function', 'js pages pageLayout must be a function')
Expand Down
3 changes: 2 additions & 1 deletion lib/build-pages/page-builders/md/get-md.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import markdownItTaskList from 'markdown-it-task-lists'
import markdownItAnchor from 'markdown-it-anchor'
import markdownItAttrs from 'markdown-it-attrs'
import markdownItTOC from 'markdown-it-table-of-contents'
import { importFile } from '#helpers/import-file.js'
import { compileHandlebars } from '../compile-handlebars.js'

const mdOpts = {
Expand Down Expand Up @@ -50,7 +51,7 @@ export async function getMd (settingsPath = null) {
// Apply user settings if available
if (settingsPath) {
try {
const settingsModule = await import(settingsPath)
const settingsModule = await importFile(settingsPath)
const settingsFunction = settingsModule.default
if (typeof settingsFunction === 'function') {
return await settingsFunction(md)
Expand Down
3 changes: 2 additions & 1 deletion lib/build-pages/page/resolve-page-companion.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { importFile } from '#helpers/import-file.js'
import { resolveVarsExport } from '../vars/resolve-vars.js'

/**
Expand All @@ -8,7 +9,7 @@ import { resolveVarsExport } from '../vars/resolve-vars.js'
export async function resolvePageCompanion (varsPath) {
if (!varsPath) return { vars: {}, exports: undefined }

const exports = await import(varsPath)
const exports = await importFile(varsPath)
const vars = await resolveVarsExport(exports.default, 'Var')
if (exports.postVars) {
throw new Error(
Expand Down
3 changes: 2 additions & 1 deletion lib/build-pages/templates/template-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { dirname, join, relative, resolve } from 'node:path'
import { writeFile, mkdir } from 'fs/promises'
import { createDomstackManifestRecord } from '../../domstack-manifest/index.js'
import { importFile } from '#helpers/import-file.js'
import { assertInsideDest, toPosix } from '../../helpers/path.js'
import { isAsyncIterable, isPlainObject } from '../../helpers/type-guards.js'
import { createSubscribedData, resolveDataDeps } from '../global-data/data-deps.js'
Expand Down Expand Up @@ -102,7 +103,7 @@ export async function templateBuilder ({
watchDependencyTracker,
outputRecords = [],
}) {
const importResults = await import(template.templateFile.filepath)
const importResults = await importFile(template.templateFile.filepath)
if (!importResults.default || typeof importResults.default !== 'function') {
throw new Error('Template file resolved to something other than a template function')
}
Expand Down
3 changes: 2 additions & 1 deletion lib/build-pages/vars/resolve-vars.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { importFile } from '#helpers/import-file.js'
import { isFunction, isPlainObject } from '../../helpers/type-guards.js'

/**
Expand Down Expand Up @@ -35,6 +36,6 @@ export async function resolveVars ({
}) {
if (!varsPath) return {}

const imported = await import(varsPath)
const imported = await importFile(varsPath)
return await resolveVarsExport(imported[key], 'Var')
}
11 changes: 11 additions & 0 deletions lib/helpers/import-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { pathToFileURL } from 'node:url'

/**
* Dynamically import a module from a filesystem path using a `file://` URL.
*
* @param {string} filepath
* @returns {Promise<any>}
*/
export function importFile (filepath) {
return import(pathToFileURL(filepath).href)
}
24 changes: 24 additions & 0 deletions lib/helpers/import-file.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { importFile } from './import-file.js'

describe('importFile', () => {
it('imports modules from paths containing # and ? characters', async () => {
const root = await mkdtemp(join(tmpdir(), 'domstack-import-file-'))
const dir = join(root, 'dir#hash?query')
await mkdir(dir, { recursive: true })
const modPath = join(dir, 'mod#1?v=2.js')
await writeFile(modPath, 'export const value = 42\nexport default "ok"\n')

try {
const mod = await importFile(modPath)
assert.equal(mod.value, 42)
assert.equal(mod.default, 'ok')
} finally {
await rm(root, { recursive: true, force: true })
}
})
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"#types": {
"types": "./types.d.ts",
"default": "./types.ts"
}
},
"#helpers/*": "./lib/helpers/*"
},
"bin": {
"domstack": "./bin.js",
Expand Down
Loading