1. webpack-demo
    2. |- package.json
    3. |- webpack.config.js
    4. |- server.js
    5. + |- DEV-server.js
    6. |- /dist
    7. |- /src
    8. |- data.xml
    9. |- icon.png+
    10. |- style.css
    11. |- my-font.woff
    12. |- my-font.woff2
    13. |- print.js
    14. |- index.js
    15. |- /node_modules
    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. const webpack = require('webpack');
    6. module.exports = {
    7. mode: 'development',
    8. devtool: 'inline-source-map', // 站点地图,可以输出错误发生所在地
    9. devServer: {
    10. contentBase: './dist', // webpack-dev-server在哪里寻找文件
    11. hot: true
    12. },
    13. entry: {
    14. app: './src/index.js'
    15. },
    16. plugins: [
    17. new CleanWebpackPlugin(['dist']),
    18. new HtmlWebpackPlugin({
    19. title: 'VISION-CANVAS-L',
    20. filename: 'index.html',
    21. template: 'view/index.html',
    22. }),
    23. new webpack.HotModuleReplacementPlugin() // HMR的插件
    24. ],
    25. output: {
    26. filename: '[name].bundle.js',
    27. path: path.resolve(__dirname, 'dist'),
    28. publicPath: '/'
    29. },
    30. module: {
    31. rules: [
    32. {
    33. test: /\.css$/,
    34. use: [
    35. 'style-loader',
    36. 'css-loader'
    37. ]
    38. },
    39. {
    40. test: /\.(png|svg|jpg|gif)$/,
    41. use: [
    42. 'file-loader'
    43. ]
    44. },
    45. {
    46. test: /\.(woff|woff2|eot|ttf|otf)$/,
    47. use: [
    48. 'file-loader'
    49. ]
    50. },
    51. {
    52. test: /\.(csv|tsv)$/,
    53. use: [
    54. 'csv-loader'
    55. ]
    56. },
    57. {
    58. test: /\.xml$/,
    59. use: [
    60. 'xml-loader'
    61. ]
    62. }
    63. ],
    64. }
    65. };
    1. // index.js
    2. import _ from 'lodash';
    3. import './style.css';
    4. import Icon from './icon.svg';
    5. import Data from './data.xml';
    6. import printMe from './print.js';
    7. function component() {
    8. let element = document.createElement('div');
    9. element.innerHTML = _.join(['Hello', 'webpack'], ' ');
    10. element.classList.add('hello');
    11. var myIcon = new Image();
    12. myIcon.src = Icon;
    13. element.appendChild(myIcon);
    14. var btn = document.createElement('button');
    15. btn.innerHTML = 'Click me and check the console!';
    16. btn.onclick = printMe;
    17. element.appendChild(btn);
    18. console.log(Data);
    19. return element;
    20. }
    21. // document.body.appendChild(component());
    22. let element = component(); // Store the element to re-render on print.js changes
    23. document.body.appendChild(element);
    24. if (module.hot) {
    25. module.hot.accept('./print.js', function () {
    26. console.log('Accepting the updated printMe module!');
    27. printMe();
    28. document.body.removeChild(element);
    29. element = component(); // Re-render the "component" to update the click handler
    30. document.body.appendChild(element);
    31. })
    32. }
    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. noInfo: true,
    13. }));
    14. app.use(require("webpack-hot-middleware")(compiler));
    15. // Serve the files on port 3000.
    16. app.listen(3000, function () {
    17. console.log('Example app listening on port 3000!\n');
    18. });
    1. // DEV-server.js
    2. const webpackDevServer = require('webpack-dev-server');
    3. const webpack = require('webpack');
    4. const config = require('./webpack.config.js');
    5. const options = {
    6. contentBase: './dist',
    7. hot: true,
    8. host: 'localhost'
    9. };
    10. webpackDevServer.addDevServerEntrypoints(config, options);
    11. const compiler = webpack(config);
    12. const server = new webpackDevServer(compiler, options);
    13. server.listen(5000, 'localhost', () => {
    14. console.log('dev server listening on port 5000');
    15. });