1、准备

  1. html, body{
  2. margin: 0;
  3. padding: 0;
  4. height: 100%;
  5. background-color: pink;
  6. }
  1. #title {
  2. color: #fff;
  3. }
  1. // 引入样式资源
  2. import './index1.css';
  3. import './index.less';

2、webpack.config.js配置

  1. /*
  2. webpack.config.js webpack的配置文件
  3. 作用: 当你运行 webpack 指令时,会加载里面的配置
  4. 所有构建工具都是基于nodejs平台运行的~模块化默认采用commonjs。
  5. */
  6. const { resolve } = require('path');
  7. module.exports = {
  8. // webpack配置
  9. // 入口起点
  10. entry: './src/index.js',
  11. // 输出
  12. output: {
  13. // 输出文件名
  14. filename: 'built.js',
  15. // 输出路径
  16. path: resolve(__dirname, 'build')
  17. },
  18. // loader的配置
  19. module: {
  20. rules: [
  21. // 详细loader配置
  22. // 不同文件必须配置不同loader处理
  23. {
  24. test: /\.css$/,
  25. use: [
  26. // use数组中loader执行顺序:从右到左,从下到上 依次执行
  27. //css-loader导出的模块数组,将样式通过style标签或者其他形式插入到DOM中
  28. 'style-loader',
  29. //1、会对 @import 和 url() 进行处理,2、默认生成一个数组存放存放处理后的样式字符串,并将其导出
  30. 'css-loader'
  31. ]
  32. },
  33. {
  34. test: /\.less$/,
  35. use: [
  36. 'style-loader',
  37. 'css-loader',
  38. // 将less文件编译成css文件
  39. 'less-loader'
  40. ]
  41. }
  42. ]
  43. },
  44. // plugins的配置
  45. plugins: [
  46. // 详细plugins的配置
  47. ],
  48. // 模式
  49. mode: 'development', // 开发模式
  50. // mode: 'production'
  51. }

3、打包结果
image.png