liaoyf的博客

一分耕耘一分收获


  • 首页

  • 标签

  • 归档

  • 关于

  • 搜索

未命名

发表于 2017-08-02 | 阅读次数

webpack学习02——代码分割

定义一个代码分割点

commonjs: require.ensure

require.ensure(dependencies, callback)

example:

//require.ensure仅仅加载而不执行模块
require.ensure(["module-a", "module-b"], function(require) {
    var a = require("module-a");
    // ...
});

AMD: require

require(dependencies, callback)

example:

//AMD的require加载并会执行模块
require(["module-a", "module-b"], function(a, b) {
    // ...
});

ES6模块

webpack1是不支持原生es6模块的,可以通过babal转换

块的内容

所有分隔的文件会成为一个块,这个块是由其依赖递归加进去的。

块压缩

如果两个块包含同一个模块,他们会被合并成一个。这可能造成块有相同的父级。
如果一个模块在一个块的所有父级中是可获取的,这个模块将会在这个块中删除。
如果一个块包含另一个块的所有模块,则存储这个块,它实现多个块。

块加载

根据配置target目标,将将块加载的运行时逻辑添加到包中。例如:web目标块通过jsonp加载。只有一个块被加载一次,并行请求被合并成一个。运行时检查加载的块是否完成多个块。

块的类型

入口块

一个入口块包含了请求时加载的模块。如果这个块包含模块0则加载并执行它,如果没有,它等待有请求模块0的块。

正常块

一个正常的块不会在运行时加载,它仅仅包含一些模块,这个结构依赖于块加载算法,例如:如果目标是jsonp,则这些模块会包含一个jsonp回调函数,这个块当然还包含它负责的一些块id列表。

初始化块(非入口)

一个初始化块是一个正常的块,不同的仅仅是压缩工具视它更重要,因为它计算向初始加载时间(像入口块),这个块类型可以结合CommonsChunkPlugin发生。

分隔app和vendor代码

var webpack = require("webpack");

module.exports = {
  entry: {
    app: "./app.js",
    vendor: ["jquery", "underscore", ...],
  },
  output: {
    filename: "bundle.js"
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin(/* chunkName= */"vendor", /* filename= */"vendor.bundle.js")
  ]
};

vendor块将会从移除所有在app块中的模块,使得bundle块仅仅包含你的代码而不包含其依赖。

<script src="vendor.bundle.js"></script>
<script src="bundle.js"></script>

多入口块

加载多入口

通过CommonsChukPlugin运行时被移动到公共块,入口文件变成初始化块。只有当初始块可以被加载时,其他入口块才可以被加载。

var webpack = require("webpack");
module.exports = {
    entry: { a: "./a", b: "./b" },
    output: { filename: "[name].js" },
    plugins: [ new webpack.optimize.CommonsChunkPlugin("init.js") ]
}

<script src="init.js"></script>
<script src="a.js"></script>
<script src="b.js"></script>

公共块

CommonsChunkPlugin可以移动发生在多个入口的模块到一个新的块(公共块),运行时也移动到公共块,这意味着旧的入口块已经变成初始化块。

压缩优化插件

  • LimitChunkCountPlugin
  • MinChunkSizePlugin
  • AggressiveMergingPlugin

命名块

The require.ensure function accepts an additional 3rd parameter. This must be a string. If two split point pass the same string they use the same chunk.

require.include

require.include(request)

require.include is a webpack specific function that adds a module to the current chunk, but doesn’t evaluate it (The statement is removed from the bundle).

Example:

require.ensure(["./file"], function(require) {
  require("./file2");
});

// is equal to

require.ensure([], function(require) {
  require.include("./file");
  require("./file2");
});

require.include can be useful if a module is in multiple child chunks. A require.include in the parent would include the module and the instances of the modules in the child chunks would disappear.

未命名

发表于 2017-08-02 | 阅读次数

webpack学习01——简介


webpack是一个模块打包工具,

  • webpack特性:
    • 插件:webpack有大量的插件,大多数的功能是使用这个接口的内部插件,这使得webpack很很大的灵活性;
    • 性能:webpack使用异步I/O和多级缓存机制,使得webpack有更快的速度,而且编辑速度超快;
    • 加载器(loaders):webpack通过定义loaders预处理文件,这允许你打包任何资源而不仅仅是javascript,你也可以自己写适合自己的加载器;
    • 模块化:webpack支持AMD和CommonJs模块化规范,它能够聪明地分析你的代码,甚至有一个评估引擎来评估简单的表达式,这个允许你支持大多数现存模块化规范
    • 代码分割:webpack允许你把代码分割成不同的块thunk,块是按需加载的,这个可以减少初始加载所发费的时间。
    • 代码压缩:webpack可以压缩你的代码,它当然也支持通过哈希缓存;
    • 开发工具:WebPACK支持调试简单sourceurls和sourcemaps。还可以通过development middleware和development server实现自动刷新功能;
    • 多个目标:webpack的首要目标是web,但是它也支持在WebWorkers、Node.js中使用;
  • 加载器:
    • 转换文件;
    • 加载器可以通过管道被链接,最后一个加载器则返回javascript,其他的加载器则可以返回任意的格式代码;
    • 加载器可以是同步或异步的;
    • 加载器在Node.js环境中运行,所以你可以做任何你想要的事情;
    • 加载器支持查询参数;
    • 加载器可以在配置中绑定到扩展名或正则表达式的监听;
    • 加载器可以被发布到npm上或从npm中安装下来;
    • Normal modules can export a loader in addition to the normal main via package.json loader.
    • 加载器可以访问配置;
    • 插件可以向加载器提供更多的特性和功能;
    • 加载器可以调用额外的任意文件;
    • 。。。
  • 加载器解决方案

    加载器类似模块,一个加载器导出一个兼容于Node的javascript函数,一般情况下,你通过npm管理你的加载器,当然你也可以作为文件在你的项目中使用

    • 引用加载器:加载器一般以xxx-loader命名,xxx是其名字,例如:json-loader;
    • 你可以通过全名xxx-loader或者短名xxx来使用加载器;
    • 加载器的命名惯例和优先顺序是由resolveLoader.moduleTemplates配置决定的;
    • 加载器的命名惯例一些情况下是很有用的,特别是在require()里面引用加载器的时候;

      加载器的使用情况:

    • 在require()语句内;

      //使用文件loader.js来转换file.txt文件
      require('./loader!./dir/file.txt');
      
      //使用jade-loader,如果配置文件中已经有绑定到此文件的加载器,他们仍然会运行的
      require('jade!./template.jade');
      
      //由less-loader->css-loader->style-loader转换顺序,由于有前缀`!`,所以如果在配置文件中已经
      //有绑定到此文件的加载器,他们将不会运行
      require('!style!css!less!bootstrap/less/bootstrap.less');    
      
    • 在配置文件中配置;(推荐这种方式)

      {
          module: {
              loaders: [
                  {test: /\.jade$/, loader: 'jade'},
                  {test: /\.css$/, loader: 'style!css'},
                  {test: /\.css$/, loaders: ['style', 'css']}
              ]
          }
      }        
      
    • 在CLI命令行中配置;

      webpack --module-bind jade --module-bind 'css=style!css'
      
  • 加载器的查询参数

    可以在加载器后面加上?添加多个查询参数,例如:url-loader?mimetype=image/png.大多数的加载器支持正常的格式?key=value&key2=value2和JSON对象格式?{key: "value", key2: "value2"}

    • 在require()语句中:

      require('url-loader?mimetype=image/png!./file.png');
      
    • 在配置中:

      {test: /\.png$/, loader: 'url-loader?mimetype=image/png'}
      

      或者

      {
          test: /\.png$/,
          loader: 'url-loader',
          query: {
              mimetype: 'image/png'
          }
      }
      
    • 在CLI中:

      webpack --module-bind "png=url-loader?mimetype=image/png"
      

使用css视口单位

发表于 2016-08-02 | 阅读次数

css提供了许多单位用来规定元素,最熟悉的莫属px、%、pt、em以及最近比较火的rem,还有其他两个vw、vh,它们是相对单位,但是不同于em和rem那样相对于当前元素或相对于根元素,他们是相对于视口,一个视口单位等于1%的视口的宽度(vw)或高度(vh)。

这是很有用的。vw单位可以用于一些有大小的规则(如:font-size、或者一个div的高度),下面是一些使用案例:

使标题固定

如果你想让一个标题占满横向屏幕且让他固定在一行,你可以使用vw单位,这实际上是用原生的方法实现了jquery插件FitText的功能,但是相对于使用vw,FitText需要手动管理大小,使用检查工具,是一个快速的方法以确定适当的值。

//html
<h1>Always a great fit!</h1>
//css
h1{
    font-size: 12vw;
    text-align: center;
}

Infinite Lines

Whilst building the falcon633 WordPress theme (used on this site), I needed to create an angled background that would appear to continue indefinitely. This is achievable by 1) making sure that the angle in the centre stays the same regardless of window size and 2) setting the height to be relative to the viewport width. I used an SVG background for the overall cut-out then set the height based on the width using vw units.

简单的视频包装

Let’s say you want to set the proportions of an element, an iframe, to stay at a fixed aspect ratio. You previously might have chosen to create a relative div filling the required space, then set carefully selected padding values with iframe inside absolutely positioned to cling to div on all sides (e.g. the approach demonstrated here).

A better solution could be to use the vw and vh units. This way you can set your height and width directly on the element in question, whilst also keeping the ‘layers-for-layout’ number down.

全屏的hero Image

你要做的只是在body和html上运用height: 100%,然后在元素上简单地使用width: 100vw; heihgt: 100vh就行了。

div居中

一个常见的需求是要让一个div在页面中居中,这时候只要设置margin: 20vh 20vw; width: 60vw, height: 60vh; padding: 10vh 10vw即可。

浏览器支持情况

IE9及以上

HTML不同空格的特性和表现研究

发表于 2016-08-02 | 阅读次数

Unicode编码空格

  • &nbsp;: 不换行空格,用于不会被浏览器判断为可以在中间打断,比如:There is&nbsp;Space,如果会换行,只会在There和is之间换行,而不会在is和Space之间换行。
  • 跟随字体大小产生相应空白的空格:&ensp;(1/2em),&emsp;(1em),&thinsp;(1/6em),这类就很适合坐表单label的排版了,坑爹,终于找到好的解决方法了

angular2遇到的问题.md

发表于 2016-08-02 | 阅读次数
  1. 在组件时moduleId: module.id时,报Cannot find name 'module'?

    解决方法:

    You need to install node ambientDependencies. Typings.json

    "ambientDependencies": {
        "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#138ad74b9e8e6c08af7633964962835add4c91e2"
    }
    

    Another way can use typings manager to install node definition file globally:

    typings install dt~node --global --save
    

    Then your typings.json file will look like this:

    "globalDependencies": {
          "node": "registry:dt/node#6.0.0+20160608110640"
    }
    

    If you use Typescript 2^ just use the following command:

    npm install @types/node --global --save
    

    Or install it within the project:

    npm install @types/node --save
    

    and then add a reference to it from your bootstrap:

    ///<reference path="../../node_modules/@types/node/index.d.ts" />
    
  2. 而后出现Url.match is not a function?

    解决方法:

  3. 报错:Property 'find' does not exist on type 'Hero[]'.?

    解决方法:在typings.json中配置如下代码

    {
          "globalDependencies": {
            "core-js": "registry:dt/core-js#0.0.0+20160725163759",
            "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
               "node": "registry:dt/node#6.0.0+20160909174046"
          }
    }
    
1…56
liaoyf

liaoyf

加油!

55 日志
23 标签
GitHub
© 2018 liaoyf
由 Hexo 强力驱动
主题 - NexT.Pisces