16a23e08bSopenharmony_ci/* 26a23e08bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 36a23e08bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 46a23e08bSopenharmony_ci * you may not use this file except in compliance with the License. 56a23e08bSopenharmony_ci * You may obtain a copy of the License at 66a23e08bSopenharmony_ci * 76a23e08bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 86a23e08bSopenharmony_ci * 96a23e08bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 106a23e08bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 116a23e08bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 126a23e08bSopenharmony_ci * See the License for the specific language governing permissions and 136a23e08bSopenharmony_ci * limitations under the License. 146a23e08bSopenharmony_ci */ 156a23e08bSopenharmony_ci 166a23e08bSopenharmony_ciconst _require = require('child_process'); 176a23e08bSopenharmony_ciconst exec = _require.exec; 186a23e08bSopenharmony_ciconst _path = require('path'); 196a23e08bSopenharmony_ciconst snapshot = _path.join(__dirname, '..', '..', 'bin', 'jerry-snapshot'); 206a23e08bSopenharmony_ci/** 216a23e08bSopenharmony_ci * Convert Javascript file to snapshot. 226a23e08bSopenharmony_ci */ 236a23e08bSopenharmony_ciclass SnapshotPlugin { 246a23e08bSopenharmony_ci /** 256a23e08bSopenharmony_ci * constructor of the SnapshotPlugin class. 266a23e08bSopenharmony_ci * @param {String} options The object of build folder. 276a23e08bSopenharmony_ci */ 286a23e08bSopenharmony_ci constructor(options) { 296a23e08bSopenharmony_ci this.options = options; 306a23e08bSopenharmony_ci } 316a23e08bSopenharmony_ci /** 326a23e08bSopenharmony_ci * Find all javascript file paths in the directory. 336a23e08bSopenharmony_ci * @param {Object} assets the object of javascript file path. 346a23e08bSopenharmony_ci * @param {String} buildPath The path of build folder. 356a23e08bSopenharmony_ci * @return {Array} Image path array. 366a23e08bSopenharmony_ci */ 376a23e08bSopenharmony_ci getDir(assets, buildPath) { 386a23e08bSopenharmony_ci const pathArray = []; 396a23e08bSopenharmony_ci Object.keys(assets).map((item) => { 406a23e08bSopenharmony_ci if (/.js$/.test(item)) { 416a23e08bSopenharmony_ci pathArray.push(_path.join(buildPath, item)); 426a23e08bSopenharmony_ci } 436a23e08bSopenharmony_ci }); 446a23e08bSopenharmony_ci return pathArray; 456a23e08bSopenharmony_ci }; 466a23e08bSopenharmony_ci /** 476a23e08bSopenharmony_ci * Convert javascript file asynchronously. If an error occurs, print an error message. 486a23e08bSopenharmony_ci * @param {Object} compiler API specification, all configuration information of Webpack environment. 496a23e08bSopenharmony_ci */ 506a23e08bSopenharmony_ci apply(compiler) { 516a23e08bSopenharmony_ci const buildPath = this.options.build; 526a23e08bSopenharmony_ci compiler.hooks.done.tap('snapshot coverter', (stats) => { 536a23e08bSopenharmony_ci const pathArray = this.getDir(stats.compilation.assets, buildPath); 546a23e08bSopenharmony_ci pathArray.forEach((element) => { 556a23e08bSopenharmony_ci const bcPath = element.replace('.js', '.bc'); 566a23e08bSopenharmony_ci const fileName = _path.basename(element); 576a23e08bSopenharmony_ci exec(`"${snapshot}" generate -o "${bcPath}" "${element}"`, (error) => { 586a23e08bSopenharmony_ci if (error) { 596a23e08bSopenharmony_ci console.error('\u001b[31m', `Failed to convert the ${fileName} file to a snapshot.`, '\u001b[39m'); 606a23e08bSopenharmony_ci } 616a23e08bSopenharmony_ci }); 626a23e08bSopenharmony_ci }); 636a23e08bSopenharmony_ci }); 646a23e08bSopenharmony_ci } 656a23e08bSopenharmony_ci} 666a23e08bSopenharmony_cimodule.exports = SnapshotPlugin; 67