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

fix(html): build mode ignore html define transform #6663

Merged
merged 8 commits into from Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions packages/playground/define/__tests__/define.spec.ts
Expand Up @@ -20,4 +20,6 @@ test('string', async () => {
expect(await page.textContent('.spread-array')).toBe(
JSON.stringify([...defines.__STRING__])
)
// html would't need to define replacement
expect(await page.textContent('.exp-define')).toBe('__EXP__')
poyoho marked this conversation as resolved.
Show resolved Hide resolved
})
1 change: 1 addition & 0 deletions packages/playground/define/index.html
Expand Up @@ -9,6 +9,7 @@ <h1>Define</h1>
<p>process as property: <code class="process-as-property"></code></p>
<p>spread object: <code class="spread-object"></code></p>
<p>spread array: <code class="spread-array"></code></p>
<p>define variable in html: <code class="exp-define">__EXP__</code></p>

<script type="module">
const __VAR_NAME__ = true // ensure define doesn't replace var name
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/plugins/define.ts
Expand Up @@ -3,6 +3,7 @@ import type { TransformResult } from 'rollup'
import type { ResolvedConfig } from '../config'
import type { Plugin } from '../plugin'
import { isCSSRequest } from './css'
import { isHTMLRequest } from './html'

export function definePlugin(config: ResolvedConfig): Plugin {
const isBuild = config.command === 'build'
Expand Down Expand Up @@ -80,6 +81,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {

return {
name: 'vite:define',

transform(code, id, options) {
const ssr = options?.ssr === true
if (!ssr && !isBuild) {
Expand All @@ -89,7 +91,8 @@ export function definePlugin(config: ResolvedConfig): Plugin {
}

if (
// exclude css and static assets for performance
// exclude html, css and static assets for performance
isHTMLRequest(id) ||
poyoho marked this conversation as resolved.
Show resolved Hide resolved
isCSSRequest(id) ||
config.assetsInclude(id)
) {
Expand Down
5 changes: 5 additions & 0 deletions packages/vite/src/node/plugins/html.ts
Expand Up @@ -43,9 +43,14 @@ interface ScriptAssetsUrl {

const htmlProxyRE = /\?html-proxy[&inline\-css]*&index=(\d+)\.(js|css)$/
const inlineCSSRE = /__VITE_INLINE_CSS__([^_]+_\d+)__/g
const htmlLangRE = /.(html|htm)$/
poyoho marked this conversation as resolved.
Show resolved Hide resolved
const inlineImportRE = /\bimport\s*\(("[^"]*"|'[^']*')\)/g

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

export const isHTMLRequest = (request: string): boolean =>
htmlLangRE.test(request)

// HTML Proxy Caches are stored by config -> filePath -> index
export const htmlProxyMap = new WeakMap<
ResolvedConfig,
Expand Down