Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for keeping public assets and ember-addon.public-assets meta in sync #1368

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/addon-dev/src/rollup-public-reexports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { readJsonSync, writeJsonSync } from 'fs-extra';
import walkSync from 'walk-sync';
import type { Plugin } from 'rollup';

export default function publicAssets(opts: { exclude: string[] }): Plugin {
return {
name: 'public-assets-bundler',
generateBundle() {
let pkg = readJsonSync('package.json');
const filenames = walkSync('public', {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To my knowledge, the public folder is not something that's configurable. Otherwise, this could be part of the options (w/ a default value)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is any specific path mandated here, according to https://rfcs.emberjs.com/id/0507-embroider-v2-package-format#assets. Also this is about the v2 addon's internal file layout, and that should be seen as an implementation detail.

So I would suggest to make this configurable. Also, maybe we should also have an include pattern that we can pass to glob of walkSync? So users could say e.g. publicAssets({ path: 'assets', include: '**/*.svg'), to make all svgs in ./assets (instead of ./public).

directories: false,
ignore: opts?.exclude || [],
});
const publicAssets: Record<string, string> = filenames.reduce(
(acc: Record<string, string>, v): Record<string, string> => {
acc['./public/' + v] = ['/', pkg.name, '/', v].join('');
return acc;
},
{}
);

pkg['ember-addon'] = Object.assign({}, pkg['ember-addon'], {
'public-assets': publicAssets,
});

writeJsonSync('package.json', pkg, { spaces: 2 });
},
};
}
5 changes: 5 additions & 0 deletions packages/addon-dev/src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 publicReexports } from './rollup-public-reexports';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I wouldn't call it (re)exports, as this is not getting imported. Rather it's more of the classic push models vs. pull. Maybe just name it just similar to the public API for users, like rollup-public-assets?

import type { Plugin } from 'rollup';

export class Addon {
Expand Down Expand Up @@ -83,4 +84,8 @@ export class Addon {
dependencies() {
return dependencies();
}

publicAssets(opts: { exclude: string[] }) {
return publicReexports(opts);
}
}