1. webpack-demo
    2. |- package.json
    3. |- webpack.config.js
    4. + |- server.js
    5. |- /dist
    6. |- /src
    7. |- data.xml
    8. |- icon.png+
    9. |- style.css
    10. |- my-font.woff
    11. |- my-font.woff2
    12. |- print.js
    13. |- index.js
    14. |- /node_modules
    1. npm install --save-dev webpack-dev-server
    2. npm install --save-dev express webpack-dev-middleware
    1. // webpack.config.js
    2. const path = require('path');
    3. const HtmlWebpackPlugin = require('html-webpack-plugin'); // 自动生成页面
    4. const CleanWebpackPlugin = require('clean-webpack-plugin'); // 自动清理,清理dist旧文件
    5. module.exports = {
    6. mode: 'development',
    7. devtool: 'inline-source-map', // 站点地图,可以输出错误发生所在地
    8. devServer: {
    9. contentBase: './dist' // webpack-dev-server在哪里寻找文件
    10. },
    11. entry: {
    12. index: './src/index.js',
    13. print: './src/print.js'
    14. },
    15. plugins: [
    16. new CleanWebpackPlugin(['dist']),
    17. new HtmlWebpackPlugin({
    18. title: 'VISION-CANVAS-L',
    19. filename: 'index.html',
    20. template: 'view/index.html',
    21. }),
    22. ],
    23. output: {
    24. filename: '[name].bundle.js',
    25. path: path.resolve(__dirname, 'dist'),
    26. publicPath: '/'
    27. },
    28. module: {
    29. rules: [
    30. {
    31. test: /\.css$/,
    32. use: [
    33. 'style-loader',
    34. 'css-loader'
    35. ]
    36. },
    37. {
    38. test: /\.(png|svg|jpg|gif)$/,
    39. use: [
    40. 'file-loader'
    41. ]
    42. },
    43. {
    44. test: /\.(woff|woff2|eot|ttf|otf)$/,
    45. use: [
    46. 'file-loader'
    47. ]
    48. },
    49. {
    50. test: /\.(csv|tsv)$/,
    51. use: [
    52. 'csv-loader'
    53. ]
    54. },
    55. {
    56. test: /\.xml$/,
    57. use: [
    58. 'xml-loader'
    59. ]
    60. }
    61. ],
    62. }
    63. };
    1. // /server.js
    2. const express = require('express');
    3. const webpack = require('webpack');
    4. const webpackDevMiddleware = require('webpack-dev-middleware');
    5. const app = express();
    6. const config = require('./webpack.config.js');
    7. const compiler = webpack(config);
    8. // Tell express to use the webpack-dev-middleware and use the webpack.config.js
    9. // configuration file as a base.
    10. app.use(webpackDevMiddleware(compiler, {
    11. publicPath: config.output.publicPath
    12. }));
    13. // Serve the files on port 3000.
    14. app.listen(3000, function () {
    15. console.log('Example app listening on port 3000!\n');
    16. });
    1. {
    2. "name": "webpackDemo",
    3. "version": "1.0.0",
    4. "description": "",
    5. "private": true,
    6. "main": "index.js",
    7. "scripts": {
    8. "test": "echo \"Error: no test specified\" && exit 1",
    9. "watch": "webpack --watch",
    10. "build": "webpack",
    11. "start": "webpack-dev-server --open",
    12. "server": "node server.js"
    13. },
    14. "keywords": [],
    15. "author": "",
    16. "license": "ISC",
    17. "devDependencies": {
    18. "clean-webpack-plugin": "^1.0.0",
    19. "css-loader": "^2.0.0",
    20. "csv-loader": "^3.0.2",
    21. "express": "^4.16.4",
    22. "file-loader": "^2.0.0",
    23. "html-webpack-plugin": "^3.2.0",
    24. "style-loader": "^0.23.1",
    25. "webpack": "^4.27.1",
    26. "webpack-cli": "^3.1.2",
    27. "webpack-dev-middleware": "^3.4.0",
    28. "webpack-dev-server": "^3.1.10",
    29. "xml-loader": "^1.2.1"
    30. },
    31. "dependencies": {
    32. "lodash": "^4.17.11"
    33. }
    34. }