hops icon indicating copy to clipboard operation
hops copied to clipboard

Serve static assets

Open tjwelde opened this issue 6 years ago • 2 comments

This is my intent (choose one)

  • [ ] I want to report a bug
  • [x] I want to request a feature or change
  • [ ] I want to provide or change a feature

The problem

I want to provide static assets, for example favicon.ico and robots.txt via the integrated express webserver.

AFAICS I can only import them in code, which will give me the path to the file, postfixed with a random id.

Proposed solution

Have a public folder with assets, I want to serve as-is.

tjwelde avatar Sep 12 '19 14:09 tjwelde

Hi @tjwelde thank you for reporting this. At the moment we're in the middle of preparing the next major version (Hops 12) which we will release at the end of the year, but I've found a way to configure a custom mixin which will bring you the desired behaviour for now.

mixins/public-dir/mixin.core.js

const { Mixin } = require('hops');
const { ensureLeadingSlash } = require('pathifist');
const { relative } = require('path');

module.exports = class PublicMixin extends Mixin {
  configureBuild(webpackConfig) {
    webpackConfig.output.publicPath = ensureLeadingSlash(relative(this.config.distDir, this.config.buildDir));
  }
};

Then register the mixin in your config and configure distDir (the directory that is being served by express) and buildDir (the directory in which webpack writes the artifacts) accordingly:

package.json

"hops": {
  "mixins": ["./mixins/public-dir"],
  "distDir": "<rootDir>/public",
  "buildDir": "<distDir>/assets"
}

Caution: This approach unfortunately does not work when using static rendering (hops build -s).

ZauberNerd avatar Sep 25 '19 15:09 ZauberNerd

Hey @ZauberNerd thank you for the suggestion. We will implement it as a mixin for now. If you don't want to use this ticket as a feature request for hops 12, feel free to close it.

tjwelde avatar Oct 16 '19 09:10 tjwelde