107ac75b1Sopenharmony_ci/*
207ac75b1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
307ac75b1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
407ac75b1Sopenharmony_ci * you may not use this file except in compliance with the License.
507ac75b1Sopenharmony_ci * You may obtain a copy of the License at
607ac75b1Sopenharmony_ci *
707ac75b1Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
807ac75b1Sopenharmony_ci *
907ac75b1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1007ac75b1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1107ac75b1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1207ac75b1Sopenharmony_ci * See the License for the specific language governing permissions and
1307ac75b1Sopenharmony_ci * limitations under the License.
1407ac75b1Sopenharmony_ci */
1507ac75b1Sopenharmony_ci
1607ac75b1Sopenharmony_ciimport fs from 'fs';
1707ac75b1Sopenharmony_ciimport { createFilter } from '@rollup/pluginutils';
1807ac75b1Sopenharmony_ciimport { 
1907ac75b1Sopenharmony_ci  findVisualFile, 
2007ac75b1Sopenharmony_ci  visualTransform as processVisual 
2107ac75b1Sopenharmony_ci} from '../../process_visual';
2207ac75b1Sopenharmony_ciimport MagicString from 'magic-string';
2307ac75b1Sopenharmony_ciimport { PluginContext } from 'rollup';
2407ac75b1Sopenharmony_ciimport { projectConfig } from '../../../main';
2507ac75b1Sopenharmony_ci
2607ac75b1Sopenharmony_ciconst filter: any = createFilter(/(?<!\.d)\.ets$/);
2707ac75b1Sopenharmony_ci
2807ac75b1Sopenharmony_ciexport function visualTransform() {
2907ac75b1Sopenharmony_ci  return {
3007ac75b1Sopenharmony_ci    name: 'visualTransform',
3107ac75b1Sopenharmony_ci    transform(code: string, id: string) {
3207ac75b1Sopenharmony_ciif (!filter(id)) {
3307ac75b1Sopenharmony_ci        return null;
3407ac75b1Sopenharmony_ci      }
3507ac75b1Sopenharmony_ci      if (process.env.watchMode !== 'true' && 'esmodule' === projectConfig.compileMode) {
3607ac75b1Sopenharmony_ci        return null;
3707ac75b1Sopenharmony_ci      }
3807ac75b1Sopenharmony_ci      const logger = this.share.getLogger('visualTransform');
3907ac75b1Sopenharmony_ci      code = processVisual(code, id, logger);
4007ac75b1Sopenharmony_ci      const magicString = new MagicString(code);
4107ac75b1Sopenharmony_ci      return {
4207ac75b1Sopenharmony_ci        code,
4307ac75b1Sopenharmony_ci        map: magicString.generateMap({ hires: true })
4407ac75b1Sopenharmony_ci      };
4507ac75b1Sopenharmony_ci    },
4607ac75b1Sopenharmony_ci    shouldInvalidCache(this: PluginContext, options: any): boolean {
4707ac75b1Sopenharmony_ci      const moduleId: string = options.id;
4807ac75b1Sopenharmony_ci      if (!filter(moduleId) || !moduleId) {
4907ac75b1Sopenharmony_ci        return false;
5007ac75b1Sopenharmony_ci      }
5107ac75b1Sopenharmony_ci      const visualId: string = findVisualFile(moduleId);
5207ac75b1Sopenharmony_ci      if (!visualId || !fs.existsSync(visualId)) {
5307ac75b1Sopenharmony_ci        if (this.cache.has(visualId)) {
5407ac75b1Sopenharmony_ci          this.cache.delete(visualId);
5507ac75b1Sopenharmony_ci        }
5607ac75b1Sopenharmony_ci        return false;
5707ac75b1Sopenharmony_ci      }
5807ac75b1Sopenharmony_ci      const stat: fs.Stats = fs.statSync(visualId);
5907ac75b1Sopenharmony_ci      const currentTimestamp: number = stat.mtime.getTime();
6007ac75b1Sopenharmony_ci      if (!this.cache.has(visualId)) {
6107ac75b1Sopenharmony_ci        this.cache.set(visualId, currentTimestamp);
6207ac75b1Sopenharmony_ci        return true;
6307ac75b1Sopenharmony_ci      }
6407ac75b1Sopenharmony_ci      const lastTimestamp: number = this.cache.get(visualId);
6507ac75b1Sopenharmony_ci      this.cache.set(visualId, currentTimestamp);
6607ac75b1Sopenharmony_ci      if (currentTimestamp === lastTimestamp) {
6707ac75b1Sopenharmony_ci        return false;
6807ac75b1Sopenharmony_ci      }
6907ac75b1Sopenharmony_ci      return true;
7007ac75b1Sopenharmony_ci    }
7107ac75b1Sopenharmony_ci  }
7207ac75b1Sopenharmony_ci}