-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1801 from BlueCutOfficial/virtualize-vendor
Module resolver: virtualize vendor.js
- Loading branch information
Showing
7 changed files
with
170 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { type Package, locateEmbroiderWorkingDir } from '@embroider/shared-internals'; | ||
import type { V2AddonPackage } from '@embroider/shared-internals/src/package'; | ||
import { lstatSync, readFileSync, readJSONSync } from 'fs-extra'; | ||
import { sortBy } from 'lodash'; | ||
import { join } from 'path'; | ||
import resolve from 'resolve'; | ||
import type { Resolver } from './module-resolver'; | ||
import type { VirtualContentResult } from './virtual-content'; | ||
|
||
export function decodeVirtualVendor(filename: string): boolean { | ||
return filename.endsWith('-embroider-vendor.js'); | ||
} | ||
|
||
export function renderVendor(filename: string, resolver: Resolver): VirtualContentResult { | ||
const owner = resolver.packageCache.ownerOfFile(filename); | ||
if (!owner) { | ||
throw new Error(`Failed to find a valid owner for ${filename}`); | ||
} | ||
return { src: getVendor(owner, resolver, filename), watches: [] }; | ||
} | ||
|
||
function getVendor(owner: Package, resolver: Resolver, filename: string): string { | ||
let engineConfig = resolver.owningEngine(owner); | ||
let addons = new Map( | ||
engineConfig.activeAddons.map(addon => [ | ||
resolver.packageCache.get(addon.root) as V2AddonPackage, | ||
addon.canResolveFromFile, | ||
]) | ||
); | ||
|
||
let path = join(locateEmbroiderWorkingDir(resolver.options.appRoot), 'ember-env.json'); | ||
if (!lstatSync(path).isFile()) { | ||
throw new Error(`Failed to read the ember-env.json when generating content for ${filename}`); | ||
} | ||
let emberENV = readJSONSync(path); | ||
|
||
return generateVendor(addons, emberENV); | ||
} | ||
|
||
function generateVendor(addons: Map<V2AddonPackage, string>, emberENV?: unknown): string { | ||
// Add addons implicit-scripts | ||
let vendor: string[] = impliedAddonVendors(addons).map((sourcePath: string): string => { | ||
let source = readFileSync(sourcePath); | ||
return `${source}`; | ||
}); | ||
// Add _testing_prefix_.js | ||
vendor.unshift(`var runningTests=false;`); | ||
// Add _ember_env_.js | ||
vendor.unshift(`window.EmberENV={ ...(window.EmberENV || {}), ...${JSON.stringify(emberENV, null, 2)} };`); | ||
// Add _loader_.js | ||
vendor.push(`loader.makeDefaultExport=false;`); | ||
|
||
return vendor.join('') as string; | ||
} | ||
|
||
function impliedAddonVendors(addons: Map<V2AddonPackage, string>): string[] { | ||
let result: Array<string> = []; | ||
for (let addon of sortBy(Array.from(addons.keys()), pkg => { | ||
switch (pkg.name) { | ||
case 'loader.js': | ||
return 0; | ||
case 'ember-source': | ||
return 10; | ||
default: | ||
return 1000; | ||
} | ||
})) { | ||
let implicitScripts = addon.meta['implicit-scripts']; | ||
if (implicitScripts) { | ||
let options = { basedir: addon.root }; | ||
for (let mod of implicitScripts) { | ||
result.push(resolve.sync(mod, options)); | ||
} | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters