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 this 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 16import path from 'path'; 17 18import { 19 ESMODULE, 20 GEN_ABC_PLUGIN_NAME 21} from './common/ark_define'; 22import { ModuleSourceFile } from './module/module_source_file'; 23import { 24 isAotMode, 25 isCommonJsPluginVirtualFile, 26 isDebug, 27 isJsonSourceFile, 28 isJsSourceFile, 29 isTsOrEtsSourceFile, 30 shouldETSOrTSFileTransformToJS 31} from './utils'; 32import { 33 toUnixPath, 34 emitLogInfo, 35 getTransformLog 36} from '../../utils'; 37import { 38 getHookEventFactory, 39 createAndStartEvent, 40 stopEvent 41} from '../../ark_utils'; 42import { 43 resetReExportCheckLog, 44 reExportNoCheckMode, 45 processJsCodeLazyImport, 46 reExportCheckLog, 47 LazyImportOptions 48} from '../../process_lazy_import'; 49import { SourceMapGenerator } from './generate_sourcemap'; 50import { MemoryMonitor } from '../meomry_monitor/rollup-plugin-memory-monitor'; 51import { MemoryDefine } from '../meomry_monitor/memory_define'; 52 53/** 54 * rollup transform hook 55 * @param {string} code: transformed source code of an input file 56 * @param {string} id: absolute path of an input file 57 */ 58export function transformForModule(code: string, id: string): string { 59 const hookEventFactory = getHookEventFactory(this.share, 'genAbc', 'transform'); 60 const eventTransformForModule = createAndStartEvent(hookEventFactory, 'transform for module'); 61 const { 62 autoLazyImport, 63 reExportCheckMode 64 }: LazyImportOptions = { 65 autoLazyImport: this.share.projectConfig?.autoLazyImport ?? false, 66 reExportCheckMode: this.share.projectConfig?.reExportCheckMode ?? reExportNoCheckMode 67 }; 68 if (this.share.projectConfig.compileMode === ESMODULE) { 69 const metaInfo: Object = this.getModuleInfo(id).meta; 70 const projectConfig: Object = Object.assign(this.share.arkProjectConfig, this.share.projectConfig); 71 if (isTsOrEtsSourceFile(id) && shouldETSOrTSFileTransformToJS(id, projectConfig, metaInfo)) { 72 preserveSourceMap(id, this.getCombinedSourcemap(), projectConfig, metaInfo, eventTransformForModule); 73 // when ets/ts -> js, we need to convert lazy-import based on the js code generated after tsc conversion 74 code = processJsCodeLazyImport(id, code, autoLazyImport, reExportCheckMode); 75 const newSourceFileRecordInfo = MemoryMonitor.recordStage(MemoryDefine.MODULE_SOURCE_FILE_NEW_SOURCE_FILE); 76 ModuleSourceFile.newSourceFile(id, code, metaInfo, projectConfig.singleFileEmit); 77 MemoryMonitor.stopRecordStage(newSourceFileRecordInfo); 78 } 79 80 if (isJsSourceFile(id) || isJsonSourceFile(id)) { 81 let code: string = this.getModuleInfo(id).originalCode; 82 if (isJsSourceFile(id)) { 83 code = processJsCodeLazyImport(id, code, autoLazyImport, reExportCheckMode); 84 if (projectConfig.compatibleSdkVersion <= 10) { 85 const transformedResult: object = transformJsByBabelPlugin(code, eventTransformForModule); 86 code = transformedResult.code; 87 preserveSourceMap(id, transformedResult.map, projectConfig, metaInfo, eventTransformForModule); 88 } else { 89 // preserve sourceMap of js file without transforming 90 preserveSourceMap(id, this.getCombinedSourcemap(), projectConfig, metaInfo, eventTransformForModule); 91 } 92 } 93 const recordInfo = MemoryMonitor.recordStage(MemoryDefine.MODULE_SOURCE_FILE_NEW_SOURCE_FILE); 94 ModuleSourceFile.newSourceFile(id, code, metaInfo, projectConfig.singleFileEmit); 95 MemoryMonitor.stopRecordStage(recordInfo); 96 } 97 } 98 stopEvent(eventTransformForModule); 99 100 if (reExportCheckLog && reExportCheckLog.errors.length && 101 reExportCheckMode !== reExportNoCheckMode && !this.share.projectConfig.ignoreWarning) { 102 emitLogInfo(this.share.getLogger(GEN_ABC_PLUGIN_NAME), [...getTransformLog(reExportCheckLog)], true, id); 103 resetReExportCheckLog(); 104 } 105 106 // if we perform lazy-import conversion, we need to return the converted js code 107 return code; 108} 109 110function preserveSourceMap(sourceFilePath: string, sourcemap: Object, projectConfig: Object, metaInfo: Object, parentEvent: Object): void { 111 if (isCommonJsPluginVirtualFile(sourceFilePath)) { 112 // skip automatic generated files like 'jsfile.js?commonjs-exports' 113 return; 114 } 115 116 const eventAddSourceMapInfo = createAndStartEvent(parentEvent, 'preserve source map for ts/ets files'); 117 const relativeSourceFilePath = sourceFilePath.startsWith(projectConfig.projectRootPath) ? 118 toUnixPath(sourceFilePath.replace(projectConfig.projectRootPath + path.sep, '')) : 119 toUnixPath(sourceFilePath.replace(metaInfo.belongProjectPath + path.sep, '')); 120 sourcemap.sources = [relativeSourceFilePath]; 121 sourcemap.file = path.basename(relativeSourceFilePath); 122 sourcemap.sourcesContent && delete sourcemap.sourcesContent; 123 const sourceMapGenerator = SourceMapGenerator.getInstance(); 124 const key = sourceMapGenerator.isNewSourceMaps() ? sourceFilePath : relativeSourceFilePath; 125 sourceMapGenerator.fillSourceMapPackageInfo(sourceFilePath, sourcemap); 126 sourceMapGenerator.updateSourceMap(key, sourcemap); 127 stopEvent(eventAddSourceMapInfo); 128} 129 130function transformJsByBabelPlugin(code: string, parentEvent: Object): Object { 131 const eventTransformByBabel = createAndStartEvent(parentEvent, 'transform Js by babel plugin'); 132 const transformed: Object = require('@babel/core').transformSync(code, 133 { 134 plugins: [ 135 [require("@babel/plugin-proposal-class-properties"), { "loose": true }] 136 ], 137 compact: false, 138 sourceMaps: true 139 } 140 ); 141 stopEvent(eventTransformByBabel); 142 return transformed; 143}