-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This extends the keepAssets feature in `@embroider/addon-dev` so that it composes nicely with other plugins. For example, if you use a plugin that synthesizes CSS imports, it's nice for keepAssets to consume those and keep them as a real CSS files on disk when you build your addon.
- Loading branch information
Showing
3 changed files
with
194 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,93 @@ | ||
import walkSync from 'walk-sync'; | ||
import type { Plugin } from 'rollup'; | ||
import { readFileSync } from 'fs'; | ||
import { dirname, join, resolve } from 'path'; | ||
import minimatch from 'minimatch'; | ||
import { dirname, relative } from 'path'; | ||
|
||
// randomly chosen, we're just looking to have high-entropy identifiers that | ||
// won't collide with anyting else in the source | ||
let counter = 11559; | ||
|
||
export default function keepAssets({ | ||
from, | ||
include, | ||
exports, | ||
}: { | ||
from: string; | ||
include: string[]; | ||
exports?: undefined | 'default' | '*'; | ||
}): Plugin { | ||
const marker = `__copy_asset_marker_${counter++}__`; | ||
|
||
return { | ||
name: 'copy-assets', | ||
|
||
// imports of assets should be left alone in the source code. This can cover | ||
// the case of .css as defined in the embroider v2 addon spec. | ||
async resolveId(source, importer, options) { | ||
const resolution = await this.resolve(source, importer, { | ||
skipSelf: true, | ||
...options, | ||
}); | ||
if ( | ||
resolution && | ||
importer && | ||
include.some((pattern) => minimatch(resolution.id, pattern)) | ||
) { | ||
return { id: resolve(dirname(importer), source), external: 'relative' }; | ||
} | ||
return resolution; | ||
}, | ||
|
||
// the assets go into the output directory in the same relative locations as | ||
// in the input directory | ||
async generateBundle() { | ||
for (let name of walkSync(from, { | ||
globs: include, | ||
directories: false, | ||
})) { | ||
this.addWatchFile(join(from, name)); | ||
|
||
this.emitFile({ | ||
transform(code: string, id: string) { | ||
if (include.some((pattern) => minimatch(id, pattern))) { | ||
let ref = this.emitFile({ | ||
type: 'asset', | ||
fileName: name, | ||
source: readFileSync(join(from, name)), | ||
fileName: relative(from, id), | ||
source: code, | ||
}); | ||
if (exports === '*') { | ||
return `export * from ${marker}("${ref}")`; | ||
} else if (exports === 'default') { | ||
return `export default ${marker}("${ref}")`; | ||
} else { | ||
// side-effect only | ||
return `${marker}("${ref}")`; | ||
} | ||
} | ||
}, | ||
renderChunk(code, chunk) { | ||
const { getName, imports } = nameTracker(code, exports); | ||
|
||
code = code.replace( | ||
new RegExp(`${marker}\\("([^"]+)"\\)`, 'g'), | ||
(_x, ref) => { | ||
let assetFileName = this.getFileName(ref); | ||
let relativeName = | ||
'./' + relative(dirname(chunk.fileName), assetFileName); | ||
return getName(relativeName) ?? ''; | ||
} | ||
); | ||
return imports() + code; | ||
}, | ||
}; | ||
} | ||
|
||
function nameTracker(code: string, exports: undefined | 'default' | '*') { | ||
let counter = 0; | ||
let assets = new Map<string, string | undefined>(); | ||
|
||
function getName(assetName: string): string | undefined { | ||
if (assets.has(assetName)) { | ||
return assets.get(assetName)!; | ||
} | ||
if (!exports) { | ||
assets.set(assetName, undefined); | ||
return undefined; | ||
} | ||
while (true) { | ||
let candidate = `_asset_${counter++}_`; | ||
if (!code.includes(candidate)) { | ||
assets.set(assetName, candidate); | ||
return candidate; | ||
} | ||
} | ||
} | ||
|
||
function imports() { | ||
return ( | ||
[...assets] | ||
.map(([assetName, importedName]) => { | ||
if (importedName) { | ||
return `import ${importedName} from "${assetName}"`; | ||
} else { | ||
return `import "${assetName}"`; | ||
} | ||
}) | ||
.join('\n') + '\n' | ||
); | ||
} | ||
|
||
return { getName, imports }; | ||
} |
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