首页 > 其他分享 >webpack - 资源管理

webpack - 资源管理

时间:2022-11-22 11:44:31浏览次数:45  
标签:src js webpack test path loader 资源管理 css

1. 准备文件配置文件

dist/index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Asset Management</title>
</head>
<body>
  <script src="bundle.js"></script>
</body>
</html>

webpack.config.js

const path = require('path');
 module.exports = {
   entry: './src/index.js',
   output: {
     filename: 'bundle.js',
     path: path.resolve(__dirname, 'dist'),
   },
 };

2. 加载CSS

  • 安装style-loader 与css-loader
    npm install --save-dev style-loader css-loader

  • 配置loader

const path = require('path');

 module.exports = {
   entry: './src/index.js',
   output: {
     filename: 'bundle.js',
     path: path.resolve(__dirname, 'dist'),
   },
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
 };
  • 准备文件
    src/style.css
.hello {
  color: red;
}

src/index.js

import _ from 'lodash';
import './style.css';

 function component() {
   const element = document.createElement('div');

   // Lodash, now imported by this script
   element.innerHTML = _.join(['Hello', 'webpack'], ' ');
   element.classList.add('hello');

   return element;
 }

 document.body.appendChild(component());

3. 加载图片

  • 配置loader
    webpack.config.js
const path = require('path');

 module.exports = {
   entry: './src/index.js',
   output: {
     filename: 'bundle.js',
     path: path.resolve(__dirname, 'dist'),
   },
   module: {
     rules: [
       {
         test: /\.css$/i,
         use: ['style-loader', 'css-loader'],
       },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
      },
     ],
   },
 };
  • 准备文件
    src/style.css
.hello {
  color: red;
  background: url('./icon.png');
 }

src/index.js

import _ from 'lodash';
import './style.css';
import Icon from './icon.png';

 function component() {
   const element = document.createElement('div');

   // Lodash, now imported by this script
   element.innerHTML = _.join(['Hello', 'webpack'], ' ');
   element.classList.add('hello');

  // Add the image to our existing div.
  const myIcon = new Image();
  myIcon.src = Icon;

  element.appendChild(myIcon);

   return element;
 }

 document.body.appendChild(component());

4. 加载字体

  • 配置loader
    webpack.config.js
const path = require('path');

 module.exports = {
   entry: './src/index.js',
   output: {
     filename: 'bundle.js',
     path: path.resolve(__dirname, 'dist'),
   },
   module: {
     rules: [
       {
         test: /\.css$/i,
         use: ['style-loader', 'css-loader'],
       },
       {
         test: /\.(png|svg|jpg|jpeg|gif)$/i,
         type: 'asset/resource',
       },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/resource',
      },
     ],
   },
 };
  • 准备文件
    src/style.css
font-face {
  font-family: 'MyFont';
  src: url('./my-font.woff2') format('woff2'),
    url('./my-font.woff') format('woff');
  font-weight: 600;
  font-style: normal;
}

.hello {
   color: red;
   font-family: 'MyFont';
   background: url('./icon.png');
 }

5. 加载数据

json数据格式是默认支持的

  • 安装csv文件加载器和xml文件加载器
    npm install --save-dev csv-loader xml-loader
  • 配置loader
const path = require('path');

 module.exports = {
   entry: './src/index.js',
   output: {
     filename: 'bundle.js',
     path: path.resolve(__dirname, 'dist'),
   },
   module: {
     rules: [
       {
         test: /\.css$/i,
         use: ['style-loader', 'css-loader'],
       },
       {
         test: /\.(png|svg|jpg|jpeg|gif)$/i,
         type: 'asset/resource',
       },
       {
         test: /\.(woff|woff2|eot|ttf|otf)$/i,
         type: 'asset/resource',
       },
      {
        test: /\.(csv|tsv)$/i,
        use: ['csv-loader'],
      },
      {
        test: /\.xml$/i,
        use: ['xml-loader'],
      },
     ],
   },
 };
  • 准备文件
    src/data.xml
<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Mary</to>
  <from>John</from>
  <heading>Reminder</heading>
  <body>Call Cindy on Tuesday</body>
</note>

src/data.csv

to,from,heading,body
Mary,John,Reminder,Call Cindy on Tuesday
Zoe,Bill,Reminder,Buy orange juice
Autumn,Lindsey,Letter,I miss you

src/index.js

import _ from 'lodash';
import './style.css';
import Icon from './icon.png';
import Data from './data.xml';
import Notes from './data.csv';

 function component() {
   const element = document.createElement('div');

   // Lodash, now imported by this script
   element.innerHTML = _.join(['Hello', 'webpack'], ' ');
   element.classList.add('hello');

   // Add the image to our existing div.
   const myIcon = new Image();
   myIcon.src = Icon;

   element.appendChild(myIcon);

  console.log(Data);
  console.log(Notes);

   return element;
 }

 document.body.appendChild(component());

6. 自定义JSON模块解析器

  • 安装解析器模块
    npm install toml yamljs json5 --save-dev

  • 配置loader

const path = require('path');
const toml = require('toml');
const yaml = require('yamljs');
const json5 = require('json5');

 module.exports = {
   entry: './src/index.js',
   output: {
     filename: 'bundle.js',
     path: path.resolve(__dirname, 'dist'),
   },
   module: {
     rules: [
       {
         test: /\.css$/i,
         use: ['style-loader', 'css-loader'],
       },
       {
         test: /\.(png|svg|jpg|jpeg|gif)$/i,
         type: 'asset/resource',
       },
       {
         test: /\.(woff|woff2|eot|ttf|otf)$/i,
         type: 'asset/resource',
       },
       {
         test: /\.(csv|tsv)$/i,
         use: ['csv-loader'],
       },
       {
         test: /\.xml$/i,
         use: ['xml-loader'],
       },
      {
        test: /\.toml$/i,
        type: 'json',
        parser: {
          parse: toml.parse,
        },
      },
      {
        test: /\.yaml$/i,
        type: 'json',
        parser: {
          parse: yaml.parse,
        },
      },
      {
        test: /\.json5$/i,
        type: 'json',
        parser: {
          parse: json5.parse,
        },
      },
     ],
   },
 };
  • 准备文件
    src/data.toml
title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
organization = "GitHub"
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
dob = 1979-05-27T07:32:00Z

src/data.yaml

title: YAML Example
owner:
  name: Tom Preston-Werner
  organization: GitHub
  bio: |-
    GitHub Cofounder & CEO
    Likes tater tots and beer.
  dob: 1979-05-27T07:32:00.000Z
src/data.json5

src/data.json5

{
  // comment
  title: 'JSON5 Example',
  owner: {
    name: 'Tom Preston-Werner',
    organization: 'GitHub',
    bio: 'GitHub Cofounder & CEO\n\
Likes tater tots and beer.',
    dob: '1979-05-27T07:32:00.000Z',
  },
}

src/index.js

import _ from 'lodash';
import './style.css';
import Icon from './icon.png';
import Data from './data.xml';
import Notes from './data.csv';
import toml from './data.toml';
import yaml from './data.yaml';
import json from './data.json5';

console.log(toml.title); // output `TOML Example`
console.log(toml.owner.name); // output `Tom Preston-Werner`

console.log(yaml.title); // output `YAML Example`
console.log(yaml.owner.name); // output `Tom Preston-Werner`

console.log(json.title); // output `JSON5 Example`
console.log(json.owner.name); // output `Tom Preston-Werner`

 function component() {
   const element = document.createElement('div');

   // Lodash, now imported by this script
   element.innerHTML = _.join(['Hello', 'webpack'], ' ');
   element.classList.add('hello');

   // Add the image to our existing div.
   const myIcon = new Image();
   myIcon.src = Icon;

   element.appendChild(myIcon);

   console.log(Data);
   console.log(Notes);

   return element;
 }

 document.body.appendChild(component());

标签:src,js,webpack,test,path,loader,资源管理,css
From: https://www.cnblogs.com/xiaodi-js/p/16914648.html

相关文章

  • Web 前端之 Webpack5 教程
    Webpack是一个静态资源打包工具。它会以一个或多个文件作为打包的入口,将我们整个项目所有文件编译组合成一个或多个文件输出出去。输出的文件就是编译好的文件,就可以在浏......
  • #yyds干货盘点#聊一聊webpack
    webpack核心概念Entry入口起点(entrypoint)指示webpack应该使用哪个模块,来作为构建其内部依赖图的开始。进入入口起点后,webpack会找出有哪些模块和库是入口起点(直接......
  • 乘风破浪,遇见最美Windows 11之升级安装Windows 11 22H2(含突破TPM等限制),资源管理器多
    前言终于,在Windows1122H2迎来了资源管理器的多标签功能,等得时间太久,但终究还是来了,忍不住再写一次。而且这个版本,任务管理器变化也挺大,但是并不好用。任务栏搜索按......
  • 初识webpack配置项
    webpackwebpack是JS的静态模块打包器。当webpack处理应用程序时,它会将所有这些模块打包成一个或多个文件。配置webpackentry入口起点指示webpack打包的起点。进......
  • k8S资源管理
    资源管理资源管理介绍在kubernetes中,所有的内容都抽象为资源,用户需要通过操作资源来管理kubernetes。kubernetes的本质上就是一个集群系统,用户可以在集群中部署各种......
  • webpack热加载等一些常用配置
    1、查看webpack打包文件以及对应信息webpack--display-modules--display-reasons2、webpack-p:会对文件进行优化,压缩等3、webpack-d:对应配置文件的devtool4、webpack......
  • Vue中使用Mock,devSever中before方法弃用>webpack新版本出现的vue.config.js配置问题:op
    话不多说直接上代码:1、mock相关配置(mock/index.js),这里仅使用 setupMiddlewares其余旧版级过渡版本方法见官网1//引入mock2constMock=require('mockjs');......
  • vscode左侧资源管理器字号大小修改(Mac版)
    本文以MAC版vsCode为例1、在应用程序中找到VisualStudioCode.app,选中,右键菜单中选择显示包内容。具体如下图所示2、进入包内容后,按照如下文件名路径,找到workbench.des......
  • 深度解读Webpack中的loader原理
    一、前言webpack是一个现代JavaScript应用的静态模块打包器。那么webpack是怎样实现不同种类资源模块加载的呢?没错就是通过loader。loader用于对模块的源代码进行......
  • Webpack中的plugin插件机制
    大家有没有遇到过这些问题:webpack打包之后的文件没有压缩静态文件要手动拷贝到输出目录代码中写了很多环境判断的多余代码上一篇「webpack核心特性」loader说到......