Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): create Image component #26

Merged
merged 14 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vulmix/assets/img/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `.vulmix/assets/img`

All your images will be compiled here.
Binary file added .vulmix/assets/img/sample@1200.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .vulmix/assets/img/sample@1200.webp
Binary file not shown.
Binary file added .vulmix/assets/img/sample@1920.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .vulmix/assets/img/sample@1920.webp
Binary file not shown.
Binary file added .vulmix/assets/img/sample@300.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .vulmix/assets/img/sample@300.webp
Binary file not shown.
Binary file added .vulmix/assets/img/sample@600.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .vulmix/assets/img/sample@600.webp
Binary file not shown.
Binary file added .vulmix/assets/img/sample@900.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .vulmix/assets/img/sample@900.webp
Binary file not shown.
103 changes: 103 additions & 0 deletions .vulmix/components/Image.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<template>
<img
:src="image"
:width="props.width && props.width.replace(/(\d+)(px|rem|em|%|)/i, '$1')"
:height="props.height && props.height.replace(/(\d+)(px|rem|em|%|)/i, '$1')"
:alt="props.alt"
:title="props.title"
:loading="props.loading"
/>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useWindowSize } from '@vueuse/core'

const { width: windowWidth, height: windowHeight } = useWindowSize()

const imageSize = ref({})

const props = defineProps({
src: {
type: String,
default: '',
},
width: {
type: String,
},
height: {
type: String,
},
alt: {
type: String,
default: 'Imagem',
},
title: {
type: String,
default: '',
},
loading: {
type: String,
default: 'eager', // or 'lazy'
},
webp: {
type: String,
default: 'true',
},
})

const image = ref('')

function replace(size) {
let currentIndex
const sizes = ['300', '600', '900', '1200', '1920']

sizes.forEach((item, i) => {
if (item === size) {
currentIndex = i
}
})

const img = new Image()

image.value = props.src.replace(
/\/assets\/img\/(|.*)([a-zA-Z0-9_-])\.(png|jpg|jpeg|gif)$/i,
`/.vulmix/assets/img/$1$2@${size}.${
props.webp === 'true' ? 'webp' : '$3'
}`
)

img.src = image.value

img.onerror = function () {
image.value = props.src.replace(
/\/assets\/img\/(|.*)([a-zA-Z0-9_-])\.(png|jpg|jpeg|gif)$/i,
`/.vulmix/assets/img/$1$2@${sizes[currentIndex - 1]}.${
props.webp === 'true' ? 'webp' : '$3'
}`
)

replace(sizes[currentIndex - 1])
}
}

function loadImage() {
if (windowWidth.value < 320) {
replace('300')
} else if (windowWidth.value >= 320 && windowWidth.value < 578) {
replace('600')
} else if (windowWidth.value >= 578 && windowWidth.value < 768) {
replace('900')
} else if (windowWidth.value >= 992 && windowWidth.value < 1600) {
replace('1200')
} else {
replace('1920')
}
}

onMounted(() => {
window.addEventListener('load', () => {
ojvribeiro marked this conversation as resolved.
Show resolved Hide resolved
loadImage()
})
})
</script>
16 changes: 16 additions & 0 deletions .vulmix/mix.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const mix = require('laravel-mix')
const config = require('../webpack.config.js')

require('laravel-mix-serve')
require('laravel-mix-simple-image-processing')

class VueMixInit {
name() {
Expand All @@ -11,6 +13,8 @@ class VueMixInit {
mix
.setPublicPath('.dist/')

.webpackConfig(config)

.serve(
'npx http-server -p 8000 -a localhost -c-1 --proxy http://localhost:8000?',
{
Expand All @@ -25,6 +29,18 @@ class VueMixInit {
devtool: 'source-map',
})

.imgs({
source: 'assets/img',
destination: '.vulmix/assets/img',
webp: true,
thumbnailsSizes: [1920, 1200, 900, 600, 300],
smallerThumbnailsOnly: true,
thumbnailsOnly: true,
imageminWebpOptions: {
quality: 80,
},
})

.sass('.vulmix/assets/sass/main.scss', '.dist/.vulmix/dist/css')

.js('.vulmix/main.js', '.dist/.vulmix/dist/js')
Expand Down
Binary file added assets/img/sample.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 10 additions & 3 deletions components/MyComponent.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<template>
<div class="p-8 bg-slate-600 text-white rounded-xl w-fit">
<h2 class="text-3xl mb-6">My test component</h2>
<div class="p-8 bg-slate-600 text-white rounded-xl w-fit max-w-full flex flex-col md:flex-row gap-6">
<Image
class="w-[300px] max-w-full aspect-square object-cover object-bottom"
src="/assets/img/sample.jpg"
/>

<p>Here's some paragraph for ya.</p>
<div>
<h2 class="text-3xl mb-6">My test component</h2>

<p>Here's some paragraph for ya.</p>
</div>
</div>
</template>
24 changes: 14 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vulmix",
"version": "0.0.2-alpha",
"version": "0.1.0-alpha",
"description": "Tiny boilerplate to work with Vue and Laravel Mix.",
"main": "index.js",
"scripts": {
Expand All @@ -13,21 +13,25 @@
"author": "Victor Ribeiro",
"license": "MIT",
"devDependencies": {
"laravel-mix": "^6.0.49",
"vue": "^3.2.37",
"vue-loader": "^16.8.3",
"vue-router": "^4.1.3",
"@vue/compiler-sfc": "^3.2.37",
"sass": "^1.54.5",
"sass-loader": "^10.3.1",
"@vueuse/core": "^9.2.0",
"autoprefixer": "^10.4.8",
"browser-sync": "^2.27.10",
"browser-sync-webpack-plugin": "^2.3.0",
"resolve-url-loader": "^5.0.0",
"imagemin-jpegtran": "^7.0.0",
"laravel-mix": "^6.0.49",
"laravel-mix-serve": "^2.2.2",
"laravel-mix-simple-image-processing": "^1.0.7",
"path": "^0.12.7",
"postcss": "^8.4.16",
"autoprefixer": "^10.4.8",
"resolve-url-loader": "^5.0.0",
"sass": "^1.54.5",
"sass-loader": "^10.3.1",
"tailwindcss": "^3.1.8",
"laravel-mix-serve": "^2.2.2"
"vue": "^3.2.37",
"vue-loader": "^16.8.3",
"vue-router": "^4.1.3",
"webpack": "^5.74.0"
},
"dependencies": {}
}
15 changes: 15 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const path = require('path')

module.exports = {
resolve: {
extensions: ['.js', '.vue'],
alias: {
'@': path.resolve(__dirname, '.vulmix'),
'@assets': path.resolve(__dirname, 'assets'),
'@components': path.resolve(__dirname, 'components'),
'@composables': path.resolve(__dirname, 'composables'),
'@pages': path.resolve(__dirname, 'pages'),
'@sass': path.resolve(__dirname, 'assets/sass'),
},
},
}
10 changes: 0 additions & 10 deletions webpack.mix.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
const mix = require('laravel-mix')
const path = require('path')

require('./.vulmix/mix.js')

mix
.vueMix()

.alias({
'@': path.join(__dirname, '.vulmix'),
'@assets': path.join(__dirname, 'assets'),
'@components': path.join(__dirname, 'components'),
'@composables': path.join(__dirname, 'composables'),
'@pages': path.join(__dirname, 'pages'),
'@sass': path.join(__dirname, 'assets/sass'),
})

.sass('assets/sass/main.scss', '.dist/css')