Skip to content

Commit 99d3239

Browse files
authored
enforce explicit import strings (#2980)
1 parent 27e1807 commit 99d3239

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

packages/vike/src/node/vite/shared/importString.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export type { ImportString }
55
export type { ImportStringList }
66
export type { ImportStringParsed }
77

8-
import { assert } from '../utils.js'
8+
import pc from '@brillout/picocolors'
9+
import { assert, assertWarning } from '../utils.js'
910

1011
const IMPORT = 'import'
1112
const SEP = ':'
@@ -36,14 +37,16 @@ function parseImportString(
3637
importString: string,
3738
{ legacy = false }: { legacy?: boolean } = {},
3839
): null | ImportStringParsed {
39-
if (!isImportString(importString)) return null
40+
if (!importString.startsWith(`${IMPORT}${SEP}`)) return null
4041
const parts = importString.split(SEP)
4142
assert(parts[0] === IMPORT)
4243

4344
if (legacy && parts.length === 2) {
44-
/* TODO
45-
assertWarning(false, 'To-Do', { onlyOnce: true, showStackTrace: true })
46-
*/
45+
assertWarning(
46+
false,
47+
`Replace ${pc.cyan(importString)} with ${pc.cyan(importString)}${pc.cyan(':default')} (import strings must include the export name)`,
48+
{ onlyOnce: true },
49+
)
4750
const exportName = 'default'
4851
const importPath = parts[1]
4952
assert(importPath)
@@ -68,7 +71,7 @@ function parseImportString(
6871
* // => false
6972
*/
7073
function isImportString(str: string): str is ImportString {
71-
return str.startsWith(`${IMPORT}${SEP}`)
74+
return !!parseImportString(str, { legacy: true })
7275
}
7376

7477
/**

test/playground/pages/about-page/+config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Config } from 'vike/types'
33
export default {
44
// TEST: define `Page` over import string at `+config.js > export default { Page }` instead of +Page.js file.
55
// @ts-expect-error I ain't sure whether import strings should be part of the official type, but I think so.
6-
Page: 'import:./Page.jsx',
6+
Page: 'import:./Page.jsx:default',
77
// TEST: route defined over `+config.js > export default { route }`
88
route: '/about',
99
prerenderSetOverEffect: false,

0 commit comments

Comments
 (0)