index-new.js 5.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
const fs = require("fs");
const path = require("path");
fxy060608's avatar
fxy060608 已提交
3

fxy060608's avatar
fxy060608 已提交
4
const loaderUtils = require("loader-utils");
fxy060608's avatar
fxy060608 已提交
5

fxy060608's avatar
fxy060608 已提交
6 7 8 9 10
const {
  parsePages,
  normalizePath,
  parsePagesJson,
  parseManifestJson
fxy060608's avatar
fxy060608 已提交
11
} = require("@dcloudio/uni-cli-shared");
fxy060608's avatar
fxy060608 已提交
12 13 14 15 16

const {
  updateAppJson,
  updatePageJson,
  updateProjectJson
fxy060608's avatar
fxy060608 已提交
17
} = require("@dcloudio/uni-cli-shared/lib/cache");
fxy060608's avatar
fxy060608 已提交
18

fxy060608's avatar
fxy060608 已提交
19
const { initTheme, parseTheme } = require("@dcloudio/uni-cli-shared/lib/theme");
fxy060608's avatar
fxy060608 已提交
20

fxy060608's avatar
fxy060608 已提交
21
const {
fxy060608's avatar
fxy060608 已提交
22
  // pagesJsonJsFileName,
23
  initAutoImportComponents
fxy060608's avatar
fxy060608 已提交
24 25 26
} = require("@dcloudio/uni-cli-shared/lib/pages");

const uniI18n = require("@dcloudio/uni-cli-i18n");
fxy060608's avatar
fxy060608 已提交
27

fxy060608's avatar
fxy060608 已提交
28
const parseStyle = require("./util").parseStyle;
d-u-a's avatar
d-u-a 已提交
29

fxy060608's avatar
fxy060608 已提交
30 31
const { initI18nOptions } = require("@dcloudio/uni-cli-shared/lib/i18n");
const { parseI18nJson } = require("@dcloudio/uni-i18n");
fxy060608's avatar
fxy060608 已提交
32 33

// 将开发者手动设置的 usingComponents 调整名称,方便与自动解析到的 usingComponents 做最后合并
fxy060608's avatar
fxy060608 已提交
34
function renameUsingComponents(jsonObj) {
fxy060608's avatar
fxy060608 已提交
35
  if (jsonObj.usingComponents) {
fxy060608's avatar
fxy060608 已提交
36 37
    jsonObj.customUsingComponents = jsonObj.usingComponents;
    delete jsonObj.usingComponents;
fxy060608's avatar
fxy060608 已提交
38
  }
fxy060608's avatar
fxy060608 已提交
39
  return jsonObj;
fxy060608's avatar
fxy060608 已提交
40 41
}

fxy060608's avatar
fxy060608 已提交
42 43
module.exports = function(content, map) {
  this.cacheable && this.cacheable();
fxy060608's avatar
fxy060608 已提交
44

fxy060608's avatar
fxy060608 已提交
45
  initTheme();
fxy060608's avatar
fxy060608 已提交
46

fxy060608's avatar
fxy060608 已提交
47
  let isAppView = false;
fxy060608's avatar
fxy060608 已提交
48
  if (this.resourceQuery) {
fxy060608's avatar
fxy060608 已提交
49 50
    const params = loaderUtils.parseQuery(this.resourceQuery);
    isAppView = params.type === "view";
fxy060608's avatar
fxy060608 已提交
51 52
  }

fxy060608's avatar
fxy060608 已提交
53
  // const pagesJsonJsPath = path.resolve(process.env.UNI_INPUT_DIR, pagesJsonJsFileName)
fxy060608's avatar
fxy060608 已提交
54 55 56 57 58 59 60
  const manifestJsonPath = path.resolve(
    process.env.UNI_INPUT_DIR,
    "manifest.json"
  );
  const manifestJson = parseManifestJson(
    fs.readFileSync(manifestJsonPath, "utf8")
  );
fxy060608's avatar
fxy060608 已提交
61

fxy060608's avatar
fxy060608 已提交
62
  // this.addDependency(pagesJsonJsPath)
fxy060608's avatar
fxy060608 已提交
63
  this.addDependency(manifestJsonPath);
fxy060608's avatar
fxy060608 已提交
64

fxy060608's avatar
fxy060608 已提交
65
  let pagesJson = parsePagesJson(content, {
fxy060608's avatar
fxy060608 已提交
66 67 68 69 70
    addDependency: file => {
      (process.UNI_PAGES_DEPS || (process.UNI_PAGES_DEPS = new Set())).add(
        normalizePath(file)
      );
      this.addDependency(file);
fxy060608's avatar
fxy060608 已提交
71
    }
fxy060608's avatar
fxy060608 已提交
72
  });
73

74
  if (!pagesJson.pages || pagesJson.pages.length === 0) {
fxy060608's avatar
fxy060608 已提交
75 76
    console.error(uniI18n.__("pagesLoader.pagesNodeCannotNull"));
    process.exit(0);
77 78
  }

fxy060608's avatar
fxy060608 已提交
79
  if (global.uniPlugin.defaultTheme) {
fxy060608's avatar
fxy060608 已提交
80 81
    pagesJson = parseTheme(pagesJson);
    this.addDependency(path.resolve(process.env.UNI_INPUT_DIR, "theme.json"));
fxy060608's avatar
fxy060608 已提交
82 83
  }

84
  // 组件自动导入配置
fxy060608's avatar
fxy060608 已提交
85 86 87 88
  process.UNI_AUTO_SCAN_COMPONENTS = !(
    pagesJson.easycom && pagesJson.easycom.autoscan === false
  );
  initAutoImportComponents(pagesJson.easycom);
89

fxy060608's avatar
fxy060608 已提交
90 91
  // TODO 与 usingComponents 放在一块读取设置
  if (manifestJson.transformPx === false) {
fxy060608's avatar
fxy060608 已提交
92
    process.UNI_TRANSFORM_PX = false;
fxy060608's avatar
fxy060608 已提交
93
  } else {
fxy060608's avatar
fxy060608 已提交
94
    process.UNI_TRANSFORM_PX = true;
fxy060608's avatar
fxy060608 已提交
95 96
  }

fxy060608's avatar
fxy060608 已提交
97 98 99 100 101 102
  if (process.env.UNI_PLATFORM === "h5") {
    return this.callback(
      null,
      require("./platforms/h5")(pagesJson, manifestJson, this),
      map
    );
fxy060608's avatar
fxy060608 已提交
103
  }
fxy060608's avatar
fxy060608 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  if (process.env.UNI_PLATFORM === "quickapp-native") {
    return this.callback(
      null,
      require("./platforms/quickapp-native")(pagesJson, manifestJson, this),
      map
    );
  }
  // 仅限小程序
  if (process.env.UNI_PLATFORM !== "app-plus") {
    const i18nOptions = initI18nOptions(
      process.env.UNI_PLATFORM,
      process.env.UNI_INPUT_DIR,
      true,
      true
    );
    if (i18nOptions) {
      const { locale, locales, delimiters } = i18nOptions;
      parseI18nJson(pagesJson, locales[locale], delimiters);
    }
fxy060608's avatar
fxy060608 已提交
123
  }
fxy060608's avatar
init v3  
fxy060608 已提交
124
  if (!process.env.UNI_USING_V3) {
fxy060608's avatar
fxy060608 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    parsePages(
      pagesJson,
      function(page) {
        updatePageJson(
          page.path,
          renameUsingComponents(parseStyle(page.style))
        );
      },
      function(root, page) {
        updatePageJson(
          normalizePath(path.join(root, page.path)),
          renameUsingComponents(parseStyle(page.style, root))
        );
      }
    );
fxy060608's avatar
init v3  
fxy060608 已提交
140
  }
fxy060608's avatar
fxy060608 已提交
141

fxy060608's avatar
fxy060608 已提交
142 143 144 145 146
  const jsonFiles = require("./platforms/" + process.env.UNI_PLATFORM)(
    pagesJson,
    manifestJson,
    isAppView
  );
fxy060608's avatar
fxy060608 已提交
147

fxy060608's avatar
fxy060608 已提交
148
  if (jsonFiles && jsonFiles.length) {
fxy060608's avatar
fxy060608 已提交
149
    if (process.env.UNI_USING_V3) {
fxy060608's avatar
fxy060608 已提交
150
      let appConfigContent = "";
fxy060608's avatar
fxy060608 已提交
151 152
      jsonFiles.forEach(jsonFile => {
        if (jsonFile) {
fxy060608's avatar
fxy060608 已提交
153 154 155 156 157 158 159 160 161 162 163
          if (!isAppView && jsonFile.name === "manifest.json") {
            const content = JSON.parse(jsonFile.content);
            if (
              !content.launch_path &&
              content.plus["uni-app"].nvueLaunchMode === "fast"
            ) {
              console.log(
                uniI18n.__("pagesLoader.nvueFirstPageStartModeIsFast", {
                  0: "https://ask.dcloud.net.cn/article/36749"
                })
              );
Q
qiang 已提交
164 165
            }
          }
fxy060608's avatar
fxy060608 已提交
166 167
          if (jsonFile.name === "define-pages.js") {
            appConfigContent = jsonFile.content;
168 169
          } else {
            // app-view 不需要生成 app-config-service.js,manifest.json
fxy060608's avatar
fxy060608 已提交
170
            !isAppView && this.emitFile(jsonFile.name, jsonFile.content);
fxy060608's avatar
fxy060608 已提交
171 172
          }
        }
fxy060608's avatar
fxy060608 已提交
173 174
      });
      return this.callback(null, appConfigContent, map);
fxy060608's avatar
fxy060608 已提交
175
    }
fxy060608's avatar
fxy060608 已提交
176
    if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
fxy060608's avatar
fxy060608 已提交
177
      let appConfigContent = "";
fxy060608's avatar
fxy060608 已提交
178 179
      jsonFiles.forEach(jsonFile => {
        if (jsonFile) {
fxy060608's avatar
fxy060608 已提交
180 181 182 183 184
          if (
            jsonFile.name === "app-config.js" ||
            jsonFile.name === "define-pages.js"
          ) {
            appConfigContent = jsonFile.content;
fxy060608's avatar
fxy060608 已提交
185
          } else {
fxy060608's avatar
fxy060608 已提交
186
            this.emitFile(jsonFile.name, jsonFile.content);
fxy060608's avatar
fxy060608 已提交
187 188
          }
        }
fxy060608's avatar
fxy060608 已提交
189 190
      });
      return this.callback(null, appConfigContent, map);
fxy060608's avatar
fxy060608 已提交
191 192 193 194
    }

    jsonFiles.forEach(jsonFile => {
      if (jsonFile) {
fxy060608's avatar
fxy060608 已提交
195 196
        if (jsonFile.name === "app") {
          updateAppJson(jsonFile.name, renameUsingComponents(jsonFile.content));
fxy060608's avatar
fxy060608 已提交
197
        } else {
fxy060608's avatar
fxy060608 已提交
198
          updateProjectJson(jsonFile.name, jsonFile.content);
fxy060608's avatar
fxy060608 已提交
199 200
        }
      }
fxy060608's avatar
fxy060608 已提交
201
    });
fxy060608's avatar
fxy060608 已提交
202 203
  }

fxy060608's avatar
fxy060608 已提交
204 205
  this.callback(null, "", map);
};