1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use rollupObject file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 17import { expect } from 'chai'; 18import mocha from 'mocha'; 19import fs from "fs"; 20 21import { 22 OBFUSCATION_TOOL, 23 ESMODULE, 24 RELEASE, 25 TS2ABC 26} from '../../../lib/fast_build/ark_compiler/common/ark_define'; 27import { 28 NODE, 29 BUNDLE_NAME_DEFAULT, 30 ENTRY_MODULE_NAME_DEFAULT, 31 NODE_JS_PATH, 32 LOADER_AOTMODE 33} from '../mock/rollup_mock/common'; 34import { 35 ES2ABC_PATH, 36 TS2ABC_PATH, 37 MERGERABC_PATH, 38 JS2ABC_PATH, 39 AOTCOMPILER_PATH, 40 ARKCONFIG_TS2ABC_PATH 41} from '../mock/rollup_mock/path_config'; 42import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock'; 43import { 44 initArkConfig, 45 initArkProjectConfig, 46 utProcessArkConfig 47} from '../../../lib/fast_build/ark_compiler/common/process_ark_config'; 48import { 49 ObConfigResolver, 50 MergedConfig 51} from '../../../lib/fast_build/ark_compiler/common/ob_config_resolver'; 52 53mocha.describe('test process_ark_config file api', function () { 54 mocha.before(function () { 55 this.rollup = new RollUpPluginMock(); 56 }); 57 58 mocha.after(() => { 59 delete this.rollup; 60 }); 61 62 mocha.it('1-1: test initArkConfig under build debug', function () { 63 this.rollup.build(); 64 this.rollup.share.projectConfig.arkFrontendDir = this.rollup.share.projectConfig.projectTopDir; 65 this.rollup.share.projectConfig.nodeJs = true; 66 this.rollup.share.projectConfig.nodePath = NODE_JS_PATH; 67 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 68 69 expect(arkConfig.nodePath === NODE_JS_PATH).to.be.true; 70 expect(arkConfig.es2abcPath.indexOf(ES2ABC_PATH) > 0).to.be.true; 71 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 72 expect(arkConfig.mergeAbcPath.indexOf(MERGERABC_PATH) > 0).to.be.true; 73 expect(arkConfig.js2abcPath.indexOf(JS2ABC_PATH) > 0).to.be.true; 74 expect(arkConfig.aotCompilerPath.indexOf(AOTCOMPILER_PATH) > 0).to.be.true; 75 expect(arkConfig.isDebug === true).to.be.true; 76 expect(arkConfig.arkRootPath === this.rollup.share.projectConfig.arkFrontendDir).to.be.true; 77 }); 78 79 mocha.it('1-2: test initArkConfig under build release', function () { 80 this.rollup.build(RELEASE); 81 this.rollup.share.projectConfig.arkFrontendDir = this.rollup.share.projectConfig.projectTopDir; 82 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 83 84 expect(arkConfig.nodePath === NODE).to.be.true; 85 expect(arkConfig.es2abcPath.indexOf(ES2ABC_PATH) > 0).to.be.true; 86 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 87 expect(arkConfig.mergeAbcPath.indexOf(MERGERABC_PATH) > 0).to.be.true; 88 expect(arkConfig.js2abcPath.indexOf(JS2ABC_PATH) > 0).to.be.true; 89 expect(arkConfig.aotCompilerPath.indexOf(AOTCOMPILER_PATH) > 0).to.be.true; 90 expect(arkConfig.isDebug === false).to.be.true; 91 expect(arkConfig.arkRootPath === this.rollup.share.projectConfig.arkFrontendDir).to.be.true; 92 }); 93 94 mocha.it('1-3: test initArkConfig under preview debug', function () { 95 this.rollup.preview(); 96 this.rollup.share.projectConfig.arkFrontendDir = this.rollup.share.projectConfig.projectTopDir; 97 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 98 99 expect(arkConfig.nodePath === NODE).to.be.true; 100 expect(arkConfig.es2abcPath.indexOf(ES2ABC_PATH) > 0).to.be.true; 101 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 102 expect(arkConfig.mergeAbcPath.indexOf(MERGERABC_PATH) > 0).to.be.true; 103 expect(arkConfig.js2abcPath.indexOf(JS2ABC_PATH) > 0).to.be.true; 104 expect(arkConfig.aotCompilerPath.indexOf(AOTCOMPILER_PATH) > 0).to.be.true; 105 expect(arkConfig.isDebug === true).to.be.true; 106 expect(arkConfig.arkRootPath === this.rollup.share.projectConfig.arkFrontendDir).to.be.true; 107 }); 108 109 mocha.it('1-4: test initArkConfig under hot reload debug', function () { 110 this.rollup.hotReload(); 111 this.rollup.share.projectConfig.arkFrontendDir = this.rollup.share.projectConfig.projectTopDir; 112 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 113 114 expect(arkConfig.nodePath === NODE).to.be.true; 115 expect(arkConfig.es2abcPath.indexOf(ES2ABC_PATH) > 0).to.be.true; 116 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 117 expect(arkConfig.mergeAbcPath.indexOf(MERGERABC_PATH) > 0).to.be.true; 118 expect(arkConfig.js2abcPath.indexOf(JS2ABC_PATH) > 0).to.be.true; 119 expect(arkConfig.aotCompilerPath.indexOf(AOTCOMPILER_PATH) > 0).to.be.true; 120 expect(arkConfig.isDebug === true).to.be.true; 121 expect(arkConfig.arkRootPath === this.rollup.share.projectConfig.arkFrontendDir).to.be.true; 122 }); 123 124 mocha.it('2-1-1: test initArkProjectConfig under build debug: moduleJsonInfo exists', function () { 125 this.rollup.build(); 126 const arkConfig = initArkProjectConfig(this.rollup.share); 127 const buildJsonInfo = 128 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString()); 129 const moduleJsonInfo = 130 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceModuleJsonPath).toString()); 131 132 expect(arkConfig.nodeModulesPath === buildJsonInfo.nodeModulesPath).to.be.true; 133 expect(arkConfig.minPlatformVersion === moduleJsonInfo.app.minAPIVersion).to.be.true; 134 expect(arkConfig.processTs === false).to.be.true; 135 expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 136 expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true; 137 expect(arkConfig.compileMode === ESMODULE).to.be.true; 138 }); 139 140 mocha.it('2-1-2: test initArkProjectConfig under build debug: buildJsonInfo.patchConfig is true', function () { 141 this.rollup.build(); 142 this.rollup.share.projectConfig.aceBuildJson = 143 `${this.rollup.share.projectConfig.aceModuleBuild}/${LOADER_AOTMODE}`; 144 const arkConfig = initArkProjectConfig(this.rollup.share); 145 const buildJsonInfo = 146 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString()); 147 148 expect(arkConfig.oldMapFilePath === buildJsonInfo.patchConfig.oldMapFilePath).to.be.true; 149 expect(arkConfig.pandaMode === TS2ABC).to.be.true; 150 expect(arkConfig.processTs === true).to.be.true; 151 expect(arkConfig.anBuildOutPut === buildJsonInfo.anBuildOutPut).to.be.true; 152 expect(arkConfig.anBuildMode === buildJsonInfo.anBuildMode).to.be.true; 153 expect(arkConfig.apPath === buildJsonInfo.apPath).to.be.true; 154 expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 155 expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true; 156 expect(arkConfig.compileMode === ESMODULE).to.be.true; 157 }); 158 159 mocha.it('2-2: test initArkProjectConfig under build release', function () { 160 this.rollup.build(RELEASE); 161 const arkConfig = initArkProjectConfig(this.rollup.share); 162 const buildJsonInfo = 163 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString()); 164 const moduleJsonInfo = 165 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceModuleJsonPath).toString()); 166 167 expect(arkConfig.nodeModulesPath === buildJsonInfo.nodeModulesPath).to.be.true; 168 expect(arkConfig.minPlatformVersion === moduleJsonInfo.app.minAPIVersion).to.be.true; 169 expect(arkConfig.processTs === false).to.be.true; 170 expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 171 expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true; 172 expect(arkConfig.compileMode === ESMODULE).to.be.true; 173 }); 174 175 mocha.it('2-3: test initArkProjectConfig under preview debug', function () { 176 this.rollup.preview(); 177 const arkConfig = initArkProjectConfig(this.rollup.share); 178 const buildJsonInfo = 179 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString()); 180 const moduleJsonInfo = 181 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceModuleJsonPath).toString()); 182 183 expect(arkConfig.nodeModulesPath === buildJsonInfo.nodeModulesPath).to.be.true; 184 expect(arkConfig.minPlatformVersion === moduleJsonInfo.app.minAPIVersion).to.be.true; 185 expect(arkConfig.processTs === false).to.be.true; 186 expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 187 expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true; 188 expect(arkConfig.compileMode === ESMODULE).to.be.true; 189 }); 190 191 mocha.it('2-4: test initArkProjectConfig under hot reload debug', function () { 192 this.rollup.hotReload(); 193 const arkConfig = initArkProjectConfig(this.rollup.share); 194 const buildJsonInfo = 195 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceBuildJson).toString()); 196 const moduleJsonInfo = 197 JSON.parse(fs.readFileSync(this.rollup.share.projectConfig.aceModuleJsonPath).toString()); 198 199 expect(arkConfig.nodeModulesPath === buildJsonInfo.nodeModulesPath).to.be.true; 200 expect(arkConfig.minPlatformVersion === moduleJsonInfo.app.minAPIVersion).to.be.true; 201 expect(arkConfig.processTs === false).to.be.true; 202 expect(arkConfig.moduleName === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 203 expect(arkConfig.bundleName === BUNDLE_NAME_DEFAULT).to.be.true; 204 expect(arkConfig.compileMode === ESMODULE).to.be.true; 205 }); 206 207 mocha.it('3-1: test initTerserConfig under build debug', function () { 208 this.rollup.build(); 209 const logger: object = this.rollup.share.getLogger(OBFUSCATION_TOOL); 210 const obConfig: ObConfigResolver = new ObConfigResolver(this.rollup.share.projectConfig, logger, true); 211 const mergedObConfig: MergedConfig = obConfig.resolveObfuscationConfigs(); 212 const isHarCompiled: boolean = this.rollup.share.projectConfig.compileHar; 213 const minifyOptions = 214 utProcessArkConfig.initTerserConfig(this.rollup.share.projectConfig, logger, mergedObConfig, isHarCompiled); 215 216 expect(minifyOptions.format.beautify === true).to.be.true; 217 expect(minifyOptions.format.indent_level === 2).to.be.true; 218 expect(minifyOptions.compress.join_vars === false).to.be.true; 219 expect(minifyOptions.compress.sequences === 0).to.be.true; 220 expect(minifyOptions.compress.directives === false).to.be.true; 221 expect(minifyOptions.compress.drop_console === false).to.be.true; 222 expect(minifyOptions.mangle.toplevel === false).to.be.true; 223 }); 224 225 mocha.it('3-2: test initTerserConfig under build release', function () { 226 this.rollup.build(RELEASE); 227 const logger: object = this.rollup.share.getLogger(OBFUSCATION_TOOL); 228 const obConfig: ObConfigResolver = new ObConfigResolver(this.rollup.share.projectConfig, logger, true); 229 const mergedObConfig: MergedConfig = obConfig.resolveObfuscationConfigs(); 230 const isHarCompiled: boolean = this.rollup.share.projectConfig.compileHar; 231 const minifyOptions = 232 utProcessArkConfig.initTerserConfig(this.rollup.share.projectConfig, logger, mergedObConfig, isHarCompiled); 233 234 expect(minifyOptions.format.beautify === true).to.be.true; 235 expect(minifyOptions.format.indent_level === 2).to.be.true; 236 expect(minifyOptions.compress.join_vars === false).to.be.true; 237 expect(minifyOptions.compress.sequences === 0).to.be.true; 238 expect(minifyOptions.compress.directives === false).to.be.true; 239 expect(minifyOptions.compress.drop_console === false).to.be.true; 240 expect(minifyOptions.mangle.toplevel === false).to.be.true; 241 }); 242 243 mocha.it('3-3: test initTerserConfig under preview debug', function () { 244 this.rollup.preview(); 245 const logger: object = this.rollup.share.getLogger(OBFUSCATION_TOOL); 246 const obConfig: ObConfigResolver = new ObConfigResolver(this.rollup.share.projectConfig, logger, true); 247 const mergedObConfig: MergedConfig = obConfig.resolveObfuscationConfigs(); 248 const isHarCompiled: boolean = this.rollup.share.projectConfig.compileHar; 249 const minifyOptions = 250 utProcessArkConfig.initTerserConfig(this.rollup.share.projectConfig, logger, mergedObConfig, isHarCompiled); 251 252 expect(minifyOptions.format.beautify === true).to.be.true; 253 expect(minifyOptions.format.indent_level === 2).to.be.true; 254 expect(minifyOptions.compress.join_vars === false).to.be.true; 255 expect(minifyOptions.compress.sequences === 0).to.be.true; 256 expect(minifyOptions.compress.directives === false).to.be.true; 257 expect(minifyOptions.compress.drop_console === false).to.be.true; 258 expect(minifyOptions.mangle.toplevel === false).to.be.true; 259 }); 260 261 mocha.it('3-4: test initTerserConfig under hot reload debug', function () { 262 this.rollup.hotReload(); 263 const logger: object = this.rollup.share.getLogger(OBFUSCATION_TOOL); 264 const obConfig: ObConfigResolver = new ObConfigResolver(this.rollup.share.projectConfig, logger, true); 265 const mergedObConfig: MergedConfig = obConfig.resolveObfuscationConfigs(); 266 const isHarCompiled: boolean = this.rollup.share.projectConfig.compileHar; 267 const minifyOptions = 268 utProcessArkConfig.initTerserConfig(this.rollup.share.projectConfig, logger, mergedObConfig, isHarCompiled); 269 270 expect(minifyOptions.format.beautify === true).to.be.true; 271 expect(minifyOptions.format.indent_level === 2).to.be.true; 272 expect(minifyOptions.compress.join_vars === false).to.be.true; 273 expect(minifyOptions.compress.sequences === 0).to.be.true; 274 expect(minifyOptions.compress.directives === false).to.be.true; 275 expect(minifyOptions.compress.drop_console === false).to.be.true; 276 expect(minifyOptions.mangle.toplevel === false).to.be.true; 277 }); 278 279 mocha.it('4-1: test processCompatibleVersion under build debug', function () { 280 this.rollup.build(); 281 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 282 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 283 expect(this.rollup.share.projectConfig.pandaMode === undefined).to.be.true; 284 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 285 286 this.rollup.share.projectConfig.minPlatformVersion = 8; 287 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 288 expect(this.rollup.share.projectConfig.pandaMode === TS2ABC).to.be.true; 289 expect(arkConfig.ts2abcPath.indexOf(ARKCONFIG_TS2ABC_PATH) > 0).to.be.true; 290 }); 291 292 mocha.it('4-2: test processCompatibleVersion under build release', function () { 293 this.rollup.build(RELEASE); 294 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 295 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 296 expect(this.rollup.share.projectConfig.pandaMode === undefined).to.be.true; 297 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 298 299 this.rollup.share.projectConfig.minPlatformVersion = 8; 300 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 301 expect(this.rollup.share.projectConfig.pandaMode === TS2ABC).to.be.true; 302 expect(arkConfig.ts2abcPath.indexOf(ARKCONFIG_TS2ABC_PATH) > 0).to.be.true; 303 }); 304 305 mocha.it('4-3: test processCompatibleVersion under preview debug', function () { 306 this.rollup.preview(); 307 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 308 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 309 expect(this.rollup.share.projectConfig.pandaMode === undefined).to.be.true; 310 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 311 312 this.rollup.share.projectConfig.minPlatformVersion = 8; 313 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 314 expect(this.rollup.share.projectConfig.pandaMode === TS2ABC).to.be.true; 315 expect(arkConfig.ts2abcPath.indexOf(ARKCONFIG_TS2ABC_PATH) > 0).to.be.true; 316 }); 317 318 mocha.it('4-4: test processCompatibleVersion under hot reload debug', function () { 319 this.rollup.hotReload(); 320 const arkConfig = initArkConfig(this.rollup.share.projectConfig); 321 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 322 expect(this.rollup.share.projectConfig.pandaMode === undefined).to.be.true; 323 expect(arkConfig.ts2abcPath.indexOf(TS2ABC_PATH) > 0).to.be.true; 324 325 this.rollup.share.projectConfig.minPlatformVersion = 8; 326 utProcessArkConfig.processCompatibleVersion(this.rollup.share.projectConfig, arkConfig); 327 expect(this.rollup.share.projectConfig.pandaMode === TS2ABC).to.be.true; 328 expect(arkConfig.ts2abcPath.indexOf(ARKCONFIG_TS2ABC_PATH) > 0).to.be.true; 329 }); 330});