grunt-contrib-connect/grunt-contrib-watch livereload: Refused to apply style from [path] because its MIME type - gruntjs

I'm using grunt-contrib-connect and grunt-contrib-watch for the live reload feature, like so:
watch: {
options: {
livereload: true,
},
files: ['src/**/*'],
tasks: ['dev']
},
connect: {
server: {
options: {
port: 8000,
base: './dist',
hostname: '0.0.0.0',
protocol: 'http',
livereload: true,
open: true,
}
}
},
It works great, but every time I save a css file, I get this error from Chrome:
Refused to apply style from 'http://localhost:8000/section-featured-work/mod-mobile-design.css?livereload=1533148529995' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
Is there a way to specify the MIME type to the script that loads the CSS files?
I do not believe this is a duplicate of stackoverflow.com/questions/48248832/, as I believe this is specific to
grunt-config and grunt-watch livereload. The CSS is loaded with JavaScript. So how can I fix this for those packages?
By the way, the styles seem to load anyways, but I do not allow runtime errors of any sort in my builds, if at all avoidable.

Related

Grunt run task depending of file modified, it's possible?

can you help-me with this problem?
I need run a specific task depending of file modified, for example:
Modifie: _header.scss
Run: sass:header task
Modifie: _footer.scss
Run: sass:footer task
Modifie: _banners.scss
Run: sass:banners task
I've been trying to get the name of the file at save time to use it as a parameter, but I can not figure out ways to do this.
My project allows more people to work simultaneously but the work of defining which component of the project will be exported to CSS is manual, so I am trying to make this process of compiling the final CSS of each module as automatic.
My problem is how I can identify the name of the modified file, not the type of file.
Thank you very much!
It should look like this. When sass files are changed, grunt generates css files and copies (to dist directory) changed files only. Also as far as you can see connect and watch task implement live reload.
connect: {
options: {
port: ...
livereload: 35729,
hostname: '127.0.0.1'
},
livereload: {
options: {
base: [
'some dist'
]
}
}
},
// check that files are changed
watch: {
livereload: {
options: {
livereload: '<%= connect.options.livereload %>'
},
files: [
'your dist/*.css'
]
},
// when sass file is changed, tasks will run
// newer:copy means that we have to do task copy with changed files only
sass: {
files: ['your dir/*.sass'],
tasks: ['sass', ...some other tasks like cssmin..., 'newer:copy']
}
},
sass: {
dist: {
files: [
...sass to css
]
}
},
copy: {
css: {
....
}
}
Also you can be interested in task like grunt-changed. The task configures another task to run with src files that have been changed. For more information please check the page grunt-changed

Grunt how to get files outside main folder

I am using grunt connect server for maven angular project. I have some static files outside the angular folder. Is there way to redirect every call for file "redirect_folder/x/y/file.txt" to "target_folder/x/y/file.txt" just grunt need to change redirect_folder word for my static folder path.
Edit:
I copied the user.json to style folder, and trying to make style folder as a base but it doesnt works:
$http.get('user.json')
grunt.js:
grunt.initConfig({
connect: {
all: {
options: {
base: ['/media/data/project/src/main/webapp/resources/ngSrc/app'],
port: 10100,
hostname: "0.0.0.0",
livereload: true,
combine : [ '/media/data/project/src/main/webapp/resources/ngSrc/app', '/media/data/project/src/main/webapp/resources/ngSrc/app/style']
}
}
},
...

Grunt.js watch - node_modules always being watched

I'm using Grunt for livereload only. It works fine, but I noticed that it has high CPU, and when I run it with "--verbose" I see that it's watching the whole "node_modules" folder.
So, I made some research, and tried to ignore this. Unfortunately, with no success.
My watch part of "gruntfile.js" is:
// the watch stuff ..
watch: {
all: {
files: ['!**/node_modules/**', 'js/**/*.js', 'css/**/*.css', 'index.html'],
options: {
interval: 5007,
livereload: true
}
}
},
and basically I'm saying I want grunt to watch all js, css and the index.html file. Explicitly added the code for ignoring "node_modules", but it still says it's watching it and the CPU goes around 30%. (Mac OSx)
==================
One thing I noticed though:
When I make a change in the "gruntfile.js" - for example add one more file to the "files" property of the "watch" task - then it restarts the grunt, and in the console I see it start watching only the files I want, and then CPU goes below 1%. (I guess it's how it should be originally.)
What am I doing wrong?
====================
Edit: Unfortunately, when I change the gruntfile and I see only the files I want are being watched - then the livereload stuff is no longer working.
====================
This is the article I started from:
http://thecrumb.com/2014/03/15/using-grunt-for-live-reload/
Here is my package.json file:
{
"name": "grunt-reload",
"version": "1.0.0",
"devDependencies": {
"grunt": "~0.4.3",
"matchdep": "~0.3.0",
"grunt-express": "~1.2.1",
"grunt-contrib-watch": "~0.6.0",
"grunt-open": "~0.2.3"
}
}
And here is my Gruntfile.js:
module.exports = function(grunt) {
require('matchdep')
.filterDev('grunt-*')
.forEach(grunt.loadNpmTasks);
grunt.initConfig({
// the web server ..
express: {
all: {
options: {
bases: [__dirname],
port: 8888,
hostname: 'localhost',
livereload: true
}
}
},
// the watch stuff ..
watch: {
all: {
files: ['js/**/*.js', 'css/**/*.css', 'index.html'],
options: {
livereload: true
}
}
},
// the automatic opening stuff ..
open: {
all: {
path: 'http://localhost:8888/index.html'
}
}
});
// create the server task ..
grunt.registerTask(
'server',
['express', 'open', 'watch']
);
}; // end of "module.exports" ..
And I start all of this with "grunt server".
edit: After you shared your Gruntfile, the problem became more clearer.
In your grunt-express config, you have set livereload to true and bases to __dirname, which is the folder Gruntfile in ran from.
Now, let's look at the grunt-express documentation:
bases
Type: String|Array Default: null
The bases (or root) directories from which static files will be served. A connect.static() will be generated for each entry of bases. When livereload is set to true (or set to a specific port number), a watch task will be created for you (at runtime) to watch your basePath/**/*.*.
(Emphasis mine)
So in your grunt-express config, you set your livereload to watch all the files in your basepath, which of course includes node_modules.
You have couple of options:
Remove your other watch task and config grunt-expresses basepath accordingly (just copy your other config basically).
Keep both of the watch tasks and just ignore the node_modules in the other grunt-express -> bases config.
Remove the bases and handle livereloading in the other watch task
Where is the node_modules folder located? If it's on root, you can just remove the node_modules parameter altogether as long as there aren't any matching glob patterns:
// the watch stuff ..
watch: {
all: {
files: ['js/**/*.js', 'css/**/*.css', 'index.html'],
options: {
interval: 5007,
livereload: true
}
}
},
Now you are watching: All .js-files under js folder, index.html on root and so on.
However if you have node_modules under js/node_modules, you can the explicitly ignore that folder and the files in it:
files: ['!js/node_modules/**', 'js/**/*.js', 'css/**/*.css', 'index.html'],
Anyway, depending on your Gruntfiles and node_module-folders location your configuration should work just fine anyway.

Gruntjs Livereload not working

I can't seem to get the livereload option to work on the grunt-contrib-watch task.
I have the local host livereload.js file attached at the bottom of my html file.
When i go to http://localhost:35729/ all i get is the following:
{"tinylr":"Welcome","version":"0.0.5"}
Everything seems to be set up and working correctly. Even when i run grunt --verbose it says Live reload server started on port: 35729.
Below is my watch configuration in my gruntfile
Any help would be appreciated!
watch: {
options:{
livereload: true,
}
styles: {
files: ['less/**/*.less'], // which files to watch
tasks: ['less'],
options: {
nospawn: true
}
},
html:{
files: ['site/**/*.html', 'includes/**/*.html'],
tasks: ['includes'],
options: {
nospawn: true,
}
}
}
});
The output you are seeing is correct. http://localhost:35729 is the LiveReload server, not your app.
Looking at your code, you haven't included the LiveReload script in your index.html.
I've sent you a pull request and can confirm the LiveReload is working.

Grunt-watch livereload does not work with compiled files (stylus, jade, etc.)

My livereload is "working" in that it live reloads the page when a file is changed, but only if I modify the file directly. If the file is generated through the grunt stylus or jade compiler, nothing happens.
When I look at grunt with verbose turned on, the Live reloading root.css... line appears only if I save root.css directly. If root.css is modified through the stylus compiler, the line does not appear. It's as if watch doesn't detect the file has been changed if it's changed through a compiler. This same issue occurs with jade as well.
Here's my stylus task (trimmed):
stylus: {
options: {
compress: false,
use: [
require('autoprefixer-stylus')
]
},
src: [
'app/styl/**/*.styl'
],
dest: 'build/css/root.css'
}
And here's my watch task (trimmed):
livereload: {
options: {
livereload: 1337,
},
files: 'build/**/*'
},
stylus: {
files: [
'app/styl/**/*.styl',
],
tasks: ['stylus:dev']
},
I really hope I'm just doing something stupid. I can't find any problems similar to this one.
EDIT:
Just in case this helps, I discovered that by changing my grunt task from running ['clean','jade:dev', 'stylus:dev', 'connect:dev', 'watch'] to only running ['connect:dev', 'watch'], livereload works as intended once, then never again. (Modifying the css directly still works though.)
EDIT 2:
I was able to fix this by adding livereload to each specific task in watch, like so:
livereload: {
options: {
livereload: 1337,
},
files: 'build/**/*'
},
stylus: {
files: [
'app/styl/**/*.styl',
],
tasks: ['stylus:dev'],
options: {
livereload: 1337
}
},
As to why this works, I have no idea. If anyone can shed some light on this, it'd be much appreciated. Though to be honest, I don't know why I didn't try this earlier.

Resources