Skip to content

Commit

Permalink
fix(build): handle .mjs/.mts files as data / path loaders (#3058)
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd authored Oct 9, 2023
1 parent 1a461c0 commit 7991180
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion __tests__/e2e/data-loading/data.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Static Data

<script setup lang="ts">
import { data } from './basic.data.js'
import { data } from './basic.data.mjs'
import { data as contentData } from './contentLoader.data.js'
</script>

Expand Down
26 changes: 14 additions & 12 deletions src/node/plugins/dynamicRoutesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,21 @@ export async function resolveDynamicRoutes(
for (const route of routes) {
// locate corresponding route paths file
const fullPath = normalizePath(path.resolve(srcDir, route))
const jsPathsFile = fullPath.replace(/\.md$/, '.paths.js')
let pathsFile = jsPathsFile
if (!fs.existsSync(jsPathsFile)) {
pathsFile = fullPath.replace(/\.md$/, '.paths.ts')
if (!fs.existsSync(pathsFile)) {
console.warn(
c.yellow(
`Missing paths file for dynamic route ${route}: ` +
`a corresponding ${jsPathsFile} or ${pathsFile} is needed.`
)

const paths = ['js', 'ts', 'mjs', 'mts'].map((ext) =>
fullPath.replace(/\.md$/, `.paths.${ext}`)
)

const pathsFile = paths.find((p) => fs.existsSync(p))

if (pathsFile == null) {
console.warn(
c.yellow(
`Missing paths file for dynamic route ${route}: ` +
`a corresponding ${paths[0]} (or .ts/.mjs/.mts) file is needed.`
)
continue
}
)
continue
}

// load the paths loader module
Expand Down
2 changes: 1 addition & 1 deletion src/node/plugins/staticDataPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import path, { dirname, resolve } from 'path'
import { isMatch } from 'micromatch'
import glob from 'fast-glob'

const loaderMatch = /\.data\.(j|t)s($|\?)/
const loaderMatch = /\.data\.m?(j|t)s($|\?)/

let server: ViteDevServer

Expand Down

0 comments on commit 7991180

Please sign in to comment.