@shuvi/service 2.0.0-dev.23 → 2.0.0-dev.24

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/core/api.d.ts CHANGED
@@ -50,6 +50,7 @@ declare class Api {
50
50
  init(): Promise<void>;
51
51
  getBundler(): Promise<Bundler>;
52
52
  get assetPublicPath(): string;
53
+ private _getAssetPublicPath;
53
54
  buildApp(): Promise<void>;
54
55
  addRuntimeFile(option: FileOptionWithId<any>): void;
55
56
  addInternalRuntimeFile(option: FileOptionWithId<any>): void;
package/lib/core/api.js CHANGED
@@ -119,6 +119,7 @@ class Api {
119
119
  phase: this._phase,
120
120
  pluginRunner: this._pluginManager.runner,
121
121
  assetPublicPath: this.assetPublicPath,
122
+ getAssetPublicPath: this._getAssetPublicPath.bind(this),
122
123
  resolveAppFile: this.resolveAppFile.bind(this),
123
124
  resolveUserFile: this.resolveUserFile.bind(this),
124
125
  resolveBuildFile: this.resolveBuildFile.bind(this),
@@ -193,12 +194,26 @@ class Api {
193
194
  });
194
195
  }
195
196
  get assetPublicPath() {
196
- let publicPath = this._config.publicPath || constants_1.DEFAULT_PUBLIC_PATH;
197
+ const configPublicPath = this._config.publicPath;
198
+ let publicPath;
199
+ if (typeof configPublicPath === 'function') {
200
+ // 如果是函数,返回默认值,运行时会在 getAssetUrl 中动态计算
201
+ publicPath = constants_1.DEFAULT_PUBLIC_PATH;
202
+ }
203
+ else {
204
+ publicPath = configPublicPath || constants_1.DEFAULT_PUBLIC_PATH;
205
+ }
197
206
  if (!publicPath.endsWith('/')) {
198
207
  publicPath += '/';
199
208
  }
200
209
  return publicPath;
201
210
  }
211
+ _getAssetPublicPath(req) {
212
+ const { publicPath } = this._config;
213
+ return typeof publicPath === 'function'
214
+ ? publicPath(req)
215
+ : this.assetPublicPath;
216
+ }
202
217
  buildApp() {
203
218
  return __awaiter(this, void 0, void 0, function* () {
204
219
  yield Promise.all([this._removeLastArtifacts(), this._initArtifacts()]);
@@ -2,7 +2,7 @@ import { CompilerOptions } from '@shuvi/toolpack/lib/webpack/loaders/shuvi-swc-l
2
2
  import { CorePluginConstructor, CorePluginFactory, CorePluginInstance, PluginRunner } from './plugin';
3
3
  import { FileOptionWithId } from '../project';
4
4
  import { IProxyConfig } from '../server/middlewares/httpProxyMiddleware';
5
- import { IServerMiddleware, IServerPluginContext, ServerPluginInstance, ServerPluginConstructor, ServerPluginFactory } from '../server';
5
+ import { IServerMiddleware, IServerPluginContext, ServerPluginInstance, ServerPluginConstructor, ServerPluginFactory, ShuviRequest } from '../server';
6
6
  import { DevMiddleware } from '../server/middlewares/dev';
7
7
  export type { IProxyConfig };
8
8
  export type DeepPartial<T> = {
@@ -82,7 +82,7 @@ export interface InternalConfig {
82
82
  plugins?: IPluginConfig[];
83
83
  env: Record<string, string>;
84
84
  outputPath: string;
85
- publicPath: string;
85
+ publicPath: string | ((req: ShuviRequest) => string);
86
86
  proxy?: IProxyConfig;
87
87
  analyze: boolean;
88
88
  disposeInactivePage: boolean;
@@ -108,6 +108,7 @@ export interface IPluginContext {
108
108
  phase: IServicePhase;
109
109
  pluginRunner: PluginRunner;
110
110
  assetPublicPath: string;
111
+ getAssetPublicPath(req: ShuviRequest): string;
111
112
  resolveAppFile(...paths: string[]): string;
112
113
  resolveUserFile(...paths: string[]): string;
113
114
  resolveBuildFile(...paths: string[]): string;
@@ -26,7 +26,11 @@ class ShuviServer {
26
26
  _normalizeReq(req) {
27
27
  const shuviReq = req;
28
28
  shuviReq.getAssetUrl = (assetPath) => {
29
- const fullAssetPath = (0, string_1.joinPath)(this._serverContext.assetPublicPath, assetPath);
29
+ const { config } = this._serverContext;
30
+ const publicPath = typeof config.publicPath === 'function'
31
+ ? config.publicPath(shuviReq)
32
+ : this._serverContext.assetPublicPath;
33
+ const fullAssetPath = (0, string_1.joinPath)(publicPath, assetPath);
30
34
  return fullAssetPath;
31
35
  };
32
36
  // Parsing of cookies
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/service",
3
- "version": "2.0.0-dev.23",
3
+ "version": "2.0.0-dev.24",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/shuvijs/shuvi.git",
@@ -29,14 +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": "2.0.0-dev.23",
33
- "@shuvi/router": "2.0.0-dev.23",
34
- "@shuvi/runtime": "2.0.0-dev.23",
35
- "@shuvi/shared": "2.0.0-dev.23",
36
- "@shuvi/toolpack": "2.0.0-dev.23",
37
- "@shuvi/utils": "2.0.0-dev.23",
38
- "@shuvi/error-overlay": "2.0.0-dev.23",
39
- "@shuvi/reporters": "2.0.0-dev.23",
32
+ "@shuvi/hook": "2.0.0-dev.24",
33
+ "@shuvi/router": "2.0.0-dev.24",
34
+ "@shuvi/runtime": "2.0.0-dev.24",
35
+ "@shuvi/shared": "2.0.0-dev.24",
36
+ "@shuvi/toolpack": "2.0.0-dev.24",
37
+ "@shuvi/utils": "2.0.0-dev.24",
38
+ "@shuvi/error-overlay": "2.0.0-dev.24",
39
+ "@shuvi/reporters": "2.0.0-dev.24",
40
40
  "commander": "5.1.0",
41
41
  "comment-json": "4.2.2",
42
42
  "cross-spawn": "7.0.3",