Skip to content

Commit

Permalink
optimize osd plugin bundles
Browse files Browse the repository at this point in the history
Signed-off-by: Yulong Ruan <[email protected]>
  • Loading branch information
ruanyl committed Dec 17, 2024
1 parent 245f920 commit 3e460bc
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/osd-optimizer/src/optimizer/bundle_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export function getBundleCacheEvent$(

const bundleRefsDiff = diffCacheKey(
refs.map((r) => r.exportId).sort((a, b) => a.localeCompare(b)),
bundleRefExportIds
bundleRefExportIds.sort((a, b) => a.localeCompare(b))
);
if (bundleRefsDiff) {
events.push({
Expand Down
26 changes: 26 additions & 0 deletions packages/osd-optimizer/src/worker/bundle_refs_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,36 @@ export class BundleRefsPlugin {
private readonly resolvedRequestCache = new Map<string, Promise<string | undefined>>();
private readonly ignorePrefix = Path.resolve(this.bundle.contextDir) + Path.sep;
private allowedBundleIds = new Set<string>();
// private virtualModules: Record<string, string> = {};

constructor(private readonly bundle: Bundle, private readonly bundleRefs: BundleRefs) {}

/**
* Called by webpack when the plugin is passed in the webpack config
*/
public apply(compiler: webpack.Compiler) {
/* compiler.hooks.normalModuleFactory.tap('BundleRefsPlugin/test', (normalModuleFactory) => {
normalModuleFactory.hooks.beforeResolve.tapAsync(
'BundleRefsPlugin/test',
(resolveData, callback) => {
if (!resolveData || !resolveData.request.startsWith('@/core/public')) {
return callback();
}
// const pluginPath = resolveData.request.replace('@/', '');
const virtualModulePath = `/@/virtual-modules/core/public.js`;
// TODO: replace plugin path with bundle export id
const virtualModuleContent = `
const ns = __osdBundles__.get('entry/core/public');
module.exports = ns;
`;
resolveData.request = virtualModulePath;
this.virtualModules[virtualModulePath] = virtualModuleContent;
return callback(null, resolveData);
}
);
});*/
// called whenever the compiler starts to compile, passed the params
// that will be used to create the compilation
compiler.hooks.compile.tap('BundleRefsPlugin', (compilationParams: any) => {
Expand Down Expand Up @@ -201,6 +224,9 @@ export class BundleRefsPlugin {
* undefined is returned. Otherwise it returns the referenced bundleRef.
*/
private async maybeReplaceImport(context: string, request: string) {
// if (request.startsWith('@/core')) {
// console.log(request);
// }
// ignore imports that have loaders defined or are not relative seeming
if (request.includes('!') || !request.startsWith('.')) {
return;
Expand Down
1 change: 1 addition & 0 deletions packages/osd-plugin-helpers/src/tasks/create_archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export async function createArchive({ opensearchDashboardsVersion, plugin, log }
cwd: buildDir,
base: buildDir,
dot: true,
ignore: ['**/.osd-optimizer-cache'],
}),
zip(zipName),
vfs.dest(buildDir)
Expand Down
10 changes: 7 additions & 3 deletions packages/osd-plugin-helpers/src/tasks/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@ export async function optimize({ log, plugin, sourceDir, buildDir }: BuildContex
log.indent(2);

// build bundles into target
const outputRoot = Path.resolve(REPO_ROOT, 'build/opensearch-dashboards');
const config = OptimizerConfig.create({
repoRoot: REPO_ROOT,
pluginPaths: [sourceDir],
cache: false,
cache: true,
dist: true,
pluginScanDirs: [],
// pluginScanDirs: [Path.resolve(REPO_ROOT, 'build/opensearch-dashboards')],
includeCoreBundle: true,
outputRoot,
});
const pluginOutputDir = Path.resolve(outputRoot, Path.relative(REPO_ROOT, sourceDir));

await runOptimizer(config).pipe(logOptimizerState(log, config)).toPromise();

// move target into buildDir
await asyncRename(Path.resolve(sourceDir, 'target'), Path.resolve(buildDir, 'target'));
await asyncRename(Path.resolve(pluginOutputDir, 'target'), Path.resolve(buildDir, 'target'));
log.indent(-2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ export const BuildOpenSearchDashboardsPlatformPlugins: Task = {
.pipe(reportOptimizerStats(reporter, config, log), logOptimizerState(log, config))
.toPromise();

await Promise.all(config.bundles.map((b) => b.cache.clear()));
// await Promise.all(config.bundles.map((b) => b.cache.clear()));
},
};

0 comments on commit 3e460bc

Please sign in to comment.