17c804472Sopenharmony_ci/*
27c804472Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47c804472Sopenharmony_ci * you may not use this file except in compliance with the License.
57c804472Sopenharmony_ci * You may obtain a copy of the License at
67c804472Sopenharmony_ci *
77c804472Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87c804472Sopenharmony_ci *
97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c804472Sopenharmony_ci * See the License for the specific language governing permissions and
137c804472Sopenharmony_ci * limitations under the License.
147c804472Sopenharmony_ci */
157c804472Sopenharmony_ci
167c804472Sopenharmony_ciconst fs = require('fs');
177c804472Sopenharmony_ci
187c804472Sopenharmony_ciconst path = require('path');
197c804472Sopenharmony_ci
207c804472Sopenharmony_ciconst rollup = require('rollup');
217c804472Sopenharmony_ci
227c804472Sopenharmony_ciconst resolve = require('rollup-plugin-node-resolve');
237c804472Sopenharmony_ci
247c804472Sopenharmony_ciconst commonjs = require('rollup-plugin-commonjs');
257c804472Sopenharmony_ci
267c804472Sopenharmony_ciconst json = require('rollup-plugin-json');
277c804472Sopenharmony_ci
287c804472Sopenharmony_ciconst babel = require('rollup-plugin-babel');
297c804472Sopenharmony_ci
307c804472Sopenharmony_ciconst typescript = require('rollup-plugin-typescript2');
317c804472Sopenharmony_ci
327c804472Sopenharmony_ciconst { uglify } = require('rollup-plugin-uglify');
337c804472Sopenharmony_ci
347c804472Sopenharmony_ciconst {
357c804472Sopenharmony_ci  eslint
367c804472Sopenharmony_ci} = require('rollup-plugin-eslint');
377c804472Sopenharmony_ci
387c804472Sopenharmony_ciconst frameworkBanner = 'var global=this; var process={env:{}}; ' + 'var setTimeout=global.setTimeout;\n';
397c804472Sopenharmony_ci
407c804472Sopenharmony_ciconst frameworkBannerForJSAPIMock = 'var global=globalThis;';
417c804472Sopenharmony_ci
427c804472Sopenharmony_ciconst onwarn = warning => {
437c804472Sopenharmony_ci  // Silence circular dependency warning
447c804472Sopenharmony_ci  if (warning.code === 'CIRCULAR_DEPENDENCY') {
457c804472Sopenharmony_ci    return;
467c804472Sopenharmony_ci  }
477c804472Sopenharmony_ci  console.warn(`(!) ${warning.message}`);
487c804472Sopenharmony_ci};
497c804472Sopenharmony_ci
507c804472Sopenharmony_ciconst tsPlugin = typescript({
517c804472Sopenharmony_ci  tsconfig: path.resolve(__dirname, 'tsconfig.json'),
527c804472Sopenharmony_ci  check: true
537c804472Sopenharmony_ci});
547c804472Sopenharmony_ci
557c804472Sopenharmony_ciconst esPlugin = eslint({
567c804472Sopenharmony_ci  include: ['**/*.ts'],
577c804472Sopenharmony_ci  exclude: ['node_modules/**', 'lib/**']
587c804472Sopenharmony_ci});
597c804472Sopenharmony_ci
607c804472Sopenharmony_ciconst configJSAPIMockInput = {
617c804472Sopenharmony_ci  input: path.resolve(__dirname, 'runtime/main/extend/systemplugin/index.js'),
627c804472Sopenharmony_ci  onwarn,
637c804472Sopenharmony_ci  plugins: [
647c804472Sopenharmony_ci    esPlugin,
657c804472Sopenharmony_ci    tsPlugin,
667c804472Sopenharmony_ci    json(),
677c804472Sopenharmony_ci    resolve(),
687c804472Sopenharmony_ci    commonjs(),
697c804472Sopenharmony_ci    babel({
707c804472Sopenharmony_ci      exclude: 'node_moduels/**'
717c804472Sopenharmony_ci    })
727c804472Sopenharmony_ci  ]
737c804472Sopenharmony_ci};
747c804472Sopenharmony_ci
757c804472Sopenharmony_ciconst configJSAPIMockOutput = {
767c804472Sopenharmony_ci  file: path.resolve(__dirname, 'dist/jsMockSystemPlugin.js'),
777c804472Sopenharmony_ci  format: 'umd',
787c804472Sopenharmony_ci  treeshake: false,
797c804472Sopenharmony_ci  banner: frameworkBannerForJSAPIMock
807c804472Sopenharmony_ci};
817c804472Sopenharmony_ci
827c804472Sopenharmony_cirollup.rollup(configJSAPIMockInput).then(bundle => {
837c804472Sopenharmony_ci  bundle.write(configJSAPIMockOutput).then(() => {
847c804472Sopenharmony_ci    countSize(configJSAPIMockOutput.file);
857c804472Sopenharmony_ci    const fileContent = fs.readFileSync(configJSAPIMockOutput.file, 'utf-8');
867c804472Sopenharmony_ci    fs.writeFileSync(configJSAPIMockOutput.file, fileContent.replace(/CommonMethod\$\d*/g, 'CommonMethod'), 'utf-8');
877c804472Sopenharmony_ci  });
887c804472Sopenharmony_ci});
897c804472Sopenharmony_ci
907c804472Sopenharmony_cifunction countSize(filePath) {
917c804472Sopenharmony_ci  const file = path.relative(__dirname, filePath);
927c804472Sopenharmony_ci  fs.stat(filePath, function (error, stats) {
937c804472Sopenharmony_ci    if (error) {
947c804472Sopenharmony_ci      console.error('file size is wrong');
957c804472Sopenharmony_ci    } else {
967c804472Sopenharmony_ci      const KB_BYTE_LENGTH = 1024;
977c804472Sopenharmony_ci      const num = 2;
987c804472Sopenharmony_ci      const size = (stats.size / KB_BYTE_LENGTH).toFixed(num) + 'KB';
997c804472Sopenharmony_ci      console.log(`generate snapshot file: ${file}...\nthe snapshot file size: ${size}...`);
1007c804472Sopenharmony_ci    }
1017c804472Sopenharmony_ci  });
1027c804472Sopenharmony_ci}
1037c804472Sopenharmony_ci
104