search
HomeWeb Front-endCSS TutorialUsage of css file separation plug-in (extract-text-webpack-plugin)

本篇文章给大家带来的内容是关于css文件实现分离的插件(extract-text-webpack-plugin)的用法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

css分离:extract-text-webpack-plugin

1、安装该插件:

for webpack 1

npm install –save-dev extract-text-webpack-plugin@1.0.1

for webpack 2

npm install –save-dev extract-text-webpack-plugin@2.1.2

for webpack 3

npm install –save-dev extract-text-webpack-plugin

for webpack 4

npm i extract-text-webpack-plugin@next -D

2、在webpack-config.js中引入插件
 const extractTextPlugin=require(“extract-text-webpack-plugin”);

3、配置plugins:这里new一下这个对象,与上面那个配置插件用逗号分隔
 new extractTextPlugin(“/css/index,.css”)

4、这里的/css/index.css是分离后的路径位置。这部配置完成后,包装代码:还要修改原来我们的style-loader和css-loader

    • [x] 修改代码如下:

      module:{        rules: [
                  {
                    test: /\.css$/,
                    use: extractTextPlugin.extract({
                      fallback: "style-loader",
                      use: "css-loader"
                    })
                  },{               test:/\.(png|jpg|gif)/ ,
                     use:[{
                         loader:'url-loader',
                         options:{
                             limit:500000
                         }
                     }]
                  }
                ]
          },

      5、使用webpack进行打包

    • publicPath:是在webpack.config.js文件的output选项中,主要作用就是处理静态文件路径的

    • 在处理前需要在webpack.config.js上方声明一个website对象

    • 注意:这里的IP和端口是本机的ip或者是你devServer配置的IP和端口    //==publicPath里面的内内容一定要写正确:用ipconfig查看电脑的ip地址,然后冒号后面跟自己设置的端口==

    • 注意:虽然把css文件分离出来了,但是css路径不对

    • 用==publishPath==来解决

      var website={
          publicPath:"http://192.168.1.108:1717"}

      6、在output选项中引用这个对象的publicPath属性

      //出口文件的配置项output:{    //输出的路径,用了Node语法
          path:path.resolve(__dirname,'dist'),    //输出的文件名称
          filename:'[name].js',
          publicPath:website.publicPath
      },

      7、使用webpack进行打包,这时原来的相对路径就会变为绝对路径(绝对路径速度会更快)

    *若出现下列错误,说明ip没写对

    > y@1.0.0 server F:\webLearn\webpackLearn
    > webpack-dev-server
    
    events.js:183
          throw er; // Unhandled 'error' event
          ^
    
    Error: listen EADDRNOTAVAIL 10.212.109.18:8087
        at Object._errnoException (util.js:992:11)    at _exceptionWithHostPort (util.js:1014:20)    at Server.setupListenHandle [as _listen2] (net.js:1338:19)    at listenInCluster (net.js:1396:12)    at doListen (net.js:1505:7)    at _combinedTickCallback (internal/process/next_tick.js:141:11)    at process._tickCallback (internal/process/next_tick.js:180:9)    at Function.Module.runMain (module.js:695:11)    at startup (bootstrap_node.js:191:16)    at bootstrap_node.js:612:3npm ERR! code ELIFECYCLE
    npm ERR! errno 1npm ERR! y@1.0.0 server: `webpack-dev-server`
    npm ERR! Exit status 1npm ERR!
    npm ERR! Failed at the y@1.0.0 server script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    npm ERR! A complete log of this run can be found in:
    npm ERR!     C:\Users\勾丽娜\AppData\Roaming\npm-cache\_logs\2018-07-11T07_46_12_914Z-debug.logPS F:\webLearn\webpackLearn>

    修改好正确的ip地址就可以运行成功了,哈哈

    相关推荐:

    css文件如何进行打包?css文件打包的方法

    html文件如何打包 ?html文件打包的方法介绍

    The above is the detailed content of Usage of css file separation plug-in (extract-text-webpack-plugin). For more information, please follow other related articles on the PHP Chinese website!

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Weekly Platform News: Web Apps in Galaxy Store, Tappable Stories, CSS SubgridWeekly Platform News: Web Apps in Galaxy Store, Tappable Stories, CSS SubgridApr 14, 2025 am 11:20 AM

    In this week's roundup: Firefox gains locksmith-like powers, Samsung's Galaxy Store starts supporting Progressive Web Apps, CSS Subgrid is shipping in Firefox

    Weekly Platform News: Internet Explorer Mode, Speed Report in Search Console, Restricting Notification PromptsWeekly Platform News: Internet Explorer Mode, Speed Report in Search Console, Restricting Notification PromptsApr 14, 2025 am 11:15 AM

    In this week's roundup: Internet Explorer finds its way into Edge, Google Search Console touts a new speed report, and Firefox gives Facebook's notification

    The Power (and Fun) of Scope with CSS Custom PropertiesThe Power (and Fun) of Scope with CSS Custom PropertiesApr 14, 2025 am 11:11 AM

    You’re probably already at least a little familiar with CSS variables. If not, here’s a two-second overview: they are really called custom properties, you set

    We Are ProgrammersWe Are ProgrammersApr 14, 2025 am 11:04 AM

    Building websites is programming. Writing HTML and CSS is programming. I am a programmer, and if you're here, reading CSS-Tricks, chances are you're a

    How Do You Remove Unused CSS From a Site?How Do You Remove Unused CSS From a Site?Apr 14, 2025 am 10:59 AM

    Here's what I'd like you to know upfront: this is a hard problem. If you've landed here because you're hoping to be pointed at a tool you can run that tells

    An Introduction to the Picture-in-Picture Web APIAn Introduction to the Picture-in-Picture Web APIApr 14, 2025 am 10:57 AM

    Picture-in-Picture made its first appearance on the web in the Safari browser with the release of macOS Sierra in 2016. It made it possible for a user to pop

    Ways to Organize and Prepare Images for a Blur-Up Effect Using GatsbyWays to Organize and Prepare Images for a Blur-Up Effect Using GatsbyApr 14, 2025 am 10:56 AM

    Gatsby does a great job processing and handling images. For example, it helps you save time with image optimization because you don’t have to manually

    Oh Hey, Padding Percentage is Based on the Parent Element's WidthOh Hey, Padding Percentage is Based on the Parent Element's WidthApr 14, 2025 am 10:55 AM

    I learned something about percentage-based (%) padding today that I had totally wrong in my head! I always thought that percentage padding was based on the

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.