Preact-cli: npm run build , output background:url in style.css for pash is absolute , i need relative for path , no config

Created on 2 Aug 2017  Â·  10Comments  Â·  Source: preactjs/preact-cli

npm run build , output background:url in style.css for path is absolute , i need relative for path , no config

stale

Most helpful comment

Setting preact.config.js file to this fixed it for me.

export default function (config, env, helpers) {
  if (env.production) {
    config.output.publicPath = '';
  }
}

All 10 comments

I have to change the preact-cli file for the job, it is very troublesome

Can you show what you changed?

I think I have the same question. The build produces an index.html with absolute paths for assets (js, css, images). Examples:

<link href="/style.0bacc.css" rel="stylesheet">
<script defer="defer" src="/bundle.b1985.js">

This means you have to deploy at the root of your web server which is not always the case. Is there a recipe to change this, ideally by stripping the leading / ?

@jbelis it comes from webpack. Ideally, you can edit webpack publicPath config. But not sure if we hardcoded it anywhere in the cli. Could you give it a try?

It is hardcoded in src/lib/webpack/webpack-client-config.js and src/lib/webpack/webpack-server-config.js

@jbelis we provided an escape hatch for you. You can read more about configuring webpack config here https://github.com/developit/preact-cli#webpack

Setting preact.config.js file to this fixed it for me.

export default function (config, env, helpers) {
  if (env.production) {
    config.output.publicPath = '';
  }
}

It's best to use an absolute URL for these so that they don't break when using client-side routing.

@garrows answer works except for prerendered urls. any of those will create a file in its own folder like viewer/index.html and the reference to the bundle.js will be broken.

feel free to open a new issue

Was this page helpful?
0 / 5 - 0 ratings