diff --git a/packages/addon-dev/src/rollup-public-assets.ts b/packages/addon-dev/src/rollup-public-assets.ts new file mode 100644 index 000000000..6856624d1 --- /dev/null +++ b/packages/addon-dev/src/rollup-public-assets.ts @@ -0,0 +1,36 @@ +import { readJsonSync, writeJsonSync } from 'fs-extra'; +import walkSync from 'walk-sync'; +import type { Plugin } from 'rollup'; + +export default function publicAssets( + path: string, + opts: { include: string[]; exclude: string[] } +): Plugin { + const includeGlobPatterns = opts?.include; + const excludedGlobPatterns = opts?.exclude || []; + + return { + name: 'public-assets-bundler', + generateBundle() { + let pkg = readJsonSync('package.json'); + const filenames = walkSync(path, { + directories: false, + globs: includeGlobPatterns, + ignore: excludedGlobPatterns, + }); + const publicAssets: Record = filenames.reduce( + (acc: Record, v): Record => { + acc[`./${path}/${v}`] = ['/', pkg.name, '/', path, '/', v].join(''); + return acc; + }, + {} + ); + + pkg['ember-addon'] = Object.assign({}, pkg['ember-addon'], { + 'public-assets': publicAssets, + }); + + writeJsonSync('package.json', pkg, { spaces: 2 }); + }, + }; +} diff --git a/packages/addon-dev/src/rollup.ts b/packages/addon-dev/src/rollup.ts index 381e69e95..5c49149ce 100644 --- a/packages/addon-dev/src/rollup.ts +++ b/packages/addon-dev/src/rollup.ts @@ -4,6 +4,7 @@ import { default as appReexports } from './rollup-app-reexports'; import { default as clean } from 'rollup-plugin-delete'; import { default as keepAssets } from './rollup-keep-assets'; import { default as dependencies } from './rollup-addon-dependencies'; +import { default as publicAssets } from './rollup-public-assets'; import type { Plugin } from 'rollup'; export class Addon { @@ -83,4 +84,8 @@ export class Addon { dependencies() { return dependencies(); } + + publicAssets(path: string, opts: { include: string[]; exclude: string[] }) { + return publicAssets(path, opts); + } }