babel-loader 生成多处 /******/ 前缀

使用 babel-loader 处理 .js 文件之后会在公共文件里多处行首添加 /******/,不明白为什么要这样做。

难道仅仅是为了区别框架代码和用户代码?

/******/ (function(modules) { // webpackBootstrap
/******/    // install a JSONP callback for chunk loading
/******/    var parentJsonpFunction = window["webpackJsonp"];
/******/    window["webpackJsonp"] = function webpackJsonpCallback(chunkIds, moreModules) {
/******/        // add "moreModules" to the modules object,
/******/        // then flag all "chunkIds" as loaded and fire callback
/******/        var moduleId, chunkId, i = 0, callbacks = [];
/******/        for(;i < chunkIds.length; i++) {
/******/            chunkId = chunkIds[i];
/******/            if(installedChunks[chunkId])
/******/                callbacks.push.apply(callbacks, installedChunks[chunkId]);
/******/            installedChunks[chunkId] = 0;
/******/        }
/******/        for(moduleId in moreModules) {
/******/            modules[moduleId] = moreModules[moduleId];
/******/        }
/******/        if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules);
/******/        while(callbacks.length)
/******/            callbacks.shift().call(null, __webpack_require__);
/******/        if(moreModules[0]) {
/******/            installedModules[0] = 0;
/******/            return __webpack_require__(0);
/******/        }
/******/    };

目前应该没有参数可以控制移除,因为在文件 ./node_modules/webpack/lib/MainTemplate.js 中硬编码了这一段前缀

this.plugin("render", function(bootstrapSource, chunk, hash, moduleTemplate, dependencyTemplates) {
    var source = new ConcatSource();
    source.add("/******/ (function(modules) { // webpackBootstrap\n");
    source.add(new PrefixSource("/******/", bootstrapSource));
    source.add("/******/ })\n");
    source.add("/************************************************************************/\n");
    source.add("/******/ (");
    var modules = this.renderChunkModules(chunk, moduleTemplate, dependencyTemplates, "/******/ ");
    source.add(this.applyPluginsWaterfall("modules", modules, chunk, hash, moduleTemplate, dependencyTemplates));
    source.add(")");
    return source;
});

babel-loader 配置参考: API · Babel

PHP Bcrypt 更安全的密码加密机制

为了避免在服务器受到攻击,数据库被拖库时,用户的明文密码不被泄露,一般会对密码进行单向不可逆加密——哈希

常见的方式是:

哈希方式 加密密码
md5(‘123456’) e10adc3949ba59abbe56e057f20f883e
md5(‘123456’ . ($salt = ‘salt’)) 207acd61a3c1bd506d7e9a4535359f8a
sha1(‘123456’) 40位密文
hash(‘sha256’, ‘123456’) 64位密文
hash(‘sha512’, ‘123456’) 128位密文

密文越长,在相同机器上,进行撞库消耗的时间越长,相对越安全。

比较常见的哈希方式是 md5 + 盐,避免用户设置简单密码,被轻松破解。

password_hash

但是,现在要推荐的是 password_hash() 函数,可以轻松对密码实现加盐加密,而且几乎不能破解。

$password = '123456';

var_dump(password_hash($password, PASSWORD_DEFAULT));
var_dump(password_hash($password, PASSWORD_DEFAULT));

password_hash 生成的哈希长度是 PASSWORD_BCRYPT —— 60位,PASSWORD_DEFAULT —— 60位 ~ 255位。PASSWORD_DEFAULT 取值跟 php 版本有关系,会等于其他值,但不影响使用。

每一次 password_hash 运行结果都不一样,因此需要使用 password_verify 函数进行验证。

$password = '123456';

$hash = password_hash($password, PASSWORD_DEFAULT);
var_dump(password_verify($password, $hash));

password_hash 会把计算 hash 的所有参数都存储在 hash 结果中,可以使用 password_get_info 获取相关信息。

$password = '123456';
$hash = password_hash($password, PASSWORD_DEFAULT);
var_dump(password_get_info($hash));
输出
array(3) {
  ["algo"]=>
  int(1)
  ["algoName"]=>
  string(6) "bcrypt"
  ["options"]=>
  array(1) {
    ["cost"]=>
    int(10)
  }
}
注意不包含 salt

可以看出我当前版本的 PHP 使用 PASSWORD_DEFAULT 实际是使用 PASSWORD_BCRYPT。

password_hash($password, $algo, $options) 的第三个参数 $options 支持设置至少 22 位的 salt。但仍然强烈推荐使用 PHP 默认生成的 salt,不要主动设置 salt。

当要更新加密算法和加密选项时,可以通过 password_needs_rehash 判断是否需要重新加密,下面的代码是一段官方示例

$options = array('cost' => 11);
// Verify stored hash against plain-text password
if (password_verify($password, $hash))
{
    // Check if a newer hashing algorithm is available
    // or the cost has changed
    if (password_needs_rehash($hash, PASSWORD_DEFAULT, $options))
    {
        // If so, create a new hash, and replace the old one
        $newHash = password_hash($password, PASSWORD_DEFAULT, $options);
    }
    // Log user in
}

password_needs_rehash 可以理解为比较 $algo + $option 和 password_get_info($hash) 返回值。

password_hash 运算慢

password_hash 是出了名的运行慢,也就意味着在相同时间内,密码重试次数少,泄露风险降低。

$password = '123456';
var_dump(microtime(true));
var_dump(password_hash($password, PASSWORD_DEFAULT));
var_dump(microtime(true));

echo "\n";

var_dump(microtime(true));
var_dump(md5($password));
for ($i = 0; $i < 999; $i++)
{
    md5($password);
}
var_dump(microtime(true));
输出
float(1495594920.7034)
string(60) "$2y$10$9ZLvgzqmiZPEkYiIUchT6eUJqebekOAjFQO8/jW/Q6DMrmWNn0PDm"
float(1495594920.7818)

float(1495594920.7818)
string(32) "e10adc3949ba59abbe56e057f20f883e"
float(1495594920.7823)

password_hash 运行一次耗时 784 毫秒, md5 运行 1000 次耗时 5 毫秒。这是一个非常粗略的比较,跟运行机器有关,但也可以看出 password_hash 运行确实非常慢。

使用 JavaScript 对中文进行排序

在网页上展示列表时经常需要对列表进行排序:按照修改/访问时间排序、按照地区、按照名称排序。

对于中文列表按照名称排序就是按照拼音排序,不能简单通过字符串比较—— ‘a’ > ‘b’——这种方式来实现。

比如比较 ‘北京’ vs ‘上海’,实际是比较 ‘běijīng’ vs ‘shànghǎi’;比较 ‘北京’ vs ‘背景’,实际是比较 ‘běijīng’ vs ‘bèijǐng’。
一般需要获取到字符串的拼音,再比较各自的拼音。

JavaScript 提供本地化文字排序,比如对中文按照拼音排序,不需要程序显示比较字符串拼音。

String.prototype.localeCompare 在不考虑多音字的前提下,基本可以完美实现按照拼音排序。

在没有出现意外的情况下,各个支持 localeCompare 的浏览器都很正常。最近将 Chrome 更新到 58.0.3029.110,突然发现中文排序不正常。

// 正常应该返回 1, 拼音 jia 在前, kai 在后
'开'.localeCompare('驾');
// 得到
-1;

// Chrome 58.0.3029.110 下返回 -1, 其他浏览器正常

// 确认之后是 localeCompare 需要明确指定 locales 参数
'开'.localeCompare('驾', 'zh');
// 得到
1

在 Chrome 下传递 locales 参数才能获得正常预期结果

Edge 浏览器支持 localeCompare

Firefox 浏览器支持 localeCompare

IE 11 浏览器支持 localeCompare

其他浏览器对 localeCompare 支持也很友好,目前也不需要明确传递 locales,浏览器支持参考 developer.mozilla.org

使用CSS变量提高代码复用率

CSS 支持通过 ––variable-name: variable-value 声明变量,并通过 var(––variable-name) 使用变量。

CSS 使用变量示例

.parent {
    --color-red: red;
}

.child {
    color: var(--color-red);
}

Less 和 Sass 已分别使用 $ 和 @ 声明变量,CSS 则采用双划线声明变量。

Less / Sass 变量示例

/* Less 变量示例 */

@blue: #f938ab;

.blue {
    color: @blue;
}

/* Sass 变量示例 */

$blue: #1875e7;

.blue {
    color: $blue;
}

不同于 Less/Sass 在最外层声明, CSS Variable 需要在具体 selector 下声明变量,因此可以在子元素下覆盖父元素定义的变量

一般将 CSS Variable 定义在伪类 :root 下,让所有子元素都可以继承。

定义在伪类 :root 下

:root {
  --base-color: #333;
  --base-size: 14px;
}

.btn {
    color: var(--base-color);
    font-size: var(--base-size);
}

变量继承与重写

div {
    padding: 2px;
}

.one {
    --border: 1px solid red;
    border: var(--border);
}

.two {
    border: var(--border);
    --border: 2px solid green;
}

.three {
    border: var(--border);
}
<div class="one">
    1
    <div class="two">
        2-1
        <div class="three">3</div>
    </div>
    <div class="two">2-2</div>
</div>

解析到 .two selector 会优先解析 .two 拥有的变量,再应用。因此 .two 应用的 --border2px solid green
.three 也会应用 .two 定义的变量。

动态修改 CSS Variable

网页渲染完成之后,再修改变量值,会重新应用 CSS。

因为 val(variable-name) 返回的是 variable-value,所以当 variable-value 不包含单位是,某些样式需要明确指定单位。

:root {
    --line-height: 20;
}

p {
    line-height: var(--line-height)px;
}

这点不像 jQuery 自动检测浏览器设置的默认单位。需要显示设置单位

JavaScript Proxy 代理

Proxy 对象可以捕获源对象(对象、数组、函数)的操作,包括增删改查(获取、赋值、枚举、删除)以及函数调用,可以更加全面地控制源对象。

代理对象

// 源对象 必须是复杂数据类型,如 对象、数组、函数
var target = {age: 27};

var handler = {
    get: function(target, key) {
        // 代理 target[key]
        console.log('get');
        if (key in target) {
            return target[key];
        }
    },
    set: function(target, key, value) {
        // 代理 target[key] = value
        console.log('set');
        target[key] = value;
    },
    deleteProperty: function(target, key) {
        // 代理 delete target[key]
        console.log('deleteProperty');
        return key in target ? delete target[key] : false;
    },
    enumerate: function(target, key) {
        console.log('enumerate');
        return Object.keys(target);
    },
    ownKeys: function(target, key) {
        // 代理 Object.getOwnPropertyNames(target)
        // 代理 Object.getOwnPropertySymbols(target)
        console.log('ownKeys');
        return Object.keys(target);
    },
    has: function(target, key) {
        // 代理 key in target
        console.log('has');
        return key in target;
    },
    defineProperty: function(target, key, desc) {
        // 代理 Object.defineProperty(proxy, key, desc)
        console.log('defineProperty');
        Object.defineProperty(target, key, desc);
        return target;
    },
    getOwnPropertyDescriptor: function(target, key) {
        console.log('getOwnPropertyDescriptor');
        return key in target ? {
            value: target[key],
            writable: true,
            enumerable: false,
            configurable: true
        } : undefined;    
    }
};

// 新建代理
var proxy = new Proxy(target, handler);

proxy.age;

proxy.age = 28;

Object.defineProperty(proxy, 'name', {value: 'seven'});

proxy.name;
target.name;

Object.keys(proxy),会分别调用 handler.ownKeys 和 getOwnPropertyDescriptor,前者用于获取 target 的 key 列表,后者用于依次判断每个 key 是否可以迭代。
若 getOwnPropertyDescriptor 返回值得 enumerate 为 false,则该 key 不会出现在 Object.keys(proxy) 返回结果中。

Object.getOwnPropertyDescriptors(proxy) 和 Object.keys(proxy) 一样,会分别调用 handler.ownKeys 和 getOwnPropertyDescriptor,但不会根据 getOwnPropertyDescriptor 返回结果过滤 key。


代理数组

var target = [1, 2, 3];

var handler = {
    set: function(target, key, value) {
        // 代理 target[key] = value
        console.log('set');
        target[key] = value;
        // 代理数组 set,需要 返回源对象
        // 代理对象不需要
        return target;
    }
};

var proxy = new Proxy(target, handler);

proxy.push(4);
proxy.pop();
proxy.unshift(5);
proxy.shift();

代理数组 handler.set 需要返回源对象,其余与代理对象一致。


代理函数

var target = function (name) {
    this.name = name;

    this.sayHello = function() {
        return 'Hi, I am ' + this.name;
    };

    return 'Hi, I am ' + name;
};

var handler = {
    construct: function(target, args) {
        console.log('construct');
        var inst = Object.create(target.prototype);
        target.apply(inst, args);
        return inst;
    },
    apply: function(target, args)  {
        console.log('apply');
        return target.apply(target, args);
    }
};

var proxy = new Proxy(target, handler);

proxy();

var inst = new proxy('seven');
inst.sayHello();

代理函数可以设置 handler.construct 和 handler.apply,其余与代理对象一致。


创建可以撤销的代理

var target = {age: 27};

var handler = {
    get: function(target, key) {
        // 代理 target[key]
        console.log('get');
        if (key in target) {
            return target[key];
        }
    }
};

var revocable = Proxy.revocable(target, handler);
var proxy = revocable.proxy;

proxy.age;

// 执行撤销
revocable.revoke();

// 撤销之后再执行任何代理操作都会报错

// TypeError
proxy.age;

推荐HTTP站点使用CSP禁止网页被注入的未知JS脚本执行

无意间发现站点 ua.zhengxianjun.com 的 Network 中出现两条未知的 js 请求。

未知的 js 请求

这两个域名跟 ua.zhengxianjun.com 没有任何关系,查看源码发现网页被注入了一个 script 标签。

注入的 script 标签,来自 i.fcy6.cc

刚开始以为网站被黑了,可是黑我的小网站没意思呀。

在服务器上访问 ua.zhengxianjun.com 发现一切正常,感觉可能是被运营商注入了广告。

通过 curl 访问,显示正常

在 Network 中看到第二条脚本没有加载成功,可能是这个原因网页没有出现异常。试了多次也没出现广告。

网页被插入一个 script 标签,第1个创建了第2个

查了一下域名 fcy6.cc,但对方开启了隐私保护。

从另外一个域名 sho9wbox.com 的 whois 信息可以看到,它属于 veryci.com,跟电驴(verycd.com)非常接近。

veryci.com 官网大部分是图片,而且用词不清。最主要是启信宝上的公司地址跟官网的地址不一样。(如下图)

当然这些都不是重点。

重点是:推荐 HTTP 站点启用 CSP,尽量保证即使网页被注入脚本,也不会出现危害。

如何使用 CSP,参考 https://blog.zhengxianjun.com/2015/04/web-security-csp/

选择合适的字体区分 1Il、0Oo

为了能够清晰区分 1Il 0Oo、准确识别相似字符,测试了 70+ 种 Windows 系统下使用的字体,得到 3 种能清晰区分 1Il 0Oo 的字体,效果如下:

# 字体 1, i, l, 零和欧, 全角
1 Consolas 1iIl 0Oo0Oo ø
2 Tahoma 1iIl 0Oo0Oo ø
3 Verdana 1iIl 0Oo0Oo ø

 

在这 3 种字体中,Verdana 和 Tahoma 效果最好,Consolas 次之,在 Excel 中也常用到 Tahoma。

最终采取的方案是:

.input {
    font-family: Verdana, Tahoma, Consolas, monospace;
}

全部对比效果如下:

# 字体 1, i, l, 零和欧, 全角
1 Consolas 1iIl 0Oo0Oo ø
2 Tahoma 1iIl 0Oo0Oo ø
3 Verdana 1iIl 0Oo0Oo ø
4 ‘Angsana New’ 1iIl 0Oo0Oo ø
5 Arial 1iIl 0Oo0Oo ø
6 ‘Arial Black’ 1iIl 0Oo0Oo ø
7 Basemic 1iIl 0Oo0Oo ø
8 Batang 1iIl 0Oo0Oo ø
9 ‘Book Antiqua’ 1iIl 0Oo0Oo ø
10 ‘Browallia New’ 1iIl 0Oo0Oo ø
11 Calibri 1iIl 0Oo0Oo ø
12 Cambria 1iIl 0Oo0Oo ø
13 Candara 1iIl 0Oo0Oo ø
14 Century 1iIl 0Oo0Oo ø
15 ‘Comic Sans MS’ 1iIl 0Oo0Oo ø
16 Constantia 1iIl 0Oo0Oo ø
17 Corbel 1iIl 0Oo0Oo ø
18 ‘Cordia New’ 1iIl 0Oo0Oo ø
19 Courier 1iIl 0Oo0Oo ø
20 ‘Courier New’ 1iIl 0Oo0Oo ø
21 DilleniaUPC 1iIl 0Oo0Oo ø
22 Dotum 1iIl 0Oo0Oo ø
23 Garamond 1iIl 0Oo0Oo ø
24 Geneva 1iIl 0Oo0Oo ø
25 Georgia 1iIl 0Oo0Oo ø
26 Gulim 1iIl 0Oo0Oo ø
27 GungSuh 1iIl 0Oo0Oo ø
28 Impact 1iIl 0Oo0Oo ø
29 JasmineUPC 1iIl 0Oo0Oo ø
30 ‘Lucida Console’ 1iIl 0Oo0Oo ø
31 ‘Lucida Sans Unicode’ 1iIl 0Oo0Oo ø
32 ‘Malgun Gothic’ 1iIl 0Oo0Oo ø
33 Mangal 1iIl 0Oo0Oo ø
34 Marlett 1iIl 0Oo0Oo ø
35 Meiryo 1iIl 0Oo0Oo ø
36 ‘Microsoft JhengHei’ 1iIl 0Oo0Oo ø
37 MingLiu 1iIl 0Oo0Oo ø
38 MingLiU_HKSCS 1iIl 0Oo0Oo ø
39 monospace 1iIl 0Oo0Oo ø
40 Modern 1iIl 0Oo0Oo ø
41 ‘MS Gothic’ 1iIl 0Oo0Oo ø
42 ‘MS Mincho’ 1iIl 0Oo0Oo ø
43 ‘MS PGothic’ 1iIl 0Oo0Oo ø
44 ‘MS PMincho’ 1iIl 0Oo0Oo ø
45 ‘MS Sans Serif’ 1iIl 0Oo0Oo ø
46 ‘MS Serif’ 1iIl 0Oo0Oo ø
47 ‘Palatino Linotype’ 1iIl 0Oo0Oo ø
48 PMingliU 1iIl 0Oo0Oo ø
49 PMingLiU-ExtB 1iIl 0Oo0Oo ø
50 serif 1iIl 0Oo0Oo ø
51 Symbol 1iIl 0Oo0Oo ø
52 Times 1iIl 0Oo0Oo ø
53 ‘Times New Roman’ 1iIl 0Oo0Oo ø
54 ‘Trebuchet MS’ 1iIl 0Oo0Oo ø
55 Wingdings 1iIl 0Oo0Oo ø
56 ‘Yu Gothic’ 1iIl 0Oo0Oo ø
57 ‘Yu Mincho’ 1iIl 0Oo0Oo ø
58 方正舒体 1iIl 0Oo0Oo ø
59 方正姚体 1iIl 0Oo0Oo ø
60 仿宋 1iIl 0Oo0Oo ø
61 黑体 1iIl 0Oo0Oo ø
62 华文彩云 1iIl 0Oo0Oo ø
63 华文仿宋 1iIl 0Oo0Oo ø
64 华文行楷 1iIl 0Oo0Oo ø
65 华文琥珀 1iIl 0Oo0Oo ø
66 华文楷体 1iIl 0Oo0Oo ø
67 华文隶书 1iIl 0Oo0Oo ø
68 华文宋体 1iIl 0Oo0Oo ø
69 华文细黑 1iIl 0Oo0Oo ø
70 华文新魏 1iIl 0Oo0Oo ø
71 华文中宋 1iIl 0Oo0Oo ø
72 楷体 1iIl 0Oo0Oo ø
73 隶书 1iIl 0Oo0Oo ø
74 宋体 1iIl 0Oo0Oo ø
75 宋体-ExtB 1iIl 0Oo0Oo ø
76 微软雅黑 1iIl 0Oo0Oo ø
77 新宋体 1iIl 0Oo0Oo ø
78 幼圆 1iIl 0Oo0Oo ø

在 Firefox 下 bootstrap-datetimepicker 报错

日期插件 bootstrap-datetimepicker 在火狐下出现一条报错

TypeError: (intermediate value).toString(…).split(…)[1] is undefined

这条错误必然出现,难道没有在 Firefox 下进行测试。

在 Firefox 下查看项目 demo (http://www.malot.fr/bootstrap-datetimepicker/demo.php)可以正常运行,但这个 demo.php 使用的是 2013-3-2 的 datetimepicker,github 项目(https://github.com/smalot/bootstrap-datetimepicker/releases)已经发布到 2017-3-3,这个最新的版本(以及最近的一些版本)在 Firefox 下测试不完善,计算 defaultTimeZone 时虽然没有出错,但给出的结果也不正确

源代码如下,运行环境 Firefox 51.0.1(32位)

this.defaultTimeZone = (new Date).toString().split('(')[1].slice(0, -1);
this.timezone = options.timezone || this.defaultTimeZone;

// 2.4.4 改进版本
this.timezone = options.timezone || timeZoneAbbreviation();

function timeZoneAbbreviation() {
    var abbreviation, date, formattedStr, i, len, matchedStrings, ref, str;
    date = (new Date()).toString();
    formattedStr = ((ref = date.split('(')[1]) != null ? ref.slice(0, -1) : 0) || date.split(' ');
    if (formattedStr instanceof Array) {
        matchedStrings = [];
        for (var i = 0, len = formattedStr.length; i < len; i++) {
            str = formattedStr[i];
            if ((abbreviation = (ref = str.match(/\b[A-Z]+\b/)) !== null) ? ref[0] : 0) {
                matchedStrings.push(abbreviation);
            }
        }
        formattedStr = matchedStrings.pop();
    }
    return formattedStr;
}

出错原因是 Firefox 下 Date.prototype.toString 返回结果不包含 TimeZone 的文字描述。

2.4.4 改进版本使用的 timeZoneAbbreviation 函数在 Firefox 下返回  true


对 timeZoneAbbreviation 使用的三元表达式依次简化

  • ((abbreviation = (ref = str.match(/\b[A-Z]+\b/)) !== null) ? ref[0] : 0)
  • (abbreviation = (ref = str.match(/\b[A-Z]+\b/)) !== null)
  • (abbreviation = (xxx) !== null)
  • (abbreviation = xxx !== null)

abbreviation 必然是布尔值,如果将 matchedStrings.push(abbreviation) 换成 matchedStrings.push(str) 更接近预期值。

推荐使用文末的方案。


  • Firefox date toString 返回结果没有(中国标准时间)“, 因此 split('(')[1] 是 undefined

  • Chrome date toString 返回结果包含 “(中国标准时间)

  • IE/Edge date toString 返回结果包含 “(中国标准时间)

解决方案

将 date toString 最后一个空格之后的字符串作为 TimeZone。

// this.defaultTimeZone = (new Date).toString().split('(')[1].slice(0, -1);
this.defaultTimeZone = (new Date + '').split(' ').slice(-1)[0].replace(/\(|\)/g, '');
this.timezone = options.timezone || this.defaultTimeZone;

CSS 压缩 initial、normal

CSS 压缩在完成简单的字符替换,空白符压缩之后,还可以进一步进行特定 value 压缩。

比如:

/* 压缩前 */
.main{margin:initial}

/* 压缩后 */
.main{margin:0px}

/* 进一步压缩 */
.main{margin:0}

最后只需要占用一个字符就达到 initial 效果。

css initial 是一个特殊值,用于设置一些复杂、容易混淆的、难记忆的初始值非常有效。

/* 压缩前 */
.main{background:initial}

/* 压缩后 */
.main{background:rgba(0, 0, 0, 0) none repeat scroll 0% 0% / auto padding-box border-box}

对于这种真实样式比 initial 更长的样式可以不用压缩。

Chrome 下 css initial 对应真实值

在多个浏览器遍历设置元素 css 样式为 initial,再通过 getComputedStyle 获取真实样式,将其中相同的部分归纳到 initial 集合中,提供给压缩脚本使用,即可完成 css 特定 value 压缩。

同样可以压缩 css normal,使文件更小。

Webpack 打包 css,z-index 被重新计算

使用 Webpack 打包 css 文件,发现打包后的 z-index 值跟源文件 z-index 不一致。

如下图,左侧是源文件,右侧是打包后的文件:

即使加上 !important,经过 OptimizeCssAssetsPlugin 调用 cssProcessor cssnano 处理之后也是 z-index: 2

因此,很可能是 cssnano 进行了重新计算(cssnano 称为 rebase),而且这种计算是不够准确的

因为打包后的文件有两处 z-index,这里是第二处,所以此处 z-index 是 2。

cssnano 将 z-index rebase 归类为 unsafe,而不是 bug,只有在单个网页的 css 全部写入一个 css 文件,并且不通过 JavaScript 进行改动时是 safe。

参考:http://cssnano.co/optimisations/zindex/

项目中提取了公共的 css,已经对 layout 设置了很小的 z-index,因此受到 cssnano z-index rebase 的影响。

cssnano 默认进行 z-index rebase。

unsafe (potential bug) 优化项默认不开启应该比较友好。

new OptimizeCssAssetsPlugin({
    cssProcessor: require('cssnano'),
    cssProcessorOptions: {
        discardComments: {removeAll: true},
        // 避免 cssnano 重新计算 z-index
        safe: true
    },
    canPrint: false
})