forked from M3-Academy/practice-time-shopping-list
feat: Cria arquivo javascript minificado com gulp assim como os arquivos sass
This commit is contained in:
parent
18d9a5c749
commit
860542baa4
27
gulpfile.js
27
gulpfile.js
@ -1,14 +1,29 @@
|
||||
const { src, dest, watch } = require('gulp');
|
||||
const sass = require('gulp-sass')(require('sass'));
|
||||
const { src, dest, watch } = require("gulp");
|
||||
const sass = require("gulp-sass")(require("sass"));
|
||||
const browserify = require("browserify");
|
||||
const source = require("vinyl-source-stream");
|
||||
const uglify = require("gulp-uglify");
|
||||
const buffer = require("vinyl-buffer");
|
||||
|
||||
function styles() {
|
||||
return src('src/styles/main.scss')
|
||||
.pipe(sass({ outputStyle: "compressed" }).on('error', sass.logError))
|
||||
.pipe(dest('dist'));
|
||||
return src("src/styles/main.scss")
|
||||
.pipe(sass({ outputStyle: "compressed" }).on("error", sass.logError))
|
||||
.pipe(dest("dist"));
|
||||
}
|
||||
|
||||
function scripts() {
|
||||
return browserify("src/scripts/app.js")
|
||||
.transform("babelify", { presets: ["@babel/preset-env"] })
|
||||
.bundle()
|
||||
.pipe(source("bundle.js"))
|
||||
.pipe(buffer())
|
||||
.pipe(uglify())
|
||||
.pipe(dest("dist"));
|
||||
}
|
||||
|
||||
function sentinel() {
|
||||
watch('src/styles/*.scss', { ignoreInitial: false }, styles);
|
||||
watch("src/styles/**/*.scss", { ignoreInitial: false }, styles);
|
||||
watch("src/scripts/**/*.js", { ignoreInitial: false }, scripts);
|
||||
}
|
||||
|
||||
exports.sentinel = sentinel;
|
6464
package-lock.json
generated
6464
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -14,8 +14,15 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.20.2",
|
||||
"@babel/preset-env": "^7.20.2",
|
||||
"babelify": "^10.0.0",
|
||||
"browserify": "^17.0.0",
|
||||
"gulp": "^4.0.2",
|
||||
"gulp-sass": "^5.1.0",
|
||||
"sass": "^1.56.1"
|
||||
"gulp-uglify": "^3.0.2",
|
||||
"sass": "^1.56.1",
|
||||
"vinyl-buffer": "^1.0.1",
|
||||
"vinyl-source-stream": "^2.0.0"
|
||||
}
|
||||
}
|
@ -1,68 +1,5 @@
|
||||
document.addEventListener('DOMContentLoaded',
|
||||
function() {
|
||||
const list = [];
|
||||
import { ShoppingList } from "./components/ShoppingList";
|
||||
|
||||
const form = document.querySelector('.shopping-form');
|
||||
const quantityInput = document.querySelector('.shopping-form-quantity-input');
|
||||
const incrementButton = document.querySelector('.shopping-form-increment-button');
|
||||
const decrementButton = document.querySelector('.shopping-form-decrement-button');
|
||||
const items = document.querySelector('.shopping-items');
|
||||
|
||||
incrementButton.addEventListener('click', incrementQuantity);
|
||||
|
||||
decrementButton.addEventListener('click', decrementQuantity);
|
||||
|
||||
form.addEventListener('submit', addItemToList);
|
||||
|
||||
function incrementQuantity(){
|
||||
const currentValue = Number(quantityInput.value);
|
||||
const newValue = currentValue + 1;
|
||||
|
||||
quantityInput.value = newValue;
|
||||
}
|
||||
|
||||
function decrementQuantity(){
|
||||
const currentValue = Number(quantityInput.value);
|
||||
if(currentValue > 1){
|
||||
const newValue = currentValue - 1;
|
||||
|
||||
quantityInput.value = newValue;
|
||||
}
|
||||
}
|
||||
|
||||
function addItemToList(event){
|
||||
event.preventDefault();
|
||||
|
||||
const itemName = event.target["item-name"].value;
|
||||
if(itemName === "") return;
|
||||
const itemQuantity = event.target["item-quantity"].value;
|
||||
|
||||
const item = {
|
||||
itemName,
|
||||
itemQuantity
|
||||
};
|
||||
|
||||
list.push(item);
|
||||
renderListItem(item);
|
||||
resetInputs(event);
|
||||
}
|
||||
|
||||
function renderListItem(item) {
|
||||
const li = document.createElement('li');
|
||||
li.setAttribute("class", "shopping-item");
|
||||
|
||||
const span1 = document.createElement('span');
|
||||
const span2 = document.createElement('span');
|
||||
|
||||
span1.innerText = item.itemName;
|
||||
span2.innerText = item.itemQuantity;
|
||||
|
||||
li.append(span1,span2);
|
||||
items.append(li);
|
||||
}
|
||||
|
||||
function resetInputs(event){
|
||||
event.target["item-name"].value = '';
|
||||
event.target["item-quantity"].value = '1';
|
||||
}
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
new ShoppingList();
|
||||
});
|
74
src/scripts/components/ShoppingList.js
Normal file
74
src/scripts/components/ShoppingList.js
Normal file
@ -0,0 +1,74 @@
|
||||
export class ShoppingList {
|
||||
constructor() {
|
||||
this.list = [];
|
||||
|
||||
this.selectors();
|
||||
this.events();
|
||||
}
|
||||
|
||||
selectors() {
|
||||
this.form = document.querySelector(".shopping-form");
|
||||
this.quantityInput = document.querySelector(".shopping-form-quantity-input");
|
||||
this.incrementButton = document.querySelector(".shopping-form-increment-button");
|
||||
this.decrementButton = document.querySelector(".shopping-form-decrement-button");
|
||||
this.items = document.querySelector(".shopping-items");
|
||||
}
|
||||
|
||||
events() {
|
||||
this.incrementButton.addEventListener("click", this.incrementQuantity.bind(this));
|
||||
this.decrementButton.addEventListener("click", this.decrementQuantity.bind(this));
|
||||
this.form.addEventListener("submit", this.addItemToList.bind(this));
|
||||
}
|
||||
|
||||
incrementQuantity() {
|
||||
const currentValue = Number(this.quantityInput.value);
|
||||
const newValue = currentValue + 1;
|
||||
|
||||
this.quantityInput.value = newValue;
|
||||
}
|
||||
|
||||
decrementQuantity() {
|
||||
const currentValue = Number(this.quantityInput.value);
|
||||
if (currentValue > 1) {
|
||||
const newValue = currentValue - 1;
|
||||
|
||||
this.quantityInput.value = newValue;
|
||||
}
|
||||
}
|
||||
|
||||
addItemToList(event) {
|
||||
event.preventDefault();
|
||||
|
||||
const itemName = event.target["item-name"].value;
|
||||
if (itemName === "") return;
|
||||
const itemQuantity = event.target["item-quantity"].value;
|
||||
|
||||
const item = {
|
||||
itemName,
|
||||
itemQuantity,
|
||||
};
|
||||
|
||||
this.list.push(item);
|
||||
this.renderListItem(item);
|
||||
this.resetInputs(event);
|
||||
}
|
||||
|
||||
renderListItem(item) {
|
||||
const li = document.createElement("li");
|
||||
li.setAttribute("class", "shopping-item");
|
||||
|
||||
const span1 = document.createElement("span");
|
||||
const span2 = document.createElement("span");
|
||||
|
||||
span1.innerText = item.itemName;
|
||||
span2.innerText = item.itemQuantity;
|
||||
|
||||
li.append(span1, span2);
|
||||
this.items.append(li);
|
||||
}
|
||||
|
||||
resetInputs(event) {
|
||||
event.target["item-name"].value = "";
|
||||
event.target["item-quantity"].value = "1";
|
||||
}
|
||||
}
|
@ -70,6 +70,6 @@
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<script src="../scripts/app.js"></script>
|
||||
<script src="../../dist/bundle.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user