Skip to content

Commit

Permalink
feat: convert Object.values(import.meta.glob('))
Browse files Browse the repository at this point in the history
  • Loading branch information
btea committed Nov 21, 2024
1 parent cd424ca commit d869760
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`fixture > transform 1`] = `
"import * as __vite_glob_2_0 from "./modules/a.ts";import * as __vite_glob_2_1 from "./modules/b.ts";import * as __vite_glob_2_2 from "./modules/index.ts";import { name as __vite_glob_6_0 } from "./modules/a.ts";import { name as __vite_glob_6_1 } from "./modules/b.ts";import { name as __vite_glob_6_2 } from "./modules/index.ts";import { default as __vite_glob_10_0 } from "./modules/a.ts?raw";import { default as __vite_glob_10_1 } from "./modules/b.ts?raw";import "types/importMeta";
"import * as __vite_glob_3_0 from "./modules/a.ts";import * as __vite_glob_3_1 from "./modules/b.ts";import * as __vite_glob_3_2 from "./modules/index.ts";import * as __vite_glob_5_0 from "./modules/a.ts";import * as __vite_glob_5_1 from "./modules/b.ts";import * as __vite_glob_5_2 from "./modules/index.ts";import { name as __vite_glob_9_0 } from "./modules/a.ts";import { name as __vite_glob_9_1 } from "./modules/b.ts";import { name as __vite_glob_9_2 } from "./modules/index.ts";import { name as __vite_glob_11_0 } from "./modules/a.ts";import { name as __vite_glob_11_1 } from "./modules/b.ts";import { name as __vite_glob_11_2 } from "./modules/index.ts";import { default as __vite_glob_15_0 } from "./modules/a.ts?raw";import { default as __vite_glob_15_1 } from "./modules/b.ts?raw";import "types/importMeta";
export const basic = /* #__PURE__ */ Object.assign({"./modules/a.ts": () => import("./modules/a.ts"),"./modules/b.ts": () => import("./modules/b.ts"),"./modules/index.ts": () => import("./modules/index.ts")});
export const basicWithObjectKeys = Object.keys({"./modules/a.ts": '',"./modules/b.ts": '',"./modules/index.ts": ''});
export const basicEager = /* #__PURE__ */ Object.assign({"./modules/a.ts": __vite_glob_2_0,"./modules/b.ts": __vite_glob_2_1,"./modules/index.ts": __vite_glob_2_2
export const basicWithObjectValues = Object.values([() => import("./modules/a.ts"),() => import("./modules/b.ts"),() => import("./modules/index.ts")]);
export const basicEager = /* #__PURE__ */ Object.assign({"./modules/a.ts": __vite_glob_3_0,"./modules/b.ts": __vite_glob_3_1,"./modules/index.ts": __vite_glob_3_2
});
export const basicEagerWithObjectKeys = Object.keys(
{"./modules/a.ts": '',"./modules/b.ts": '',"./modules/index.ts": ''
}
);
export const basicEagerWithObjectValues = Object.values(
[__vite_glob_5_0,__vite_glob_5_1,__vite_glob_5_2
]
);
export const ignore = /* #__PURE__ */ Object.assign({"./modules/a.ts": () => import("./modules/a.ts"),"./modules/b.ts": () => import("./modules/b.ts")});
export const ignoreWithObjectKeys = Object.keys(
{"./modules/a.ts": '',"./modules/b.ts": ''}
);
export const namedEager = /* #__PURE__ */ Object.assign({"./modules/a.ts": __vite_glob_6_0,"./modules/b.ts": __vite_glob_6_1,"./modules/index.ts": __vite_glob_6_2
export const ignoreWithObjectValues = Object.values(
[() => import("./modules/a.ts"),() => import("./modules/b.ts")]
);
export const namedEager = /* #__PURE__ */ Object.assign({"./modules/a.ts": __vite_glob_9_0,"./modules/b.ts": __vite_glob_9_1,"./modules/index.ts": __vite_glob_9_2
});
Expand All @@ -26,6 +35,12 @@ export const namedEagerWithObjectKeys = Object.keys(
}
);
export const namedEagerWithObjectValues = Object.values(
[__vite_glob_11_0,__vite_glob_11_1,__vite_glob_11_2
]
);
export const namedDefault = /* #__PURE__ */ Object.assign({"./modules/a.ts": () => import("./modules/a.ts").then(m => m["default"]),"./modules/b.ts": () => import("./modules/b.ts").then(m => m["default"]),"./modules/index.ts": () => import("./modules/index.ts").then(m => m["default"])
});
Expand All @@ -34,7 +49,12 @@ export const namedDefaultWithObjectKeys = Object.keys(
}
);
export const eagerAs = /* #__PURE__ */ Object.assign({"./modules/a.ts": __vite_glob_10_0,"./modules/b.ts": __vite_glob_10_1
export const namedDefaultWithObjectValues = Object.values(
[() => import("./modules/a.ts").then(m => m["default"]),() => import("./modules/b.ts").then(m => m["default"]),() => import("./modules/index.ts").then(m => m["default"])
]
);
export const eagerAs = /* #__PURE__ */ Object.assign({"./modules/a.ts": __vite_glob_15_0,"./modules/b.ts": __vite_glob_15_1
});
Expand Down Expand Up @@ -76,22 +96,31 @@ export const cleverCwd2 = /* #__PURE__ */ Object.assign({"./modules/a.ts": () =>
`;

exports[`fixture > transform with restoreQueryExtension 1`] = `
"import * as __vite_glob_2_0 from "./modules/a.ts";import * as __vite_glob_2_1 from "./modules/b.ts";import * as __vite_glob_2_2 from "./modules/index.ts";import { name as __vite_glob_6_0 } from "./modules/a.ts";import { name as __vite_glob_6_1 } from "./modules/b.ts";import { name as __vite_glob_6_2 } from "./modules/index.ts";import { default as __vite_glob_10_0 } from "./modules/a.ts?raw";import { default as __vite_glob_10_1 } from "./modules/b.ts?raw";import "types/importMeta";
"import * as __vite_glob_3_0 from "./modules/a.ts";import * as __vite_glob_3_1 from "./modules/b.ts";import * as __vite_glob_3_2 from "./modules/index.ts";import * as __vite_glob_5_0 from "./modules/a.ts";import * as __vite_glob_5_1 from "./modules/b.ts";import * as __vite_glob_5_2 from "./modules/index.ts";import { name as __vite_glob_9_0 } from "./modules/a.ts";import { name as __vite_glob_9_1 } from "./modules/b.ts";import { name as __vite_glob_9_2 } from "./modules/index.ts";import { name as __vite_glob_11_0 } from "./modules/a.ts";import { name as __vite_glob_11_1 } from "./modules/b.ts";import { name as __vite_glob_11_2 } from "./modules/index.ts";import { default as __vite_glob_15_0 } from "./modules/a.ts?raw";import { default as __vite_glob_15_1 } from "./modules/b.ts?raw";import "types/importMeta";
export const basic = /* #__PURE__ */ Object.assign({"./modules/a.ts": () => import("./modules/a.ts"),"./modules/b.ts": () => import("./modules/b.ts"),"./modules/index.ts": () => import("./modules/index.ts")});
export const basicWithObjectKeys = Object.keys({"./modules/a.ts": '',"./modules/b.ts": '',"./modules/index.ts": ''});
export const basicEager = /* #__PURE__ */ Object.assign({"./modules/a.ts": __vite_glob_2_0,"./modules/b.ts": __vite_glob_2_1,"./modules/index.ts": __vite_glob_2_2
export const basicWithObjectValues = Object.values([() => import("./modules/a.ts"),() => import("./modules/b.ts"),() => import("./modules/index.ts")]);
export const basicEager = /* #__PURE__ */ Object.assign({"./modules/a.ts": __vite_glob_3_0,"./modules/b.ts": __vite_glob_3_1,"./modules/index.ts": __vite_glob_3_2
});
export const basicEagerWithObjectKeys = Object.keys(
{"./modules/a.ts": '',"./modules/b.ts": '',"./modules/index.ts": ''
}
);
export const basicEagerWithObjectValues = Object.values(
[__vite_glob_5_0,__vite_glob_5_1,__vite_glob_5_2
]
);
export const ignore = /* #__PURE__ */ Object.assign({"./modules/a.ts": () => import("./modules/a.ts"),"./modules/b.ts": () => import("./modules/b.ts")});
export const ignoreWithObjectKeys = Object.keys(
{"./modules/a.ts": '',"./modules/b.ts": ''}
);
export const namedEager = /* #__PURE__ */ Object.assign({"./modules/a.ts": __vite_glob_6_0,"./modules/b.ts": __vite_glob_6_1,"./modules/index.ts": __vite_glob_6_2
export const ignoreWithObjectValues = Object.values(
[() => import("./modules/a.ts"),() => import("./modules/b.ts")]
);
export const namedEager = /* #__PURE__ */ Object.assign({"./modules/a.ts": __vite_glob_9_0,"./modules/b.ts": __vite_glob_9_1,"./modules/index.ts": __vite_glob_9_2
});
Expand All @@ -101,6 +130,12 @@ export const namedEagerWithObjectKeys = Object.keys(
}
);
export const namedEagerWithObjectValues = Object.values(
[__vite_glob_11_0,__vite_glob_11_1,__vite_glob_11_2
]
);
export const namedDefault = /* #__PURE__ */ Object.assign({"./modules/a.ts": () => import("./modules/a.ts").then(m => m["default"]),"./modules/b.ts": () => import("./modules/b.ts").then(m => m["default"]),"./modules/index.ts": () => import("./modules/index.ts").then(m => m["default"])
});
Expand All @@ -109,7 +144,12 @@ export const namedDefaultWithObjectKeys = Object.keys(
}
);
export const eagerAs = /* #__PURE__ */ Object.assign({"./modules/a.ts": __vite_glob_10_0,"./modules/b.ts": __vite_glob_10_1
export const namedDefaultWithObjectValues = Object.values(
[() => import("./modules/a.ts").then(m => m["default"]),() => import("./modules/b.ts").then(m => m["default"]),() => import("./modules/index.ts").then(m => m["default"])
]
);
export const eagerAs = /* #__PURE__ */ Object.assign({"./modules/a.ts": __vite_glob_15_0,"./modules/b.ts": __vite_glob_15_1
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface ModuleType {
export const basic = import.meta.glob<ModuleType>('./modules/*.ts')
// prettier-ignore
export const basicWithObjectKeys = Object.keys(import.meta.glob<ModuleType>('./modules/*.ts'))
// prettier-ignore
export const basicWithObjectValues = Object.values(import.meta.glob<ModuleType>('./modules/*.ts'))

export const basicEager = import.meta.glob<ModuleType>('./modules/*.ts', {
eager: true,
Expand All @@ -16,11 +18,19 @@ export const basicEagerWithObjectKeys = Object.keys(
eager: true,
}),
)
export const basicEagerWithObjectValues = Object.values(
import.meta.glob<ModuleType>('./modules/*.ts', {
eager: true,
}),
)

export const ignore = import.meta.glob(['./modules/*.ts', '!**/index.ts'])
export const ignoreWithObjectKeys = Object.keys(
import.meta.glob(['./modules/*.ts', '!**/index.ts']),
)
export const ignoreWithObjectValues = Object.values(
import.meta.glob(['./modules/*.ts', '!**/index.ts']),
)

export const namedEager = import.meta.glob<string>('./modules/*.ts', {
eager: true,
Expand All @@ -32,6 +42,12 @@ export const namedEagerWithObjectKeys = Object.keys(
import: 'name',
}),
)
export const namedEagerWithObjectValues = Object.values(
import.meta.glob<string>('./modules/*.ts', {
eager: true,
import: 'name',
}),
)

export const namedDefault = import.meta.glob<string>('./modules/*.ts', {
import: 'default',
Expand All @@ -41,6 +57,11 @@ export const namedDefaultWithObjectKeys = Object.keys(
import: 'default',
}),
)
export const namedDefaultWithObjectValues = Object.values(
import.meta.glob<string>('./modules/*.ts', {
import: 'default',
}),
)

export const eagerAs = import.meta.glob<ModuleType>(
['./modules/*.ts', '!**/index.ts'],
Expand Down
37 changes: 29 additions & 8 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface ParsedImportGlob {
start: number
end: number
onlyKeys: boolean
onlyValues: boolean
}

interface ParsedGeneralImportGlobOptions extends GeneralImportGlobOptions {
Expand Down Expand Up @@ -109,6 +110,7 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin {

const importGlobRE = /\bimport\.meta\.glob(?:<\w+>)?\s*\(/g
const objectKeysRE = /\bObject\.keys\(\s*$/
const objectValuesRE = /\bObject\.values\(\s*$/

const knownOptions = {
as: ['string'],
Expand Down Expand Up @@ -308,7 +310,12 @@ export async function parseImportGlob(
globs.map((glob) => toAbsoluteGlob(glob, root, importer, resolveId)),
)
const isRelative = globs.every((i) => '.!'.includes(i[0]))
const onlyKeys = objectKeysRE.test(cleanCode.slice(0, start))
const sliceCode = cleanCode.slice(0, start)
const onlyKeys = objectKeysRE.test(sliceCode)
let onlyValues = false
if (!onlyKeys) {
onlyValues = objectValuesRE.test(sliceCode)
}

return {
index,
Expand All @@ -319,6 +326,7 @@ export async function parseImportGlob(
start,
end,
onlyKeys,
onlyValues,
}
})

Expand Down Expand Up @@ -402,6 +410,7 @@ export async function transformGlobImport(
start,
end,
onlyKeys,
onlyValues,
}) => {
const cwd = getCommonBase(globsResolved) ?? root
const files = (
Expand Down Expand Up @@ -475,13 +484,19 @@ export async function transformGlobImport(
staticImports.push(
`import ${expression} from ${JSON.stringify(importPath)}`,
)
objectProps.push(`${JSON.stringify(filePath)}: ${variableName}`)
objectProps.push(
onlyValues
? `${variableName}`
: `${JSON.stringify(filePath)}: ${variableName}`,
)
} else {
let importStatement = `import(${JSON.stringify(importPath)})`
if (importKey)
importStatement += `.then(m => m[${JSON.stringify(importKey)}])`
objectProps.push(
`${JSON.stringify(filePath)}: () => ${importStatement}`,
onlyValues
? `() => ${importStatement}`
: `${JSON.stringify(filePath)}: () => ${importStatement}`,
)
}
})
Expand All @@ -494,11 +509,17 @@ export async function transformGlobImport(
originalLineBreakCount > 0
? '\n'.repeat(originalLineBreakCount)
: ''
const replacement = onlyKeys
? `{${objectProps.join(',')}${lineBreaks}}`
: `/* #__PURE__ */ Object.assign({${objectProps.join(
',',
)}${lineBreaks}})`
let replacement = ''
if (onlyKeys) {
replacement = `{${objectProps.join(',')}${lineBreaks}}`
} else if (onlyValues) {
replacement = `[${objectProps.join(',')}${lineBreaks}]`
} else {
replacement = `/* #__PURE__ */ Object.assign({${objectProps.join(
',',
)}${lineBreaks}})`
}

s.overwrite(start, end, replacement)

return staticImports
Expand Down

0 comments on commit d869760

Please sign in to comment.