Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: explicit the word boundary #6876

Merged
merged 6 commits into from Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/playground/assets/index.html
Expand Up @@ -176,9 +176,24 @@ <h2>new URL(`non-existent`, import.meta.url)</h2>

<h2>simple script tag import-expression</h2>
<code class="import-expression"></code>
<code class="obj-import-express"></code>
<code class="string-import-express"></code>
<script>
const obj = {
import(t) {
text('.obj-import-express', t)
}
}
const stringImport = "import('package')"
function text(el, text) {
document.querySelector(el).textContent = text
}
import('./static/import-expression.js')
import('/import-expression.js')
// import('./static/raw.js')
/* import('./static/raw.js') */
obj.import('ignore object import prop')
text('.string-import-express', t)
</script>
<h2>url in style tag</h2>
<h3>url</h3>
Expand Down
20 changes: 17 additions & 3 deletions packages/vite/src/node/plugins/html.ts
Expand Up @@ -13,8 +13,10 @@ import {
generateCodeFrame,
isDataUrl,
isExternalUrl,
multilineCommentsRE,
normalizePath,
processSrcSet,
singlelineCommentsRE,
slash
} from '../utils'
import type { ResolvedConfig } from '../config'
Expand Down Expand Up @@ -44,8 +46,9 @@ interface ScriptAssetsUrl {

const htmlProxyRE = /\?html-proxy=?[&inline\-css]*&index=(\d+)\.(js|css)$/
const inlineCSSRE = /__VITE_INLINE_CSS__([^_]+_\d+)__/g
// Do not allow preceding '.', but do allow preceding '...' for spread operations
const inlineImportRE = /(?<!(?<!\.\.)\.)\bimport\s*\(("[^"]*"|'[^']*')\)/g
const htmlLangRE = /\.(html|htm)$/
const inlineImportRE = /\bimport\s*\(("[^"]*"|'[^']*')\)/g

export const isHTMLProxy = (id: string): boolean => htmlProxyRE.test(id)

Expand Down Expand Up @@ -304,13 +307,24 @@ 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)}'`
)

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