My less organization right now is as follows:
styles/pages/page1/index.less
styles/pages/page1/tab1.less
...
styles/widgets/widget1.less
styles/widgets/widget2.less
...
styles/tools/partials.less
...
styles/app.less
The entirety of my app.less file are #import statements to bring all the other parts in.
#import "tools/partials";
#import "widgets/widget1";
#import "widgets/widget2";
#import "pages/page1/index";
#import "pages/page1/tab1";
//...
These statements are maintained manually, which sucks. Is there a better way?
I'm dreaming of something like this:
#import "tools/partials";
#import "widgets/*";
#import "pages/**/*";
Maybe some kind of script to handle this on the editor level (using WebStorm)? Or perhaps some kind of plugin for the less compiler itself?
Right now I'm serving less files from an express middleware in my app, but I can easily switch to grunt if there's a solution there.
Take a look at grunt-less-imports. It maintains an imports file based on your files (app.less in your case).
Here is how I use it in my recent project. I have a bunch of less files, like so:
And I use grunt-less-imports like so (gruntfile.js):
less_imports: {
options: {
banner: '// Compiled stylesheet'
},
styles: {
src: ['<%= config.app %>/assets/less/**/*.less', '!<%= config.app %>/assets/less/styles.less'],
dest: '<%= config.app %>/assets/less/styles.less'
}
},
This task make a styles.less with imports:
So you just add-delete your less files, and task do the job importing them. Is it what you looking for?
Update
If you need more structure in your imports file, you can get some. For example, i have two folders with less files, page-frame-blocks and popups and want them to be imported first:
less_imports: {
options: {
banner: '// Compiled stylesheet'
},
styles: {
src: [
'<%= config.app %>/assets/less/page-frame-blocks/*.less',
'<%= config.app %>/assets/less/popups/*.less',
'<%= config.app %>/assets/less/*.less',
'!<%= config.app %>/assets/less/styles.less'],
dest: '<%= config.app %>/assets/less/styles.less'
}
},
Now my imports file looks like this:
Want popups styles be imported first? Just move it to top in src section. You get the idea - you can explicitly say what folders you want and in what order using grunt globbing patterns.
Related
I'm struggling with the Setting up Loaders for my VueJS project (vue3), to be more specific i have a following structure of the folders
My Images is located at path called assets and I would like url-loader to know, that he need to grab images from THIS path.
my loaders configuration at vue.config.js looks following
configureWebpack: {
module: {
rules: [
{ test: /\.(jpg|jpeg|png|woff|woff2|eot|ttf|svg)$/,
loader: 'url-loader',
options: {limits: 100000, name: "/assets/[name].[ext]"}},
],
}
},
If there is any obvious mistakes PLEASE point out on them.
Cuz i'm not really sure if i did it right
By default Grunt compiles bootstrap.less into bootstrap.css. The question is how do I make it compile my custom styles.less where I am going to import all Bootstrap less files plus some of my own into styles.css?
In think there are many answers for that question. I think you should following the advice of #seven-phases-max and try to not modify the source code (or at least make minimal changes).
In your Gruntfile.js you will find, two Less compile arguments (compileCore and compileTheme):
less: {
compileCore: {
options: {
strictMath: true,
sourceMap: true,
outputSourceFiles: true,
sourceMapURL: '<%= pkg.name %>.css.map',
sourceMapFilename: 'dist/css/<%= pkg.name %>.css.map'
},
src: 'less/bootstrap.less',
dest: 'dist/css/<%= pkg.name %>.css'
},
compileTheme: {
options: {
strictMath: true,
sourceMap: true,
outputSourceFiles: true,
sourceMapURL: '<%= pkg.name %>-theme.css.map',
sourceMapFilename: 'dist/css/<%= pkg.name %>-theme.css.map'
},
src: 'less/theme.less',
dest: 'dist/css/<%= pkg.name %>-theme.css'
}
}
And the following task defined: grunt.registerTask('less-compile', ['less:compileCore', 'less:compileTheme']);
It should be easy to add your own sub task and compile argument. When doing that you can compile your styles.less into styles.css. Notice that you have a separated CSS file as result. Loading that file requires a extra http request.
Unless you import bootstrap's Less code in your styles.less you can also not reuse Bootstrap's variables and mixins. If you need a separated CSS file consider to create your styles.less as follows:
#import (reference) "bootstrap";
//your custom code here
In the case that you are able to compile all you code in a single CSS file, you can do the following:
Create a file custom.less and add your customization in that file. And save that file in the same folder as your bootstrap.less file.
Than write at the end of the bootstrap.less file the following code: #import "custom";
After that you can compile bootstrap as usually.
Alternatively create custom.less as follow:
#import "bootstrap";
//your custom code here
To compile custom.less instead of bootstrap.less by default you should have to modify your Gruntfile.js. In the CompileCore argument change the src option into src: 'less/custom.less',.
I am almost absolutely new to grunt.
I used grunt with bootstrap 3.1.1 and the grunt watch-command worked out great. My bootstrap.min.css file was compiled every time. (i upload my bootstrap.min.css)
Later on i lost my 3.1.1 grunt file (long story short my computer crashed).
So now with Bootstrap 3.2.0 i was going to restablish my grunt-work-flow.
But now when i use grunt watch i only get the "bootstrap.css" and "bootstrap.theme.css" compiled.
I have spent the last hours to figure this out without success.
WHAT I WANT
I want grunt watch to compile the minified "bootstrap.min.css"
So how do i call the min.css-function on the watch?
I will be glad to get some help.
Grunt watch will only watch the files then run tasks you have set. I am assuming in your gruntfile you ahve something like this:
css: {
files: [
'bootstrap.css',
],
tasks: ['less'],
},
In the less task you should have something like below. Note the cleancss option being set to true:
options: {
cleancss: true
},
files: {
"dest/bootstrap.min.css": "src/bootstrap.css",
"dest/bootstrap.theme.min.css": "src/bootstrap.theme.css"
}
UPDATE:
Based on the file you uploaded you should be running the cssmin:core task when the watch is triggered.
UPDATE 2:
To update the watch task you can just add the cssmin:core task to the less subtask:
less: {
files: 'less/**/*.less',
tasks: ['less', 'cssmin:core]
}
Here you are telling it to run the less task, followed by the cssmin task whenever one of the less files is changed while watching.
Your gruntfile.js will have a 'watch' section as below
watch: {
src: {
files: '<%= jshint.core.src %>',
tasks: ['jshint:src', 'qunit', 'concat']
},
test: {
files: '<%= jshint.test.src %>',
tasks: ['jshint:test', 'qunit']
},
less: {
files: 'less/**/*.less',
tasks: 'less'
}
},
The tasks under less subsection define the tasks that 'grunt watch' will run. In Bootstrap 3.3.2 (I guess in 3.1.1 also it would be the same) is the 'cssmin' task that minifies the core bootstrap css. So you need to add the task to less so the code above becomes
watch: {
src: {
files: '<%= jshint.core.src %>',
tasks: ['jshint:src', 'qunit', 'concat']
},
test: {
files: '<%= jshint.test.src %>',
tasks: ['jshint:test', 'qunit']
},
less: {
files: 'less/**/*.less',
tasks: ['less', 'cssmin']
}
},
None of the above worked for me. Looks like the css minification command changed to cssmin:minifyCore so I have updated my watch task to the following:
watch: {
src: {
files: '<%= jshint.core.src %>',
tasks: ['jshint:core', 'qunit', 'concat']
},
test: {
files: '<%= jshint.test.src %>',
tasks: ['jshint:test', 'qunit']
},
less: {
files: 'less/**/*.less',
tasks: ['less', 'cssmin:minifyCore']
}
},
Hopefully this helps others!
Here is my problem. In my css directory I have a less directory that holds page.less, and search.less. Inside page.less I am using the import method to render my search styles when grunt complies my less files. I end up with page.css. In page.css the search styles are being added twice even though inside the page.less file I am only importing it in one place
My gruntfile.js is as follows
less: {
options: {
compress: false,
ieCompat: false
},
dev: {
dest: "css/page.css",
src: [
"css/less/*"
]
}
},
The pattern src: ["css/less/*"] is compiling all files.
This pattern should only include files that are not imported into other files, otherwise you will be including file B (which imports file A) and file A itself - again.
In compass-sass the underscore is used to identify files being imported into others, while "main" files (which import others) have no underscore.
I have setup a project with Bootstrap's less files and gruntjs in order to be able to have live edit in the Chrome Workspaces.
Hereunder is my Gruntfile.js. It auto-compiles less files into the desired style.css and creates a source map file when I save my changes. I'm also able to edit and save less files from Chrome Workspaces after adding the project directory to the Workspaces.
module.exports = function(grunt) {
'use strict';
require('matchdep').filterDev('grunt-!(cli)').forEach(grunt.loadNpmTasks);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
less: {
dev: {
options: {
sourceMap: true,
sourceMapFilename: 'public/css/style.map',
sourceMapBasepath: 'public/css'
},
files: {
'public/css/style.css': 'less/style.less'
}
}
},
watch: {
all: {
files: ['less/**/*.less'],
tasks: ['less'],
}
}
});
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['less', 'watch']);
};
The problem I'm having is that I'm unable to see the live reload of my modifications on the less files via Sublime Text 2 or Chrome Workspaces directly in the browser without refreshing the page.
What am I missing? I have to map files? What file to what file exactly? Will I need to map multiple the same way or only one file.
I've also added an image where you can see the files tree.
FYI, also please note that the style.less imports the bootstrap less files and my custom less files.
// Core variables and mixins
#import "bootstrap/bootstrap";
#import "showtime/lib";
#import "showtime/intro";
#import "showtime/nav";
#import "showtime/portfolio";
#import "showtime/contact";
#import "showtime/footer";
#import "showtime/album";
Update If edit from the Elements tab, my style.less file gets overwritten with the content that is also in style.css and then it works. What am I doing wrong ?
Many thanks for your time and help.
OK the only I was able to make everything work is by putting the less files in my css folder.