模塊打包機css
分析項目結構,找到JavaScript模塊以及其它 的一些瀏覽器不能直接運行的拓展語言(Scss,TypeScript等),並將其轉換和打包爲合適的格式供瀏覽器使用html
npm init //建立package.json
複製代碼
npm install webpack -D
npm install webpack-cli -D
複製代碼
const path = require('path');//導入path模塊
module.exports = {
//設置開發環境
mode: 'development',//development開發環境production上線環境
entry:{//入口文件
index: './src/index.js'
},
output:{//出口文件
//當前絕對路徑
path: path.resolve(__dirname,'dist'),
filename:'[name].js',//與入口文件同名
}
}
複製代碼
"scripts": {
"build": "webpack"
},
複製代碼
//配置webpack開發服務功能 服務與熱更新配置
devServer: {
//設置基本目錄功能
contentBase: path.resolve(__dirname,'dist'),
//服務器ip,可使用localhost
host: 'localhost',
//配置服務端口號
port: 8088,
//服務壓縮是否開啓
compress: true
},
複製代碼
npm install webpack-dev-server -D
複製代碼
"scripts": {
"dev": "webpack-dev-server"
},
複製代碼
//配置webpack開發服務功能
devServer: {
//自動打開瀏覽器
open: true
},
複製代碼
npm install html-webpack-plugin -D
複製代碼
const HtmlPlugin = require('html-webpack-plugin');
plugins: [
new HtmlPlugin({
//編譯後文件名稱
filename: 'test.html',
//頁面標題
title: '標題',
//引入的入口文件
chunks: ['index'],
minifiy:{
removeAttributeQuotes:true
//對html文件進行壓縮,去掉屬性的雙引號
},
hash:true,
template: './src/index.html'
})
]
複製代碼
plugins: [
new HtmlPlugin({
//編譯後文件名稱
filename: 'test.html',
//頁面標題
title: '標題',
//引入的入口文件
chunks: ['index'],
minifiy:{
removeAttributeQuotes:true
//對html文件進行壓縮,去掉屬性的雙引號
},
hash:true,
template: './src/index.html'
}),
new HtmlPlugin({
filename: 'test2.html',
title: '標題2',
chunks: ['index2'],
minifiy:{
removeAttributeQuotes:true
},
hash:true,
template: './src/index2.html'
})
]
複製代碼
npm install html-withimg-loader -D
複製代碼
module:{
rules: [
{
test: /\.(html|htm)$/i,
loader: 'html-withimg-loader'
}
]
}
複製代碼
npm install uglifyjs-webpack-plugin -D
複製代碼
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
plugins:[
new UglifyJsPlugin()
]
複製代碼
module.exports = {
//設置開發環境
mode: 'production',//development開發環境production上線環境
},
複製代碼
import './index.css';
複製代碼
npm install style-loader -D
npm install css-loader -D
複製代碼
module:{
rules: [
{
test: /\.css$/,
use: ['style-loader','css-loader']
}
]
}
複製代碼
npm install extract-text-webpack-plugin@next -D
//next表示下一個版本
複製代碼
const ExtractTestPlugin = require('extract-text-webpack-plugin');
plugins: [
new ExtractTestPlugin('css/index.css')
]
module:{
rules: [
{
test: /\.css$/,
use: ExtractTestPlugin.extract({
fallback: 'style-loader',
use: "css-loader"
})
}
]
}
複製代碼
npm install file-loader url-loader -D
複製代碼
module:{
rules: [
{
test: /\.(png|jpg|jpeg|gif)/,
use: [{
loader: 'url-loader',
options: {
limit: 500,
//是把小於500B的文件打成Base64的格式,寫入css
outputPath:'images/'
}
}]
}
]
}
複製代碼
module.exports = {
output:{
publicPath: 'http://localhost:8088/'
}
}
複製代碼
npm install node-sass sass-loader -D
複製代碼
import './common.scss';
複製代碼
module:{
rules: [
{
test: /\.scss$/,
use: ['style-loader','css-loader','sass-loader']
}
]
}
複製代碼
module:{
rules: [
{
test: /\.scss$/,
use: ExtractTestPlugin.extract({
fallback: 'style-loader',
use: ["css-loader","sass-loader"]
})
}
]
}
複製代碼
npm install postcss-loader autoprefixer -D
複製代碼
module.exports = {
plugins: [
require('autoprefixer')({
"browsers":[
"defaults",
"not ie<11",
"last 2 versions",
">1%",
"iOS 7",
"last 3 iOS versions"
]
})
]
};
複製代碼
module:{
rules: [
{
test: /\.css$/,
use: ExtractTestPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
importLoaders: 1
}
},
'postcss-loader'
]
})
}
]
}
複製代碼
npm install purifycss-webpack purify-css -D
複製代碼
const glob = require('glob');
const PurifyCSSPlugin = require('purifycss-webpack');
module.exports = {
plugins: [
new PurifyCSSPlugin({
paths: glob.sync(path.join(__dirname,'src/*.html'))
})
]
};
複製代碼
npm install babel-core babel-loader @babel/core @babel/preset-env -D
複製代碼
module:{
rules: [
{
test:/\.js$/,
use:[
{
loader:'babel-loader',
options:{
presets:['@babel/preset-env']
}
}
],
exclude:/node_modules/
}
]
}
複製代碼
const webpack = require('webpack');
module.exports = {
plugins: [
new webpack.BannerPlugin('不要抄襲!!!'),
]
};
複製代碼
npm install --save jquery
複製代碼
const webpack = require('webpack');
module.exports = {
plugins: [
new webpack.ProvidePlugin({
$:'jquery'
})
]
};
複製代碼
npm install copy-webpack-plugin -D
複製代碼
var copyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
plugins: [
new copyWebpackPlugin([
{
from: __dirname + '/src/public',
to:'./public'
}
])
]
};
複製代碼
const entry = require("./webpack/entry_webpack.js");
const entry = {
entry:'./src/entry.js'
};
module.exports = entry;
複製代碼
module.exports = {
entry: entry,
}
複製代碼