Skip to content

Commit

Permalink
refactor: move common behavior to the base class
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlafroscia committed Apr 15, 2021
1 parent ce23376 commit bb4ffba
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
37 changes: 34 additions & 3 deletions packages/core/src/packager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { join } from 'path';
import { Stats, copySync, readFileSync, realpathSync, statSync } from 'fs-extra';
import makeDebug from 'debug';
import { AppMeta } from '@embroider/shared-internals';

const debug = makeDebug('embroider:debug');

// This is a collection of flags that convey what kind of build you want. They
// are intended to be generic across Packagers, and it's up to Packager authors
// to support each option (or not).
Expand All @@ -24,14 +31,20 @@ export interface Variant {
}

export abstract class Packager<Options> {
pathToVanillaApp: string;

protected outputPath: string;

private passthroughCache: Map<string, Stats> = new Map();

constructor(
// where on disk the packager will find the app it's supposed to build. The
// app and its addons will necessarily already be in v2 format, which is
// what makes a Packager a cleanly separable stage that needs only a small
// amount of ember-specific knowledge.
_inputPath: string,
inputPath: string,
// where the packager should write the packaged app.
_outputPath: string,
outputPath: string,
// list of active build variants. There is always at least one variant, but
// there can be many.
//
Expand All @@ -54,12 +67,30 @@ export abstract class Packager<Options> {
// packager is based on a third-party tool, this is where that tool's
// configuration can go.
_options?: Options
) {}
) {
this.pathToVanillaApp = realpathSync(inputPath);
this.outputPath = outputPath;
}

// a description for this packager that aids debugging & profiling
static annotation: string;

abstract build(): Promise<void>;

protected copyThrough(relativePath: string) {
let sourcePath = join(this.pathToVanillaApp, relativePath);
let newStats = statSync(sourcePath);
let oldStats = this.passthroughCache.get(sourcePath);
if (!oldStats || oldStats.mtimeMs !== newStats.mtimeMs || oldStats.size !== newStats.size) {
debug(`emitting ${relativePath}`);
copySync(sourcePath, join(this.outputPath, relativePath));
this.passthroughCache.set(sourcePath, newStats);
}
}

protected getAppMeta() {
return JSON.parse(readFileSync(join(this.pathToVanillaApp, 'package.json'), 'utf8'))['ember-addon'] as AppMeta;
}
}

export interface PackagerConstructor<Options> {
Expand Down
20 changes: 3 additions & 17 deletions packages/webpack/src/ember-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
StatSummary,
} from '@embroider/core';
import webpack, { Configuration } from 'webpack';
import { readFileSync, outputFileSync, copySync, realpathSync, Stats, statSync, readJsonSync } from 'fs-extra';
import { readFileSync, outputFileSync, realpathSync, readJsonSync } from 'fs-extra';
import { join, dirname, relative, sep } from 'path';
import isEqual from 'lodash/isEqual';
import mergeWith from 'lodash/mergeWith';
Expand Down Expand Up @@ -76,21 +76,18 @@ interface Options {
export class Webpack extends Packager<Options> {
static annotation = '@embroider/webpack';

pathToVanillaApp: string;
private extraConfig: Configuration | undefined;
private passthroughCache: Map<string, Stats> = new Map();
private publicAssetURL: string | undefined;

constructor(
pathToVanillaApp: string,
private outputPath: string,
outputPath: string,
private variants: Variant[],
private consoleWrite: (msg: string) => void,
options?: Options
) {
super(pathToVanillaApp, outputPath, variants, consoleWrite, options);

this.pathToVanillaApp = realpathSync(pathToVanillaApp);
this.extraConfig = options?.webpackConfig;
this.publicAssetURL = options?.publicAssetURL;
warmUp();
Expand All @@ -104,7 +101,7 @@ export class Webpack extends Packager<Options> {
}

private examineApp(): AppInfo {
let meta = JSON.parse(readFileSync(join(this.pathToVanillaApp, 'package.json'), 'utf8'))['ember-addon'] as AppMeta;
let meta = this.getAppMeta();
let templateCompiler = meta['template-compiler'];
let rootURL = meta['root-url'];
let babel = meta['babel'];
Expand Down Expand Up @@ -364,17 +361,6 @@ export class Webpack extends Packager<Options> {
}
}

private copyThrough(relativePath: string) {
let sourcePath = join(this.pathToVanillaApp, relativePath);
let newStats = statSync(sourcePath);
let oldStats = this.passthroughCache.get(sourcePath);
if (!oldStats || oldStats.mtimeMs !== newStats.mtimeMs || oldStats.size !== newStats.size) {
debug(`emitting ${relativePath}`);
copySync(sourcePath, join(this.outputPath, relativePath));
this.passthroughCache.set(sourcePath, newStats);
}
}

private getFingerprintedFilename(filename: string, content: string): string {
let md5 = crypto.createHash('md5');
md5.update(content);
Expand Down

0 comments on commit bb4ffba

Please sign in to comment.