Skip to content

Commit

Permalink
chore: use empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed Apr 12, 2022
1 parent 9d66c1e commit f2dd1cb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
7 changes: 7 additions & 0 deletions packages/playground/assets/__tests__/assets.spec.ts
Expand Up @@ -307,3 +307,10 @@ if (!isBuild) {
await untilUpdated(() => getColor('.import-css'), 'rgb(0, 255, 136)')
})
}

test('html import word boundary', async () => {
expect(await page.textContent('.obj-import-express')).toMatch(
'ignore object import prop'
)
expect(await page.textContent('.string-import-express')).toMatch('no load')
})
8 changes: 6 additions & 2 deletions packages/playground/assets/index.html
Expand Up @@ -184,7 +184,7 @@ <h2>simple script tag import-expression</h2>
text('.obj-import-express', t)
}
}
const stringImport = "import('package')"
const stringImport = "const t = import('package')"
function text(el, text) {
document.querySelector(el).textContent = text
}
Expand All @@ -193,7 +193,11 @@ <h2>simple script tag import-expression</h2>
// import('./static/raw.js')
/* import('./static/raw.js') */
obj.import('ignore object import prop')
text('.string-import-express', t)
try {
text('.string-import-express', t)
} catch {
text('.string-import-express', 'no load')
}
</script>
<h2>url in style tag</h2>
<h3>url</h3>
Expand Down
19 changes: 7 additions & 12 deletions packages/vite/src/node/plugins/html.ts
Expand Up @@ -37,6 +37,7 @@ import type {
TextNode
} from '@vue/compiler-dom'
import { NodeTypes } from '@vue/compiler-dom'
import { emptyString } from '../cleanString'

interface ScriptAssetsUrl {
start: number
Expand Down Expand Up @@ -306,24 +307,18 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
}
} else if (node.children.length) {
const scriptNode = node.children.pop()! as TextNode
const code = scriptNode.content
.replace(multilineCommentsRE, (m) => ' '.repeat(m.length))
.replace(singlelineCommentsRE, (m) => ' '.repeat(m.length))
.replace(
/"[^"]*"|'[^']*'|`[^`]*`/g,
(m) => `'${' '.repeat(m.length - 2)}'`
)
const cleanCode = emptyString(scriptNode.content)

let match: RegExpExecArray | null
while ((match = inlineImportRE.exec(code))) {
const { 0: full, 1: url, index } = match
const startUrl = full.indexOf(url)
const start = index + startUrl + 1
while ((match = inlineImportRE.exec(cleanCode))) {
const { 1: url, index } = match
const startUrl = cleanCode.indexOf(url, index)
const start = startUrl + 1
const end = start + url.length - 2
scriptUrls.push({
start: start + scriptNode.loc.start.offset,
end: end + scriptNode.loc.start.offset,
url: scriptNode.content.slice(index + startUrl + 1, end)
url: scriptNode.content.slice(start, end)
})
}
}
Expand Down

0 comments on commit f2dd1cb

Please sign in to comment.