@shuvi/service 1.0.4 → 1.0.6

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.
@@ -0,0 +1,5 @@
1
+ import { IPluginContext, Telemetry } from '../core';
2
+ export declare const analysis: ({ context, telemetry }: {
3
+ context: IPluginContext;
4
+ telemetry: Telemetry;
5
+ }) => Promise<void>;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.analysis = void 0;
39
+ const path = __importStar(require("path"));
40
+ const fs = __importStar(require("fs"));
41
+ const logger_1 = __importDefault(require("@shuvi/utils/lib/logger"));
42
+ const events_1 = require("@shuvi/telemetry/lib/events");
43
+ const recursiveReaddir_1 = require("@shuvi/utils/lib/recursiveReaddir");
44
+ const typescript_1 = require("../bundler/typescript");
45
+ const analysis = ({ context, telemetry }) => __awaiter(void 0, void 0, void 0, function* () {
46
+ if (!telemetry || !context) {
47
+ return;
48
+ }
49
+ logger_1.default.info('Start collecting data...');
50
+ const analysisBegin = process.hrtime();
51
+ const routesExtensions = ['ts', 'tsx', 'js', 'jsx'];
52
+ const routePaths = yield (0, recursiveReaddir_1.recursiveReadDir)(context.paths.routesDir, {
53
+ filter: new RegExp(`\\.(?:${routesExtensions.join('|')})$`)
54
+ });
55
+ const middlewareCount = routePaths.filter(path => /middleware\..*/i.test(path)).length;
56
+ const srcDirFiles = yield fs.promises.readdir(context.paths.srcDir, {
57
+ encoding: 'utf-8'
58
+ });
59
+ const hasStatic404 = srcDirFiles.some(value => /error/i.test(value));
60
+ const pageLoadersPath = path.join(context.paths.appDir, '/files/page-loaders.js');
61
+ const pageLoadersFile = yield fs.promises.readFile(pageLoadersPath, {
62
+ encoding: 'utf8'
63
+ });
64
+ const totalLoaderCount = pageLoadersFile
65
+ .toString()
66
+ .split('\n')
67
+ .filter(loader => loader.startsWith('import')).length;
68
+ const { useTypeScript } = (0, typescript_1.getJavaScriptInfo)();
69
+ const analysisEnd = process.hrtime(analysisBegin);
70
+ telemetry.record((0, events_1.eventBuildOptimize)(routePaths, {
71
+ durationInSeconds: analysisEnd[0],
72
+ hasStatic404,
73
+ middlewareCount,
74
+ totalLoaderCount,
75
+ useTypeScript
76
+ }));
77
+ });
78
+ exports.analysis = analysis;
package/lib/core/api.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Telemetry as TelemetryImpl, RecordObject, TelemetryEvent } from '@shuvi/telemetry';
1
2
  import { FileOption } from '../project';
2
3
  import { Bundler } from '../bundler';
3
4
  import { ServerPluginInstance } from '../server';
@@ -12,12 +13,16 @@ interface IApiOPtions {
12
13
  plugins?: IPluginConfig[];
13
14
  presets?: IPresetConfig[];
14
15
  normalizePlatformConfig?: (rawConfig: ShuviConfig) => ShuviConfig;
16
+ telemetry?: TelemetryImpl;
15
17
  }
16
18
  interface ServerConfigs {
17
19
  serverPlugins: ServerPluginInstance[];
18
20
  getMiddlewares: IPlatformContent['getMiddlewares'];
19
21
  getMiddlewaresBeforeDevMiddlewares: IPlatformContent['getMiddlewaresBeforeDevMiddlewares'];
20
22
  }
23
+ export interface Telemetry {
24
+ record(events: TelemetryEvent | TelemetryEvent[]): Promise<RecordObject>;
25
+ }
21
26
  declare class Api {
22
27
  private _inited;
23
28
  private _cwd;
@@ -33,6 +38,8 @@ declare class Api {
33
38
  private _config;
34
39
  private _plugins;
35
40
  private _presets;
41
+ private _telemetryImpl?;
42
+ private _telemetry;
36
43
  private _platform?;
37
44
  private _normalizePlatformConfig?;
38
45
  private _serverConfigs;
@@ -41,12 +48,13 @@ declare class Api {
41
48
  private _pluginContext;
42
49
  /** will be included by @shuvi/swc-loader */
43
50
  private _runtimePluginDirs;
44
- constructor({ cwd, mode, config, configFile, presets, plugins, phase, platform, normalizePlatformConfig }: IApiOPtions);
51
+ constructor({ cwd, mode, config, configFile, presets, plugins, phase, platform, normalizePlatformConfig, telemetry }: IApiOPtions);
45
52
  get cwd(): string;
46
53
  get mode(): IServiceMode;
47
54
  get pluginManager(): import("@shuvi/hook/lib/hookGroup").HookManager<import("./plugin").PluginHooks, IPluginContext>;
48
55
  get pluginContext(): IPluginContext;
49
56
  get serverConfigs(): ServerConfigs;
57
+ get telemetry(): Telemetry;
50
58
  init(): Promise<void>;
51
59
  getBundler(): Promise<Bundler>;
52
60
  get assetPublicPath(): string;
package/lib/core/api.js CHANGED
@@ -55,7 +55,7 @@ const webpack_watch_wait_for_file_builder_plugin_1 = __importDefault(require("..
55
55
  const config_2 = require("../config");
56
56
  const ServiceModes = ['development', 'production'];
57
57
  class Api {
58
- constructor({ cwd, mode, config, configFile, presets, plugins, phase, platform, normalizePlatformConfig }) {
58
+ constructor({ cwd, mode, config, configFile, presets, plugins, phase, platform, normalizePlatformConfig, telemetry }) {
59
59
  this._inited = false;
60
60
  this._plugins = [];
61
61
  this._presets = [];
@@ -70,6 +70,7 @@ class Api {
70
70
  this._customConfig = config || {};
71
71
  this._customPresets = presets || [];
72
72
  this._customPlugins = plugins || [];
73
+ this._telemetryImpl = telemetry;
73
74
  this._pluginManager = (0, plugin_1.getManager)();
74
75
  this._pluginManager.clear();
75
76
  this._projectBuilder = new project_1.ProjectBuilder();
@@ -90,6 +91,22 @@ class Api {
90
91
  get serverConfigs() {
91
92
  return this._serverConfigs;
92
93
  }
94
+ get telemetry() {
95
+ if (this._telemetry) {
96
+ this._telemetry = {
97
+ record: (events) => {
98
+ if (!this._telemetryImpl) {
99
+ return Promise.resolve({
100
+ isFulfilled: true,
101
+ isRejected: true
102
+ });
103
+ }
104
+ return this._telemetryImpl.record(events);
105
+ }
106
+ };
107
+ }
108
+ return this._telemetry;
109
+ }
93
110
  init() {
94
111
  return __awaiter(this, void 0, void 0, function* () {
95
112
  if (this._inited) {
@@ -373,7 +390,8 @@ function getApi(options = {}) {
373
390
  platform: options.platform,
374
391
  presets: options.presets,
375
392
  plugins: options.plugins,
376
- normalizePlatformConfig: options.normalizePlatformConfig
393
+ normalizePlatformConfig: options.normalizePlatformConfig,
394
+ telemetry: options.telemetry
377
395
  });
378
396
  try {
379
397
  yield api.init();
@@ -1,4 +1,4 @@
1
- export type { Api } from './api';
1
+ export type { Api, Telemetry } from './api';
2
2
  export { getApi } from './api';
3
3
  export { resolvePlugin } from './getPlugins';
4
4
  export * from './apiTypes';
package/lib/index.d.ts CHANGED
@@ -2,5 +2,6 @@ export * from './namespace';
2
2
  export * from './constants';
3
3
  export { Bundler } from './bundler';
4
4
  export { ProjectBuilder } from './project';
5
+ export { analysis } from './analysis';
5
6
  export { IShuviServer, ShuviRequest, ShuviResponse, IServerPluginContext, ShuviRequestHandler, ServerPluginConstructor, ServerPluginInstance, IServerMiddleware, createShuviServer, createServerPlugin, createServerPluginBefore, createServerPluginAfter } from './server';
6
- export { Api, IPaths, ShuviConfig, IServicePhase, IServiceMode, IPluginConfig, IPresetConfig, PresetFunction, IPresetContent, NormalizedShuviConfig, IPluginContext, IPlatform, IPlatformContent, CorePluginConstructor, CorePluginInstance, getApi, createPlugin, createPluginBefore, createPluginAfter, ResolvedPlugin } from './core';
7
+ export { Api, Telemetry, IPaths, ShuviConfig, IServicePhase, IServiceMode, IPluginConfig, IPresetConfig, PresetFunction, IPresetContent, NormalizedShuviConfig, IPluginContext, IPlatform, IPlatformContent, CorePluginConstructor, CorePluginInstance, getApi, createPlugin, createPluginBefore, createPluginAfter, ResolvedPlugin } from './core';
package/lib/index.js CHANGED
@@ -14,11 +14,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.createPluginAfter = exports.createPluginBefore = exports.createPlugin = exports.getApi = exports.createServerPluginAfter = exports.createServerPluginBefore = exports.createServerPlugin = exports.createShuviServer = exports.ProjectBuilder = void 0;
17
+ exports.createPluginAfter = exports.createPluginBefore = exports.createPlugin = exports.getApi = exports.createServerPluginAfter = exports.createServerPluginBefore = exports.createServerPlugin = exports.createShuviServer = exports.analysis = exports.ProjectBuilder = void 0;
18
18
  __exportStar(require("./namespace"), exports);
19
19
  __exportStar(require("./constants"), exports);
20
20
  var project_1 = require("./project");
21
21
  Object.defineProperty(exports, "ProjectBuilder", { enumerable: true, get: function () { return project_1.ProjectBuilder; } });
22
+ var analysis_1 = require("./analysis");
23
+ Object.defineProperty(exports, "analysis", { enumerable: true, get: function () { return analysis_1.analysis; } });
22
24
  var server_1 = require("./server");
23
25
  Object.defineProperty(exports, "createShuviServer", { enumerable: true, get: function () { return server_1.createShuviServer; } });
24
26
  Object.defineProperty(exports, "createServerPlugin", { enumerable: true, get: function () { return server_1.createServerPlugin; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/service",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/shuvijs/shuvi.git",
@@ -29,13 +29,14 @@
29
29
  "@babel/generator": "7.14.5",
30
30
  "@babel/parser": "7.14.7",
31
31
  "@babel/traverse": "7.14.7",
32
- "@shuvi/hook": "1.0.4",
33
- "@shuvi/router": "1.0.4",
34
- "@shuvi/runtime": "1.0.4",
35
- "@shuvi/shared": "1.0.4",
36
- "@shuvi/toolpack": "1.0.4",
37
- "@shuvi/utils": "1.0.4",
38
- "@shuvi/error-overlay": "1.0.4",
32
+ "@shuvi/hook": "1.0.6",
33
+ "@shuvi/router": "1.0.6",
34
+ "@shuvi/runtime": "1.0.6",
35
+ "@shuvi/shared": "1.0.6",
36
+ "@shuvi/toolpack": "1.0.6",
37
+ "@shuvi/utils": "1.0.6",
38
+ "@shuvi/error-overlay": "1.0.6",
39
+ "@shuvi/telemetry": "1.0.6",
39
40
  "commander": "5.1.0",
40
41
  "comment-json": "4.2.2",
41
42
  "cross-spawn": "7.0.3",