Skip to content

Commit 4bae1cc

Browse files
committed
feat: handle .mjs as type="module"
1 parent eaf5e5f commit 4bae1cc

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/renderer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createMapper, AsyncFileMapper } from './mapper'
2-
import { normalizeFile, isCSS, isJS, ensureTrailingSlash } from './utils'
2+
import { normalizeFile, isCSS, isJS, isModule, ensureTrailingSlash } from './utils'
33

44
export type ClientManifest = {
55
publicPath: string;
@@ -144,7 +144,7 @@ export function renderScripts (ssrContext: SSRContext, renderContext: RenderCont
144144
const async = (getUsedAsyncFiles(ssrContext, renderContext) || []).filter(({ file }) => isJS(file))
145145
const needed = [initial[0]].concat(async, initial.slice(1))
146146
return needed.map(({ file }) => {
147-
return `<script src="${renderContext.publicPath}${file}" defer></script>`
147+
return `<script${isModule(file) ? ' type="module" ' : ''}src="${renderContext.publicPath}${file}" defer></script>`
148148
}).join('')
149149
} else {
150150
return ''

src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { Resource } from './renderer'
22

33
const IS_JS_RE = /\.[cm]?js(\?[^.]+)?$/
4+
const IS_MODULE_RE = /\.mjs(\?[^.]+)?$/
45
const HAS_EXT_RE = /[^./]+\.[^./]+$/
56
const IS_CSS_RE = /\.css(\?[^.]+)?$/
67

78
export function isJS (file: string) {
89
return IS_JS_RE.test(file) || !HAS_EXT_RE.test(file)
910
}
1011

12+
export function isModule (file: string) {
13+
return IS_MODULE_RE.test(file) || !HAS_EXT_RE.test(file)
14+
}
15+
1116
export function isCSS (file: string) {
1217
return IS_CSS_RE.test(file)
1318
}

0 commit comments

Comments
 (0)