remaxjs / remax
1 1
import * as fs from 'fs';
2
import { loader } from 'webpack';
3 1
import { getNativeAssetOutputPath, replaceExtension } from '../utils/paths';
4 1
import VirtualEntry from './VirtualEntry';
5
import Builder from '../Builder';
6 1
import NativeAssets from '../NativeAssets';
7 1
import output from '../utils/output';
8 1
import { slash } from '@remax/shared';
9

10
interface Manifest {
11
  usingComponents?: Record<string, string>;
12
}
13

14 1
function normalizeName(name: string) {
15 1
  return name.replace(/@/g, '_');
16
}
17

18 1
export default class NativeEntry extends VirtualEntry {
19 1
  dependentEntries: NativeEntry[] = [];
20
  assets: NativeAssets;
21

22 1
  constructor(builder: Builder, name: string, filename: string) {
23 1
    super(builder, normalizeName(name), filename);
24 1
    this.assets = new NativeAssets(builder, filename);
25
  }
26

27 1
  getManifest() {
28 1
    const dependentEntries = this.getDependentEntries();
29 1
    const rawManifest = this.readRawManifest();
30 1
    const usingComponents: Manifest['usingComponents'] = rawManifest.usingComponents ?? {};
31 1
    dependentEntries.forEach((entry, name) => {
32 1
      usingComponents[name] = '/' + entry.name;
33
    });
34 1
    return {
35
      ...rawManifest,
36
      usingComponents,
37
    };
38
  }
39

40 1
  getDependentEntries() {
41 1
    const { usingComponents = {} } = this.readRawManifest();
42 1
    return Object.keys(usingComponents).reduce((acc: Map<string, NativeEntry>, name: string) => {
43 1
      const request: string = usingComponents[name];
44 1
      if (request && request.startsWith('plugin://')) {
45 1
        return acc;
46
      }
47 1
      const fileExist = ['.js', '.ts'].some(ext => {
48 1
        const filename = this.builder.projectPath.resolveAsset(request + ext, this.filename);
49 1
        if (filename && fs.existsSync(filename)) {
50 1
          if (slash(filename) === slash(this.filename)) {
51 1
            return true;
52
          }
53 1
          let entry = this.builder.entryCollection.nativeComponentEntries.get(filename);
54 1
          if (entry) {
55 1
            entry.updateSource();
56
          } else {
57 1
            const output = getNativeAssetOutputPath(replaceExtension(filename, ''), this.builder.options);
58 1
            entry = new NativeEntry(this.builder, output, filename);
59
          }
60 1
          acc.set(name, entry);
61 1
          return true;
62
        }
63 0
        return false;
64
      });
65

66 1
      if (!fileExist) {
67 0
        output.warn(`${request} can not be resolved in ${this.name}'s \`usingComponents\`.`);
68
      }
69

70 1
      return acc;
71
    }, new Map());
72
  }
73

74 1
  getAssets() {
75 1
    return this.assets.getAll();
76
  }
77

78 1
  watchAssets(loaderContext: loader.LoaderContext) {
79 1
    this.assets.getAll().forEach(asset => {
80 1
      loaderContext.addDependency(asset.filename);
81
    });
82 1
    loaderContext.addDependency(this.rawManifestFile);
83 1
    loaderContext.addDependency(this.filename);
84
  }
85

86 1
  get rawManifestFile() {
87 1
    return replaceExtension(this.filename, '.json');
88
  }
89

90 1
  private readRawManifest(): Manifest {
91 1
    const manifestFile = this.rawManifestFile;
92 1
    if (fs.existsSync(manifestFile)) {
93 1
      try {
94 1
        return JSON.parse(fs.readFileSync(manifestFile).toString());
95
      } catch (e) {
96 0
        return {};
97
      }
98
    }
99 1
    return {};
100
  }
101
}

Read our documentation on viewing source code .

Loading