1、目录
2、js/index.js
import createElement from '../components/header/createHeader.js';let header = document.getElementById('header');// 创建header模块const heading = createElement();header.append(heading);// 导入所有文件中的内容const mdFiles = require.context('../markdown', true, /\.md$/);const modules = mdFiles.keys().reduce((modules, modulePath) => { const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1'); const value = mdFiles(modulePath); modules[moduleName] = value.default; return modules;}, {});console.log(modules, 'modules>>>');
3、page/index.html
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Rock</title> </head> <style> * { margin: 0; padding: 0; } #wrapper { width: 100%; height: 100vh; } #header { width: 100%; height: 100px; line-height: 100px; border-bottom: 1px solid #aaa; } .nav { width: 20%; height: 100px; line-height: 100px; display: flex; justify-content: space-around; } #iframe { width: 100%; height: 100%; display: none; } </style> <body> <div id="wrapper"> <div id="header"></div> <iframe id="iframe" name="box" src="#" frameborder="none"></iframe> </div> </body> <script src="index.js"></script></html>
3、config.js
export default { mk1: '/dist/markdown/mk1.html', mk2: '/dist/markdown/mk2.html', mk3: '/dist/markdown/mk3.html', mk4: '/dist/markdown/mk4.html', mk5: '/dist/markdown/mk5.html',};
4、markdown-loader.js
const marked = require('marked');const fs = require('fs');const path = require('path');const light = require('highlight.js');marked.setOptions({ highlight: function (code) { let res; res = light.highlightAuto(code).value; return res; },});// solarized-lightmodule.exports = async function (source) { // 返回的必须是处理后的js字符串 let html = marked(source); var str = ` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document111</title> <link href="http://cdn.bootcss.com/highlight.js/8.0/styles/monokai_sublime.min.css" rel="stylesheet"> </head> <body> <pre> <code class="language-html"> ${html} </code> </pre> </body> </html> `; const code = `export default ${JSON.stringify(str)}`; let resPath = path.parse(this.resourcePath); try { await fs.mkdirSync('dist/markdown'); } catch (error) {} await fs.writeFileSync(`dist/markdown/${resPath.name}.html`, str); return code;};
6、package.json
{ "name": "webpack", "version": "1.0.0", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "npx webpack", "start": "webpack-dev-server --open" }, "author": "", "license": "ISC", "description": "", "dependencies": {}, "devDependencies": { "clean-webpack-plugin": "^3.0.0", "copy-webpack-plugin": "^6.0.3", "css-loader": "^4.2.1", "file-loader": "^6.0.0", "highlight.js": "^10.1.2", "html-webpack-plugin": "^4.3.0", "loader-utils": "^2.0.0", "marked": "^1.1.1", "style-loader": "^1.2.1", "webpack": "^4.44.1", "webpack-cli": "^3.3.12", "webpack-dev-server": "^3.11.0" }}
7、README.md
# webpack### 介绍- [链接](https://webpack.js.org/configuration/mode/) a bundler for javascript and friends---### 简单配置使用技巧### loader 机制真正解决了 html 中文件的引入,实现了万物皆模块,实现 javascript 驱动### plugin 插件机制常见应用场景- 打包前清楚 dist 目录(clean-webpack-plugin)- 生成所需要的 HTML 文件 (html-webpack-plugin)- 根据环境动态注入变化的部分(如 API 地址等)- 拷贝不需要打包的资源文件到输出目录- 压缩 Webpack 打包后输出的文件- 自动发布打包结果到服务器实现自动部署
8、webpack.config.js
const path = require('path');const { CleanWebpackPlugin } = require('clean-webpack-plugin');const HtmlWebpackPlugin = require('html-webpack-plugin');const CopyWebpackPlugin = require('copy-webpack-plugin');/**; * @author rockshang * @type {import('webpack').Configuration} * @time 20-08-12 */module.exports = { entry: { index: './pages/js/index.js', about: './pages/js/about.js', }, output: { filename: '[name].js', path: __dirname + '/dist', }, devServer: { contentBase: path.join(__dirname, 'dist'), compress: true, port: 8000, hot: true, hotOnly: true, }, mode: 'development', module: { rules: [ { test: /\.css$/, use: ['style-loader', 'css-loader'], }, { test: /\.md$/, use: { loader: './markdown-loader', options: { name: 'name', }, }, }, { test: /\.(png|svg|jpg|gif|mp4)$/, use: [ { loader: 'file-loader', options: { outputPath: './img', publicPath: './img', }, }, ], }, ], }, plugins: [ new CleanWebpackPlugin({ cleanOnceBeforeBuildPatterns: ['!markdown/**/*'], }), new HtmlWebpackPlugin({ template: './pages/index.html', meta: { viewport: 'width = device-width', }, chunks: ['index.js'], hash: true, }), new HtmlWebpackPlugin({ filename: 'about.html', template: './pages/about.html', chunks: ['about.js'], hash: true, }), ],};
9、预览
