• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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 onwarn = warning => {
41  // Silence circular dependency warning
42  if (warning.code === 'CIRCULAR_DEPENDENCY') {
43    return;
44  }
45  console.warn(`(!) ${warning.message}`);
46};
47
48const tsPlugin = typescript({
49  tsconfig: path.resolve(__dirname, 'tsconfig.json'),
50  check: true
51});
52
53const esPlugin = eslint({
54  include: ['**/*.ts'],
55  exclude: ['node_modules/**', 'lib/**']
56});
57
58const configInput = {
59  input: path.resolve(__dirname, 'runtime/preparation/index.ts'),
60  onwarn,
61  plugins: [
62    esPlugin,
63    tsPlugin,
64    json(),
65    resolve(),
66    commonjs(),
67    babel({
68      exclude: 'node_moduels/**'
69    }),
70    uglify()
71  ]
72};
73
74const configJSAPIMockInput = {
75  input: path.resolve(__dirname, 'runtime/main/extend/systemplugin/entry.js'),
76  onwarn,
77  plugins: [
78    esPlugin,
79    tsPlugin,
80    json(),
81    resolve(),
82    commonjs(),
83    babel({
84      exclude: 'node_moduels/**'
85    })
86  ]
87};
88
89const configOutput = {
90  file: path.resolve(__dirname, 'dist/strip.native.min.js'),
91  format: 'umd',
92  banner: frameworkBanner
93};
94
95const configJSAPIMockOutput = {
96  file: path.resolve(__dirname, 'dist/jsMockSystemPlugin.js'),
97  format: 'umd'
98};
99
100rollup.rollup(configInput).then(bundle => {
101  bundle.write(configOutput).then(() => {
102    countSize(configOutput.file);
103  });
104});
105
106rollup.rollup(configJSAPIMockInput).then(bundle => {
107  bundle.write(configJSAPIMockOutput).then(() => {
108    countSize(configJSAPIMockOutput.file);
109  });
110});
111
112function countSize(filePath) {
113  const file = path.relative(__dirname, filePath);
114  fs.stat(filePath, function(error, stats) {
115    if (error) {
116      console.error('file size is wrong');
117    } else {
118      const size = (stats.size / 1024).toFixed(2) + 'KB';
119      console.log(`generate snapshot file: ${file}...\nthe snapshot file size: ${size}...`);
120    }
121  });
122}
123
124