@ray-js/build-plugin-router 1.3.1-beta.7 → 1.3.2

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) {
@@ -241,13 +245,19 @@ function PluginRouter(api) {
241
245
  const subPkgs = (_d = subPackages !== null && subPackages !== void 0 ? subPackages : subpackages) !== null && _d !== void 0 ? _d : [];
242
246
  const pages = lodash_1.default.uniq(_routes.map((r) => r.path.replace(/^\//, '')));
243
247
  const tabBar = normalizeTabBarConfig(_tabBar, _routes, standardAppConfigFieldMap);
244
- let workers = otherConfig.workers;
245
- if (workers && typeof workers !== 'string') {
246
- workers = workers.root;
247
- }
248
+ const { workers = [], root: workerRoot } = otherConfig.workers || {};
248
249
  const config = Object.assign(Object.assign(Object.assign({}, g), otherConfig), { pages, tabBar });
249
- if (workers) {
250
- config.workers = workers;
250
+ if (workerRoot) {
251
+ config.workers =
252
+ target !== 'thing'
253
+ ? workerRoot
254
+ : workers.map((w) => {
255
+ const ext = path_1.default.extname(w);
256
+ if (ext) {
257
+ w = w.replace(new RegExp(`${ext}$`), '');
258
+ }
259
+ return `${workerRoot}/${w}.js`;
260
+ });
251
261
  }
252
262
  if (subPkgs && subPkgs.length) {
253
263
  config.subpackages = subPkgs.map((pkg) => {
@@ -266,59 +276,66 @@ function PluginRouter(api) {
266
276
  * @param data
267
277
  */
268
278
  generateAppConfig(target, data) {
279
+ if (themeLocation) {
280
+ data.themeLocation = themeLocation.replace(/(?:\.{1,2}\/)*/, '');
281
+ }
269
282
  const templateFile = path_1.default.join(__dirname, '../templates/app.config.ejs');
270
283
  const context = ejsRender(templateFile, { appConfig: { [target]: data } });
271
284
  api.writeFile('src/app.config.ts', context);
272
285
  shared_1.log.verbose(LOG_PREFIX, `generate`, 'src/app.config.ts'.underline.green, `for wechat platform`);
273
286
  },
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;
287
+ };
288
+ // todo theme 逻辑不应该在这个文件
289
+ function generateThemeConfig() {
290
+ const src = checkThemeConfig(themeLocation);
291
+ if (cacheThemeLocation.source && cacheThemeLocation.source !== src) {
292
+ fs_extra_1.default.unlinkSync(cacheThemeLocation.dest);
293
+ fs_extra_1.default.unwatchFile(cacheThemeLocation.source);
294
+ cacheThemeLocation = {};
295
+ }
296
+ if (src) {
297
+ handle();
298
+ // only watch
299
+ if (api.options.watch) {
300
+ fs_extra_1.default.watchFile(src, handle);
308
301
  }
309
- },
310
- checkThemeConfig(src) {
311
- if (!src) {
312
- return;
302
+ }
303
+ function handle() {
304
+ // eslint-disable-next-line prefer-const
305
+ let { cwd, target } = api.options;
306
+ // TODO 这里待优化
307
+ if (target === 'thing') {
308
+ /**
309
+ * 又是历史原因
310
+ * 因老项目生成的产物目录是tuya,所以就干脆是tuya,不然会出现既 tuya 又 thing 的目录
311
+ * 将开发整懵逼
312
+ */
313
+ // @ts-ignore
314
+ target = 'tuya';
313
315
  }
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
- }
316
+ const themeP = themeLocation.replace(/(?:\.{1,2}\/)*/, '');
317
+ const output = path_1.default.join(cwd, 'dist', target, themeP);
318
+ const data = fs_extra_1.default.readFileSync(src).toString('utf-8');
319
+ fs_extra_1.default.outputFileSync(output, data, 'utf-8');
320
+ cacheThemeLocation.dest = output;
321
+ cacheThemeLocation.source = src;
322
+ }
323
+ }
324
+ function checkThemeConfig(src) {
325
+ if (!src) {
326
+ return;
327
+ }
328
+ const { cwd, source } = api.options;
329
+ const inputs = [path_1.default.resolve(cwd, source, src), path_1.default.resolve(cwd, src)];
330
+ for (const input of inputs) {
331
+ if (fs_extra_1.default.pathExistsSync(input)) {
332
+ return input;
320
333
  }
321
- },
322
- };
334
+ }
335
+ }
336
+ if (!api.options.watch) {
337
+ plugin.configWebpack = (0, theme_1.default)(generateThemeConfig).configWebpack;
338
+ }
339
+ return plugin;
323
340
  }
324
341
  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.3.1-beta.7",
3
+ "version": "1.3.2",
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.3.1-beta.7",
24
+ "@ray-js/shared": "^1.3.2",
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.3.1-beta.7"
33
+ "@ray-js/types": "^1.3.2"
34
34
  },
35
35
  "maintainers": [
36
36
  {
@@ -38,6 +38,6 @@
38
38
  "email": "tuyafe@tuya.com"
39
39
  }
40
40
  ],
41
- "gitHead": "3d68284d96ae18ddf2eaffa46f5d3607a51d60f8",
41
+ "gitHead": "a6af0026d8770839d9c1209a9e933b23ed77bf4a",
42
42
  "repository": {}
43
43
  }