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

Accounted for public assets #23

Merged
merged 6 commits into from
Mar 6, 2023
Merged

Accounted for public assets #23

merged 6 commits into from
Mar 6, 2023

Conversation

ijlee2
Copy link
Owner

@ijlee2 ijlee2 commented Mar 6, 2023

Background

In v1 addons, static assets (like images) are placed in the public folder:

This directory is not created by default in a new addon, but you can add it manually if there are static assets (like images) that you want to include in your addon. You can add static assets in the public/ directory in your addon, and they will automatically be merged into the public directory in the host app under a folder with the name of your addon.

For example, if your addon is published to npm as @foo/my-awesome-addon, and it contains an image at public/icon.png, the host app can reference it as /@foo/my-awesome-addon/icon.png in CSS or JS.

https://cli.emberjs.com/release/writing-addons/#public

In v2 addons, the assets can be stored anywhere in the addon package, because the mapping ember-addon.public-assets in package.json defines where the assets are and how the consuming project are to refer to them. For example, in [email protected], there is an image called construction.png, which lives in the images directory.

{
  "name": "ember-welcome-page",
  "files": [
    "addon-main.js",
    "dist",
    "images"  // ⬅
  ],
  "ember-addon": {
    "version": 2,
    "type": "addon",
    "main": "addon-main.js",
    "app-js": {
      "./components/welcome-page.js": "./dist/_app_/components/welcome-page.js"
    },
    "public-assets": {
      "./images/construction.png": "/ember-welcome-page/images/construction.png"  // ⬅
    }
  }
}

Thanks to @phndiaye for suggesting the feature.

What changed?

The codemod can now migrate the addon's assets by

  • Moving assets from public to <addon-name>/public
  • Updating ember-addon.files and ember-addon.public-assets

(When there are no assets, the two steps above do not occur.)

Note an upcoming feature of @embroider/addon-dev called addon.publicAssets(), which lets rollup keep the ember-addon.public-assets meta in sync. Once there is a new release of @embroider/addon-dev, we can downstream the changes to @embroider/addon-blueprint and this codemod.

/* src/blueprints/ember-addon/__addonLocation__/rollup.config.mjs */

...

import { Addon } from '@embroider/addon-dev/rollup';

const addon = new Addon({
  srcDir: 'src',
  destDir: 'dist',
});

export default {
  ...

  addon.publicAssets('public', {
    include: [
      /* File paths */
    ],
  });

  ...
};

References

@ijlee2 ijlee2 added the enhance: code Issue asks for new feature or refactor label Mar 6, 2023
@ijlee2 ijlee2 marked this pull request as ready for review March 6, 2023 22:04
@ijlee2 ijlee2 merged commit dd3e0bc into main Mar 6, 2023
@ijlee2 ijlee2 deleted the feature-migrate-public-assets branch March 6, 2023 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhance: code Issue asks for new feature or refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant