-
查看全部
-
// 生成多个html const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); module.exports = { entry: { app: './src/index.js', print: './src/print.js', a: './src/a.js', b: './src/b.js', c: './src/c.js', }, output: { filename: 'js/[name].bundle.js', path: path.resolve(__dirname, 'dist'), publicPath: 'http://cdn.com' }, plugins: [ new CleanWebpackPlugin(), new HtmlWebpackPlugin({ filename: 'a.html', // html标题 title: 'this is a.html', // 模板 template: 'index.html', inject: 'head', // 引用的js chunks: ['a'] // minify: { // removeComments: false, // collapseWhitespace: false // } }), new HtmlWebpackPlugin({ filename: 'b.html', title: 'this is b.html', template: 'index.html', inject: 'head', chunks: ['b'] // minify: { // removeComments: false, // collapseWhitespace: false // } }), new HtmlWebpackPlugin({ filename: 'c.html', title: 'this is c.html', template: 'index.html', inject: 'head', chunks: ['c'] // 压缩 // minify: { // removeComments: false, // collapseWhitespace: false // } }) ], };
查看全部 -
const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); module.exports = { entry: { app: './src/index.js', print: './src/print.js' }, plugins: [ new CleanWebpackPlugin(), new HtmlWebpackPlugin({ title: 'Output Management', inject: 'head', // 压缩 minify: { // 删除注释 removeComments: true, // 去除空格 collapseWhitespace: true } }) ], output: { filename: 'js/[name].bundle.js', path: path.resolve(__dirname, 'dist'), publicPath: 'http://cdn.com' } };
查看全部 -
const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); module.exports = { entry: { app: './src/index.js', print: './src/print.js' }, plugins: [ new CleanWebpackPlugin(), new HtmlWebpackPlugin({ title: 'Output Management', inject: 'head' }) ], output: { filename: 'js/[name].bundle.js', path: path.resolve(__dirname, 'dist'), // 可以为生产环境地址,执行npm run build命令之后,dist目录下 index.html中的js路径会加上这个前缀 publicPath: 'http://cdn.com' } };
查看全部 -
自动化生成项目中的html页面
npm install --save-dev html-webpack-plugin
参考网址:
(1)webpack插件列表:
https://www.webpackjs.com/plugins/,
(2)htmlWebpackPlugin插件介绍:
https://www.webpackjs.com/plugins/html-webpack-plugin/
(3)htmlWebpackPlugin插件配置:
https://github.com/jantimon/html-webpack-plugin#configuration
webpack.config.js
const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); module.exports = { entry: { app: './src/index.js', print: './src/print.js' }, plugins: [ new CleanWebpackPlugin(), new HtmlWebpackPlugin({ title: 'Output Management' }) ], output: { // 加上js,可以使js和html分离开来 filename: 'js/[name].bundle.js', path: path.resolve(__dirname, 'dist') } };
查看全部 -
参考网址:https://www.webpackjs.com/guides/output-management/
index.html
<!doctype html> <html> <head> <title>Output Management</title> </head> <body> <script src="./app.bundle.js"></script> </body> </html>
index.js
import _ from 'lodash'; import printMe from './print.js'; function component() { var element = document.createElement('div'); var btn = document.createElement('button'); element.innerHTML = _.join(['Hello', 'webpack'], ' '); btn.innerHTML = 'Click me and check the console!'; btn.onclick = printMe; element.appendChild(btn); return element; } document.body.appendChild(component());
webpack.config.js
const path = require('path'); module.exports = { entry: { app: './src/index.js', print: './src/print.js' }, output: { filename: '[name].bundle.js', path: path.resolve(__dirname, 'dist') } };
print.js
export default function printMe() { console.log('I get called from print.js!'); }
// 清理输出目录
const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); module.exports = { entry: { app: './src/index.js', print: './src/print.js' }, plugins: [ new CleanWebpackPlugin(), new HtmlWebpackPlugin({ title: 'Output Management' }) ], output: { filename: '[name].bundle.js', path: path.resolve(__dirname, 'dist') } };
查看全部 -
package.json配置脚本
{ "name": "webpack-demo", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "webpack --config webpack.dev.config.js" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "css-loader": "^3.4.2", "style-loader": "^1.1.3", "webpack": "^4.41.5", "webpack-cli": "^3.3.10" }, "dependencies": { "lodash": "^4.17.15" } }
查看全部 -
// 指定config文件
webpack --config web.dev.config.js
或
npx webpack --config webpack.dev.config.js
查看全部 -
// webpack.config.js
const path = require('path'); module.exports = { entry: './src/index.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist') }, module: { rules: [ // 加载css { test: /\.css$/, use: [ 'style-loader', 'css-loader' ] }, // 加载图片 { test: /\.(png|svg|jpg|gif)$/, use: [ 'file-loader' ] }, // 加载字体 { test: /\.(woff|woff2|eot|ttf|otf)$/, use: [ 'file-loader' ] } ] } };
查看全部 -
//加载 CSS npm install --save-dev style-loader css-loader npm run build 参考网址:https://www.webpackjs.com/guides/asset-management/
查看全部 -
npm init -y npm install webpack webpack-cli --save-dev npx webpack 参考网址:https://www.webpackjs.com/guides/getting-started/
查看全部 -
<%= %> 有等号直接取值,没有等号直接运行js代码
查看全部 -
css-loader使得webpack可以识别css文件
style-loader可以把生成 一个style标签并把css样式插入进style标签里面
查看全部 -
webpack命令
webpack hello.js hello.bundle.js
查看全部 -
webpack.config.js配置:
module.exports={
entry: './src/script/main.js', // 打包的入口文件
output: { // 打包后的文件
path: './dist/js', // 地址
filename: 'bundle.js' // 打包的文件名称
}
}
查看全部
举报