From be181371a4c1f86a4aa389f44c317ce4bca6bc54 Mon Sep 17 00:00:00 2001 From: wuhailong Date: Tue, 2 Sep 2025 10:32:44 +0800 Subject: [PATCH] autoLazy supports blacklist and whitelist Issue: #ICX459 Signed-off-by: wuhailong Change-Id: I58ba38b7b1420d6cb888f821875dde46139cae66 --- .../src/fast_build/ark_compiler/transform.ts | 10 +- .../ets_ui/rollup-plugin-ets-typescript.ts | 3 +- .../src/fast_build/ark_compiler/transform.ts | 10 +- .../ets_ui/rollup-plugin-ets-typescript.ts | 3 +- .../src/interop/src/process_kit_import.ts | 6 +- .../src/interop/src/process_lazy_import.ts | 27 ++- compiler/src/process_kit_import.ts | 6 +- compiler/src/process_lazy_import.ts | 27 ++- .../common/process_lazy_import.test.ts | 155 +++++++++++++++++- 9 files changed, 218 insertions(+), 29 deletions(-) diff --git a/compiler/src/fast_build/ark_compiler/transform.ts b/compiler/src/fast_build/ark_compiler/transform.ts index a40b85c7a..ce971a448 100644 --- a/compiler/src/fast_build/ark_compiler/transform.ts +++ b/compiler/src/fast_build/ark_compiler/transform.ts @@ -61,10 +61,12 @@ export function transformForModule(code: string, id: string): string { const eventTransformForModule = createAndStartEvent(hookEventFactory, 'transform for module'); const { autoLazyImport, - reExportCheckMode + reExportCheckMode, + autoLazyFilter }: LazyImportOptions = { autoLazyImport: this.share.projectConfig?.autoLazyImport ?? false, - reExportCheckMode: this.share.projectConfig?.reExportCheckMode ?? reExportNoCheckMode + reExportCheckMode: this.share.projectConfig?.reExportCheckMode ?? reExportNoCheckMode, + autoLazyFilter: this.share.projectConfig?.autoLazyFilter ?? {} }; if (this.share.projectConfig.compileMode === ESMODULE) { const metaInfo: Object = this.getModuleInfo(id).meta; @@ -72,7 +74,7 @@ export function transformForModule(code: string, id: string): string { if (isTsOrEtsSourceFile(id) && shouldETSOrTSFileTransformToJS(id, projectConfig, metaInfo)) { preserveSourceMap(id, this.getCombinedSourcemap(), projectConfig, metaInfo, eventTransformForModule); // when ets/ts -> js, we need to convert lazy-import based on the js code generated after tsc conversion - code = processJsCodeLazyImport(id, code, autoLazyImport, reExportCheckMode); + code = processJsCodeLazyImport(id, code, autoLazyImport, reExportCheckMode, metaInfo, autoLazyFilter); const newSourceFileRecordInfo = MemoryMonitor.recordStage(MemoryDefine.MODULE_SOURCE_FILE_NEW_SOURCE_FILE); ModuleSourceFile.newSourceFile(id, code, metaInfo, projectConfig.singleFileEmit); MemoryMonitor.stopRecordStage(newSourceFileRecordInfo); @@ -81,7 +83,7 @@ export function transformForModule(code: string, id: string): string { if (isJsSourceFile(id) || isJsonSourceFile(id)) { let code: string = this.getModuleInfo(id).originalCode; if (isJsSourceFile(id)) { - code = processJsCodeLazyImport(id, code, autoLazyImport, reExportCheckMode); + code = processJsCodeLazyImport(id, code, autoLazyImport, reExportCheckMode, metaInfo, autoLazyFilter); if (projectConfig.compatibleSdkVersion <= 10) { const transformedResult: object = transformJsByBabelPlugin(code, eventTransformForModule); code = transformedResult.code; diff --git a/compiler/src/fast_build/ets_ui/rollup-plugin-ets-typescript.ts b/compiler/src/fast_build/ets_ui/rollup-plugin-ets-typescript.ts index 3a4e9f0cd..b81e6dd3b 100644 --- a/compiler/src/fast_build/ets_ui/rollup-plugin-ets-typescript.ts +++ b/compiler/src/fast_build/ets_ui/rollup-plugin-ets-typescript.ts @@ -516,7 +516,8 @@ async function transform(code: string, id: string) { const metaInfo: Object = this.getModuleInfo(id).meta; const lazyImportOptions: LazyImportOptions = { autoLazyImport: this.share.projectConfig?.autoLazyImport ?? false, - reExportCheckMode: this.share.projectConfig?.reExportCheckMode ?? reExportNoCheckMode + reExportCheckMode: this.share.projectConfig?.reExportCheckMode ?? reExportNoCheckMode, + autoLazyFilter: this.share.projectConfig?.autoLazyFilter ?? {} }; // use `try finally` to restore `noEmit` when error thrown by `processUISyntax` in preview mode const eventShouldEmitJs = createAndStartEvent(eventEtsTransformForEsmodule, 'shouldEmitJs'); diff --git a/compiler/src/interop/src/fast_build/ark_compiler/transform.ts b/compiler/src/interop/src/fast_build/ark_compiler/transform.ts index a40b85c7a..ce971a448 100644 --- a/compiler/src/interop/src/fast_build/ark_compiler/transform.ts +++ b/compiler/src/interop/src/fast_build/ark_compiler/transform.ts @@ -61,10 +61,12 @@ export function transformForModule(code: string, id: string): string { const eventTransformForModule = createAndStartEvent(hookEventFactory, 'transform for module'); const { autoLazyImport, - reExportCheckMode + reExportCheckMode, + autoLazyFilter }: LazyImportOptions = { autoLazyImport: this.share.projectConfig?.autoLazyImport ?? false, - reExportCheckMode: this.share.projectConfig?.reExportCheckMode ?? reExportNoCheckMode + reExportCheckMode: this.share.projectConfig?.reExportCheckMode ?? reExportNoCheckMode, + autoLazyFilter: this.share.projectConfig?.autoLazyFilter ?? {} }; if (this.share.projectConfig.compileMode === ESMODULE) { const metaInfo: Object = this.getModuleInfo(id).meta; @@ -72,7 +74,7 @@ export function transformForModule(code: string, id: string): string { if (isTsOrEtsSourceFile(id) && shouldETSOrTSFileTransformToJS(id, projectConfig, metaInfo)) { preserveSourceMap(id, this.getCombinedSourcemap(), projectConfig, metaInfo, eventTransformForModule); // when ets/ts -> js, we need to convert lazy-import based on the js code generated after tsc conversion - code = processJsCodeLazyImport(id, code, autoLazyImport, reExportCheckMode); + code = processJsCodeLazyImport(id, code, autoLazyImport, reExportCheckMode, metaInfo, autoLazyFilter); const newSourceFileRecordInfo = MemoryMonitor.recordStage(MemoryDefine.MODULE_SOURCE_FILE_NEW_SOURCE_FILE); ModuleSourceFile.newSourceFile(id, code, metaInfo, projectConfig.singleFileEmit); MemoryMonitor.stopRecordStage(newSourceFileRecordInfo); @@ -81,7 +83,7 @@ export function transformForModule(code: string, id: string): string { if (isJsSourceFile(id) || isJsonSourceFile(id)) { let code: string = this.getModuleInfo(id).originalCode; if (isJsSourceFile(id)) { - code = processJsCodeLazyImport(id, code, autoLazyImport, reExportCheckMode); + code = processJsCodeLazyImport(id, code, autoLazyImport, reExportCheckMode, metaInfo, autoLazyFilter); if (projectConfig.compatibleSdkVersion <= 10) { const transformedResult: object = transformJsByBabelPlugin(code, eventTransformForModule); code = transformedResult.code; diff --git a/compiler/src/interop/src/fast_build/ets_ui/rollup-plugin-ets-typescript.ts b/compiler/src/interop/src/fast_build/ets_ui/rollup-plugin-ets-typescript.ts index d052b0cc9..5d626f879 100644 --- a/compiler/src/interop/src/fast_build/ets_ui/rollup-plugin-ets-typescript.ts +++ b/compiler/src/interop/src/fast_build/ets_ui/rollup-plugin-ets-typescript.ts @@ -482,7 +482,8 @@ async function transform(code: string, id: string) { const metaInfo: Object = this.getModuleInfo(id).meta; const lazyImportOptions: LazyImportOptions = { autoLazyImport: this.share.projectConfig?.autoLazyImport ?? false, - reExportCheckMode: this.share.projectConfig?.reExportCheckMode ?? reExportNoCheckMode + reExportCheckMode: this.share.projectConfig?.reExportCheckMode ?? reExportNoCheckMode, + autoLazyFilter: this.share.projectConfig?.autoLazyFilter ?? {} }; const mixCompile: boolean = this.share.projectConfig?.mixCompile ?? false; // use `try finally` to restore `noEmit` when error thrown by `processUISyntax` in preview mode diff --git a/compiler/src/interop/src/process_kit_import.ts b/compiler/src/interop/src/process_kit_import.ts index f5c445e0f..985e7fb2e 100644 --- a/compiler/src/interop/src/process_kit_import.ts +++ b/compiler/src/interop/src/process_kit_import.ts @@ -77,8 +77,8 @@ const KEEPTS = '// @keepTs'; */ export function processKitImport(id: string, metaInfo: Object, parentEvent: CompileEvent, shouldReturnOriginalNode: boolean = true, - lazyImportOptions: LazyImportOptions = { autoLazyImport: false, reExportCheckMode: reExportNoCheckMode }): Function { - const { autoLazyImport, reExportCheckMode } = lazyImportOptions; + lazyImportOptions: LazyImportOptions = { autoLazyImport: false, reExportCheckMode: reExportNoCheckMode, autoLazyFilter: {} }): Function { + const { autoLazyImport, reExportCheckMode, autoLazyFilter } = lazyImportOptions; return (context: ts.TransformationContext) => { const visitor: ts.Visitor = node => { // only transform static import/export declaration @@ -143,7 +143,7 @@ export function processKitImport(id: string, metaInfo: Object, parentEvent: Comp // eg. import { xxx } form "xxx" --> import lazy { xxx } form "xxx" let processedNode: ts.SourceFile = ts.visitEachChild(ts.getTypeExportImportAndConstEnumTransformer(context)(node), visitor, context); - processedNode = (autoLazyImport ? transformLazyImport(processedNode, resolver) : processedNode); + processedNode = (autoLazyImport ? transformLazyImport(metaInfo, processedNode, autoLazyFilter, resolver) : processedNode); lazyImportReExportCheck(processedNode, reExportCheckMode); ModuleSourceFile.newSourceFile(id, processedNode, metaInfo, projectConfig.singleFileEmit); MemoryMonitor.stopRecordStage(newSourceFileRecordInfo); diff --git a/compiler/src/interop/src/process_lazy_import.ts b/compiler/src/interop/src/process_lazy_import.ts index 89be8945b..859af92d7 100644 --- a/compiler/src/interop/src/process_lazy_import.ts +++ b/compiler/src/interop/src/process_lazy_import.ts @@ -36,19 +36,24 @@ const reExportStrictMode: string = 'strict'; export interface LazyImportOptions { autoLazyImport: boolean; reExportCheckMode: string; + autoLazyFilter: Object; } export function processJsCodeLazyImport(id: string, code: string, - autoLazyImport: boolean, reExportCheckMode: string): string { + autoLazyImport: boolean, reExportCheckMode: string, metaInfo: Object, autoLazyFilter: Object): string { let sourceNode: ts.SourceFile = ts.createSourceFile(id, code, ts.ScriptTarget.ES2021, true, ts.ScriptKind.JS); if (autoLazyImport) { - sourceNode = transformLazyImport(sourceNode); + sourceNode = transformLazyImport(metaInfo, sourceNode, autoLazyFilter); } lazyImportReExportCheck(sourceNode, reExportCheckMode); return autoLazyImport ? ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }).printFile(sourceNode) : code; } -export function transformLazyImport(sourceNode: ts.SourceFile, resolver?: Object): ts.SourceFile { +export function transformLazyImport(metaInfo: Object, sourceNode: ts.SourceFile, + autoLazyFilter: Object, resolver?: Object): ts.SourceFile { + if (isNotAutoLazyImport(metaInfo, autoLazyFilter)) { + return sourceNode; + } const moduleNodeTransformer: ts.TransformerFactory = context => { const visitor: ts.Visitor = node => { if (ts.isImportDeclaration(node)) { @@ -64,6 +69,22 @@ export function transformLazyImport(sourceNode: ts.SourceFile, resolver?: Object return result.transformed[0]; } +function isNotAutoLazyImport(metaInfo: Object, autoLazyFilter: Object): boolean { + if (!autoLazyFilter || Object.keys(autoLazyFilter).length === 0) { + return false; + } + const pkgName: string = metaInfo?.pkgName; + const includeList: string[] = autoLazyFilter?.include; + const excludeList: string[] = autoLazyFilter?.exclude; + if (pkgName && includeList && includeList.length > 0) { + return !includeList.includes(pkgName); + } + if (pkgName && excludeList && excludeList.length > 0) { + return excludeList.includes(pkgName); + } + return false; +} + function updateImportDecl(node: ts.ImportDeclaration, resolver: Object): ts.ImportDeclaration { const importClause: ts.ImportClause | undefined = node.importClause; const moduleRequest: string = (node.moduleSpecifier! as ts.StringLiteral).text.replace(/'|"/g, ''); diff --git a/compiler/src/process_kit_import.ts b/compiler/src/process_kit_import.ts index f5c445e0f..c6de79b25 100644 --- a/compiler/src/process_kit_import.ts +++ b/compiler/src/process_kit_import.ts @@ -77,8 +77,8 @@ const KEEPTS = '// @keepTs'; */ export function processKitImport(id: string, metaInfo: Object, parentEvent: CompileEvent, shouldReturnOriginalNode: boolean = true, - lazyImportOptions: LazyImportOptions = { autoLazyImport: false, reExportCheckMode: reExportNoCheckMode }): Function { - const { autoLazyImport, reExportCheckMode } = lazyImportOptions; + lazyImportOptions: LazyImportOptions = { autoLazyImport: false, reExportCheckMode: reExportNoCheckMode, autoLazyFilter: {} }): Function { + const { autoLazyImport, reExportCheckMode, autoLazyFilter } = lazyImportOptions; return (context: ts.TransformationContext) => { const visitor: ts.Visitor = node => { // only transform static import/export declaration @@ -143,7 +143,7 @@ export function processKitImport(id: string, metaInfo: Object, parentEvent: Comp // eg. import { xxx } form "xxx" --> import lazy { xxx } form "xxx" let processedNode: ts.SourceFile = ts.visitEachChild(ts.getTypeExportImportAndConstEnumTransformer(context)(node), visitor, context); - processedNode = (autoLazyImport ? transformLazyImport(processedNode, resolver) : processedNode); + processedNode = (autoLazyImport ? transformLazyImport(metaInfo, processedNode, autoLazyFilter, resolver) : processedNode); lazyImportReExportCheck(processedNode, reExportCheckMode); ModuleSourceFile.newSourceFile(id, processedNode, metaInfo, projectConfig.singleFileEmit); MemoryMonitor.stopRecordStage(newSourceFileRecordInfo); diff --git a/compiler/src/process_lazy_import.ts b/compiler/src/process_lazy_import.ts index 89be8945b..859af92d7 100644 --- a/compiler/src/process_lazy_import.ts +++ b/compiler/src/process_lazy_import.ts @@ -36,19 +36,24 @@ const reExportStrictMode: string = 'strict'; export interface LazyImportOptions { autoLazyImport: boolean; reExportCheckMode: string; + autoLazyFilter: Object; } export function processJsCodeLazyImport(id: string, code: string, - autoLazyImport: boolean, reExportCheckMode: string): string { + autoLazyImport: boolean, reExportCheckMode: string, metaInfo: Object, autoLazyFilter: Object): string { let sourceNode: ts.SourceFile = ts.createSourceFile(id, code, ts.ScriptTarget.ES2021, true, ts.ScriptKind.JS); if (autoLazyImport) { - sourceNode = transformLazyImport(sourceNode); + sourceNode = transformLazyImport(metaInfo, sourceNode, autoLazyFilter); } lazyImportReExportCheck(sourceNode, reExportCheckMode); return autoLazyImport ? ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }).printFile(sourceNode) : code; } -export function transformLazyImport(sourceNode: ts.SourceFile, resolver?: Object): ts.SourceFile { +export function transformLazyImport(metaInfo: Object, sourceNode: ts.SourceFile, + autoLazyFilter: Object, resolver?: Object): ts.SourceFile { + if (isNotAutoLazyImport(metaInfo, autoLazyFilter)) { + return sourceNode; + } const moduleNodeTransformer: ts.TransformerFactory = context => { const visitor: ts.Visitor = node => { if (ts.isImportDeclaration(node)) { @@ -64,6 +69,22 @@ export function transformLazyImport(sourceNode: ts.SourceFile, resolver?: Object return result.transformed[0]; } +function isNotAutoLazyImport(metaInfo: Object, autoLazyFilter: Object): boolean { + if (!autoLazyFilter || Object.keys(autoLazyFilter).length === 0) { + return false; + } + const pkgName: string = metaInfo?.pkgName; + const includeList: string[] = autoLazyFilter?.include; + const excludeList: string[] = autoLazyFilter?.exclude; + if (pkgName && includeList && includeList.length > 0) { + return !includeList.includes(pkgName); + } + if (pkgName && excludeList && excludeList.length > 0) { + return excludeList.includes(pkgName); + } + return false; +} + function updateImportDecl(node: ts.ImportDeclaration, resolver: Object): ts.ImportDeclaration { const importClause: ts.ImportClause | undefined = node.importClause; const moduleRequest: string = (node.moduleSpecifier! as ts.StringLiteral).text.replace(/'|"/g, ''); diff --git a/compiler/test/ark_compiler_ut/common/process_lazy_import.test.ts b/compiler/test/ark_compiler_ut/common/process_lazy_import.test.ts index 51f49584d..19531f228 100644 --- a/compiler/test/ark_compiler_ut/common/process_lazy_import.test.ts +++ b/compiler/test/ark_compiler_ut/common/process_lazy_import.test.ts @@ -103,6 +103,92 @@ const LAZY_IMPORT_RE_EXPORT_ERROR_JS: string = 'export { e2, a3 };\n' + 'export { d1, d2, a4, e5, componentUtils, k1, uiObserver, lazy };'; +const TRANSFORM_LAZY_IMPORT_CODE_INCLUDE_LIST = +` + import { test } from "./test"; + import { test1 as t } from "./test1"; + import { Ability } from "@kit.AbilityKit"; + import { featureAbility as k } from "@kit.AbilityKit"; + import { AbilityConstant, abilityAccessCtrl } from "@kit.AbilityKit"; + import test2 from "./test2"; + import type { testType } from "./testType"; + import * as ns3 from "./test3"; + import test4, { test5 } from "./test4"; + import test6, * as ns6 from "./test6"; + import type dt from "./test7"; + import testJson from "./testJson.json"; + let a: testType = test + t + test2 + testType + ns3.b + test4 + test5 + test6 + ns6.b + dt; + let localAbility = new Ability(); + let localFeatureAbility = new k(); + let localAbilityConstant = new AbilityConstant(); + let localAbilityAccessCtrl = new abilityAccessCtrl(); + let newTestJson = testJson; +` + +const TRANSFORM_LAZY_IMPORT_CODE_INCLUDE_LIST_EXPECT: string = +'import lazy { test } from "./test";\n' + +'import lazy { test1 as t } from "./test1";\n' + +'import lazy Ability from "@ohos.app.ability.Ability";\n' + +'import lazy k from "@ohos.ability.featureAbility";\n' + +'import lazy AbilityConstant from "@ohos.app.ability.AbilityConstant";\n' + +'import lazy abilityAccessCtrl from "@ohos.abilityAccessCtrl";\n' + +'import lazy test2 from "./test2";\n' + +'import type { testType } from "./testType";\n' + +'import * as ns3 from "./test3";\n' + +'import lazy test4, { test5 } from "./test4";\n' + +'import test6, * as ns6 from "./test6";\n' + +'import type dt from "./test7";\n' + +'import testJson from "./testJson.json";\n' + +'let a: testType = test + t + test2 + testType + ns3.b + test4 + test5 + test6 + ns6.b + dt;\n' + +'let localAbility = new Ability();\n' + +'let localFeatureAbility = new k();\n' + +'let localAbilityConstant = new AbilityConstant();\n' + +'let localAbilityAccessCtrl = new abilityAccessCtrl();\n' + +'let newTestJson = testJson;\n'; + +const TRANSFORM_LAZY_IMPORT_CODE_EXCLUDE_LIST = +` + import { test } from "./test"; + import { test1 as t } from "./test1"; + import { Ability } from "@kit.AbilityKit"; + import { featureAbility as k } from "@kit.AbilityKit"; + import { AbilityConstant, abilityAccessCtrl } from "@kit.AbilityKit"; + import test2 from "./test2"; + import type { testType } from "./testType"; + import * as ns3 from "./test3"; + import test4, { test5 } from "./test4"; + import test6, * as ns6 from "./test6"; + import type dt from "./test7"; + import testJson from "./testJson.json"; + let a: testType = test + t + test2 + testType + ns3.b + test4 + test5 + test6 + ns6.b + dt; + let localAbility = new Ability(); + let localFeatureAbility = new k(); + let localAbilityConstant = new AbilityConstant(); + let localAbilityAccessCtrl = new abilityAccessCtrl(); + let newTestJson = testJson; +` + +const TRANSFORM_LAZY_IMPORT_CODE_EXCLUDE_LIST_EXPECT: string = +'import { test } from "./test";\n' + +'import { test1 as t } from "./test1";\n' + +'import Ability from "@ohos.app.ability.Ability";\n' + +'import k from "@ohos.ability.featureAbility";\n' + +'import AbilityConstant from "@ohos.app.ability.AbilityConstant";\n' + +'import abilityAccessCtrl from "@ohos.abilityAccessCtrl";\n' + +'import test2 from "./test2";\n' + +'import type { testType } from "./testType";\n' + +'import * as ns3 from "./test3";\n' + +'import test4, { test5 } from "./test4";\n' + +'import test6, * as ns6 from "./test6";\n' + +'import type dt from "./test7";\n' + +'import testJson from "./testJson.json";\n' + +'let a: testType = test + t + test2 + testType + ns3.b + test4 + test5 + test6 + ns6.b + dt;\n' + +'let localAbility = new Ability();\n' + +'let localFeatureAbility = new k();\n' + +'let localAbilityConstant = new AbilityConstant();\n' + +'let localAbilityAccessCtrl = new abilityAccessCtrl();\n' + +'let newTestJson = testJson;\n'; + const compilerOptions = ts.readConfigFile( path.resolve(__dirname, '../../../tsconfig.json'), ts.sys.readFile).config.compilerOptions; compilerOptions['moduleResolution'] = 'nodenext'; @@ -124,7 +210,7 @@ mocha.describe('process Lazy Imports tests', function () { fileName: 'test.ets', transformers: { before: [ - processKitImport('test.ets', undefined, undefined, true, { autoLazyImport: true, reExportCheckMode: reExportNoCheckMode }) + processKitImport('test.ets', undefined, undefined, true, { autoLazyImport: true, reExportCheckMode: reExportNoCheckMode, autoLazyFilter: {} }) ] } }); @@ -156,7 +242,7 @@ mocha.describe('process Lazy Imports tests', function () { fileName: 'no.ets', transformers: { before: [ - processKitImport('no.ets', undefined, undefined, true, { autoLazyImport: false, reExportCheckMode: reExportNoCheckMode }) + processKitImport('no.ets', undefined, undefined, true, { autoLazyImport: false, reExportCheckMode: reExportNoCheckMode, autoLazyFilter: {} }) ] } }); @@ -227,7 +313,7 @@ mocha.describe('process Lazy Imports tests', function () { fileName: "kitTest.ts", transformers: { before: [ - processKitImport('kitTest.ts', undefined, undefined, true, { autoLazyImport: false, reExportCheckMode: 'strict' }) + processKitImport('kitTest.ts', undefined, undefined, true, { autoLazyImport: false, reExportCheckMode: 'strict', autoLazyFilter: {} }) ] } }); @@ -258,7 +344,7 @@ mocha.describe('process Lazy Imports tests', function () { fileName: "kitTest.ts", transformers: { before: [ - processKitImport('kitTest.ts', undefined, undefined, true, { autoLazyImport: false, reExportCheckMode: 'strict' }) + processKitImport('kitTest.ts', undefined, undefined, true, { autoLazyImport: false, reExportCheckMode: 'strict', autoLazyFilter: {} }) ] } }); @@ -282,7 +368,7 @@ mocha.describe('process Lazy Imports tests', function () { fileName: "kitTest.ts", transformers: { before: [ - processKitImport('kitTest.ts', undefined, undefined, true, { autoLazyImport: false, reExportCheckMode: 'compatible' }) + processKitImport('kitTest.ts', undefined, undefined, true, { autoLazyImport: false, reExportCheckMode: 'compatible', autoLazyFilter: {} }) ] } }); @@ -306,7 +392,7 @@ mocha.describe('process Lazy Imports tests', function () { fileName: "kitTest.ts", transformers: { before: [ - processKitImport('kitTest.ts', undefined, undefined, true, { autoLazyImport: true, reExportCheckMode: 'compatible' }) + processKitImport('kitTest.ts', undefined, undefined, true, { autoLazyImport: true, reExportCheckMode: 'compatible', autoLazyFilter: {} }) ] } }); @@ -407,6 +493,9 @@ mocha.describe('process Lazy Imports tests', function () { }; const fileNames = Object.keys(files); + const metaInfo: Object = { + pkgName: 'test', + }; const compilerHost = ts.createCompilerHost({}, true); const getFileKey = (filePath: string) => path.basename(filePath); @@ -458,7 +547,7 @@ mocha.describe('process Lazy Imports tests', function () { } }; - const transformed = transformLazyImport(originalSource, mockResolver); + const transformed = transformLazyImport(metaInfo, originalSource, {}, mockResolver); const importDecl = transformed.statements.find(ts.isImportDeclaration)!; const specifiers = (importDecl.importClause!.namedBindings as ts.NamedImports).elements; @@ -468,4 +557,56 @@ mocha.describe('process Lazy Imports tests', function () { expect(transformedSymbol).to.exist; } }); + + mocha.it('4-1: test autoLazyImport supports exclude list and include list: include list', function () { + projectConfig.processTs = true; + const metaInfo: Object = { + pkgName: 'entry', + }; + ts.transpileModule(TRANSFORM_LAZY_IMPORT_CODE_INCLUDE_LIST, { + compilerOptions: compilerOptions, + fileName: 'entry_test.ets', + transformers: { + before: [ + processKitImport('entry_test.ets', metaInfo, undefined, true, + { autoLazyImport: true, reExportCheckMode: reExportNoCheckMode, autoLazyFilter: { include: ['entry'] } }) + ] + } + }); + const sourceFile: ts.SourceFile = ModuleSourceFile.getSourceFiles().find(element => element.moduleId === 'entry_test.ets'); + const printer: ts.Printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); + // @ts-ignore + const writer: ts.EmitTextWriter = ts.createTextWriter( + // @ts-ignore + ts.getNewLineCharacter({ newLine: ts.NewLineKind.LineFeed, removeComments: false })); + printer.writeFile(sourceFile.source, writer, undefined); + expect(writer.getText() === TRANSFORM_LAZY_IMPORT_CODE_INCLUDE_LIST_EXPECT).to.be.true; + projectConfig.processTs = false; + }); + + mocha.it('4-2: test autoLazyImport supports exclude list and include list: exclude list', function () { + projectConfig.processTs = true; + const metaInfo: Object = { + pkgName: 'lib', + }; + ts.transpileModule(TRANSFORM_LAZY_IMPORT_CODE_EXCLUDE_LIST, { + compilerOptions: compilerOptions, + fileName: 'lib_test.ets', + transformers: { + before: [ + processKitImport('lib_test.ets', metaInfo, undefined, true, + { autoLazyImport: true, reExportCheckMode: reExportNoCheckMode, autoLazyFilter: { exclude: ['lib'] } }) + ] + } + }); + const sourceFile: ts.SourceFile = ModuleSourceFile.getSourceFiles().find(element => element.moduleId === 'lib_test.ets'); + const printer: ts.Printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); + // @ts-ignore + const writer: ts.EmitTextWriter = ts.createTextWriter( + // @ts-ignore + ts.getNewLineCharacter({ newLine: ts.NewLineKind.LineFeed, removeComments: false })); + printer.writeFile(sourceFile.source, writer, undefined); + expect(writer.getText() === TRANSFORM_LAZY_IMPORT_CODE_EXCLUDE_LIST_EXPECT).to.be.true; + projectConfig.processTs = false; + }); }); \ No newline at end of file -- Gitee