提交 459c0452 编写于 作者: fxy060608's avatar fxy060608

feat(mp): support dynamic import assets (#3376)

上级 bbc2e29f
import path from 'path'
const EXTNAMES = [
'.png',
'.jpg',
'.jpeg',
'.gif',
'.svg',
'.json',
'.cer',
'.mp3',
'.aac',
'.m4a',
'.mp4',
'.wav',
'.ogg',
'.silk',
'.wasm',
'.br',
'.cert',
]
export function isMiniProgramAssetFile(filename: string) {
if (!path.isAbsolute(filename)) {
return false
}
return EXTNAMES.includes(path.extname(filename))
}
......@@ -3,6 +3,7 @@ export * from './wxs'
export * from './nvue'
export * from './event'
export * from './style'
export * from './assets'
export * from './template'
export * from './constants'
......
......@@ -14,6 +14,7 @@ import {
isCSSRequest,
parseManifestJsonOnce,
M,
isMiniProgramAssetFile,
dynamicImportPolyfill,
} from '@dcloudio/uni-cli-shared'
import { GetManualChunk, GetModuleInfo, PreRenderedChunk } from 'rollup'
......@@ -41,6 +42,7 @@ export function createBuildOptions(
inputDir: string,
platform: UniApp.PLATFORM
): BuildOptions {
const { renderDynamicImport } = dynamicImportPolyfill()
return {
// sourcemap: 'inline', // TODO
// target: ['chrome53'], // 由小程序自己启用 es6 编译
......@@ -64,7 +66,21 @@ export function createBuildOptions(
manualChunks: createMoveToVendorChunkFn(),
chunkFileNames: createChunkFileNames(inputDir),
assetFileNames: '[name][extname]',
plugins: [dynamicImportPolyfill()],
plugins: [
{
name: 'dynamic-import-polyfill',
renderDynamicImport(options) {
const { targetModuleId } = options
if (targetModuleId && isMiniProgramAssetFile(targetModuleId)) {
return {
left: 'Promise.resolve(require(',
right: '))',
}
}
return renderDynamicImport!.call(this, options)
},
},
],
},
},
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册