• 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';
17import ts from 'typescript';
18import fs from 'fs';
19
20import { SourceMapGenerator } from './fast_build/ark_compiler/generate_sourcemap';
21import {
22  EXTNAME_TS,
23  EXTNAME_ETS,
24} from './pre_define';
25import {
26  genTemporaryPath,
27  mkdirsSync,
28  toUnixPath,
29} from './utils';
30import {
31  genSourceMapFileName,
32  newSourceMaps as webpackNewSourceMaps,
33  transformModuleSpecifier,
34  writeObfuscatedSourceCode,
35  createAndStartEvent,
36  stopEvent
37} from './ark_utils';
38import { processSystemApi } from './validate_ui_syntax';
39import { isDebug } from './fast_build/ark_compiler/utils';
40import { getRelativeSourcePath } from './fast_build/ark_compiler/common/ob_config_resolver';
41
42export const SRC_MAIN: string = 'src/main';
43
44export async function writeFileSyncByNode(node: ts.SourceFile, projectConfig: Object, metaInfo: Object, moduleId?: string,
45  parentEvent?: Object, logger?: Object): Promise<void> {
46  const eventWriteFileSyncByNode = createAndStartEvent(parentEvent, 'write file sync by node');
47  const eventGenContentAndSourceMapInfo = createAndStartEvent(eventWriteFileSyncByNode, 'generate content and source map information');
48  const mixedInfo: { content: string, sourceMapJson: ts.RawSourceMap } = genContentAndSourceMapInfo(node, moduleId, projectConfig, metaInfo);
49  const sourceMapGenerator = SourceMapGenerator.getInstance();
50  stopEvent(eventGenContentAndSourceMapInfo);
51
52  /**
53   * In the following situation:
54   * A typescript source file whose name is 'Test.ts', which is used via `import xxx for 'test'` in another source file.
55
56   * The value of "node.fileName" consists of "test.ts", which does not correspond with the source file's actual name and would lead to a compilation error.
57   * The value of moduleId is same as the actual file name, so it would be used here for locating the target source file.
58
59   * Note: current realization is related to the moduleId mechanism in the rollup framework, which is needed to be reconsidered to improve the code robustness.
60   * In the current realization, when moduleId mechanism is changed, there would be a compilation error.
61   */
62  let temporaryFile: string = genTemporaryPath(moduleId ? moduleId : node.fileName, projectConfig.projectPath, process.env.cachePath,
63    projectConfig, metaInfo);
64  if (temporaryFile.length === 0) {
65    return;
66  }
67  if (temporaryFile.endsWith(EXTNAME_ETS)) {
68    temporaryFile = temporaryFile.replace(/\.ets$/, EXTNAME_TS);
69  }
70  let relativeFilePath = getRelativeSourcePath(moduleId ? moduleId : node.fileName, projectConfig.projectRootPath, metaInfo?.belongProjectPath);
71  let sourceMaps: Object;
72  if (process.env.compileTool === 'rollup') {
73    const key = sourceMapGenerator.isNewSourceMaps() ? moduleId! : relativeFilePath;
74    sourceMapGenerator.fillSourceMapPackageInfo(moduleId!, mixedInfo.sourceMapJson);
75    sourceMapGenerator.updateSourceMap(key, mixedInfo.sourceMapJson);
76    sourceMaps = sourceMapGenerator.getSourceMaps();
77  } else {
78    webpackNewSourceMaps[relativeFilePath] = mixedInfo.sourceMapJson;
79    sourceMaps = webpackNewSourceMaps;
80  }
81  if (!isDebug(projectConfig)) {
82    const eventWriteObfuscatedSourceCode = createAndStartEvent(eventWriteFileSyncByNode, 'write obfuscated source code');
83    await writeObfuscatedSourceCode({
84        content: mixedInfo.content,
85        buildFilePath: temporaryFile,
86        relativeSourceFilePath: relativeFilePath,
87        originSourceFilePath: node.fileName,
88        rollupModuleId: moduleId ? moduleId : undefined
89      }, logger, projectConfig, sourceMaps);
90    stopEvent(eventWriteObfuscatedSourceCode);
91    return;
92  }
93  mkdirsSync(path.dirname(temporaryFile));
94  fs.writeFileSync(temporaryFile, mixedInfo.content);
95  stopEvent(eventWriteFileSyncByNode);
96}
97
98function genContentAndSourceMapInfo(node: ts.SourceFile, moduleId: string | undefined, projectConfig: Object, metaInfo: Object): Object {
99  const printer: ts.Printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
100  const options: ts.CompilerOptions = {
101    sourceMap: true
102  };
103  const mapOpions: Object = {
104    sourceMap: true,
105    inlineSourceMap: false,
106    inlineSources: false,
107    sourceRoot: '',
108    mapRoot: '',
109    extendedDiagnostics: false
110  };
111  const host: ts.CompilerHost = ts.createCompilerHost(options);
112  const fileName: string = moduleId ? moduleId : node.fileName;
113  // @ts-ignore
114  const sourceMapGenerator: ts.SourceMapGenerator = ts.createSourceMapGenerator(
115    host,
116    // @ts-ignore
117    ts.getBaseFileName(fileName),
118    '',
119    '',
120    mapOpions
121  );
122  // @ts-ignore
123  const writer: ts.EmitTextWriter = ts.createTextWriter(
124    // @ts-ignore
125    ts.getNewLineCharacter({ newLine: ts.NewLineKind.LineFeed, removeComments: false }));
126  printer.writeFile(node, writer, sourceMapGenerator);
127  const sourceMapJson: ts.RawSourceMap = sourceMapGenerator.toJSON();
128  sourceMapJson.sources = [
129    toUnixPath(fileName).startsWith(toUnixPath(projectConfig.projectRootPath)) ?
130    toUnixPath(fileName).replace(toUnixPath(projectConfig.projectRootPath) + '/', '') :
131    toUnixPath(fileName).replace(toUnixPath(metaInfo.belongProjectPath) + '/', '')
132  ];
133  let content: string = writer.getText();
134  if (process.env.compileTool !== 'rollup') {
135    content = transformModuleSpecifier(fileName, processSystemApi(content, true), projectConfig);
136  }
137
138  return {
139    content: content,
140    sourceMapJson: sourceMapJson
141  };
142}
143