How to configure material components web with laravel-mix? - laravel

I use mdc with laravel-mix but it's not loading scss files from node modules.
I tried some other solutions about giving includepaths in mix.sass, but they are not working.
I tried code in webpack.mix.js
const path = require('path');
let mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css', {
includePaths: ['./node_modules']
});
but its still giving me same error that
undefined
^
Can't find stylesheet to import. #import "#material/elevation/mixins";

For Laravel 6 I had to do it this way:
npm install --save material-components-web
app.scss:
#import "material-components-web/material-components-web";
webpack.mix.js:
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css', {
sassOptions: {
includePaths: ['./node_modules']
}
});

Yes, answer from Edmund Sulzanok is correct, add sassOptions but for me too:
Laravel VERSION = '6.7.0';
I had to upgrade npm package.json devDependencies
"devDependencies": {
...
"cross-env": "^6.0.3",
"laravel-mix": "^5.0.1",
...,
"resolve-url-loader": "^3.1.1",
"sass": "^1.25.0",
"sass-loader": "^8.0.2",
...
},
"dependencies": {
...
"material-components-web": "^4.0.0",
...
}
After modify package.json run npm install
npm install
You could see Packages outdated with
npm outdated
good luck

Related

Nuxt & SassError: Undefined variable

Hei there, I cannot find the solution for my error.
Here is the assets structure:
-| assets
---| scss
-----| _grid_variables.scss
-----| ....
-----| variables.scss
Here is the _grid_variables.scss file:
$mobile-grid: 577px;
$tablet-grid: 768px;
$desktop-grid: 1024px;
Here is the variables.scss file:
#import "_colors_variables";
#import "_grid_variables";
#import "_fonts";
#import "_shadows";
Here is part of the package.json file:
{
...
"devDependencies": {
...
"node-sass": "^4.14.1",
"nodemon": "^1.18.9",
"sass": "^1.43.4",
"sass-loader": "^8.0.2"
}
}
*** I have tried diferent versions of node-sass and sass-loader:
node-sass#6.0.1 + sass-loader#10.2.0
node-sass#6.0.1 + sass-loader#8.0.2
.... and some other tryings
Here is part of my nuxt.config.js:
css: [
'~assets/scss/main.scss'
],
styleResources: {
scss: ['./assets/scss/variables.scss']
},
build: {
rules: [
{
test: /\.s[ac]ss$/i,
use: ['style-loader', 'css-loader', 'sass-loader']
}
],
extend(config, ctx) {}
},
And here is where I am trying to use the variable:
<style lang="scss" scoped>
...
#media screen and (max-width: $mobile-grid) {
.description-row {
text-align: center;
}
}
</style>
I really hope someone can help me to get out from this error.
I've found the solution for my Error:
I was including ./assets/scss/variables.scss in the styleResources in my nuxt.config.js, but I was forgetting to include the '#nuxtjs/style-resources' package (which was correctly installed) in the modules.
You can try this one. Write the following code inside nuxt.confit.js file. Surely it will work.
styleResources: {
scss: [
'~/assets/scss/variables.scss',
'~/assets/scss/_grid_variables.scss'
]
}

Tailwind not getting imported in Laravel

I have a Laravel project and I'm trying to install Tailwind.
I have a tailwind.css file: \resources\assets\stylesheets\tailwind.css
#import "tailwindcss/base";
#import "tailwindcss/components";
#import "tailwindcss/utilities";
Which I import in:\resources\assets\stylesheets\index.css
#import "tailwind.css"
My webpack.mix.js file:
const mix = require('laravel-mix');
const tailwindcss = require('tailwindcss');
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css')
.options({
processCssUrls: false,
postCss: [ tailwindcss('./tailwind.config.js') ],
});
And tailwind.config.js:
module.exports = {
purge: [],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}
When I run the app and check the generated http://localhost/dist/css/app.css I see this:
/* Import Tailwind*/
#tailwind base;
#tailwind components;
#tailwind utilities
I've installed tailwind through npm, can also find the tailwind folder in my node_modules folder.
Have you included index.css in your app.scss file?
You really don't need to put Tailwind through a sass parser if all you're doing is including the base styles etc. anyway.
Assuming you have Tailwind installed as a dependency already, you can include Tailwind in your project as follows:
const mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
.postCss('resources/assets/stylesheets/index.css', 'public/css', [
require('tailwindcss'),
]);

GlobalVueStyles: not working for global Sass in vue components

I am trying to load a global sass variable file into my Vue style tags. If I am understanding globalVueStyles right, this should Indicate a file to include in every component style. But when I set it up it seems to not do anything and when I run npm run dev I get an undefined variable error.
Here is my laravel mix code:
mix.js('resources/js/app.js', 'public/js')
.vue()
.sass('resources/sass/main.scss', 'public/css')
.options({
extractVueStyles: true,
globalVueStyles: 'resources/sass/utils/_variables.scss',
})
.version()
after doing this I thought I should have access to my variables in my .vue files by doing this:
<template>
<h1>Example Component hi test</h1>
</template>
<script>
export default {
}
</script>
<style lang="scss">
h1 {
font-size: 50px;
color: $blue;
}
</style>
And here is the error I am getting:
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: Undefined variable.
╷
4 │ color: $blue;
│ ^^^^^
╵
resources/js/components/HelloWorld.vue 4:11 root stylesheet
I'm sure I'm missing something, but any help on this would be appreciated.
I was led to answer from https://github.com/JeffreyWay/laravel-mix/issues/2896;
they seemed to upgrade the syntax the documentation can be found here:
https://github.com/JeffreyWay/laravel-mix/blob/467f0c9b01b7da71c519619ba8b310422321e0d6/UPGRADE.md#vue-configuration
When I tried your solution it did not work.
Here is how I managed to fix this issue, using this new/changed property additionalData, in previous versions this property was data or prependData.
mix.webpackConfig({
module: {
rules: [
{
test: /\.scss$/,
use: [
{
loader: "sass-loader",
options: {
additionalData: `#import "#/_variables.scss";
#import "#/_mixins.scss";
#import "#/_extends.scss";
#import "#/_general.scss";`
},
},
],
}
]
},
resolve: {
alias: {
'#': path.resolve('resources/sass'),
'~': path.resolve('/resources/js'),
'Vue': 'vue/dist/vue.esm-bundler.js',
}
},
output: {
chunkFilename: '[name].js?id=[chunkhash]',
},
});
mix.js('resources/js/app.js', 'public/js').vue()
.sass('resources/sass/app.scss', 'public/css')
.copyDirectory('resources/images', 'public/images');
if (mix.inProduction()) {
mix.version();
}
Note that alias for path 'resources/scss' is "#".
I hope this saved someone some time :)

Uncaught ReferenceError: FullCalendar is not defined (Webpack/NPM)

When i load my page, calendar not working and on my console i have :
jQuery.Deferred exception: FullCalendar is not defined ReferenceError: FullCalendar is not defined
at HTMLDocument.<anonymous> (http://admin.test/agenda:279:20)
at mightThrow (http://admin.test/js/admin.js:30047:29)
at process (http://admin.test/js/admin.js:30115:12) undefined
admin.js:30340 Uncaught ReferenceError: FullCalendar is not defined
at HTMLDocument.<anonymous> (agenda:279)
at mightThrow (admin.js:30047)
at process (admin.js:30115)
1) I have a laravel project with "AdminLTE 3.0.5".
Package.json :
"devDependencies": {
"#fullcalendar/bootstrap": "^4.4.0",
"#fullcalendar/core": "^4.4.0",
"#fullcalendar/daygrid": "^4.4.0",
"#fullcalendar/interaction": "^4.4.0",
"#fullcalendar/timegrid": "^4.4.0",
"admin-lte": "^3.0.5",
"axios": "^0.19",
"bootstrap": "^4.4.1",
"jquery": "^3.4.1",
"jquery-ui": "^1.12.1",
"laravel-mix": "^5.0.1",
"lodash": "^4.17.13",
"popper.js": "^1.12",
"resolve-url-loader": "^2.3.1",
"sass": "^1.20.1",
"sass-loader": "^8.0.0",
"vue": "^2.5.17",
"vue-template-compiler": "^2.6.10"
},
2) I import Fullcalendar package with my webpack :
mix.js('resources/js/app.js', 'public/js/app.js')
.js('resources/js/calendar.js', 'public/js/calendar.js')
app.js :
require('./bootstrap');
window.Vue = require('vue');
Vue.component('example-component', require('./components/ExampleComponent.vue').default);
const app = new Vue({
el: '#app',
});
require('jquery-ui/ui/widgets/draggable');
require('admin-lte');
/* Table */
require('../../node_modules/admin-lte/plugins/datatables/jquery.dataTables.min.js');
require('../../node_modules/admin-lte/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js');
require('../../node_modules/admin-lte/plugins/datatables-responsive/js/dataTables.responsive.min.js');
require('../../node_modules/admin-lte/plugins/datatables-responsive/js/responsive.bootstrap4.min.js');
require('../../node_modules/admin-lte/plugins/bs-custom-file-input/bs-custom-file-input.min.js');
calendar.js :
/* Calendar */
require('#fullcalendar/core');
require('#fullcalendar/daygrid');
require('#fullcalendar/timegrid');
require('#fullcalendar/interaction');
require('#fullcalendar/bootstrap');
3) On my vue, i copy/past the AdminLTE3 calendar page and only add my 2 mix file before :
<script src="{{ mix('js/app.js') }}"></script>
<script src="{{ mix('js/calendar.js') }}"></script>
<script>
$(function () {
/* initialize the external events
-----------------------------------------------------------------*/
function ini_events(ele) {
ele.each(function () {
[...]
I use "require()" function to call all js files, and when i run "npm run dev", everything work, no error.
I think that it's a js file order, but it's the same order than AdminLTE3 calendar page... or i'm blind.
I changed all versions of the calendar package & jquery & jquery-ui to have the same that this page (4.4.0 for my dl version).
Someone can help me pls ?
I have the solution.
Imports files like that on my calendar.js :
/* Calendar */
import { Calendar } from '#fullcalendar/core';
import dayGrid from '#fullcalendar/daygrid';
import timeGrid from '#fullcalendar/timegrid';
import interaction, { Draggable } from '#fullcalendar/interaction';
import bootstrap from '#fullcalendar/bootstrap';
And you dont need this 2 lines on my script
<!--
var Calendar = FullCalendar.Calendar;
var Draggable = FullCalendarInteraction.Draggable;
-->

Laravel vue js integration - No values in blade file

I try to integrate vue.js with laravel 5.3. I call "gulp" which creates the main.js file in the public folder. I can see in the browser that the js file is loaded but the value of "message" is not shown. It is only written {{message}}. Any ideas why?
<body>
<div class="container">
<div class="content">
<div class="title">
<p>#{{ message }}</p>
</div>
</div>
</div>
<script src="js/main.js"></script>
</body>
gulpfile:
var elixir = require('laravel-elixir');
require('laravel-elixir-browserify-official');
require('laravel-elixir-vueify');
elixir(mix => {
mix.sass('app.scss').browserify('main.js');
});
main.js:
import Vue from 'vue';
var app = new Vue({
el: 'body',
data: {
message: 'Hello Vue!'
}
})
package.json:
"dependencies": {
"bootstrap-sass": "^3.3.7",
"gulp": "^3.9.1",
"jquery": "^3.1.0",
"laravel-elixir": "^6.0.0-9",
"laravel-elixir-browserify-official": "^0.1.3",
"laravel-elixir-vue-2": "^0.2.0",
"laravel-elixir-vueify": "^1.0.0",
"laravel-elixir-webpack-official": "^1.0.2",
"lodash": "^4.16.2",
"vue": "^2.0.1",
"vue-resource": "^1.0.3"
}
EDIT
package.json
{
"private": true,
"scripts": {
"prod": "gulp --production",
"dev": "gulp watch",
"slate": "rimraf node_modules && npm install",
"clean": "rimraf public/css/app.css public/css/app.css.map public/js/app.js && npm run dev"
},
"devDependencies": {
"bootstrap-sass": "^3.3.7",
"gulp": "^3.9.1",
"jquery": "^3.1.1",
"laravel-elixir": "^6.0.0-15",
"laravel-elixir-browsersync-official": "^1.0.0",
"laravel-elixir-vue-2": "^0.3.0",
"laravel-elixir-webpack-official": "^1.0.10",
"lodash": "^4.17.2",
"moment": "^2.17.1",
"vue": "^2.1.6",
"vue-resource": "^1.0.3",
"vuex": "^2.1.1"
}
}
gulpfile.js
const elixir = require('laravel-elixir');
require('laravel-elixir-vue-2');
/*
|--------------------------------------------------------------------------
| Elixir Asset Management
|--------------------------------------------------------------------------
|
| Elixir provides a clean, fluent API for defining some basic Gulp tasks
| for your Laravel application. By default, we are compiling the Sass
| file for our application, as well as publishing vendor resources.
|
*/
elixir(mix => {
mix
.sass('app.scss')
.webpack('main.js');
});
Your code is fine except one thing. Vue JS Doesn't recommend use of body element as vue js root.
The provided element merely serves as a mounting point. Unlike in Vue
1.x, the mounted element will be replaced with Vue-generated DOM in all cases. It is therefore not recommended to mount the root instance to or
https://v2.vuejs.org/v2/api/#el
Code
<body>
<div class="container" id="app"> <!-- Mind the id attached -->
<div class="content">
<div class="title">
<p>#{{ message }}</p>
</div>
</div>
</div>
<script src="js/main.js"></script>
</body>
and main.js
import Vue from 'vue';
var app = new Vue({
el: '#app', // mind the root element is changed to app.
data: {
return { // mind the return in data
message: 'Hello Vue!'
}
}
});

Resources