• 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
16const startTime = Date.now();
17
18const fs = require('fs');
19
20const path = require('path');
21
22const rollup = require('rollup');
23
24const resolve = require('rollup-plugin-node-resolve');
25
26const commonjs = require('rollup-plugin-commonjs');
27
28const json = require('rollup-plugin-json');
29
30const babel = require('rollup-plugin-babel');
31
32const typescript = require('rollup-plugin-typescript2');
33
34const { uglify } = require('rollup-plugin-uglify');
35
36const {
37  eslint
38} = require('rollup-plugin-eslint');
39
40const frameworkBanner = 'var global=this; var process={env:{}}; ' + 'var setTimeout=global.setTimeout;\n';
41
42const frameworkBannerForJSAPIMock = 'var global=globalThis;';
43
44const onwarn = warning => {
45  // Silence circular dependency warning
46  if (warning.code === 'CIRCULAR_DEPENDENCY') {
47    return;
48  }
49  console.warn(`(!) ${warning.message}`);
50};
51
52const tsPlugin = typescript({
53  tsconfig: path.resolve(__dirname, 'tsconfig.json'),
54  check: true
55});
56
57const esPlugin = eslint({
58  include: ['**/*.ts'],
59  exclude: ['node_modules/**', 'lib/**']
60});
61
62const configJSAPIMockInput = {
63  input: path.resolve(__dirname, 'runtime/main/extend/systemplugin/index.js'),
64  onwarn,
65  plugins: [
66    esPlugin,
67    tsPlugin,
68    json(),
69    resolve(),
70    commonjs(),
71    babel({
72      exclude: 'node_moduels/**'
73    })
74  ]
75};
76
77const configJSAPIMockOutput = {
78  file: path.resolve(__dirname, 'dist/jsMockSystemPlugin.js'),
79  format: 'umd',
80  banner: frameworkBannerForJSAPIMock
81};
82
83rollup.rollup(configJSAPIMockInput).then(bundle => {
84  bundle.write(configJSAPIMockOutput).then(() => {
85    countSize(configJSAPIMockOutput.file);
86    let fileContent = fs.readFileSync(configJSAPIMockOutput.file, 'utf-8');
87    const tipTemp = 'var ts = "The {{}} interface in the previewer is a mocked implementation and may behave differently than on a real device.";\n';
88    fileContent = tipTemp + fileContent;
89    fs.writeFileSync(configJSAPIMockOutput.file, fileContent, 'utf-8');
90  });
91});
92
93function countSize(filePath) {
94  const file = path.relative(__dirname, filePath);
95  fs.stat(filePath, function(error, stats) {
96    if (error) {
97      console.error('file size is wrong');
98    } else {
99      const KB_BYTE_LENGTH = 1024;
100      const num = 2;
101      const size = (stats.size / KB_BYTE_LENGTH).toFixed(num) + 'KB';
102      console.log(`generate snapshot file: ${file}...\nthe snapshot file size: ${size}...`);
103      const endTime = Date.now();
104      console.log(`耗时:${(endTime - startTime) / 1000}秒`);
105    }
106  });
107}
108