1 4
import * as fs from 'fs';
2 4
import path from 'path';
3
import { Platform } from '@remax/types';
4

5 4
function readTypescriptManifest(path: string, target: Platform) {
6 4
  require('@babel/register')({
7
    presets: [
8
      [require.resolve('@babel/preset-env'), { modules: 'commonjs' }],
9
      require.resolve('@babel/preset-typescript'),
10
    ],
11
    extensions: ['.ts'],
12
    cache: false,
13
  });
14 4
  delete require.cache[path];
15 4
  const config = require(path)[target] || require(path).default || require(path);
16

17 4
  return config;
18
}
19

20 4
function readJavascriptManifest(path: string, target: Platform) {
21 4
  delete require.cache[path];
22 4
  const config = require(path)[target] || require(path).default || require(path);
23

24 4
  return config;
25
}
26

27 4
export default function readManifest(filename: string, target: Platform, strict = false) {
28 4
  if (!fs.existsSync(filename)) {
29 4
    if (strict) {
30 4
      throw new Error(`${path}.ts|js 文件不存在,请先创建配置文件,参考 https://remaxjs.org/guide/config`);
31
    }
32 4
    return {};
33
  }
34 4
  if (path.extname(filename) === '.ts') {
35 4
    return readTypescriptManifest(filename, target);
36
  } else {
37 4
    return readJavascriptManifest(filename, target);
38
  }
39
}

Read our documentation on viewing source code .

Loading