Skip to content

Commit

Permalink
feat: build file name optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Dec 8, 2023
1 parent fb61e33 commit 8e96295
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ const legacyEnvVarMarker = `__VITE_IS_LEGACY__`

const $require = createRequire(import.meta.url)

const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/
const prefixedHashInFileNameRE = /\W?\[hash(:\d+)?\]/

function viteLegacyPlugin(options: Options = {}): Plugin[] {
let resolvedConfig: ResolvedConfig
let targets: Options['targets']
Expand Down Expand Up @@ -160,7 +163,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
)
}

const debugFlags = (process.env.DEBUG ?? '').split(',')
const debugFlags = (process.env.DEBUG || '').split(',')
const isDebug
= debugFlags.includes('vite:*') || debugFlags.includes('vite:legacy')

Expand Down Expand Up @@ -340,9 +343,9 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {

const getLegacyOutputFileName = (
fileNames:
| string
| ((chunkInfo: PreRenderedChunk) => string)
| undefined,
| string
| ((chunkInfo: PreRenderedChunk) => string)
| undefined,
defaultFileName = '[name]-legacy-[hash].js',
): string | ((chunkInfo: PreRenderedChunk) => string) => {
if (!fileNames) {
Expand All @@ -356,14 +359,16 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
if (fileName.includes('[name]')) {
// [name]-[hash].[format] -> [name]-legacy-[hash].[format]
fileName = fileName.replace('[name]', '[name]-legacy')
} else if (fileName.includes('[hash]')) {
} else if (nonLeadingHashInFileNameRE.test(fileName)) {
// custom[hash].[format] -> [name]-legacy[hash].[format]
// custom-[hash].[format] -> [name]-legacy-[hash].[format]
// custom.[hash].[format] -> [name]-legacy.[hash].[format]
fileName = fileName.replace(/[.-]?\[hash\]/, '-legacy$&')
// custom.[hash:10].[format] -> custom-legacy.[hash:10].[format]
fileName = fileName.replace(prefixedHashInFileNameRE, '-legacy$&')
} else {
// entry.js -> entry-legacy.js
fileName = fileName.replace(/(.+)\.(.+)/, '$1-legacy.$2')
// entry.min.js -> entry-legacy.min.js
fileName = fileName.replace(/(.+?)\.(.+)/, '$1-legacy.$2')
}

return fileName
Expand Down Expand Up @@ -692,7 +697,7 @@ export async function detectPolyfills(
const result = await swc.transform(code, {
swcrc: false,
configFile: false,
env: createSwcEnvOptions(targets),
env: createSwcEnvOptions(targets, {}),
})
const ast = await swc.parse(result.code)
for (const node of ast.body) {
Expand All @@ -710,7 +715,7 @@ export async function detectPolyfills(

function createSwcEnvOptions(
targets: any,
{ needPolyfills = true }: { needPolyfills?: boolean } = {},
{ needPolyfills = true }: { needPolyfills?: boolean },
): EnvConfig {
return {
targets,
Expand Down

0 comments on commit 8e96295

Please sign in to comment.