webpack 總結(寫了個開頭 想想還是不寫了留著官網和別人部落格地址)

來源:互聯網
上載者:User

標籤:icp   env   iss   hint   long   resolved   reset   host   ges   

   關於webpack的資料大體分3處 ,一處是官網(純英文,資料比較多),一處是中文網(不建議,就如不知道誰說的一句話,講的是1+1,而實際用的卻是微積分難度),還有各大部落格,知識點零散,無系統知識架構,所以只好慢慢根據官網總結。
設定檔config:可以自己寫也可以預設的webpack init產生
配合npm 使用 
  "scripts": {    "build": "webpack"  },可以用“--”傳自訂參數進去,請大膽使用es2015裡面的import/export
var path = require(‘path‘);module.exports = {  entry: ‘./app/index.js‘,  output: {    filename: ‘bundle.js‘,    path: path.resolve(__dirname, ‘dist‘)  第一個參數是模組絕對路徑  }};
當運行build命令之後就會在當前路徑的基礎上,參照dist組建檔案
安裝:
npm install webpack –g 至於全域和局部安裝問題,一個是在npm的安裝目錄下 一個是在項目node_modules下,但是全域安裝後還是需要局部安裝,因為需要用到require,require無法引入全域安裝的模組
"scripts": {    "start": "webpack --config mywebpack.config.js"}
 
下面是官方給的配置例子
var path = require(‘path‘);module.exports = {   entry: "./app/entry", //入口 
   output: {      path: path.resolve(__dirname, "dist"), // string輸出的目錄       filename: "bundle.js", // string 輸出的名字    publicPath: "/assets/", // string 靜態資源目錄    library: "MyLibrary", // string,    // the name of the exported library    libraryTarget: "umd", // universal module definition    // the type of the exported library    /* Advanced output configuration (click to show) */  },  module: {      rules: [      // rules for modules (configure loaders, parser options, etc.)      {        test: /\.jsx?$/,        include: [          path.resolve(__dirname, "app")        ],        exclude: [          path.resolve(__dirname, "app/demo-files")        ],        // these are matching conditions, each accepting a regular expression or string        // test and include have the same behavior, both must be matched        // exclude must not be matched (takes preferrence over test and include)        // Best practices:        // - Use RegExp only in test and for filename matching        // - Use arrays of absolute paths in include and exclude        // - Try to avoid exclude and prefer include        issuer: { test, include, exclude },        // conditions for the issuer (the origin of the import)        enforce: "pre",        enforce: "post",        // flags to apply these rules, even if they are overridden (advanced option)        loader: "babel-loader",        // the loader which should be applied, it‘ll be resolved relative to the context        // -loader suffix is no longer optional in webpack2 for clarity reasons        // see webpack 1 upgrade guide        options: {          presets: ["es2015"]        },        // options for the loader      },      {        test: "\.html$",        use: [          // apply multiple loaders and options          "htmllint-loader",          {            loader: "html-loader",            options: {              /* ... */            }          }        ]      },      { oneOf: [ /* rules */ ] },      // only use one of these nested rules      { rules: [ /* rules */ ] },      // use all of these nested rules (combine with conditions to be useful)      { resource: { and: [ /* conditions */ ] } },      // matches only if all conditions are matched      { resource: { or: [ /* conditions */ ] } },      { resource: [ /* conditions */ ] }      // matches if any condition is matched (default for arrays)      { resource: { not: /* condition */ } }      // matches if the condition is not matched    ],    /* Advanced module configuration (click to show) */  },  resolve: {    // options for resolving module requests    // (does not apply to resolving to loaders)    modules: [      "node_modules",      path.resolve(__dirname, "app")    ],    // directories where to look for modules    extensions: [".js", ".json", ".jsx", ".css"],    // extensions that are used    alias: {      // a list of module name aliases      "module": "new-module",      // alias "module" -> "new-module" and "module/path/file" -> "new-module/path/file"      "only-module$": "new-module",      // alias "only-module" -> "new-module", but not "module/path/file" -> "new-module/path/file"      "module": path.resolve(__dirname, "app/third/module.js"),      // alias "module" -> "./app/third/module.js" and "module/file" results in error      // modules aliases are imported relative to the current context    },    /* alternative alias syntax (click to show) */    /* Advanced resolve configuration (click to show) */  },  performance: {    hints: "warning", // enum    maxAssetSize: 200000, // int (in bytes),    maxEntrypointSize: 400000, // int (in bytes)    assetFilter: function(assetFilename) {       // Function predicate that provides asset filenames      return assetFilename.endsWith(‘.css‘) || assetFilename.endsWith(‘.js‘);    }  },  devtool: "source-map", // enum  // enhance debugging by adding meta info for the browser devtools  // source-map most detailed at the expense of build speed.  context: __dirname, // string (absolute path!)  // the home directory for webpack  // the entry and module.rules.loader option  //   is resolved relative to this directory  target: "web", // enum  // the environment in which the bundle should run  // changes chunk loading behavior and available modules  externals: ["react", /^@angular\//],  // Don‘t follow/bundle these modules, but request them at runtime from the environment  stats: "errors-only",  // lets you precisely control what bundle information gets displayed  devServer: {    proxy: { // proxy URLs to backend development server      ‘/api‘: ‘http://localhost:3000‘    },    contentBase: path.join(__dirname, ‘public‘), // boolean | string | array, static file location    compress: true, // enable gzip compression    historyApiFallback: true, // true for index.html upon 404, object for multiple paths    hot: true, // hot module replacement. Depends on HotModuleReplacementPlugin    https: false, // true for self-signed, object for cert authority    noInfo: true, // only errors & warns on hot reload    // ...  },  plugins: [    // ...  ],  // list of additional plugins  /* Advanced configuration (click to show) */}
 基於vue官方腳手架教程:別人部落格
http://donglegend.com/2016/08/25/webpack%E9%82%A3%E4%BA%9B%E4%BA%8B%E5%84%BF04/#

webpack 總結(寫了個開頭 想想還是不寫了留著官網和別人部落格地址)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.