preact-cli icon indicating copy to clipboard operation
preact-cli copied to clipboard

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

Open sowdf opened this issue 9 years ago • 10 comments

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

sowdf avatar Aug 01 '17 23:08 sowdf

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

sowdf avatar Aug 01 '17 23:08 sowdf

Can you show what you changed?

developit avatar Aug 02 '17 03:08 developit

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 avatar Aug 09 '17 09:08 jbelis

@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?

thangngoc89 avatar Aug 09 '17 10:08 thangngoc89

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

jbelis avatar Aug 09 '17 17:08 jbelis

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

thangngoc89 avatar Aug 09 '17 18:08 thangngoc89

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

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

garrows avatar Dec 21 '17 23:12 garrows

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

developit avatar Dec 25 '17 01:12 developit

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

BoldBigflank avatar May 12 '18 15:05 BoldBigflank

feel free to open a new issue

ForsakenHarmony avatar Jan 25 '19 23:01 ForsakenHarmony