1/* 2 * Copyright (c) 2022 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/entry.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 banner: frameworkBannerForJSAPIMock 79}; 80 81rollup.rollup(configJSAPIMockInput).then(bundle => { 82 bundle.write(configJSAPIMockOutput).then(() => { 83 countSize(configJSAPIMockOutput.file); 84 }); 85}); 86 87function countSize(filePath) { 88 const file = path.relative(__dirname, filePath); 89 fs.stat(filePath, function (error, stats) { 90 if (error) { 91 console.error('file size is wrong'); 92 } else { 93 const size = (stats.size / 1024).toFixed(2) + 'KB'; 94 console.log(`generate snapshot file: ${file}...\nthe snapshot file size: ${size}...`); 95 } 96 }); 97} 98 99