How to configure path alias in webpack.mix? - laravel

Solution:
Thanks to both Karl & Ali I can now use alias' without having to create a seperate webpack.config.js file. Just add the alias to webpack.mix.js like so:
const mix = require('laravel-mix');
const path = require('path');
mix.js('resources/js/app.js', 'public/js')
.postCss('resources/css/app.css', 'public/css')
.alias({'#': 'resources/'})
.webpackConfig({resolve: {alias: {'#': path.resolve('resources/')}}})
.vue();
Question:
From this post, I learned that you could configure your # path in webpack.mix.js to represent the assets/resources folder in a vue/laravel project so that you can use the # symbol in your paths.
mix.webpackConfig({
resolve: {
alias: {
'#resources': path.resolve('resources'),
},
},
})
Unfortunately, this leads to the following error.
yarn run v1.22.17 $ mix watch [webpack-cli] ReferenceError: path is
not defined
at Object. (/Users/artur/PhpstormProjects/safa-ameedee.com/webpack.mix.js:16:21)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at module.exports (/Users/artur/PhpstormProjects/safa-ameedee.com/node_modules/laravel-mix/setup/webpack.config.js:11:5)
at loadConfigByPath (/Users/artur/PhpstormProjects/safa-ameedee.com/node_modules/webpack-cli/lib/webpack-cli.js:1747:27)
at async Promise.all (index 0) error Command failed with exit code 2.
I've tried different variations (#assets etc.), but I always get the same error. So what am I doing wrong?

You're not adding the explicit require of the path package, but you should do so. Make sure you add the following to the top of your webpack.config.js file.
const path = require('path');
Also, you may need to add __dirname.
const path = require('path');
mix.webpackConfig({
resolve: {
alias: {
'#resources': path.resolve(__dirname, 'resources/')
}
}
});

In your webpack.mix.js:
...
.alias({'#': 'resources/js'})
...
Import Aliases - Laracasts

Related

Laravel - Webpack / Mix - how to change a script directory?

I get the error Can't resolve '../api' when running npm run dev. I wrote a script to retrieve some data via an API call. This script works well when I put it in the vendor directory (vendor/badaso/core/src/js/api/modules). It is called by a store in resources/js/badaso/stores with
import api from "../../../../vendor/badaso/core/src/resources/js/api";
When I move this script to resources/js/badaso/api/modules and call it by...
import "../api"
Running "npm run dev", I get the following error.
Can't resolve '../api`
webpack.mix.js
const mix = require('laravel-mix');
//mix.js('resources/js/app.js', 'public/js') // original
mix.js('resources/js/*.js', 'public/js') // try -> same error
.postCss('resources/css/app.css', 'public/css', [
//
]);
// Can't resolve '../api'
// Badaso
mix.js("vendor/badaso/core/src/resources/js/app.js",
"public/js/badaso.js")
.sass("vendor/badaso/core/src/resources/js/assets/scss/style.scss",
"public/css/badaso.css")
.vue()`
I try this in webpack.mix.js
//mix.js('resources/js/app.js', 'public/js') // original
mix.js('resources/js/*.js', 'public/js') // try -> same error``

Laravel Breeze Vite Dev-Server Error: "cannot test case insensitive FS, CLIENT_ENTRY does not point to an existing file"

I set up a fresh Laravel Breeze Project with Vite. When I run:
npm run dev
I get this Error:
failed to load config from PATH/vite.config.js
error when starting dev server:
Error: cannot test case insensitive FS, CLIENT_ENTRY does not point to an existing file: PATH/dist/client/client.mjs
at testCaseInsensitiveFS (PATH/node_modules/vite/dist/node-cjs/publicUtils.cjs:3420:15)
at Object.<anonymous> (PATH/node_modules/vite/dist/node-cjs/publicUtils.cjs:3425:1)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Object._require.extensions.<computed> [as .js] (file:///PATH/node_modules/vite/dist/node/chunks/dep-6b3a5aff.js:63517:17)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Module.require (node:internal/modules/cjs/loader:1028:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (PATH/node_modules/vite/index.cjs:7:31)
I can't find any Information about this error! A few Weeks ago I had no Problems with this.
Here is my Vite Config File:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: [
'resources/css/app.css',
'resources/js/app.js',
],
refresh: true,
}),
],
});
Thanks in advance
Make sure that your project path doesn't contain any special character like #.

Blank Page on production using Laravel Mix

I built a single page application (SPA) using Laravel 8 + Vue.js + InertiaJs. Everything is working fine in the development environment, but when I compile assets for production, it shows me a blank page, and there is no error in the console. All assets are loading correctly with a 200 code, and everything seems to be OK, but the Vue app is not mounting!
webpack.mix.js
const mix = require('laravel-mix');
require('laravel-mix-workbox');
mix.webpackConfig({
output: {
filename: '[name].js',
chunkFilename: 'js/[name].js',
}
}).js('resources/js/app.js', 'public/js')
.extract(['vue'])
.version();
Image1
Image2
Image3
try using the inertiajs webpack.mix.js provided by the pingCRM github:
const path = require('path')
const mix = require('laravel-mix')
const cssImport = require('postcss-import')
const cssNesting = require('postcss-nesting')
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel application. By default, we are compiling the Sass
| file for the application as well as bundling up all the JS files.
|
*/
mix
.js('resources/js/app.js', 'public/js')
.vue()
.postCss('resources/css/app.css', 'public/css', [
// prettier-ignore
cssImport(),
cssNesting(),
require('tailwindcss'),
])
.webpackConfig({
output: { chunkFilename: 'js/[name].js?id=[chunkhash]' },
resolve: {
alias: {
vue$: 'vue/dist/vue.runtime.esm.js',
'#': path.resolve('resources/js'),
},
},
})
.version()
.sourceMaps()
note you may have to follow the instructions for setting up tailwindcss which can be found here, only do this if you plan to use it otherwise remove the require('tailwindcss'), from the webpack file
you also may need to npm install --save-dev postcss-import and npm install --save-dev postcss-nesting the --save-dev flag will add it to your package.json file for future reference

"xxx.scss" is not in the SourceMap error in Laravel Mix

I'm following Laravel documentation and have this file structure:
In app.scss I have:
#import 'style12';
.real-demo{
}
and in style12.scss some css code which i try to import.
When I run npm run dev I get following error:
Module build failed: Error: "../../resources/assets/sass/style12.scss" is not in the SourceMap.
at BasicSourceMapConsumer.SourceMapConsumer_sourceContentFor [as sourceContentFor] (C:\Users\mixei\Desktop\Projects\westcredit\node_modules\source-map\lib\source-map-consumer.js:704:13)
at SourceMapGenerator.<anonymous> (C:\Users\mixei\Desktop\Projects\westcredit\node_modules\source-map\lib\source-map-generator.js:235:40)
at Array.forEach (<anonymous>)
at SourceMapGenerator_applySourceMap [as applySourceMap] (C:\Users\mixei\Desktop\Projects\westcredit\node_modules\source-map\lib\source-map-generator.js:234:32)
at MapGenerator.applyPrevMaps (C:\Users\mixei\Desktop\Projects\westcredit\node_modules\postcss\lib\map-generator.js:146:22)
at MapGenerator.generateMap (C:\Users\mixei\Desktop\Projects\westcredit\node_modules\postcss\lib\map-generator.js:194:46)
at MapGenerator.generate (C:\Users\mixei\Desktop\Projects\westcredit\node_modules\postcss\lib\map-generator.js:297:25)
at LazyResult.stringify (C:\Users\mixei\Desktop\Projects\westcredit\node_modules\postcss\lib\lazy-result.js:294:24)
at C:\Users\mixei\Desktop\Projects\westcredit\node_modules\postcss\lib\lazy-result.js:231:27
Also here is my webpack.mix.js:
const { mix } = require('laravel-mix');
mix.options({
processCssUrls: false
});
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css');
Any help will be appreciated, thanks!

Elixir is not defined error

I am using laravel 5.4 and I wanted to set up elixir to compile sass files. I tried to install elixir by running $ npm install and after that, I tried running $ gulp but it gave me the following error:
/home/vagrant/Code/Laravel/gulpfile.js:1
(function (exports, require, module, __filename, __dirname) { elixir(mix => {
^
ReferenceError: elixir is not defined
at Object.<anonymous> (/home/vagrant/Code/Laravel/gulpfile.js:1:63)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at execute (/usr/lib/node_modules/gulp-cli/lib/versioned/^3.7.0/index.js:24:18)
at Liftoff.handleArguments (/usr/lib/node_modules/gulp-cli/index.js:149:63)
After adding const elixir = require('laravel-elixir');, I am getting this error:
[02:28:30] Starting 'webpack'...
{ [Error: ./resources/assets/js/components/Example.vue
Module parse failed: /home/vagrant/Code/Laravel/resources/assets/js/components/Example.vue Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| <template>
| <div class="container">
| <div class="row">
# ./resources/assets/js/app.js 16:25-60]
message: './resources/assets/js/components/Example.vue\nModule parse failed: /home/vagrant/Code/Laravel/resources/assets/js/components/Example.vue Unexpected token (1:0)\nYou may need an appropriate loader to handle this file type.\n| <template>\n| <div class="container">\n| <div class="row">\n # ./resources/assets/js/app.js 16:25-60',
showStack: false,
showProperties: true,
plugin: 'webpack-stream',
__safety: { toString: [Function: bound ] } }
I have the same issue when installing a new laravel app 5.4. When I try to run gulp webpack it shows same error in my console. So what I did, since the laravel 5.4 comes with the new vue version which is the vue 2. You need to require require('laravel-elixir-vue-2'); See sample below:
gulpfile.js
var elixir = require('laravel-elixir');
require('laravel-elixir-vue-2'); // recommended for vue 2
elixir(function(mix) {
// some mixes here..
mix.webpack('app.js');
});
If you got errors like elixir related, just npm install them. also check this page here
.

Resources