这个项目是要webpack构建多页面应用
https://devbox.cn/p/551263335

1、项目创建

webpack项目需要两份配置,一份开发环境是以src为目录,一份生产环境配置一般打包到dist目录中。下面是

1、项目文件,依赖配置截图

image.pngimage.png

2、开发环境配置

  1. const path = require('path');
  2. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  3. const copyWebpackPlugin = require('copy-webpack-plugin')
  4. module.exports = {
  5. entry: path.resolve(__dirname, 'src/app.js'),
  6. output: {
  7. path: path.resolve(__dirname, 'public'),
  8. filename: "main.js"
  9. },
  10. module: {
  11. rules: [
  12. {
  13. test: /\.css$/i,
  14. exclude: /node_modules/,
  15. use:[
  16. 'style-loader',
  17. { loader: 'css-loader', options: { importLoaders: 1 } },
  18. 'postcss-loader',
  19. ]
  20. }
  21. ]
  22. },
  23. devServer: {
  24. host:'0.0.0.0',
  25. port: 80,
  26. hot:true,
  27. static:path.join(__dirname, '/src/pages')
  28. },
  29. mode:'development',
  30. plugins:[
  31. // new CleanWebpackPlugin({ // 打包之前清空 public目录
  32. // cleanOnceBeforeBuildPatterns: ['public'],
  33. // }),
  34. new copyWebpackPlugin({ // 复制页面到public目录
  35. patterns: [
  36. {
  37. from: path.resolve(__dirname, 'src/pages/search/search.html'),
  38. to: path.resolve(__dirname, 'public/'),
  39. },
  40. {
  41. from: path.resolve(__dirname, 'src/pages/result/result.html'),
  42. to: path.resolve(__dirname, 'public/'),
  43. },
  44. {
  45. from: path.resolve(__dirname, 'src/pages/topicDetail/topicDetail.html'),
  46. to: path.resolve(__dirname, 'public/'),
  47. },
  48. {
  49. from: path.resolve(__dirname, 'src/pages/courseDetail/courseDetail.html'),
  50. to: path.resolve(__dirname, 'public/'),
  51. }
  52. ],
  53. }),
  54. ]
  55. }

3、生产环境配置

  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const {CleanWebpackPlugin} = require('clean-webpack-plugin');
  4. module.exports = {
  5. entry: {
  6. courseDetail: path.resolve(__dirname, 'src/pages/courseDetail/courseDetail.js'),
  7. search:path.resolve(__dirname, 'src/pages/search/search.js'),
  8. jquery:path.resolve(__dirname,'src/js/jquery-1.10.2.min')
  9. },
  10. output: {
  11. path: path.resolve(__dirname, 'dist'),
  12. filename: "[name].js"
  13. },
  14. module: {
  15. rules: [
  16. {
  17. test: /\.css$/i,
  18. exclude: /node_modules/,
  19. use:[
  20. 'style-loader',
  21. { loader: 'css-loader', options: { importLoaders: 1 } },
  22. 'postcss-loader',
  23. ]
  24. }
  25. ]
  26. },
  27. mode:'production',
  28. plugins:[
  29. new CleanWebpackPlugin(),
  30. new HtmlWebpackPlugin({
  31. template:'./src/pages/search/search.html',
  32. filename:'search.html',
  33. chunks:['search']//代码块,指定js文件
  34. }),
  35. new HtmlWebpackPlugin({
  36. template:'./src/pages/courseDetail/courseDetail.html',
  37. filename:'courseDetail.html',
  38. chunks:['courseDetail']//代码块,指定js文件
  39. })
  40. ]
  41. }

4、注意点:

1、webstorm 只有使用tailwindcss 3.1.8的版本才能有智能提示;