Skip to content

Commit

Permalink
make write files a plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
krisselden committed Apr 21, 2022
1 parent b59a578 commit 23b8b2f
Showing 1 changed file with 64 additions and 51 deletions.
115 changes: 64 additions & 51 deletions packages/webpack/src/ember-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const Webpack: PackagerConstructor<Options> = class Webpack implements Packager
private extraBabelLoaderOptions: BabelLoaderOptions | undefined;
private extraCssLoaderOptions: object | undefined;
private extraStyleLoaderOptions: object | undefined;
private _bundleSummary: BundleSummary | undefined;

constructor(
pathToVanillaApp: string,
Expand All @@ -98,11 +99,23 @@ const Webpack: PackagerConstructor<Options> = class Webpack implements Packager
warmUp(this.extraThreadLoaderOptions);
}

get bundleSummary(): BundleSummary {
let bundleSummary = this._bundleSummary;
if (bundleSummary === undefined) {
this._bundleSummary = bundleSummary = {
entrypoints: new Map(),
lazyBundles: new Map(),
variants: this.variants,
};
}
return bundleSummary;
}

async build(): Promise<void> {
this._bundleSummary = undefined;
let appInfo = this.examineApp();
let webpack = this.getWebpack(appInfo);
let stats = this.summarizeStats(await this.runWebpack(webpack));
await this.writeFiles(stats, appInfo);
await this.runWebpack(webpack);
}

private examineApp(): AppInfo {
Expand All @@ -125,10 +138,9 @@ const Webpack: PackagerConstructor<Options> = class Webpack implements Packager
return { entrypoints, otherAssets, babel, rootURL, resolvableExtensions, publicAssetURL };
}

private configureWebpack(
{ entrypoints, babel, resolvableExtensions, publicAssetURL }: AppInfo,
variant: Variant
): Configuration {
private configureWebpack(appInfo: AppInfo, variant: Variant, variantIndex: number): Configuration {
const { entrypoints, babel, resolvableExtensions, publicAssetURL } = appInfo;

let entry: { [name: string]: string } = {};
for (let entrypoint of entrypoints) {
for (let moduleName of entrypoint.modules) {
Expand All @@ -145,7 +157,15 @@ const Webpack: PackagerConstructor<Options> = class Webpack implements Packager
performance: {
hints: false,
},
plugins: stylePlugins,
plugins: [
...stylePlugins,
compiler => {
compiler.hooks.done.tapPromise('EmbroiderPlugin', async stats => {
this.summarizeStats(stats, variant, variantIndex);
await this.writeFiles(this.bundleSummary, appInfo);
});
},
],
node: false,
module: {
rules: [
Expand Down Expand Up @@ -220,8 +240,8 @@ const Webpack: PackagerConstructor<Options> = class Webpack implements Packager
return this.lastWebpack;
}
debug(`configuring webpack`);
let config = this.variants.map(variant =>
mergeWith({}, this.configureWebpack(appInfo, variant), this.extraConfig, appendArrays)
let config = this.variants.map((variant, variantIndex) =>
mergeWith({}, this.configureWebpack(appInfo, variant, variantIndex), this.extraConfig, appendArrays)
);
this.lastAppInfo = appInfo;
return (this.lastWebpack = webpack(config));
Expand Down Expand Up @@ -386,54 +406,47 @@ const Webpack: PackagerConstructor<Options> = class Webpack implements Packager
return fileParts.join('.');
}

private summarizeStats(multiStats: webpack.MultiStats): BundleSummary {
let output: BundleSummary = {
entrypoints: new Map(),
lazyBundles: new Map(),
variants: this.variants,
};
for (let [variantIndex, variant] of this.variants.entries()) {
let { entrypoints, chunks } = multiStats.stats[variantIndex].toJson({
all: false,
entrypoints: true,
chunks: true,
});
private summarizeStats(stats: webpack.Stats, variant: Variant, variantIndex: number): void {
let output = this.bundleSummary;
let { entrypoints, chunks } = stats.toJson({
all: false,
entrypoints: true,
chunks: true,
});

// webpack's types are written rather loosely, implying that these two
// properties may not be present. They really always are, as far as I can
// tell, but we need to check here anyway to satisfy the type checker.
if (!entrypoints) {
throw new Error(`unexpected webpack output: no entrypoints`);
}
if (!chunks) {
throw new Error(`unexpected webpack output: no chunks`);
}
// webpack's types are written rather loosely, implying that these two
// properties may not be present. They really always are, as far as I can
// tell, but we need to check here anyway to satisfy the type checker.
if (!entrypoints) {
throw new Error(`unexpected webpack output: no entrypoints`);
}
if (!chunks) {
throw new Error(`unexpected webpack output: no chunks`);
}

for (let id of Object.keys(entrypoints)) {
let { assets: entrypointAssets } = entrypoints[id];
if (!entrypointAssets) {
throw new Error(`unexpected webpack output: no entrypoint.assets`);
}
for (let id of Object.keys(entrypoints)) {
let { assets: entrypointAssets } = entrypoints[id];
if (!entrypointAssets) {
throw new Error(`unexpected webpack output: no entrypoint.assets`);
}

getOrCreate(output.entrypoints, id, () => new Map()).set(
variantIndex,
entrypointAssets.map(asset => asset.name)
getOrCreate(output.entrypoints, id, () => new Map()).set(
variantIndex,
entrypointAssets.map(asset => asset.name)
);
if (variant.runtime !== 'browser') {
// in the browser we don't need to worry about lazy assets (they will be
// handled automatically by webpack as needed), but in any other runtime
// we need the ability to preload them
output.lazyBundles.set(
id,
flatMap(
chunks.filter(chunk => chunk.runtime?.includes(id)),
chunk => chunk.files
).filter(file => !entrypointAssets?.find(a => a.name === file)) as string[]
);
if (variant.runtime !== 'browser') {
// in the browser we don't need to worry about lazy assets (they will be
// handled automatically by webpack as needed), but in any other runtime
// we need the ability to preload them
output.lazyBundles.set(
id,
flatMap(
chunks.filter(chunk => chunk.runtime?.includes(id)),
chunk => chunk.files
).filter(file => !entrypointAssets?.find(a => a.name === file)) as string[]
);
}
}
}
return output;
}

private runWebpack(webpack: webpack.MultiCompiler): Promise<webpack.MultiStats> {
Expand Down

0 comments on commit 23b8b2f

Please sign in to comment.