Skip to content

Commit

Permalink
fix(build): correct url encoded file names
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ksem committed Mar 8, 2024
1 parent 509fb7b commit 20391db
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/ui/build/common-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { resolve as resolver } from 'path'
import { chunkSplitPlugin } from 'vite-plugin-chunk-split'
import { appendComponentCss } from './plugins/append-component-css'
import { removeSideEffectedChunks } from './plugins/remove-side-effected-chunks'
import { defineVitePlugin } from './types/define-vite-plugin'
import { fixVueGenericComponentFileNames } from './plugins/fix-vue-generic-component-file-names'
import { defineViteConfig } from './types/define-vite-config'

import type { RollupOptions } from 'rollup'

Expand Down Expand Up @@ -65,7 +66,7 @@ export default function createViteConfig (format: BuildFormat) {
const isEsm = ['es', 'esm-node'].includes(format)
const isNode = format === 'esm-node'

const config = defineVitePlugin({
const config = defineViteConfig({
resolve,

build: {
Expand Down Expand Up @@ -97,6 +98,7 @@ export default function createViteConfig (format: BuildFormat) {
isEsm && config.plugins.push(chunkSplitPlugin({ strategy: 'unbundle' }))
isEsm && !isNode && config.plugins.push(appendComponentCss())
isEsm && config.plugins.push(removeSideEffectedChunks())
isEsm && config.plugins.push(fixVueGenericComponentFileNames)
config.plugins.push(componentVBindFix())

if (isNode) {
Expand Down
53 changes: 53 additions & 0 deletions packages/ui/build/plugins/fix-vue-generic-component-file-names.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { defineVitePlugin } from './../types/define-vite-plugin'

/**
* When vue compiles file, it encode generic into file name
*
* @example
* VaDataTable.vue_vue_type_script_generic_Item%20extends%20Record%3Cstring%2C%20any%3E_setup_true_lang
*
* This might be helpful for compiler, but it makes file names unreadable and some bundlers may not allow encoded characters in file names.
* This plugin replaces encoded characters in file names and source maps with underscores.
*/
const GENERIC_NAME_REGEX = /.vue_vue_type_script_generic_.*_setup_true_lang/gm
const URL_ENCODED_REGEX = /%([0-9]|[A-F]){2}/gm

const replaceEncodedCharacters = (match: string) => match
.replace(URL_ENCODED_REGEX, '_') // Replace any encoded character with underscore
.replace(/_{2,}/g, '_') // Replace multiple underscores with single underscore

export const fixVueGenericComponentFileNames = defineVitePlugin({
name: 'vuestic:fix-vue-generic-component-file-names',

generateBundle (options, bundle) {
Object.keys(bundle).forEach(fileName => {
const file = bundle[fileName]

// Replace encoded characters in generic names in source maps
if (file.type === 'asset' && file.fileName.endsWith('.map')) {
if (typeof file.source === 'string') {
file.source = file.source
.replace(GENERIC_NAME_REGEX, replaceEncodedCharacters)
}
}

// Replace encoded characters in generic names in code
if (file.type === 'chunk') {
file.code = file.code
.replace(GENERIC_NAME_REGEX, replaceEncodedCharacters)
}

// Update file name if it has encoded characters
if (GENERIC_NAME_REGEX.test(fileName)) {
const newFileName = replaceEncodedCharacters(fileName)

bundle[newFileName] = {
...bundle[fileName],
fileName: newFileName,
}

delete bundle[fileName]
}
})
},
})
3 changes: 3 additions & 0 deletions packages/ui/build/types/define-vite-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { UserConfig, Plugin } from 'vite'

export const defineViteConfig = <T extends UserConfig>(p: T): UserConfig & T => p
4 changes: 2 additions & 2 deletions packages/ui/build/types/define-vite-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { UserConfig, Plugin } from 'vite'
import { Plugin } from 'vite'

export const defineVitePlugin = <T extends UserConfig>(p: T): UserConfig & T => p
export const defineVitePlugin = <T extends Plugin>(p: T): Plugin & T => p

0 comments on commit 20391db

Please sign in to comment.