Skip to content

Commit

Permalink
fix: assertUsage() import strings (vikejs/vike-solid#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Sep 25, 2024
1 parent ea29d42 commit 11810cf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
assertUsage,
deepEqual,
isFilePathAbsolute,
isNpmPackageImport_unreliable,
requireResolve
} from '../../../../utils.js'
import { type PointerImportData, parsePointerImportData } from './transformFileImports.js'
Expand All @@ -21,6 +22,7 @@ import {
getFilePathUnresolved
} from '../../../../shared/getFilePath.js'
import type { FilePath, FilePathResolved } from '../../../../../../shared/page-configs/FilePath.js'
import { getErrorMessage_importPathOutsideOfRoot } from './transpileAndExecuteFile.js'

const filesEnvMap: Map<string, { configEnv: ConfigEnvInternal; configName: string }[]> = new Map()

Expand Down Expand Up @@ -58,6 +60,10 @@ function resolvePointerImport(
const { importPath } = pointerImportData
const filePathAbsoluteFilesystem = resolveImportPathWithNode(pointerImportData, importerFilePath)

const errInvalidImport = `Invalid import ${pc.code(importPath)} because it should start with ${pc.code(
'./'
)} or ${pc.code('../')}, or be an npm package import.`

let filePath: FilePath
// - importPath is one of the following. (See `transpileAndExecuteFile()`.)
// - A relative import path
Expand All @@ -67,13 +73,32 @@ function resolvePointerImport(
assertPosixPath(importPath)
if (importPath.startsWith('.') || isFilePathAbsolute(importPath)) {
if (importPath.startsWith('.')) {
assert(importPath.startsWith('./') || importPath.startsWith('../'))
assertUsage(importPath.startsWith('./') || importPath.startsWith('../'), errInvalidImport)
}
assertImportPath(filePathAbsoluteFilesystem, pointerImportData, importerFilePath)

// We need filePathAbsoluteUserRootDir because we didn't find a way to have filesystem absolute import paths in virtual files: https://gist.github.com/brillout/2315231c9a8164f950c64b4b4a7bbd39
const filePathAbsoluteUserRootDir = getFilePathAbsoluteUserRootDir({ filePathAbsoluteFilesystem, userRootDir })
// This assert() is guarenteed, see assertUsage() in the onResolve() esbuild hook in transpileAndExecuteFile.ts
assert(filePathAbsoluteUserRootDir)
if (importPath.startsWith('.')) {
assertUsage(
filePathAbsoluteUserRootDir,
getErrorMessage_importPathOutsideOfRoot({
importPathOriginal: importPath,
importPathResolved: filePathAbsoluteFilesystem,
userRootDir
})
)
} else {
assertUsage(
filePathAbsoluteUserRootDir,
`The import path ${importPath} resolves to ${filePathAbsoluteFilesystem} outside of ${userRootDir} which is forbbiden.`
)
}
// Forbid node_modules/ because it's brittle: if node_modules/ lives outside of userRootDir then it crashes.
assertUsage(
!filePathAbsoluteUserRootDir.includes('/node_modules/'),
`The import path ${importPath} resolves to ${filePathAbsoluteFilesystem} inside of /node_modules/ which is forbbiden.`
)

// Imports are included in virtual files, thus the relative path of imports need to resolved.
// ```
Expand All @@ -82,7 +107,7 @@ function resolvePointerImport(
filePath = getFilePathResolved({ filePathAbsoluteUserRootDir, userRootDir })
} else {
const importPathAbsolute = importPath
// importPath cannot be a path alias (since esbuild resolves path aliases, see transpileAndExecuteFile.ts)
assertUsage(isNpmPackageImport_unreliable(importPathAbsolute), errInvalidImport)
assertIsNpmPackageImport(importPathAbsolute)
if (filePathAbsoluteFilesystem) {
filePath = getFilePathResolved({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { transpileAndExecuteFile }
export { getConfigBuildErrorFormatted }
export { getConfigExecutionErrorIntroMsg }
export { isTemporaryBuildFile }
export { getErrorMessage_importPathOutsideOfRoot }

import { build, type BuildResult, type BuildOptions, formatMessages, type Message } from 'esbuild'
import fs from 'fs'
Expand Down Expand Up @@ -201,21 +202,10 @@ async function transpileWithEsbuild(
let importPathTranspiled: string
assertPosixPath(importPathOriginal)
if (importPathOriginal.startsWith('./') || importPathOriginal.startsWith('../')) {
// - We need this assertUsage() because we didn't find a way (yet?) to use filesystem absolute import paths in virtual files.
// - Alternatively, we can again try one of the following for generating the imports of virtual files. (Last time we tried none of it worked.)
// - ~~~js
// assert(filePathAbsoluteFilesystem.startsWith('/'))
// filePath = `/@fs${filePathAbsoluteFilesystem}`
// ~~~
// - ~~~js
// assert(filePathAbsoluteUserRootDir.startsWith('../'))
// filePathAbsoluteUserRootDir = '/' + filePathAbsoluteUserRootDir
// ~~~
// We need filePathAbsoluteUserRootDir because we didn't find a way to have filesystem absolute import paths in virtual files: https://gist.github.com/brillout/2315231c9a8164f950c64b4b4a7bbd39
assertUsage(
filePathAbsoluteUserRootDir,
`Import ${pc.cyan(
importPathOriginal
)} resolves to ${importPathResolved} outside of ${userRootDir} which is forbidden: make sure your relative import paths resolve inside ${userRootDir}, or import from an npm package.`
getErrorMessage_importPathOutsideOfRoot({ importPathOriginal, importPathResolved, userRootDir })
)
importPathTranspiled = importPathResolved
} else {
Expand Down Expand Up @@ -436,3 +426,13 @@ function cleanEsbuildErrors(errors: Message[]) {
))
)
}

function getErrorMessage_importPathOutsideOfRoot({
importPathOriginal,
importPathResolved,
userRootDir
}: { importPathOriginal: string; importPathResolved: string; userRootDir: string }): string {
return `The relative import ${pc.cyan(
importPathOriginal
)} resolves to ${importPathResolved} outside of the ${userRootDir} directory which is forbidden: make sure your relative import paths resolve inside the ${userRootDir} directory, or import from an npm package instead of using a relative import.`
}
5 changes: 5 additions & 0 deletions vike/utils/isNpmPackage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { isNpmPackageImport }
export { isNpmPackageImport_unreliable }
export { assertIsNpmPackageImport }
export { isValidPathAlias }
/* Currently not used
Expand All @@ -18,6 +19,10 @@ assertIsNotBrowser()
function isNpmPackageImport(str: string, { cannotBePathAlias }: { cannotBePathAlias: true }): boolean {
// We cannot distinguish path alises that look like npm package imports
assert(cannotBePathAlias)
return isNpmPackageImport_unreliable(str)
}

function isNpmPackageImport_unreliable(str: string): boolean {
const res = parse(str)
return res !== null
}
Expand Down

0 comments on commit 11810cf

Please sign in to comment.