網頁前端自動化工具 – Grunt
今天為各位介紹的是一個前端工程師所使用的自動化工具「Grunt」,為何我們要使用這個工具呢?其實使用這個工具的原因有很多,因為他的擴充模組(Plugin)也很多,不過今天馬老師從自動最小化(Minify)的角度來介紹這個工具該如何使用。
一般來說前端網頁開發不外乎HTML、CSS、Javascript這三種語法,而在開發完成之後,為了要節省流量,我們可能會把這三個檔案壓縮後再上傳到網站伺服器上,何謂壓縮請參考下圖:
從上面的檔案可以觀察出在開發時我們為了閱讀的便利,常常會利用註解、空白…等來輔助,但真正發佈出去這些東西卻不是必要的,甚至在開發的時候我們為了方面會把CSS或JS拆成好幾個檔案,但發佈之後卻希望可以合併,但如果需要人工來做這件事,可想而知會有多麻煩,這個時候Grunt就會發揮效用了,設定好之後只要一個指令,他就可以幫我們做好這些動作,接下來就來看看該如何使用吧。
首先Grunt是一個基於node.js下的應用程序,所以必須先安裝node.js,安裝的部分並不難,從官網下載後直接執行安裝即可。
接下來可以參考Grunt網站中Getting Started的單元,或是跟著以下(以Windows為案例)的方式進行Grunt的安裝。
- 打開「命令提示字元」。
- 輸入「npm install -g grunt-cli」。
- 接下來就可以準備網頁專案資料夾了,如下圖所示,我的網頁專案資料夾簡單分成兩個:
- src:所有開發檔案
- public:發佈至網路上的檔案
- 這樣準備的目的應該很清楚,就是希望在src資料夾內以自己最習慣、最方便閱讀的方式來開發,而開發完畢之後可以把壓縮的檔案轉至public資料夾,上傳到伺服器上。
有關於網頁內容開發的部分,本篇文章就省略,所以可以看到上面圖檔中我已經準備好了HTML、CSS、JS各一個,直接進入利用Grunt開始做自動最小化的部分。
- 使用Grunt時,必須在網站根目錄建立兩個檔案:
- package.json:Grunt的專案設定檔,其中可以包含專案名稱、作者、版本,所需要使用的Plugin…等,可以參考下面我的檔案。
- Grunt.js:Grunt的執行腳本,以本案例來說就是要去哪個資料夾,壓縮哪些檔案後存放在哪邊,需在本檔案中指定。
{ "name": "gruntTest", "version": "0.1.0", "author": "Stanley Ma", "devDependencies": { "grunt": "~0.4.5", "grunt-contrib-htmlmin": "~0.4.0", "grunt-contrib-cssmin": "~0.13.0", "grunt-contrib-uglify": "~0.9.2" } }
以上面的package.json檔案來說,定義了專案名稱、作者、版本以及所需要使用的Grunt版本(撰文時Grunt穩定版為0.4.5)和Plugin列表,因為開頭有提到,本案例以最小化的角度來介紹Grunt,所以用到的三個Plugin分別為:
- grunt-contrib-htmlmin:最小化HTML檔。
- grunt-contrib-cssmin:最小化CSS檔。
- grunt-contrib-uglify:最小化JS檔。
- 接下來利用Grunt的專案設定檔來安裝所需要的Grunt和各個外掛,利用「命令提示字元」進入網頁專案的根目錄,輸入「npm install」,他就會在專案資料夾中安裝好所有需要的Plugin。
- 接下來就要開始準備撰寫Grunt.js的執行腳本,大家可以參考我的專案檔案寫法:
module.exports = function (grunt) { grunt.initConfig({ htmlmin: { Target: { options: { removeComments: true, collapseWhitespace: true, removeEmptyAttributes: true, }, files: { 'public/index.html': 'src/index.html', } } }, cssmin: { target: { files: { 'public/index.css': 'src/index.css' } } }, uglify: { target: { files: { 'public/index.js': 'src/index.js' } } } }); grunt.loadNpmTasks('grunt-contrib-htmlmin'); grunt.loadNpmTasks('grunt-contrib-cssmin'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.registerTask('default', ['htmlmin', 'cssmin', 'uglify']); };
這樣的寫法會讓Grunt把在src裡面的三個檔案,分別壓縮後複製一份到public的資料夾中,確定資料夾和檔案無誤之後,繼續下一步動作。
- 利用「命令提示字元」進入網頁專案的根目錄,輸入「grunt」之後,出現以下的畫面表示成功。
- 本案例利用三個Grunt的Plugin完成,分別是:「grunt-contrib-htmlmin」、「grunt-contrib-cssmin」、「grunt-contrib-uglify」,其實這三個外掛都有一些其他的參數,另外也可以進行多檔案的批次轉換以及檔案合併…等等功能,建議有需要的同學可以到Plugin各自的網站上去看看使用方式。
附帶一提,一開始就有提到關於Grunt自動化的Plugin非常多,本文僅用到了三個,這裡有Plugin的列表,對這方面有興趣的同學們也歡迎到網站上看更多相關的自動化功能。
here are the findings zkLogin
my canadian pharmacy Inter Pharm Online canadian pharmacies compare
Way cool! Some very valid points! I appreciate you penning this post plus the rest of the site is also really good.
try this website zkLogin
https://interpharmonline.com/# vipps canadian pharmacy
canadian mail order pharmacy
blog here wallet surf
vipps canadian pharmacy: Cheapest online pharmacy – safe canadian pharmacy
basics surf tech
Xavier completes thrilling comeback, Mount St. Mary’s advances as men’s First Four comes to a close
changenow
Wednesday saw the men’s First Four come to a close which means only one thing: the 64-team bracket is officially set following No. 11 Xavier’s thrilling come from behind win over No. 11 Texas and No. 16 Mount St. Mary’s victory over No. 16 American in Dayton, Ohio.
The Musketeers trailed by as many as 13 points, but their offense came alive in the second half behind guard Marcus Foster and forward Zach Freemantle to down the Longhorns 86-80.
The senior Foster scored a team-high 22 points while Freemantle, on his way to 15 points, threw down a dunk with a second left to seal the comeback win and ignite the fans at UD Arena, which is just over 50 miles away from campus in Cincinnati, Ohio.
With just under four minutes remaining, Xavier went on an 8-2 run to take a 78-74 lead, their first since the early going of the first half.
Musketeers head coach Sean Miller crowned Wednesday’s game as “one of the best” he’s been a part of.
“I thought we were dead in the water two different times,” Miller told the truTV broadcast after the game. “But that’s the one thing about our team — the resiliency of our group has always won out for us. Just when you thought we weren’t gonna make the tournament, we kept winning. Even in this game, just when you’re like, ‘It’s not gonna work out,’ we have a funny way of staying with it.”
The Longhorns did not go down without a fight as guard Tre Johnson scored a game-high 23 points in the loss.
Xavier will face No. 6 Illinois in the first round on Friday at the Fiserv Forum in Milwaukee.
online pharmacy india: india online pharmacy store – India Med Fast
click for more zkLogin sui
A librarian ran off with a yacht captain in the summer of 1968. It was the start of an incredible love story
metamask wallet
The first time Beverly Carriveau saw Bob Parsons, she felt like a “thunderbolt” passed between them.
“This man stepped out of a taxi, and we both just stared at each other,” Beverly tells CNN Travel today. “You have to remember, this is the ‘60s. Girls didn’t stare at men. But it was a thunderbolt.”
It was June 1968. Beverly was a 23-year-old Canadian university librarian on vacation in Mazatlan, Mexico, with a good friend in tow.
Beverly had arrived in Mazatlan that morning. She’d been blown away by the Pacific Ocean views, the colorful 19th-century buildings, the palm trees.
Now, Beverly was browsing the hotel gift store, admiring a pair of earrings, when she looked up and spotted the man getting out of the taxi. The gift shop was facing the parking lot, and there he was.
“I was riveted,” says Beverly. “He was tall, handsome…”
Eventually, Beverly tore away her gaze, bought the earrings and dashed out of the store.
“We locked eyes so long, I was embarrassed,” she says.
No words had passed between them. They hadn’t even smiled at each other. But Beverly felt like she’d revealed something of herself. She felt like something had happened, but she couldn’t describe it.
Beverly rushed to meet her friend, still feeling flustered. Over dinner in the hotel restaurant, Beverly confided in her friend about the “thunderbolt” moment.
“I told my girlfriend, ‘Something just happened to me. I stared at this man, and I couldn’t help myself.’”
Then, the server approached Beverly’s table.
“He said, ‘I have some wine for you, from a man over there.’”
The waiter was holding a bottle of white wine, indicating at the bar, which was packed with people.
As a rule, Beverly avoided accepting drinks from men in bars. She never felt especially comfortable with the power dynamic — plus, she had a long-term partner back in Canada.
“I had a serious boyfriend at home and thought my life was on course,” she says.
pdacenter.ru – сервис по ремонту бытовой техники
Ремонт компьютеров в Волгограде в официальном сервисном центре PDACENTER.
Наши инженеры выполняют ремонт любой сложности по дотупным ценам!
site here surf wallet
i loved this wallet surf
this hyperlink wallet surf
https://indiamedfast.com/# cheapest online pharmacy india
canadian pharmacy
The fish collectors hoping to save rare species from extinction
phantom wallet
In the rural town of Petersham, Massachusetts, 78-year-old Peter George keeps 1,000 fish in his basement.
“Baseball, sex, fish,” he says, listing his life’s great loves. “My single greatest attribute is that I am passionate about things. That sort of defines me.”
All of George’s fish are endangered Rift Lake cichlids: colorful, freshwater fish native to the Great Lakes of East Africa. Inside his 42 tanks, expertly squeezed into a single subterranean room, the fish shimmer under artificial lights, knowing nothing of the expansive waters in which their ancestors once swam, thousands of miles away.
Due to pollution, climate change and overfishing, freshwater fish are thought to be the second most endangered vertebrates in the world. In Lake Victoria, a giant lake shared between Kenya, Uganda and Tanzania, over a quarter of endemic species, including countless cichlids, are either critically endangered or extinct.
But for some species, there is still hope. A community of rare fish enthusiasts collect endangered species of freshwater fish from the lakes and springs of East Africa, Mexico and elsewhere, and preserve them in their personal fish tanks in the hope that they might one day be reintroduced in the wild.
“I’m a hard ass,” George says. “There is hope.”
Insurance
George has been collecting fish since 1948 when, as a four-year-old in the Bronx, he would look after his grandmother’s rainbow fish. He soon developed “multiple tank syndrome” – a colloquial term used by fish collectors to denote the spiral commonly experienced after acquiring one’s first tank, which involves the sufferer buying many more tanks within a short space of time. He has not stopped collecting since.
Now, George sees himself as a conservationist; his tanks contain what is known as “insurance populations” – populations of endangered fish that are likely to go extinct in their natural habitats. He believes that when the time is right, they can be taken from his collection and returned to their homes. “I would never accept the fact that they couldn’t be reintroduced,” he says.
canadian pharmacies comparison: Certified International Pharmacy Online – my canadian pharmacy rx
The world’s largest architectural model captures New York City in the ’90s
aerodrome finance
The Empire State building stands approximately 15 inches tall, whereas the Statue of Liberty measures at just under two inches without its base. At this scale, even ants would be too big to represent people in the streets below.
These lifelike miniatures of iconic landmarks can be found on the Panorama — which, at 9,335 square feet, is the largest model of New York City, meticulously hand-built at a scale of 1:1,200. The sprawling model sits in its own room at the Queens Museum, where it was first installed in the 1960s, softly rotating between day and night lighting as visitors on glass walkways are given a bird’s eye view of all five boroughs of the city.
To mark the model’s 60th anniversary, which was celebrated last year, the museum has published a new book offering a behind-the-scenes look at how the Panorama was made. Original footage of the last major update to the model, completed in 1992, has also gone on show at the museum as part of a 12-minute video that features interviews with some of the renovators.
The Queens Museum’s assistant director of archives and collections, Lynn Maliszewski, who took CNN on a visit of the Panorama in early March, said she hopes the book and video will help to draw more visitors and attention to the copious amount of labor — over 100 full-time workers, from July 1961 to April 1964 — that went into building the model.
“Sometimes when I walk in here, I get goosebumps, because this is so representative of dreams and hopes and family and struggle and despair and excitement… every piece of the spectrum of human emotion is here (in New York) happening at the same time,” said Maliszewski. “It shows us things that you can’t get when you’re on the ground.”
Original purpose
The Panorama was originally built for the 1964 New York World’s Fair, then the largest international exhibition in the US, aimed at spotlighting the city’s innovation. The fair was overseen by Robert Moses, the influential and notorious urban planner whose highway projects displaced hundreds of thousands New Yorkers. When Moses commissioned the Panorama, which had parts that could be removed and redesigned to determine new traffic patterns and neighborhood designs, he saw an opportunity to use it as a city planning tool.
Originally built and revised with a margin of error under 1%, the model was updated multiple times before the 1990s, though it is now frozen in time. According to Maliszewski, it cost over $672,000 to make in 1964 ($6.8 million in today’s money) and nearly $2 million (about $4.5 million today) was spent when it was last revised in 1992.
https://interpharmonline.shop/# vipps approved canadian online pharmacy
Read Full Article wallet surf
link wallet surf
discover this info here surf wallet
image source zkLogin
india online pharmacy store India Med Fast online pharmacy india
click site wallet surf
Pretty! This has been an incredibly wonderful post. Thanks for supplying this information.
online medicine shopping in india: online pharmacy india – india pharmacy without prescription
Тут можно преобрести двухскатный навес в Санкт-Петербурге подробно на сайте навесы для автомобиля