• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import fs from 'fs';
2import { createFilter } from '@rollup/pluginutils';
3import {
4  findVisualFile,
5  visualTransform as processVisual
6} from '../../process_visual';
7import MagicString from 'magic-string';
8import { PluginContext } from 'rollup';
9
10const filter: any = createFilter(/(?<!\.d)\.ets$/);
11
12export function visualTransform() {
13  return {
14    name: 'visualTransform',
15    transform(code: string, id: string) {
16      if (!filter(id)) {
17        return null;
18      }
19      const logger = this.share.getLogger('visualTransform');
20      code = processVisual(code, id, logger);
21      const magicString = new MagicString(code);
22      return {
23        code,
24        map: magicString.generateMap({ hires: true })
25      };
26    },
27    shouldInvalidCache(this: PluginContext, options: any): boolean {
28      const moduleId: string = options.id;
29      if (!filter(moduleId) || !moduleId) {
30        return false;
31      }
32      const visualId: string = findVisualFile(moduleId);
33      if (!visualId || !fs.existsSync(visualId)) {
34        if (this.cache.has(visualId)) {
35          this.cache.delete(visualId);
36        }
37        return false;
38      }
39      const stat: fs.Stats = fs.statSync(visualId);
40      const currentTimestamp: number = stat.mtime.getTime();
41      if (!this.cache.has(visualId)) {
42        this.cache.set(visualId, currentTimestamp);
43        return true;
44      }
45      const lastTimestamp: number = this.cache.get(visualId);
46      this.cache.set(visualId, currentTimestamp);
47      if (currentTimestamp === lastTimestamp) {
48        return false;
49      }
50      return true;
51    }
52  }
53}