Skip to content

Commit

Permalink
feat: add bundlerOutput api
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjinyang committed Jan 14, 2022
1 parent be8bf9a commit e417db0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/service/src/core/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export interface IApi {
addEntryCode: typeof ProjectBuilder.prototype.addEntryCode;
addAppFile: typeof ProjectBuilder.prototype.addFile;
addRuntimeService: typeof ProjectBuilder.prototype.addRuntimeService;
addResources: typeof ProjectBuilder.prototype.addResources;
addAppPolyfill: typeof ProjectBuilder.prototype.addPolyfill;

setPlatformModule: typeof ProjectBuilder.prototype.setPlatformModule;
Expand Down
5 changes: 5 additions & 0 deletions packages/service/src/core/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class Api {
serverPlugins: this.serverPlugins,
// resources
resources: this.resources,
addResources: this.addResources.bind(this),
assetPublicPath: this.assetPublicPath,
getAssetPublicUrl: this.getAssetPublicUrl.bind(this),
resolveAppFile: this.resolveAppFile.bind(this),
Expand Down Expand Up @@ -355,6 +356,10 @@ class Api {
this._projectBuilder.addRuntimeService(source, exported, filepath);
}

addResources(source: string, exported: string, filepath?: string): void {
this._projectBuilder.addResources(source, exported, filepath);
}

addAppPolyfill(file: string): void {
this._projectBuilder.addPolyfill(file);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/service/src/core/apiTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ export interface IPaths {
// dir to store shuvi generated src files
appDir: string;

privateDir: string
privateDir: string;

// dir to runtime libraries
runtimeDir: string
runtimeDir: string;

// user src dir
srcDir: string;
Expand Down Expand Up @@ -154,6 +154,7 @@ export type IPluginContext = {
// resources
assetPublicPath: string;
resources: IResources;
addResources: (source: string, exported: string, filepath?: string) => void;
resolveAppFile(...paths: string[]): string;
resolveUserFile(...paths: string[]): string;
resolveBuildFile(...paths: string[]): string;
Expand Down
50 changes: 49 additions & 1 deletion packages/service/src/project/projectBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const contextValidatingRuleMap: ContextValidatingRuleMap = {
ignore: true,
method: 'addRuntimeService'
},
resources: {
ignore: true,
method: 'addResources'
},
runtimePlugins: {
ignore: true,
method: 'addRuntimePlugin'
Expand Down Expand Up @@ -139,7 +143,11 @@ class ProjectBuilder {
this._projectContext.clientModule = module;
}

addRuntimeService(source: string, exported: string, filepath: string = 'index.js'): void {
addRuntimeService(
source: string,
exported: string,
filepath: string = 'index.js'
): void {
const services = this._projectContext.runtimeServices;
filepath = path.join('runtime', path.resolve('/', filepath));
const service = services.get(filepath);
Expand Down Expand Up @@ -176,6 +184,46 @@ class ProjectBuilder {
}
}

addResources(
source: string,
exported: string,
filepath: string = 'index.js'
): void {
const services = this._projectContext.resources;
filepath = path.join('resources', path.resolve('/', filepath));
const service = services.get(filepath);
if (service) {
const targetSource = service.get(source);
if (targetSource) {
targetSource.add(exported);
} else {
const exportedSet: Set<string> = new Set();
exportedSet.add(exported);
service.set(source, exportedSet);
}
} else {
const exportedSet: Set<string> = new Set();
exportedSet.add(exported);
const service: Map<string, Set<string>> = new Map();
service.set(source, exportedSet);
services.set(filepath, service);
this.addFile({
name: filepath,
content: (context: ProjectContext) => {
const exportsConfig: { [key: string]: string[] } = {};
const service = context.runtimeServices.get(filepath);
if (!service) {
return null;
}
for (const [s, e] of service) {
exportsConfig[s] = Array.from(e);
}
return exportsFromObject(exportsConfig);
}
});
}
}

/**
* default path is the root path
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/service/src/project/projectContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface ProjectContext {
* }
*/
runtimeServices: Map<string, Map<string, Set<string>>>;
resources: Map<string, Map<string, Set<string>>>;
runtimePlugins: IRuntimeOrServerPlugin[];
runtimeConfigContent: string | null;
platformModule: string;
Expand All @@ -53,6 +54,7 @@ export const createProjectContext = () =>
entryWrapperContent: '',
polyfills: [],
runtimeServices: new Map(),
resources: new Map(),
runtimePlugins: [],
runtimeConfigContent: null,
clientModule: {
Expand Down

0 comments on commit e417db0

Please sign in to comment.