@ray-js/build-plugin-router 1.2.20 → 1.2.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -23,6 +23,7 @@ const ejs_1 = __importDefault(require("ejs"));
23
23
  const shared_1 = require("@ray-js/shared");
24
24
  const url_1 = __importDefault(require("url"));
25
25
  const path_to_regexp_1 = require("path-to-regexp");
26
+ const theme_1 = __importDefault(require("./theme"));
26
27
  function ejsRender(file, data, options) {
27
28
  const str = fs_extra_1.default.readFileSync(file).toString();
28
29
  return ejs_1.default.render(str, data, Object.assign(Object.assign({}, options), { async: false }));
@@ -167,7 +168,8 @@ function normalizeTabBarConfig(config, routes, fields) {
167
168
  function PluginRouter(api) {
168
169
  let cacheThemeLocation = {};
169
170
  let timerId;
170
- return {
171
+ let themeLocation;
172
+ const plugin = {
171
173
  name: '@ray-js/build-plugin-router',
172
174
  setup() {
173
175
  shared_1.log.verbose(LOG_PREFIX, 'setup');
@@ -200,14 +202,16 @@ function PluginRouter(api) {
200
202
  clearTimeout(timerId);
201
203
  const { routesConfig, globalConfig } = this.readConfigFromConfigFile();
202
204
  // target 在入口处已经统一处理,tuya 会转成 thing
203
- const { target } = api.options;
205
+ const { target, watch } = api.options;
204
206
  // 不是这两端的,其他的默认为web端,tuya thing 其实为同一个端,统一成 thing
205
207
  if (!['wechat', 'thing'].includes(target)) {
206
208
  // web端逻辑已经移到 @ray-js/builder-web
207
209
  return;
208
210
  }
209
211
  const appConfig = this.getStandardAppConfigData(routesConfig, globalConfig);
210
- this.generateThemeConfig(appConfig);
212
+ themeLocation = appConfig.themeLocation;
213
+ if (watch)
214
+ generateThemeConfig();
211
215
  // FIX: 修复初始化时找不到配置文件
212
216
  // 初始化时,需要立即生成配置文件
213
217
  if (immediate) {
@@ -266,59 +270,66 @@ function PluginRouter(api) {
266
270
  * @param data
267
271
  */
268
272
  generateAppConfig(target, data) {
273
+ if (themeLocation) {
274
+ data.themeLocation = themeLocation.replace(/(?:\.{1,2}\/)*/, '');
275
+ }
269
276
  const templateFile = path_1.default.join(__dirname, '../templates/app.config.ejs');
270
277
  const context = ejsRender(templateFile, { appConfig: { [target]: data } });
271
278
  api.writeFile('src/app.config.ts', context);
272
279
  shared_1.log.verbose(LOG_PREFIX, `generate`, 'src/app.config.ts'.underline.green, `for wechat platform`);
273
280
  },
274
- // todo theme 逻辑不应该在这个文件
275
- generateThemeConfig(appConfig) {
276
- const src = this.checkThemeConfig(appConfig.themeLocation);
277
- if (cacheThemeLocation.source && cacheThemeLocation.source !== src) {
278
- fs_extra_1.default.unlinkSync(cacheThemeLocation.dest);
279
- fs_extra_1.default.unwatchFile(cacheThemeLocation.source);
280
- cacheThemeLocation = {};
281
- }
282
- if (src) {
283
- appConfig.themeLocation = path_1.default.basename(src);
284
- handle();
285
- // only watch
286
- if (api.options.watch) {
287
- fs_extra_1.default.watchFile(src, handle);
288
- }
289
- }
290
- function handle() {
291
- // eslint-disable-next-line prefer-const
292
- let { cwd, target } = api.options;
293
- // TODO 这里待优化
294
- if (target === 'thing') {
295
- /**
296
- * 又是历史原因
297
- * 因老项目生成的产物目录是tuya,所以就干脆是tuya,不然会出现既 tuya 又 thing 的目录
298
- * 将开发整懵逼
299
- */
300
- // @ts-ignore
301
- target = 'tuya';
302
- }
303
- const output = path_1.default.join(cwd, 'dist', target, path_1.default.basename(src));
304
- const data = fs_extra_1.default.readFileSync(src).toString('utf-8');
305
- fs_extra_1.default.outputFileSync(output, data, 'utf-8');
306
- cacheThemeLocation.dest = output;
307
- cacheThemeLocation.source = src;
281
+ };
282
+ // todo theme 逻辑不应该在这个文件
283
+ function generateThemeConfig() {
284
+ const src = checkThemeConfig(themeLocation);
285
+ if (cacheThemeLocation.source && cacheThemeLocation.source !== src) {
286
+ fs_extra_1.default.unlinkSync(cacheThemeLocation.dest);
287
+ fs_extra_1.default.unwatchFile(cacheThemeLocation.source);
288
+ cacheThemeLocation = {};
289
+ }
290
+ if (src) {
291
+ handle();
292
+ // only watch
293
+ if (api.options.watch) {
294
+ fs_extra_1.default.watchFile(src, handle);
308
295
  }
309
- },
310
- checkThemeConfig(src) {
311
- if (!src) {
312
- return;
296
+ }
297
+ function handle() {
298
+ // eslint-disable-next-line prefer-const
299
+ let { cwd, target } = api.options;
300
+ // TODO 这里待优化
301
+ if (target === 'thing') {
302
+ /**
303
+ * 又是历史原因
304
+ * 因老项目生成的产物目录是tuya,所以就干脆是tuya,不然会出现既 tuya 又 thing 的目录
305
+ * 将开发整懵逼
306
+ */
307
+ // @ts-ignore
308
+ target = 'tuya';
313
309
  }
314
- const { cwd, source } = api.options;
315
- const inputs = [path_1.default.resolve(cwd, source, src), path_1.default.resolve(cwd, src)];
316
- for (const input of inputs) {
317
- if (fs_extra_1.default.pathExistsSync(input)) {
318
- return input;
319
- }
310
+ const themeP = themeLocation.replace(/(?:\.{1,2}\/)*/, '');
311
+ const output = path_1.default.join(cwd, 'dist', target, themeP);
312
+ const data = fs_extra_1.default.readFileSync(src).toString('utf-8');
313
+ fs_extra_1.default.outputFileSync(output, data, 'utf-8');
314
+ cacheThemeLocation.dest = output;
315
+ cacheThemeLocation.source = src;
316
+ }
317
+ }
318
+ function checkThemeConfig(src) {
319
+ if (!src) {
320
+ return;
321
+ }
322
+ const { cwd, source } = api.options;
323
+ const inputs = [path_1.default.resolve(cwd, source, src), path_1.default.resolve(cwd, src)];
324
+ for (const input of inputs) {
325
+ if (fs_extra_1.default.pathExistsSync(input)) {
326
+ return input;
320
327
  }
321
- },
322
- };
328
+ }
329
+ }
330
+ if (!api.options.watch) {
331
+ plugin.configWebpack = (0, theme_1.default)(generateThemeConfig).configWebpack;
332
+ }
333
+ return plugin;
323
334
  }
324
335
  exports.default = PluginRouter;
package/lib/theme.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ declare const _default: (handle: any) => {
2
+ configWebpack({ config }: {
3
+ config: any;
4
+ }): void;
5
+ };
6
+ export default _default;
package/lib/theme.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class OnBuildTheme {
4
+ constructor(opts) {
5
+ this.options = opts;
6
+ }
7
+ apply(compiler) {
8
+ const { name, handle } = this.options;
9
+ compiler.hooks.afterEmit.tap(name, () => {
10
+ handle === null || handle === void 0 ? void 0 : handle();
11
+ });
12
+ }
13
+ }
14
+ exports.default = (handle) => {
15
+ const n = `build-theme-plugin`;
16
+ return {
17
+ configWebpack({ config }) {
18
+ config.plugin(n).use(OnBuildTheme, [{ name: n, handle }]);
19
+ },
20
+ };
21
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/build-plugin-router",
3
- "version": "1.2.20",
3
+ "version": "1.2.22",
4
4
  "description": "Ray build plugin for router",
5
5
  "keywords": [
6
6
  "ray"
@@ -21,7 +21,7 @@
21
21
  "watch": "tsc -p ./tsconfig.build.json --watch"
22
22
  },
23
23
  "dependencies": {
24
- "@ray-js/shared": "^1.2.20",
24
+ "@ray-js/shared": "^1.2.22",
25
25
  "chokidar": "^3.5.2",
26
26
  "colors": "1.4.0",
27
27
  "ejs": "^3.1.6",
@@ -30,7 +30,7 @@
30
30
  "url": "^0.11.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@ray-js/types": "^1.2.20"
33
+ "@ray-js/types": "^1.2.22"
34
34
  },
35
35
  "maintainers": [
36
36
  {
@@ -38,6 +38,6 @@
38
38
  "email": "tuyafe@tuya.com"
39
39
  }
40
40
  ],
41
- "gitHead": "51780fe53a42cb3cb982a5f08cc68d1baccd3cf6",
41
+ "gitHead": "a0fd2d86aaa677aa0b93188cbccda069dfee8de8",
42
42
  "repository": {}
43
43
  }