• 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';
9import { projectConfig } from '../../../main';
10
11const filter: any = createFilter(/(?<!\.d)\.ets$/);
12
13export function visualTransform() {
14  return {
15    name: 'visualTransform',
16    transform(code: string, id: string) {
17      if (!filter(id)) {
18        return null;
19      }
20      if (process.env.watchMode !== 'true' && 'esmodule' === projectConfig.compileMode) {
21        return null;
22      }
23      const logger = this.share.getLogger('visualTransform');
24      code = processVisual(code, id, logger);
25      const magicString = new MagicString(code);
26      return {
27        code,
28        map: magicString.generateMap({ hires: true })
29      };
30    },
31    shouldInvalidCache(this: PluginContext, options: any): boolean {
32      const moduleId: string = options.id;
33      if (!filter(moduleId) || !moduleId) {
34        return false;
35      }
36      const visualId: string = findVisualFile(moduleId);
37      if (!visualId || !fs.existsSync(visualId)) {
38        if (this.cache.has(visualId)) {
39          this.cache.delete(visualId);
40        }
41        return false;
42      }
43      const stat: fs.Stats = fs.statSync(visualId);
44      const currentTimestamp: number = stat.mtime.getTime();
45      if (!this.cache.has(visualId)) {
46        this.cache.set(visualId, currentTimestamp);
47        return !(process.env.watchMode !== 'true' && 'esmodule' === projectConfig.compileMode);
48      }
49      const lastTimestamp: number = this.cache.get(visualId);
50      this.cache.set(visualId, currentTimestamp);
51      if (currentTimestamp === lastTimestamp) {
52        return false;
53      }
54      return true;
55    }
56  }
57}