• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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';
32
33export let newSourceMaps: Object = {};
34
35/**
36 * rollup transform hook
37 * @param {string} code: transformed source code of an input file
38 * @param {string} id: absolute path of an input file
39 */
40export function transformForModule(code: string, id: string) {
41  if (this.share.projectConfig.compileMode === ESMODULE) {
42    const projectConfig: any = Object.assign(this.share.arkProjectConfig, this.share.projectConfig);
43    if (isTsOrEtsSourceFile(id) && shouldETSOrTSFileTransformToJS(id, projectConfig)) {
44      preserveSourceMap(id, this.getCombinedSourcemap(), projectConfig);
45      ModuleSourceFile.newSourceFile(id, code);
46    }
47
48    if (isJsSourceFile(id) || isJsonSourceFile(id)) {
49      let code: string = this.getModuleInfo(id).originalCode;
50      if (isJsSourceFile(id)) {
51        const transformedResult: any = transformJsByBabelPlugin(code);
52        code = transformedResult.code;
53        preserveSourceMap(id, transformedResult.map, projectConfig);
54      }
55      ModuleSourceFile.newSourceFile(id, code);
56    }
57  }
58}
59
60function preserveSourceMap(sourceFilePath: string, sourcemap: any, projectConfig: any): void {
61  if (isCommonJsPluginVirtualFile(sourceFilePath)) {
62    // skip automatic generated files like 'jsfile.js?commonjs-exports'
63    return;
64  }
65
66  const relativeSourceFilePath = toUnixPath(sourceFilePath.replace(projectConfig.projectRootPath + path.sep, ''));
67  sourcemap['sources'] = [ relativeSourceFilePath ];
68  sourcemap['file'] = path.basename(relativeSourceFilePath);
69  sourcemap.sourcesContent && delete sourcemap.sourcesContent;
70  newSourceMaps[relativeSourceFilePath] = sourcemap;
71}
72
73function transformJsByBabelPlugin(code: string): any {
74  const transformed: any = require('@babel/core').transformSync(code,
75    {
76      plugins: [
77        [require("@babel/plugin-proposal-class-properties"), { "loose": true }]
78      ],
79      compact: false,
80      sourceMaps: true
81    }
82  );
83  return transformed;
84}
85