From d5084c7f26a047e67de054685f7204acccdeb891 Mon Sep 17 00:00:00 2001 From: Travis Hoover Date: Tue, 15 Jun 2021 12:44:14 -0700 Subject: [PATCH 1/3] Better error message when an asset cannot be found in entry file Adds better description along wiht a link for how to fix --- packages/compat/src/compat-app.ts | 27 ++++---------- packages/compat/src/v1-app.ts | 62 +++++++++++++++++++++++++++---- 2 files changed, 61 insertions(+), 28 deletions(-) diff --git a/packages/compat/src/compat-app.ts b/packages/compat/src/compat-app.ts index 6dd917175..d67ced7c6 100644 --- a/packages/compat/src/compat-app.ts +++ b/packages/compat/src/compat-app.ts @@ -128,7 +128,7 @@ class CompatAppAdapter implements AppAdapter { kind: 'on-disk', relativePath: entry.relativePath, sourcePath: entry.fullPath, - mtime: entry.mtime as unknown as number, // https://github.com/joliss/node-walk-sync/pull/38 + mtime: (entry.mtime as unknown) as number, // https://github.com/joliss/node-walk-sync/pull/38 size: entry.size, }); }); @@ -267,20 +267,10 @@ class CompatAppAdapter implements AppAdapter { let styles = [...dom.window.document.querySelectorAll('link[rel="stylesheet"]')] as HTMLLinkElement[]; return { - javascript: definitelyReplace(dom, this.oldPackage.findAppScript(scripts), 'app javascript', entrypoint), - styles: definitelyReplace(dom, this.oldPackage.findAppStyles(styles), 'app styles', entrypoint), - implicitScripts: definitelyReplace( - dom, - this.oldPackage.findVendorScript(scripts), - 'vendor javascript', - entrypoint - ), - implicitStyles: definitelyReplace( - dom, - this.oldPackage.findVendorStyles(styles), - 'vendor styles', - entrypoint - ), + javascript: definitelyReplace(dom, this.oldPackage.findAppScript(scripts, entrypoint)), + styles: definitelyReplace(dom, this.oldPackage.findAppStyles(styles, entrypoint)), + implicitScripts: definitelyReplace(dom, this.oldPackage.findVendorScript(scripts, entrypoint)), + implicitStyles: definitelyReplace(dom, this.oldPackage.findVendorStyles(styles, entrypoint)), testJavascript: maybeReplace(dom, this.oldPackage.findTestScript(scripts)), implicitTestScripts: maybeReplace(dom, this.oldPackage.findTestSupportScript(scripts)), implicitTestStyles: maybeReplace(dom, this.oldPackage.findTestSupportStyles(styles)), @@ -462,14 +452,11 @@ export default class CompatApp extends BuildStage { function maybeReplace(dom: JSDOM, element: Element | undefined): Node | undefined { if (element) { - return definitelyReplace(dom, element, '', ''); + return definitelyReplace(dom, element); } } -function definitelyReplace(dom: JSDOM, element: Element | undefined, description: string, file: string): Node { - if (!element) { - throw new Error(`could not find ${description} in ${file}`); - } +function definitelyReplace(dom: JSDOM, element: Element): Node { let placeholder = dom.window.document.createTextNode(''); element.replaceWith(placeholder); return placeholder; diff --git a/packages/compat/src/v1-app.ts b/packages/compat/src/v1-app.ts index 6fa9fd466..3fdde4a28 100644 --- a/packages/compat/src/v1-app.ts +++ b/packages/compat/src/v1-app.ts @@ -675,20 +675,48 @@ export default class V1App { return src; } - findAppScript(scripts: HTMLScriptElement[]): HTMLScriptElement | undefined { - return scripts.find(script => this.withoutRootURL(script.src) === this.app.options.outputPaths.app.js); + findAppScript(scripts: HTMLScriptElement[], entrypoint: string): HTMLScriptElement { + let appJS = scripts.find(script => this.withoutRootURL(script.src) === this.app.options.outputPaths.app.js); + return throwIfMissing( + appJS, + this.app.options.outputPaths.app.js, + scripts.map(s => ' - ' + s.src), + entrypoint, + 'app javascript' + ); } - findAppStyles(styles: HTMLLinkElement[]): HTMLLinkElement | undefined { - return styles.find(style => this.withoutRootURL(style.href) === this.app.options.outputPaths.app.css.app); + findAppStyles(styles: HTMLLinkElement[], entrypoint: string): HTMLLinkElement { + let style = styles.find(style => this.withoutRootURL(style.href) === this.app.options.outputPaths.app.css.app); + return throwIfMissing( + style, + this.app.options.outputPaths.app.css.app, + styles.map(s => s.href), + entrypoint, + 'app css' + ); } - findVendorScript(scripts: HTMLScriptElement[]): HTMLScriptElement | undefined { - return scripts.find(script => this.withoutRootURL(script.src) === this.app.options.outputPaths.vendor.js); + findVendorScript(scripts: HTMLScriptElement[], entrypoint: string): HTMLScriptElement { + let vendor = scripts.find(script => this.withoutRootURL(script.src) === this.app.options.outputPaths.vendor.js); + return throwIfMissing( + vendor, + this.app.options.outputPaths.vendor.js, + scripts.map(s => s.src), + entrypoint, + 'vendor javascript' + ); } - findVendorStyles(styles: HTMLLinkElement[]): HTMLLinkElement | undefined { - return styles.find(style => this.withoutRootURL(style.href) === this.app.options.outputPaths.vendor.css); + findVendorStyles(styles: HTMLLinkElement[], entrypoint: string): HTMLLinkElement { + let vendorStyle = styles.find(style => this.withoutRootURL(style.href) === this.app.options.outputPaths.vendor.css); + return throwIfMissing( + vendorStyle, + this.app.options.outputPaths.vendor.css, + styles.map(s => s.href), + entrypoint, + 'vendor css' + ); } findTestSupportStyles(styles: HTMLLinkElement[]): HTMLLinkElement | undefined { @@ -706,6 +734,24 @@ export default class V1App { } } +function throwIfMissing( + asset: T | undefined, + needle: string, + haystack: string[], + entryfile: string, + context: string +): T { + if (!asset) { + throw new Error( + `Could not find ${context}: "${needle}" in ${entryfile}. Found the following instead:\n${haystack.join( + '\n' + )}\n\nFor more information about this error: https://github.com/thoov/stitch/wiki/Could-not-find-asset-in-entry-file-error-help` + ); + } + + return asset; +} + class V1DummyApp extends V1App { constructor(app: EmberApp, packageCache: PackageCache) { super(app, packageCache); From e73bb86e3ee8ee3e4cc2881240ac5a155f731ed7 Mon Sep 17 00:00:00 2001 From: Travis Hoover Date: Tue, 15 Jun 2021 12:50:11 -0700 Subject: [PATCH 2/3] cleaning up one off message format logic --- packages/compat/src/v1-app.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/compat/src/v1-app.ts b/packages/compat/src/v1-app.ts index 3fdde4a28..ad3fd04ce 100644 --- a/packages/compat/src/v1-app.ts +++ b/packages/compat/src/v1-app.ts @@ -680,7 +680,7 @@ export default class V1App { return throwIfMissing( appJS, this.app.options.outputPaths.app.js, - scripts.map(s => ' - ' + s.src), + scripts.map(s => s.src), entrypoint, 'app javascript' ); @@ -743,9 +743,11 @@ function throwIfMissing( ): T { if (!asset) { throw new Error( - `Could not find ${context}: "${needle}" in ${entryfile}. Found the following instead:\n${haystack.join( - '\n' - )}\n\nFor more information about this error: https://github.com/thoov/stitch/wiki/Could-not-find-asset-in-entry-file-error-help` + `Could not find ${context}: "${needle}" in ${entryfile}. Found the following instead:\n${haystack + .map(asset => ` - ${asset}`) + .join( + '\n' + )}\n\nFor more information about this error: https://github.com/thoov/stitch/wiki/Could-not-find-asset-in-entry-file-error-help` ); } From 3b650989e891de0a80461754a6f38331a789d609 Mon Sep 17 00:00:00 2001 From: Travis Hoover Date: Tue, 15 Jun 2021 13:23:20 -0700 Subject: [PATCH 3/3] fixing lint error --- packages/compat/src/compat-app.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compat/src/compat-app.ts b/packages/compat/src/compat-app.ts index d67ced7c6..741a311a3 100644 --- a/packages/compat/src/compat-app.ts +++ b/packages/compat/src/compat-app.ts @@ -128,7 +128,7 @@ class CompatAppAdapter implements AppAdapter { kind: 'on-disk', relativePath: entry.relativePath, sourcePath: entry.fullPath, - mtime: (entry.mtime as unknown) as number, // https://github.com/joliss/node-walk-sync/pull/38 + mtime: entry.mtime as unknown as number, // https://github.com/joliss/node-walk-sync/pull/38 size: entry.size, }); });