-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backport #1855 addon-dev: incremental updates to output
- Loading branch information
Showing
9 changed files
with
404 additions
and
125 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,79 @@ | ||
import walkSync from 'walk-sync'; | ||
import { rmSync } from 'fs'; | ||
import { join } from 'path'; | ||
import type { Plugin } from 'rollup'; | ||
import { existsSync } from 'fs-extra'; | ||
|
||
export default function incremental(): Plugin { | ||
const generatedAssets = new Map(); | ||
const generatedFiles = new Set<string>(); | ||
|
||
function isEqual(v1: string | Uint8Array, v2: string | Uint8Array): boolean { | ||
if (typeof v1 === 'string' && typeof v2 === 'string') { | ||
return v1 === v2; | ||
} | ||
if (Buffer.isBuffer(v1) && Buffer.isBuffer(v2)) { | ||
return v1.equals(v2); | ||
} | ||
return false; | ||
} | ||
|
||
let firstTime = true; | ||
|
||
function initGeneratedFiles(outDir: string) { | ||
if (existsSync(outDir)) { | ||
const files = walkSync(outDir, { | ||
globs: ['*/**'], | ||
directories: false, | ||
}); | ||
for (const file of files) { | ||
generatedFiles.add(file); | ||
} | ||
} | ||
} | ||
|
||
function deleteRemovedFiles(bundle: Record<string, any>, outDir: string) { | ||
for (const file of generatedFiles) { | ||
if (!bundle[file]) { | ||
generatedAssets.delete(file); | ||
rmSync(join(outDir, file)); | ||
} | ||
} | ||
generatedFiles.clear(); | ||
for (const file of Object.keys(bundle)) { | ||
generatedFiles.add(file); | ||
} | ||
} | ||
|
||
function syncFiles(bundle: Record<string, any>) { | ||
for (const checkKey of Object.keys(bundle)) { | ||
if (bundle[checkKey]) { | ||
let module = bundle[checkKey] as any; | ||
let code = module.source || module.code; | ||
if ( | ||
generatedAssets.has(checkKey) && | ||
isEqual(code, generatedAssets.get(checkKey)) | ||
) { | ||
delete bundle[checkKey]; | ||
} else { | ||
generatedAssets.set(checkKey, code); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return { | ||
name: 'incremental', | ||
generateBundle(options, bundle) { | ||
if (firstTime) { | ||
firstTime = false; | ||
initGeneratedFiles(options.dir!); | ||
} | ||
if (existsSync(options.dir!)) { | ||
deleteRemovedFiles(bundle, options.dir!); | ||
} | ||
|
||
syncFiles(bundle); | ||
}, | ||
}; | ||
} |
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,57 @@ | ||
import { existsSync } from 'fs-extra'; | ||
import { cleanUrl } from './paths'; | ||
import type PackageCache from './package-cache'; | ||
import { sep } from 'path'; | ||
import { resolve as resolveExports } from 'resolve.exports'; | ||
|
||
export function syntheticJStoHBS(source: string): string | null { | ||
// explicit js is the only case we care about here. Synthetic template JS is | ||
// only ever JS (never TS or anything else). And extensionless imports are | ||
// handled by the default resolving system doing extension search. | ||
if (cleanUrl(source).endsWith('.js')) { | ||
return source.replace(/.js(\?.*)?/, '.hbs$1'); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export function needsSyntheticComponentJS(requestedSpecifier: string, foundFile: string): string | null { | ||
requestedSpecifier = cleanUrl(requestedSpecifier); | ||
foundFile = cleanUrl(foundFile); | ||
if ( | ||
discoveredImplicitHBS(requestedSpecifier, foundFile) && | ||
!foundFile.split(sep).join('/').endsWith('/template.hbs') && | ||
!correspondingJSExists(foundFile) | ||
) { | ||
return foundFile.slice(0, -3) + 'js'; | ||
} | ||
return null; | ||
} | ||
|
||
function discoveredImplicitHBS(source: string, id: string): boolean { | ||
return !source.endsWith('.hbs') && id.endsWith('.hbs'); | ||
} | ||
|
||
function correspondingJSExists(id: string): boolean { | ||
return ['js', 'ts'].some(ext => existsSync(id.slice(0, -3) + ext)); | ||
} | ||
|
||
export function isInComponents(url: string, packageCache: Pick<PackageCache, 'ownerOfFile'>) { | ||
const id = cleanUrl(url); | ||
|
||
const pkg = packageCache.ownerOfFile(id); | ||
if (!pkg?.isV2App()) { | ||
return false; | ||
} | ||
|
||
let tryResolve = resolveExports(pkg.packageJSON, './components', { | ||
browser: true, | ||
conditions: ['default', 'imports'], | ||
}); | ||
let componentsDir = tryResolve?.[0] ?? './components'; | ||
return ('.' + id.slice(pkg?.root.length).split(sep).join('/')).startsWith(componentsDir); | ||
} | ||
|
||
export function templateOnlyComponentSource() { | ||
return `import templateOnly from '@ember/component/template-only';\nexport default templateOnly();\n`; | ||
} |
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
Oops, something went wrong.