Skip to content

Commit 1ffc010

Browse files
authored
fix: use strip-literal to strip string lterals (#8054)
1 parent fe704f1 commit 1ffc010

File tree

10 files changed

+49
-152
lines changed

10 files changed

+49
-152
lines changed

packages/vite/LICENSE.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3465,6 +3465,35 @@ Repository: chalk/strip-ansi
34653465
34663466
---------------------------------------
34673467

3468+
## strip-literal
3469+
License: MIT
3470+
By: Anthony Fu
3471+
Repository: git+https://github.com/antfu/strip-literal.git
3472+
3473+
> MIT License
3474+
>
3475+
> Copyright (c) 2022 Anthony Fu <https://github.com/antfu>
3476+
>
3477+
> Permission is hereby granted, free of charge, to any person obtaining a copy
3478+
> of this software and associated documentation files (the "Software"), to deal
3479+
> in the Software without restriction, including without limitation the rights
3480+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
3481+
> copies of the Software, and to permit persons to whom the Software is
3482+
> furnished to do so, subject to the following conditions:
3483+
>
3484+
> The above copyright notice and this permission notice shall be included in all
3485+
> copies or substantial portions of the Software.
3486+
>
3487+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3488+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3489+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3490+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3491+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3492+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3493+
> SOFTWARE.
3494+
3495+
---------------------------------------
3496+
34683497
## to-regex-range
34693498
License: MIT
34703499
By: Jon Schlinkert, Rouven Weßling

packages/vite/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"source-map-js": "^1.0.2",
113113
"source-map-support": "^0.5.21",
114114
"strip-ansi": "^6.0.1",
115+
"strip-literal": "^0.2.0",
115116
"terser": "^5.13.1",
116117
"tsconfck": "^1.2.2",
117118
"tslib": "^2.4.0",
Binary file not shown.

packages/vite/src/node/cleanString.ts

Lines changed: 0 additions & 145 deletions
This file was deleted.

packages/vite/src/node/plugins/assetImportMetaUrl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import MagicString from 'magic-string'
33
import path from 'path'
44
import { fileToUrl } from './asset'
55
import type { ResolvedConfig } from '../config'
6-
import { emptyString } from '../cleanString'
6+
import { stripLiteral } from 'strip-literal'
77

88
/**
99
* Convert `new URL('./foo.png', import.meta.url)` to its resolved built URL
@@ -27,7 +27,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
2727
let s: MagicString | undefined
2828
const assetImportMetaUrlRE =
2929
/\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*,?\s*\)/g
30-
const cleanString = emptyString(code)
30+
const cleanString = stripLiteral(code)
3131

3232
let match: RegExpExecArray | null
3333
while ((match = assetImportMetaUrlRE.exec(cleanString))) {

packages/vite/src/node/plugins/css.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { transform, formatMessages } from 'esbuild'
4949
import { addToHTMLProxyTransformResult } from './html'
5050
import { injectSourcesContent, getCodeWithSourcemap } from '../server/sourcemap'
5151
import type { RawSourceMap } from '@ampproject/remapping'
52-
import { emptyCssComments } from '../cleanString'
52+
import { emptyCssComments } from '../utils'
5353

5454
// const debug = createDebugger('vite:css')
5555

packages/vite/src/node/plugins/html.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import type {
3636
TextNode
3737
} from '@vue/compiler-dom'
3838
import { NodeTypes } from '@vue/compiler-dom'
39-
import { emptyString } from '../cleanString'
39+
import { stripLiteral } from 'strip-literal'
4040

4141
interface ScriptAssetsUrl {
4242
start: number
@@ -308,7 +308,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
308308
}
309309
} else if (node.children.length) {
310310
const scriptNode = node.children.pop()! as TextNode
311-
const cleanCode = emptyString(scriptNode.content)
311+
const cleanCode = stripLiteral(scriptNode.content)
312312

313313
let match: RegExpExecArray | null
314314
while ((match = inlineImportRE.exec(cleanCode))) {

packages/vite/src/node/plugins/workerImportMetaUrl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ENV_ENTRY, ENV_PUBLIC_PATH } from '../constants'
1010
import MagicString from 'magic-string'
1111
import type { ViteDevServer } from '..'
1212
import type { RollupError } from 'rollup'
13-
import { emptyString } from '../cleanString'
13+
import { stripLiteral } from 'strip-literal'
1414

1515
type WorkerType = 'classic' | 'module' | 'ignore'
1616
const ignoreFlagRE = /\/\*\s*@vite-ignore\s*\*\//
@@ -110,7 +110,7 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
110110
code.includes('new URL') &&
111111
code.includes(`import.meta.url`)
112112
) {
113-
const cleanString = emptyString(code)
113+
const cleanString = stripLiteral(code)
114114
const workerImportMetaUrlRE =
115115
/\bnew\s+(Worker|SharedWorker)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*\))/g
116116

packages/vite/src/node/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,3 +785,7 @@ function gracefulRename(
785785
if (cb) cb(er)
786786
})
787787
}
788+
789+
export function emptyCssComments(raw: string) {
790+
return raw.replace(multilineCommentsRE, (s) => ' '.repeat(s.length))
791+
}

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)