1e484b35bSopenharmony_ci/*
2e484b35bSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3e484b35bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e484b35bSopenharmony_ci * you may not use this file except in compliance with the License.
5e484b35bSopenharmony_ci * You may obtain a copy of the License at
6e484b35bSopenharmony_ci *
7e484b35bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e484b35bSopenharmony_ci *
9e484b35bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e484b35bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e484b35bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e484b35bSopenharmony_ci * See the License for the specific language governing permissions and
13e484b35bSopenharmony_ci * limitations under the License.
14e484b35bSopenharmony_ci */
15e484b35bSopenharmony_ci
16e484b35bSopenharmony_ciconst fs = require('fs');
17e484b35bSopenharmony_ci
18e484b35bSopenharmony_ciconst path = require('path');
19e484b35bSopenharmony_ci
20e484b35bSopenharmony_ciconst rollup = require('rollup');
21e484b35bSopenharmony_ci
22e484b35bSopenharmony_ciconst resolve = require('rollup-plugin-node-resolve');
23e484b35bSopenharmony_ci
24e484b35bSopenharmony_ciconst commonjs = require('rollup-plugin-commonjs');
25e484b35bSopenharmony_ci
26e484b35bSopenharmony_ciconst json = require('rollup-plugin-json');
27e484b35bSopenharmony_ci
28e484b35bSopenharmony_ciconst babel = require('rollup-plugin-babel');
29e484b35bSopenharmony_ci
30e484b35bSopenharmony_ciconst typescript = require('rollup-plugin-typescript2');
31e484b35bSopenharmony_ci
32e484b35bSopenharmony_ciconst { uglify } = require('rollup-plugin-uglify');
33e484b35bSopenharmony_ci
34e484b35bSopenharmony_ciconst {
35e484b35bSopenharmony_ci  eslint
36e484b35bSopenharmony_ci} = require('rollup-plugin-eslint');
37e484b35bSopenharmony_ci
38e484b35bSopenharmony_ciconst frameworkBanner = `var global=this; var process={env:{}}; ` + `var setTimeout=global.setTimeout;\n`;
39e484b35bSopenharmony_ci
40e484b35bSopenharmony_ciconst frameworkBannerForJSAPIMock = `var global=globalThis;`;
41e484b35bSopenharmony_ci
42e484b35bSopenharmony_ciconst onwarn = warning => {
43e484b35bSopenharmony_ci  // Silence circular dependency warning
44e484b35bSopenharmony_ci  if (warning.code === 'CIRCULAR_DEPENDENCY') {
45e484b35bSopenharmony_ci    return;
46e484b35bSopenharmony_ci  }
47e484b35bSopenharmony_ci  console.warn(`(!) ${warning.message}`);
48e484b35bSopenharmony_ci};
49e484b35bSopenharmony_ci
50e484b35bSopenharmony_ciconst tsPlugin = typescript({
51e484b35bSopenharmony_ci  tsconfig: path.resolve(__dirname, 'tsconfig.json'),
52e484b35bSopenharmony_ci  check: true
53e484b35bSopenharmony_ci});
54e484b35bSopenharmony_ci
55e484b35bSopenharmony_ciconst esPlugin = eslint({
56e484b35bSopenharmony_ci  include: ['**/*.ts'],
57e484b35bSopenharmony_ci  exclude: ['node_modules/**', 'lib/**']
58e484b35bSopenharmony_ci});
59e484b35bSopenharmony_ci
60e484b35bSopenharmony_ciconst configInput = {
61e484b35bSopenharmony_ci  input: path.resolve(__dirname, 'runtime/preparation/index.ts'),
62e484b35bSopenharmony_ci  onwarn,
63e484b35bSopenharmony_ci  plugins: [
64e484b35bSopenharmony_ci    esPlugin,
65e484b35bSopenharmony_ci    tsPlugin,
66e484b35bSopenharmony_ci    json(),
67e484b35bSopenharmony_ci    resolve(),
68e484b35bSopenharmony_ci    commonjs(),
69e484b35bSopenharmony_ci    babel({
70e484b35bSopenharmony_ci      exclude: 'node_moduels/**'
71e484b35bSopenharmony_ci    }),
72e484b35bSopenharmony_ci    uglify()
73e484b35bSopenharmony_ci  ]
74e484b35bSopenharmony_ci};
75e484b35bSopenharmony_ci
76e484b35bSopenharmony_ciconst configOutput = {
77e484b35bSopenharmony_ci  file: path.resolve(__dirname, 'dist/strip.native.min.js'),
78e484b35bSopenharmony_ci  format: 'umd',
79e484b35bSopenharmony_ci  banner: frameworkBanner
80e484b35bSopenharmony_ci};
81e484b35bSopenharmony_ci
82e484b35bSopenharmony_cirollup.rollup(configInput).then(bundle => {
83e484b35bSopenharmony_ci  bundle.write(configOutput).then(() => {
84e484b35bSopenharmony_ci    countSize(configOutput.file);
85e484b35bSopenharmony_ci  });
86e484b35bSopenharmony_ci});
87e484b35bSopenharmony_ci
88e484b35bSopenharmony_cifunction countSize(filePath) {
89e484b35bSopenharmony_ci  const file = path.relative(__dirname, filePath);
90e484b35bSopenharmony_ci  fs.stat(filePath, function (error, stats) {
91e484b35bSopenharmony_ci    if (error) {
92e484b35bSopenharmony_ci      console.error('file size is wrong');
93e484b35bSopenharmony_ci    } else {
94e484b35bSopenharmony_ci      const size = (stats.size / 1024).toFixed(2) + 'KB';
95e484b35bSopenharmony_ci      console.log(`generate snapshot file: ${file}...\nthe snapshot file size: ${size}...`);
96e484b35bSopenharmony_ci    }
97e484b35bSopenharmony_ci  });
98e484b35bSopenharmony_ci}
99e484b35bSopenharmony_ci
100