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