Assets path in css

Import CSS, assets and JavaScript

To import all the Sass rules from GOV.UK Frontend, add the following to your Sass file:

@import "node_modules/govuk-frontend/govuk/all"; 

Import specific parts of the CSS

If you want to improve how quickly your service’s pages load in browsers, you can import only the Sass rules you need.

  1. Import node_modules/govuk-frontend/govuk/base in your Sass file.
  2. Import the parts of the CSS you need.

For example, add the following to your Sass file to import the CSS you need for a basic GOV.UK page.

@import "node_modules/govuk-frontend/govuk/base"; @import "node_modules/govuk-frontend/govuk/core/all"; @import "node_modules/govuk-frontend/govuk/objects/all"; @import "node_modules/govuk-frontend/govuk/components/footer/index"; @import "node_modules/govuk-frontend/govuk/components/header/index"; @import "node_modules/govuk-frontend/govuk/components/skip-link/index"; @import "node_modules/govuk-frontend/govuk/utilities/all"; @import "node_modules/govuk-frontend/govuk/overrides/all"; 

You can remove lines that import parts of the CSS you do not need.

You do not need /index at the end of your component imports if you’re using Dart Sass, LibSass 3.6.0 or higher, or Ruby Sass 3.6.0 or higher.

Import an individual component’s CSS using a single import

You can also import a component and all its dependencies without importing node_modules/govuk-frontend/govuk/base first.

To import the button component for example, add the following to your Sass file:

@import "node_modules/govuk-frontend/govuk/components/button/button"; 

Simplify Sass import paths

If you want to make Sass import paths shorter, add node_modules/govuk-frontend to either your:

You can then import without using node_modules/govuk-frontend/ in your import path. For example:

@import "govuk/components/button/button"; 

Override with your own CSS

If you want to override GOV.UK Frontend’s styles with your own styles, @import GOV.UK Frontend’s styles before your own Sass rules.

Using GOV.UK Frontend with our old frameworks

If your project uses GOV.UK Frontend toolkit, GOV.UK Template or GOV.UK Elements, you can configure GOV.UK Frontend to work with them.

Silence deprecation warnings from dependencies in Dart Sass

If you’re using Dart Sass 1.33.0 or greater, you may see deprecation warnings when compiling your Sass. For example:

DEPRECATION WARNING: Using / for division is deprecated and will be removed in Dart Sass 2.0.0. 

We’re currently unable to fix these deprecation warnings without breaking support for LibSass. However, you can silence the warnings caused by GOV.UK Frontend and other dependencies. Make sure you’re using Dart Sass 1.49.10 or greater, and then if you’re:

  • calling the Sass compiler from the command line, pass the —quiet-deps flag
  • using the JavaScript API, include quietDeps: true in the options object
Читайте также:  Приложения для java словари

You’ll still see deprecation warnings if you’re using / for division in your own Sass code.

Font and image assets

To use the font and image assets from GOV.UK Frontend, you can either:

  • serve the assets from the GOV.UK Frontend assets folder — recommended
  • copy the font and image files into your application

Serve the assets from the GOV.UK Frontend assets folder — recommended

Set up your routing so that requests for files in /assets are served from /node_modules/govuk-frontend/govuk/assets .

For example if you’re using express.js, add the following to your app.js file:

var path = require('path'); app.use('/assets', express.static(path.join(__dirname, '/node_modules/govuk-frontend/govuk/assets'))) 

Copy the font and image files into your application

If you decide to copy the assets instead, copy the:

  • /node_modules/govuk-frontend/govuk/assets/images folder to /assets/images
  • /node_modules/govuk-frontend/govuk/assets/fonts folder to /assets/fonts

You should use an automated task or your build pipeline to copy the files, so your project folder stays up to date when we update GOV.UK Frontend.

If you have your own folder structure

If you use a different folder structure than /assets/images and /assets/fonts , you can set Sass variables so that Sass builds the CSS to point to your folders.

Set one of the following before the @import line in your Sass file:

Set the $govuk-assets-path variable if your font and image folders have the same parent folder. For example:

Set the $govuk-images-path and $govuk-fonts-path variables if your font and image folders have different parent folders. For example:

$govuk-images-path: "//"; $govuk-fonts-path: "//"; 

You can also use your own function to generate paths, for example if you’re using asset-pipeline in sass-rails. Set the $govuk-image-url-function and $govuk-font-url-function variables to the name of your function.

JavaScript

To import the JavaScript from GOV.UK Frontend, you can either:

  • add GOV.UK Frontend’s JavaScript file to your HTML
  • import the JavaScript using a bundler like Webpack

Add the JavaScript file to your HTML

If you decide to add the JavaScript to your HTML, first either:

  • set up your routing so that requests for the JavaScript file are served from node_modules/govuk-frontend/govuk/all.js
  • copy the node_modules/govuk-frontend/govuk/all.js file into your application

Then import the JavaScript file before the closing tag of your HTML page or page template, and run the initAll function to initialise all the components.

 .  src="/.js"> window.GOVUKFrontend.initAll()   

Select and initialise an individual component

You can select and initialise a specific component by using its data-module attribute. For example, use govuk-radios to initialise the first radio component on a page:

  var Radios = window.GOVUKFrontend.Radios var $radio = document.querySelector('[data-module="govuk-radios"]') if ($radio)  new Radios($radio).init() >  

Import JavaScript using a bundler

If you decide to import using a bundler, we recommend you use import to only import the JavaScript for components you’re using in your service. For example:

import  SkipLink, Radios > from 'govuk-frontend' var $skipLink = document.querySelector('[data-module="govuk-skip-link"]') if ($skipLink)  new SkipLink($skipLink).init() > var $radios = document.querySelectorAll('[data-module dl">') if ($radios)  for (var i = 0; i  $radios.length; i++)  new Radios($radios[i]).init() > > 

If you need to import all of GOV.UK Frontend’s components, then run the initAll function to initialise them:

import  initAll > from 'govuk-frontend' initAll() 

If you’re using a bundler that uses CommonJS like Browserify, you should use require :

const GOVUKFrontend = require('govuk-frontend') GOVUKFrontend.initAll() 

Select and initialise part of a page

If you update a page with new markup, for example a modal dialogue box, you can run initAll with a scope parameter to initialise the components on part of a page.

  var $modal = document.querySelector('.modal') window.GOVUKFrontend.initAll( scope: $modal >)  

If your JavaScript is not working properly

If your site has a Content Security Policy (CSP), the CSP may block the inline JavaScript in the page template. You may see a warning like the following in your browser’s developer tools:

Refused to execute inline script because it violates the following Content Security Policy directive: "default-src 'self'". 

To unblock inline JavaScript, do one of the following:

Make sure you understand the security implications of using either option, as wrong implementation could affect your service’s security. If you’re not sure what to do, talk to a security expert.

Use a hash to unblock inline JavaScript

You can unblock inline JavaScript by including the following hash in your CSP:

sha256-+6WnXIl4mbFTCARd8N3COQmT3bJJmo32N8q8ZSQAIcU= 

You do not need to make any changes to the HTML.

Use a nonce attribute to unblock inline JavaScript

If you’re unable to use the hash in your CSP, you can also use a nonce on inline JavaScript.

However, you should provide a nonce that hostile actors cannot guess. Otherwise, they could easily find a way around your CSP.

You should use a value which is:

  • unique for each HTTP response
  • generated using a cryptographically-secure random generator
  • at least 32 characters for hex, or 24 characters for base64

Make sure your script tags do not have any untrusted or unescaped variables.

If you’re using the Nunjucks page template, you can add the nonce attribute by setting the cspNonce variable.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

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

Asset paths in CSS #3582

Asset paths in CSS #3582

Comments

Webpack finds all relative module references in CSS (they start with ./) and replaces them with the final paths from the compiled bundle. If you make a typo or accidentally delete an important file, you will see a compilation error, just like when you import a non-existent JavaScript module. The final filenames in the compiled bundle are generated by Webpack from content hashes. If the file content changes in the future, Webpack will give it a different name in production so you don’t need to worry about long-term caching of assets.

But what should I do if the assets are not located in the same directory as the CSS files? All my assets are in src/assets/. , but CSS files can be in other folders as well. Neither ./assets/. nor ../assets/. seems to work.

The text was updated successfully, but these errors were encountered:

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

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

Relative static assets in CSS folder #932

Relative static assets in CSS folder #932

Comments

Just opening a new issue confirming that bug #208 still exists and @githoniel’s solution is still necessary today.

This has all to do with using relative paths in CSS files.

The text was updated successfully, but these errors were encountered:

So I tried to reproduce it, but still can’t,

Hello, I’ve a similar issue but in DEV.

When I run ‘npm run dev’, I get no errors related to the image URL path. The image simply does NOT display in the background.

After going through the issues related to this problem in detail, I tried all possible paths in hopes of getting it right:

../../assets/logo.png,
../assets/logo.png,
./assets/logo.png,
/assets/logo.png,
assets/logo.png, and even
logo.png

but no luck. I did modify the ‘webpack.base.conf.js’ file to include the below line, as suggested by @githoniel in #208

publicPath: env === ‘production’ ? ‘../../’ : ‘./’

Not sure if this matters, but in the same file, I have for the vue-loader:

Image path
src\assets\logo.png

AppShell.vue
src\components\AppShell.vue

The repo is hosted at https://github.com/sikanderv/vue-projects/tree/master/driving-school if someone would like to take a look at it.

Screenshot of the page and Dev Tools:

image

your problem is not a problem related to paths.

You are dynamically constructing CSS styles at runtime. Webpack can’t recognize that at build time without some help. That help is require() .

data ()  return  msg: 'My Driving School', tabIndex: 0, image: require('../../assets/logo.png') > >

Thank you very much, this works. Guess I have some reading to do about Webpack. 🙂

How should i use that same in vue with Typescript?
`image: require(‘../../assets/logo.png’)

I’m trying to deploy into a sub folder on the production server, and using

works fine for the javascript files, however the background image urls in the styles don’t work

I’ve tried removing the absolute path / from the front of the url and also tried using ./ but nether of these will compile into a valid Vue.js application,

I’ve also tried setting the assets path in the config e.g.

setting it to ‘./’ or ‘/assets’ but this doesn’t work either, as normally the build fails

Is there a workaround for this problem ?

I’m trying to deploy into a sub folder on the production server, and using

Источник

Оцените статью