1. webpack-demo
    2. |- package.json
    3. |- webpack.config.js
    4. |- /dist
    5. |- bundle.js
    6. |- index.html
    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

    输出设置output,输出文件根据entry的key输出js

    1. npm install --save-dev html-webpack-plugin
    2. npm install --save-dev clean-webpack-plugin
    1. // webpack.config.js
    2. const path = require('path');
    3. const HtmlWebpackPlugin = require('html-webpack-plugin'); // 自动生成index.html
    4. // 自动清理,清理dist旧文件
    5. // const CleanWebpackPlugin = require('clean-webpack-plugin');
    6. // 升级版本后,自动清理,清理dist旧文件
    7. const {CleanWebpackPlugin} = require('clean-webpack-plugin');
    8. module.exports = {
    9. entry: {
    10. index: './src/index.js',
    11. print: './src/print.js'
    12. },
    13. plugins: [
    14. // new CleanWebpackPlugin(['dist']),
    15. new CleanWebpackPlugin(), // 自动删除webpack output 对应目录
    16. new HtmlWebpackPlugin({
    17. title: 'VISION-CANVAS-L', // 设置输出文件标题名
    18. filename: 'index.html', // 设置输出文件名
    19. template: 'view/index.html', // 设置模板
    20. })
    21. ],
    22. output: {
    23. filename: '[name].bundle.js', // 输出文件根据entry的key来命名
    24. path: path.resolve(__dirname, 'dist')
    25. },
    26. module: {
    27. rules: [
    28. {
    29. test: /\.css$/,
    30. use: [
    31. 'style-loader',
    32. 'css-loader'
    33. ]
    34. },
    35. {
    36. test: /\.(png|svg|jpg|gif)$/,
    37. use: [
    38. 'file-loader'
    39. ]
    40. },
    41. {
    42. test: /\.(woff|woff2|eot|ttf|otf)$/,
    43. use: [
    44. 'file-loader'
    45. ]
    46. },
    47. {
    48. test: /\.(csv|tsv)$/,
    49. use: [
    50. 'csv-loader'
    51. ]
    52. },
    53. {
    54. test: /\.xml$/,
    55. use: [
    56. 'xml-loader'
    57. ]
    58. }
    59. ],
    60. }
    61. };
    1. <!-- /dist/index.html -->
    2. <!doctype html>
    3. <html>
    4. <head>
    5. <title><%= htmlWebpackPlugin.options.title %></title>
    6. </head>
    7. <body>
    8. </body>
    9. </html>