简体   繁体   中英

Angular 8: access the assets folder when running ng build --prod --base-href './project1'

My client want me to deploy multiple projects on the same ngnix server with the following configuration.

location /project1/ {
    alias /usr/local/var/www/project1/;
    try_files $uri$args $uri$args/ /project1/index.html;
}

In the above configuration I could not access the assets folder as it's in the project1 folder and I try to access it from the root of the domain example src:

url('/assets/fonts/texta/Texta-Regular/Texta-Regular.eot')

When I change the path with src: url('./assets/fonts/texta/Texta-Regular/Texta-Regular.eot') I'm getting following problem when building the production build

ERROR in ./src/styles.scss Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): ModuleError: Module Error (from ./node_modules/postcss-loader/src/index.js): (Emitted value instead of an instance of Error) CssSyntaxError: /Users/Desktop/personal/projects/example/src/styles.scss:10115:13: Can't resolve './assets/fonts/texta/Texta-Regular/Texta-Regular.eot%3F' in '/Users/Desktop/personal/projects/example/src'

You can't use absolute paths starting from "/" so you have 2 options:

1- Using relative paths (with "../" to go back up the hierarchy). This one is a bit messy but easy to do.

2- Define your assets folder in the compilerOptions of your tsconfig.json as follows:

"compilerOptions": {
"baseUrl": ".",
"paths": {
  "@assets/*": ["./src/assets/*"],
}
}

Then you can use "@assets/" in your Url. Take note of the baseUrl and assets url and change it accordingly based on your project structure.

Hope this helps.

I have not problem to compile url('./assets/fonts/texta/Texta-Regular/Texta-Regular.eot'). Please check the Texta-Regular.eot presence.

angular.json 中的 rebaseRootRelativeCssUrls 标志确实对我有用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM