模块打包机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,
}
复制代码