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