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';
37import { SourceMapGenerator } from './generate_sourcemap';
38
39/**
40 * rollup transform hook
41 * @param {string} code: transformed source code of an input file
42 * @param {string} id: absolute path of an input file
43 */
44export function transformForModule(code: string, id: string) {
45  const hookEventFactory = getHookEventFactory(this.share, 'genAbc', 'transform');
46  const eventTransformForModule = createAndStartEvent(hookEventFactory, 'transform for module');
47  if (this.share.projectConfig.compileMode === ESMODULE) {
48    const metaInfo: Object = this.getModuleInfo(id).meta;
49    const projectConfig: Object = Object.assign(this.share.arkProjectConfig, this.share.projectConfig);
50    if (isTsOrEtsSourceFile(id) && shouldETSOrTSFileTransformToJS(id, projectConfig, metaInfo)) {
51      preserveSourceMap(id, this.getCombinedSourcemap(), projectConfig, metaInfo, eventTransformForModule);
52      ModuleSourceFile.newSourceFile(id, code, metaInfo);
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, metaInfo, eventTransformForModule);
62        } else {
63          // preserve sourceMap of js file without transforming
64          preserveSourceMap(id, this.getCombinedSourcemap(), projectConfig, metaInfo, eventTransformForModule);
65        }
66      }
67      ModuleSourceFile.newSourceFile(id, code, metaInfo);
68    }
69  }
70  stopEvent(eventTransformForModule);
71}
72
73function preserveSourceMap(sourceFilePath: string, sourcemap: Object, projectConfig: Object, metaInfo: 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 = sourceFilePath.startsWith(projectConfig.projectRootPath) ?
81    toUnixPath(sourceFilePath.replace(projectConfig.projectRootPath + path.sep, '')) :
82    toUnixPath(sourceFilePath.replace(metaInfo.belongProjectPath + path.sep, ''));
83  sourcemap.sources = [relativeSourceFilePath];
84  sourcemap.file = path.basename(relativeSourceFilePath);
85  sourcemap.sourcesContent && delete sourcemap.sourcesContent;
86  const sourceMapGenerator = SourceMapGenerator.getInstance();
87  const key = sourceMapGenerator.isNewSourceMaps() ? sourceFilePath : relativeSourceFilePath;
88  sourceMapGenerator.fillSourceMapPackageInfo(sourceFilePath, sourcemap);
89  sourceMapGenerator.updateSourceMap(key, sourcemap);
90  stopEvent(eventAddSourceMapInfo);
91}
92
93function transformJsByBabelPlugin(code: string, parentEvent: Object): Object {
94  const eventTransformByBabel = createAndStartEvent(parentEvent, 'transform Js by babel plugin');
95  const transformed: Object = require('@babel/core').transformSync(code,
96    {
97      plugins: [
98        [require("@babel/plugin-proposal-class-properties"), { "loose": true }]
99      ],
100      compact: false,
101      sourceMaps: true
102    }
103  );
104  stopEvent(eventTransformByBabel);
105  return transformed;
106}