溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

css文件如何實現(xiàn)分離的插件

發(fā)布時間:2020-10-13 15:12:34 來源:億速云 閱讀:210 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了css文件如何實現(xiàn)分離的插件,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

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選項中,主要作用就是處理靜態(tài)文件路徑的

    • 在處理前需要在webpack.config.js上方聲明一個website對象

    • 注意:這里的IP和端口是本機的ip或者是你devServer配置的IP和端口    //==publicPath里面的內(nèi)內(nèi)容一定要寫正確:用ipconfig查看電腦的ip地址,然后冒號后面跟自己設(shè)置的端口==

    • 注意:雖然把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進行打包,這時原來的相對路徑就會變?yōu)榻^對路徑(絕對路徑速度會更快)

*若出現(xiàn)下列錯誤,說明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文件如何實現(xiàn)分離的插件內(nèi)容對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI