diff --git a/packages/taro-helper/src/__tests__/get-merge-loader-template-reg.spec.ts b/packages/taro-helper/src/__tests__/get-merge-loader-template-reg.spec.ts new file mode 100644 index 000000000000..b01e6400f42a --- /dev/null +++ b/packages/taro-helper/src/__tests__/get-merge-loader-template-reg.spec.ts @@ -0,0 +1,10 @@ +import { getMergeLoaderTemplateReg } from '../utils' + +describe('getMergeLoaderTemplateReg', () => { + it('should return match current template RegExp', () => { + const input1 = '.xhsml' // 小红书小程序模版后缀 + const input2 = '.ksml' // 快手小程序模版后缀 + expect(getMergeLoaderTemplateReg(input1)).toEqual(/\.(wxml|axml|ttml|qml|swan|jxml|xhsml)(\?.*)?$/) + expect(getMergeLoaderTemplateReg(input2)).toEqual(/\.(wxml|axml|ttml|qml|swan|jxml|ksml)(\?.*)?$/) + }) +}) \ No newline at end of file diff --git a/packages/taro-helper/src/constants.ts b/packages/taro-helper/src/constants.ts index acdd7597e3d3..be6ffba6989a 100644 --- a/packages/taro-helper/src/constants.ts +++ b/packages/taro-helper/src/constants.ts @@ -79,6 +79,7 @@ export const processTypeMap: IProcessTypeMap = { } export const CSS_EXT: string[] = ['.css', '.scss', '.sass', '.less', '.styl', '.stylus', '.wxss', '.acss'] +export const DEFAULT_SUPPORT_TEMPLATE: string[] = ['wxml', 'axml', 'ttml', 'qml', 'swan', 'jxml'] export const SCSS_EXT: string[] = ['.scss'] export const JS_EXT: string[] = ['.js', '.jsx'] export const TS_EXT: string[] = ['.ts', '.tsx'] @@ -103,7 +104,6 @@ export const REG_IMAGE = /\.(png|jpe?g|gif|bpm|svg|webp)(\?.*)?$/ export const REG_FONT = /\.(woff2?|eot|ttf|otf)(\?.*)?$/ export const REG_JSON = /\.json(\?.*)?$/ export const REG_UX = /\.ux(\?.*)?$/ -export const REG_TEMPLATE = /\.(wxml|axml|ttml|qml|swan|jxml)(\?.*)?$/ export const REG_WXML_IMPORT = / (configPath: string, op return result } +export function getMergeLoaderTemplateReg (templ: string) { + const tmepls = [...DEFAULT_SUPPORT_TEMPLATE, templ ? templ?.substring(1) : ''] + return new RegExp(`\\.(${tmepls.join('|')})(\\?.*)?$`) +} + export { fs } diff --git a/packages/taro-mini-runner/src/webpack/chain.ts b/packages/taro-mini-runner/src/webpack/chain.ts index 80b067c1dc08..5a38d78df326 100644 --- a/packages/taro-mini-runner/src/webpack/chain.ts +++ b/packages/taro-mini-runner/src/webpack/chain.ts @@ -1,6 +1,7 @@ import { chalk, fs, + getMergeLoaderTemplateReg, isNodeModule, recursiveMerge, REG_CSS, @@ -13,7 +14,6 @@ import { REG_SCRIPTS, REG_STYLE, REG_STYLUS, - REG_TEMPLATE, resolveMainFilePath, SCRIPT_EXT } from '@tarojs/helper' @@ -427,7 +427,7 @@ export const getModule = (appPath: string, { }, script: scriptRule, template: { - test: REG_TEMPLATE, + test: getMergeLoaderTemplateReg(fileType.templ), use: [getFileLoader([{ useRelativePath: true, name: (resourcePath: string) => { diff --git a/packages/taro-webpack5-runner/src/webpack/MiniWebpackModule.ts b/packages/taro-webpack5-runner/src/webpack/MiniWebpackModule.ts index 9e9747175b30..4d5e3e40df0c 100644 --- a/packages/taro-webpack5-runner/src/webpack/MiniWebpackModule.ts +++ b/packages/taro-webpack5-runner/src/webpack/MiniWebpackModule.ts @@ -1,12 +1,12 @@ import { + getMergeLoaderTemplateReg, isNodeModule, recursiveMerge, REG_CSS, REG_LESS, REG_SASS_SASS, REG_SASS_SCSS, - REG_STYLUS, - REG_TEMPLATE + REG_STYLUS } from '@tarojs/helper' import { cloneDeep } from 'lodash' import path from 'path' @@ -88,7 +88,7 @@ export class MiniWebpackModule { script: this.getScriptRule(), template: { - test: REG_TEMPLATE, + test: getMergeLoaderTemplateReg(fileType.templ), type: 'asset/resource', generator: { filename ({ filename }) {