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} from './common/ark_define'; 21import { ModuleSourceFile } from './module/module_source_file'; 22import { 23 isAotMode, 24 isCommonJsPluginVirtualFile, 25 isDebug, 26 isJsonSourceFile, 27 isJsSourceFile, 28 isTsOrEtsSourceFile, 29 shouldETSOrTSFileTransformToJS 30} from './utils'; 31import { toUnixPath } from '../../utils'; 32import { 33 getHookEventFactory, 34 createAndStartEvent, 35 stopEvent 36} from '../../ark_utils'; 37 38export let newSourceMaps: Object = {}; 39 40/** 41 * rollup transform hook 42 * @param {string} code: transformed source code of an input file 43 * @param {string} id: absolute path of an input file 44 */ 45export function transformForModule(code: string, id: string) { 46 const hookEventFactory = getHookEventFactory(this.share, 'genAbc', 'transform'); 47 const eventTransformForModule = createAndStartEvent(hookEventFactory, 'transform for module'); 48 if (this.share.projectConfig.compileMode === ESMODULE) { 49 const projectConfig: Object = Object.assign(this.share.arkProjectConfig, this.share.projectConfig); 50 if (isTsOrEtsSourceFile(id) && shouldETSOrTSFileTransformToJS(id, projectConfig)) { 51 preserveSourceMap(id, this.getCombinedSourcemap(), projectConfig, eventTransformForModule); 52 ModuleSourceFile.newSourceFile(id, code); 53 } 54 55 if (isJsSourceFile(id) || isJsonSourceFile(id)) { 56 let code: string = this.getModuleInfo(id).originalCode; 57 if (isJsSourceFile(id)) { 58 if (projectConfig.compatibleSdkVersion <= 10) { 59 const transformedResult: object = transformJsByBabelPlugin(code, eventTransformForModule); 60 code = transformedResult.code; 61 preserveSourceMap(id, transformedResult.map, projectConfig, eventTransformForModule); 62 } else { 63 // preserve sourceMap of js file without transforming 64 preserveSourceMap(id, this.getCombinedSourcemap(), projectConfig, eventTransformForModule); 65 } 66 } 67 ModuleSourceFile.newSourceFile(id, code); 68 } 69 } 70 stopEvent(eventTransformForModule); 71} 72 73function preserveSourceMap(sourceFilePath: string, sourcemap: Object, projectConfig: Object, parentEvent: Object): void { 74 if (isCommonJsPluginVirtualFile(sourceFilePath)) { 75 // skip automatic generated files like 'jsfile.js?commonjs-exports' 76 return; 77 } 78 79 const eventAddSourceMapInfo = createAndStartEvent(parentEvent, 'preserve source map for ts/ets files'); 80 const relativeSourceFilePath = toUnixPath(sourceFilePath.replace(projectConfig.projectRootPath + path.sep, '')); 81 sourcemap['sources'] = [ relativeSourceFilePath ]; 82 sourcemap['file'] = path.basename(relativeSourceFilePath); 83 sourcemap.sourcesContent && delete sourcemap.sourcesContent; 84 newSourceMaps[relativeSourceFilePath] = sourcemap; 85 stopEvent(eventAddSourceMapInfo); 86} 87 88function transformJsByBabelPlugin(code: string, parentEvent: Object): Object { 89 const eventTransformByBabel = createAndStartEvent(parentEvent, 'transform Js by babel plugin'); 90 const transformed: Object = require('@babel/core').transformSync(code, 91 { 92 plugins: [ 93 [require("@babel/plugin-proposal-class-properties"), { "loose": true }] 94 ], 95 compact: false, 96 sourceMaps: true 97 } 98 ); 99 stopEvent(eventTransformByBabel); 100 return transformed; 101} 102 103export function cleanSourceMapObject(): void { 104 newSourceMaps = {}; 105}