关键词搜索

源码搜索 ×
×

前端笔记-webpack加载前端资源(图片,css等)

发布2019-08-05浏览4422次

详情内容

目录

 

基本概念

代码与实例


 

基本概念

通过在webpack.config.js这个文件中添加module rules进行如下代码:

这里分别是加载vue,以及css,和styl样式和图片

styl用于css预处理,模块化编写css

这里还要说下图片,这里的options为其他的选项,比如:

  1. test: /\.(gif|jpg|jpeg|png|svg)$/,
  2. use: [
  3. {
  4. loader: 'url-loader',
  5. options:{
  6. limit: 1024,
  7. name: '[name] aaa.[ext]'
  8. }
  9. }
  10. ]

这里限制图片大小为1024,大了的用base64压缩,把名字打包为[name] aaa.[ext]

如下添加的图片和css、styl在bundle.js中都能找到

他会将其中的内容都放到bundle.js中

 

 

代码与实例

文件结构如下:

源码如下:

test-stylus.styl

  1. body
  2. font-size: 20px

test.css

  1. body{
  2. color: red;
  3. background-image: url('../images/gen.svg')
  4. }

app.vue

  1. <template>
  2. <div id="test">{{text}}</div>
  3. </template>
  4. <script>
  5. export default {
  6. data(){
  7. return {
  8. text: "abc"
  9. }
  10. }
  11. }
  12. </script>
  13. <style>
  14. #test{
  15. color: red;
  16. }
  17. </style>

index.js

  1. import Vue from 'vue'
  2. import App from './app.vue'
  3. import './asserts/images/bg.jpg'
  4. import './asserts/styles/test.css'
  5. import './asserts/styles/test-stylus.styl'
  6. const root = document.createElement('div')
  7. document.body.appendChild(root)
  8. new Vue({
  9. render: (h) => h(App)
  10. }).$mount(root)

package.json

  1. {
  2. "name": "vuetestdemo",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "scripts": {
  7. "test": "echo \"Error: no test specified\" && exit 1",
  8. "build": "webpack --config webpack.config.js"
  9. },
  10. "author": "",
  11. "license": "ISC",
  12. "dependencies": {
  13. "css-loader": "^3.1.0",
  14. "file-loader": "^4.1.0",
  15. "style-loader": "^0.23.1",
  16. "stylus": "^0.54.5",
  17. "stylus-loader": "^3.0.2",
  18. "url-loader": "^2.1.0",
  19. "vue": "^2.6.10",
  20. "vue-loader": "^15.7.1",
  21. "vue-template-compiler": "^2.6.10",
  22. "webpack": "^4.39.1",
  23. "webpack-cli": "^3.3.6"
  24. }
  25. }

webpack.config.js

  1. const path = require('path')
  2. const VueLoaderPlugin = require('vue-loader/lib/plugin');
  3. module.exports = {
  4. entry: path.join(__dirname, 'src/index.js'),
  5. output: {
  6. filename: 'bundle.js',
  7. path: path.join(__dirname, 'dist')
  8. },
  9. module:{
  10. rules:[
  11. {
  12. test: /\.vue$/,
  13. loader: 'vue-loader'
  14. },
  15. {
  16. test: /\.styl/,
  17. use: [
  18. 'style-loader',
  19. 'css-loader',
  20. 'stylus-loader'
  21. ]
  22. },
  23. {
  24. test: /\.css$/,
  25. // use: ['vue-style-loader', 'css-loader', 'style-loader']
  26. use: [
  27. 'style-loader',
  28. 'css-loader'
  29. ]
  30. },
  31. {
  32. test: /\.(gif|jpg|jpeg|png|svg)$/,
  33. use: [
  34. {
  35. loader: 'url-loader',
  36. options: {
  37. limit: 1024,
  38. name: '[name] aaa.[ext]'
  39. }
  40. }
  41. ]
  42. }
  43. ]
  44. },
  45. plugins: [
  46. new VueLoaderPlugin()
  47. ]
  48. }

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载