文章加密

;

2024年4月12日 星期五

webpack速成

https://www.youtube.com/watch?v=uP6KTupfyIw

https://github.com/scps960740/Webpack-crash-course-2021-bruceFE/blob/master/webpack.config.js

https://webpack.js.org/concepts/


To get started you only need to understand its Core Concepts:

  • Entry
  • Output
  • Loaders  // 處理閱讀非js的檔案(webpack 預設只讀懂js)



    babel用來協處處理最新的js可以被讀懂

    https://babeljs.io/setup#installation
    preset-env 是目前最新的意思


    (css 類似的如postcss)









    **https://webpack.js.org/guides/asset-modules/#root 







    Asset Modules allow one to use asset files (fonts, icons, etc) without configuring additional loaders.

    Prior to webpack 5 it was common to use:

    Asset Modules types replace all of these loaders by adding 4 new module types:

    • asset/resource emits a separate file and exports the URL. Previously achievable by using file-loader.
    • asset/inline exports a data URI of the asset. Previously achievable by using url-loader.
    • asset/source exports the source code of the asset. Previously achievable by using raw-loader.
    • asset automatically chooses between exporting a data URI and emitting a separate file. Previously achievable by using url-loader with asset size limit.

    When using the old assets loaders (i.e. file-loader/url-loader/raw-loader) along with Asset Modules in webpack 5, you might want to stop Asset Modules from processing your assets again as that would result in asset duplication. This can be done by setting the asset's module type to 'javascript/auto'.


  • Plugins 
    // 處理刪除檔案夾裡的檔案
    // HtmlWebpackPlugin: 在build出來的檔名加入動態hash後,自動動態產生相應的html等行為





    以./base.html內的template為模板產生新的html(我想vue或react等不會需要這樣)

另外MiniCssExtractPlugin是讓build出來的css從html裡獨立出來成自己一個檔案,可設定filename參數加入hash




        還有如CopyWebpackPlugin

**補充 devtool: 'source-map'可以讓build出來的檔案多出xxxx.map,這會在瀏覽器看source時可以看到非production被編譯過的版本的code,協助debug



build 會把檔案build出來


dev會起server,沒特別設置時抓的是當前目錄資料夾,如果有index.html就會顯示它,它裡面要載入剛build出來的output.js









沒有留言:

張貼留言