├── .npmignore ├── README.md ├── index.js └── package.json /.npmignore: -------------------------------------------------------------------------------- 1 | .idea 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Mix Alias 2 | 3 | Add aliases to your source files. 4 | 5 | ## Requirements 6 | 7 | ⚠️ ⚠️ Only use this package with Laravel Mix v5 and below! The alias function was added to the core of v6. ⚠️ ⚠️ 8 | 9 | ## Installation 10 | 11 | ``` 12 | npm install laravel-mix-alias --save 13 | ``` 14 | 15 | or 16 | 17 | ``` 18 | yarn add laravel-mix-alias 19 | ``` 20 | 21 | ## Usage 22 | 23 | Your `webpack.mix.js` could look like this: 24 | 25 | ```js 26 | const mix = require('laravel-mix'); 27 | require('laravel-mix-alias'); 28 | 29 | mix.alias({ 30 | '@': '/resources/js', 31 | '~': '/resources/sass', 32 | }); 33 | ``` 34 | 35 | or add them one by one 36 | 37 | ```js 38 | const mix = require('laravel-mix'); 39 | require('laravel-mix-alias'); 40 | 41 | mix.alias('@', '/resources/js'); 42 | mix.alias('~', '/resources/sass'); 43 | ``` 44 | 45 | Now you can import sass files from the path you specified 46 | 47 | ```scss 48 | @import "~/variables"; 49 | ``` 50 | 51 | Or import scripts from the path you specified 52 | 53 | ```js 54 | import '@/script.js'; 55 | ``` 56 | 57 | ## Author 58 | 59 | Maxim Vanhove 60 | Web developer at [Starring Jane](https://starringjane.com) 61 | 62 | [![Twitter Follow](https://img.shields.io/twitter/follow/MrMaximVanhove.svg?style=social&logo=twitter&label=Follow)](https://twitter.com/MrMaximVanhove) 63 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const mix = require('laravel-mix'); 2 | 3 | class Alias { 4 | register (alias, path = null) { 5 | const aliases = getAliasesObject(alias, path); 6 | 7 | this.aliases = Object.assign(aliases, this.aliases || {}); 8 | this.rootPath = global.path.resolve(__dirname, '../../'); 9 | } 10 | 11 | webpackConfig (webpackConfig) { 12 | // Set default alias object 13 | const aliases = webpackConfig.resolve.alias || {}; 14 | 15 | Object.entries(this.aliases).forEach(([alias, path]) => { 16 | // Path must start with forwarding slash 17 | if (!path.startsWith('/')) { 18 | path = `/${path}`; 19 | } 20 | 21 | aliases[alias] = `${this.rootPath}${path}`; 22 | }); 23 | 24 | webpackConfig.resolve.alias = aliases; 25 | } 26 | } 27 | 28 | mix.extend('alias', new Alias()); 29 | 30 | /** 31 | * Helper getAliasesObject 32 | * @param {string|Object} alias 33 | * @param {string} [path] 34 | */ 35 | function getAliasesObject (alias, path) { 36 | if (path != null) { 37 | return { 38 | [alias]: path, 39 | }; 40 | } 41 | 42 | return alias; 43 | } 44 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel-mix-alias", 3 | "description": "A Laravel Mix extension for Webpack alias support.", 4 | "main": "index.js", 5 | "repository": { 6 | "type": "git", 7 | "url": "git+https://github.com/MaximVanhove/laravel-mix-alias.git" 8 | }, 9 | "keywords": [ 10 | "laravel", 11 | "laravel mix", 12 | "mix", 13 | "webpack", 14 | "alias" 15 | ], 16 | "author": "Maxim Vanhove", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/MaximVanhove/laravel-mix-alias/issues" 20 | }, 21 | "homepage": "https://github.com/MaximVanhove/laravel-mix-alias#readme", 22 | "peerDependencies": { 23 | "laravel-mix": ">=2.0.0" 24 | }, 25 | "version": "1.0.3" 26 | } 27 | --------------------------------------------------------------------------------