mirror of
https://github.com/alex8088/electron-vite.git
synced 2025-11-10 06:24:33 +08:00
refactor(modulePath): better support for tree-shaking and code-splitting
This commit is contained in:
parent
327adc23df
commit
28bb22b353
@ -150,7 +150,14 @@ export async function resolveConfig(
|
|||||||
...electronMainVitePlugin({ root }),
|
...electronMainVitePlugin({ root }),
|
||||||
assetPlugin(),
|
assetPlugin(),
|
||||||
workerPlugin(),
|
workerPlugin(),
|
||||||
modulePathPlugin(),
|
modulePathPlugin(
|
||||||
|
mergeConfig(
|
||||||
|
{
|
||||||
|
plugins: [electronMainVitePlugin({ root })[0], assetPlugin(), importMetaPlugin(), esmShimPlugin()]
|
||||||
|
},
|
||||||
|
mainViteConfig
|
||||||
|
)
|
||||||
|
),
|
||||||
importMetaPlugin(),
|
importMetaPlugin(),
|
||||||
esmShimPlugin()
|
esmShimPlugin()
|
||||||
])
|
])
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import type { Plugin } from 'vite'
|
import path from 'node:path'
|
||||||
import type { SourceMapInput } from 'rollup'
|
import { type Plugin, type InlineConfig, build as viteBuild, mergeConfig } from 'vite'
|
||||||
|
import type { SourceMapInput, RollupOutput, OutputOptions } from 'rollup'
|
||||||
import MagicString from 'magic-string'
|
import MagicString from 'magic-string'
|
||||||
import { cleanUrl, parseRequest, toRelativePath } from '../utils'
|
import { cleanUrl, parseRequest, toRelativePath } from '../utils'
|
||||||
|
|
||||||
@ -8,7 +9,7 @@ const modulePathRE = /__VITE_MODULE_PATH__([\w$]+)__/g
|
|||||||
/**
|
/**
|
||||||
* Resolve `?modulePath` import and return the module bundle path.
|
* Resolve `?modulePath` import and return the module bundle path.
|
||||||
*/
|
*/
|
||||||
export default function modulePathPlugin(): Plugin {
|
export default function modulePathPlugin(config: InlineConfig): Plugin {
|
||||||
let sourcemap: boolean | 'inline' | 'hidden' = false
|
let sourcemap: boolean | 'inline' | 'hidden' = false
|
||||||
return {
|
return {
|
||||||
name: 'vite:module-path',
|
name: 'vite:module-path',
|
||||||
@ -23,14 +24,23 @@ export default function modulePathPlugin(): Plugin {
|
|||||||
return id + `&importer=${importer}`
|
return id + `&importer=${importer}`
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
load(id): string | void {
|
async load(id): Promise<string | void> {
|
||||||
const query = parseRequest(id)
|
const query = parseRequest(id)
|
||||||
if (query && typeof query.modulePath === 'string' && typeof query.importer === 'string') {
|
if (query && typeof query.modulePath === 'string' && typeof query.importer === 'string') {
|
||||||
const cleanPath = cleanUrl(id)
|
const entry = path.resolve(path.dirname(query.importer), cleanUrl(id))
|
||||||
|
const bundle = await bundleEntryFile(entry, config)
|
||||||
|
const [outputChunk, ...outputChunks] = bundle.output
|
||||||
const hash = this.emitFile({
|
const hash = this.emitFile({
|
||||||
type: 'chunk',
|
type: 'asset',
|
||||||
id: cleanPath,
|
fileName: outputChunk.fileName,
|
||||||
importer: query.importer
|
source: outputChunk.code
|
||||||
|
})
|
||||||
|
outputChunks.forEach(chunk => {
|
||||||
|
this.emitFile({
|
||||||
|
type: 'asset',
|
||||||
|
fileName: chunk.fileName,
|
||||||
|
source: chunk.type === 'chunk' ? chunk.code : chunk.source
|
||||||
|
})
|
||||||
})
|
})
|
||||||
const refId = `__VITE_MODULE_PATH__${hash}__`
|
const refId = `__VITE_MODULE_PATH__${hash}__`
|
||||||
return `
|
return `
|
||||||
@ -63,3 +73,28 @@ export default function modulePathPlugin(): Plugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function bundleEntryFile(input: string, config: InlineConfig): Promise<RollupOutput> {
|
||||||
|
const viteConfig = mergeConfig(config, {
|
||||||
|
build: {
|
||||||
|
rollupOptions: { input },
|
||||||
|
write: false,
|
||||||
|
watch: false
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
{
|
||||||
|
name: 'vite:entry-file-name',
|
||||||
|
outputOptions(output): OutputOptions {
|
||||||
|
if (typeof output.entryFileNames !== 'function' && output.entryFileNames) {
|
||||||
|
output.entryFileNames = '[name]-[hash]' + path.extname(output.entryFileNames)
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
logLevel: 'warn',
|
||||||
|
configFile: false
|
||||||
|
})
|
||||||
|
const bundles = await viteBuild(viteConfig)
|
||||||
|
return bundles as RollupOutput
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user