-
Notifications
You must be signed in to change notification settings - Fork 119
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
load a CSS file in pug file #139
Comments
use the pug-plugin, then you can load JS/TS and SCSS/CSS directly in Pug: link(href=require('./styles.scss') rel='stylesheet')
script(src=require('./main.js')) Pug plugin extract CSS and JS from their sources loaded in Pug. <link href="/assets/css/styles.05e4dd86.css" rel="stylesheet">
<script src="/assets/js/main.f4b855d8.js"></script> Specify the Pug files in the Webpack entry: const PugPlugin = require('pug-plugin');
module.exports = {
entry: {
// define your Pug files here
index: './src/views/home/index.pug', //=> dist/index.html
about: './src/views/about/index.pug', //=> dist/about.html
},
plugins: [
new PugPlugin(), // enable rendering of Pug files defined in Webpack entry
],
module: {
rules: [
{
test: /.pug$/,
loader: PugPlugin.loader, // Pug loader
},
{
test: /\.(css|sass|scss)$/,
use: ['css-loader', 'sass-loader']
},
],
},
}; |
@webdiscus, thanks a lot for the quick answer. I just realised you're on both webdiscus/pug-plugin and pugjs/pug-loader. It's quite confusing but the work is clever, useful and irreplaceable. I'm on the latest versions of node, npm and pug. I have 2 tasks to perform in my app: 2- Load a CSS file in a pug template Great effort, great work. Thanks again! |
I don't use the
You can see use examples of the methods.
The entry-point index.pug.
The application JS file // the pug-plugin use default method `render`,
// but to load a Pug file in JS as the template function can be used the query param '?pug-compile'
const tmpl = require('./app.pug?pug-compile');
// call template function with props used in template => HTML string
const html = tmpl({
myString: 'some text...'
});
// AND / OR if your Pug template hasn't props, then load it as HTML string:
const html2 = require('./app.pug');
document.getElementById('root').innerHTML = html; The template h1 Hello Pug!
p= myString The idea of pug-plugin - avoid to define JS, SCSS files in Webpack entry. For each web page must be defined the Pug file as entry-point. If you has many pages, you can write a function (5 lines code) to dynamic generate entries. P.S. |
@webdiscus, after declaring an index.pug entry following your webpack.config.js example, the template is loaded. I haven't tried to render a pug template yet. I'm just trying to load a CSS file in the index.pug entry file. Am I missing something? |
can you please create an issue by pug-plugin/issues with a link to your repo, then I can see where is a problem and can help you. Do you have the module rule for CSS in Webpack? {
test: /\.css$/,
use: ['css-loader']
}, |
It looks like I was having issues because of trying to render the pug files in an iframe. I opted for a s impler app structure without iframe and it works smoothly now. Thanks a lot for your help. |
How do we load a CSS file in a pug template with this module?
The text was updated successfully, but these errors were encountered: