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

Serve static assets #950

Open
1 of 3 tasks
tjwelde opened this issue Sep 12, 2019 · 2 comments
Open
1 of 3 tasks

Serve static assets #950

tjwelde opened this issue Sep 12, 2019 · 2 comments

Comments

@tjwelde
Copy link
Contributor

tjwelde commented Sep 12, 2019

This is my intent (choose one)

  • I want to report a bug
  • 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.

@ZauberNerd
Copy link
Contributor

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).

@tjwelde
Copy link
Contributor Author

tjwelde commented Oct 16, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants