@vue/cli-plugin-babel/index.js 中:


api.genCacheConfig('babel-loader', {}, [])

我们看一下 api.genCacheConfig 在文件:@vue/cli-service/lib/PluginAPI.js 中被定义:

Generate a cache identifier from a number of variables

在函数一开始接受 3 个参数:

  • id
  • partialIdentifier
  • configFiles

然后看一下函数的内部:

返回:

  • cacheIdentifier
  • cacheDirectory

genCacheConfig (id, partialIdentifier, configFiles) {
const cacheDirectory = this.resolve(`node_modules/.cache/${id}`)
const variables = {}
const cacheIdentifier = hash(variables)
return { cacheDirectory, cacheIdentifier }
}

我们打印了 cacheDirectory 目录,发现一个目录地址:


/Users/***/node_modules/.cache

我本地的有 4 个文件夹:

  • babel-loader
  • uglifyjs-webpack-plugin
  • eslint-loader
  • vue-loader

我们上面 cli-plugin-babel 就是指向了 babel-loader 的目录:

上面的 hash 用到了:


const hash = require('hash-sum')

/Users/***/node_modules/.cache/babel-loader

首先,babel-loader 是不具备去一个 .cache 目录写入文件的,那到底是谁呢?

还记得我们之前通过 vue inspect --rule js 打印的 babelwebpack 配置吗?


/* config.module.rule('js') */
{
test: /\.jsx?$/,
exclude: [
function () { /* omitted long function */ }
],
use: [
/* config.module.rule('js').use('cache-loader') */
{
loader: 'cache-loader',
options: {
cacheDirectory: '/Users/***/node_modules/.cache/babel-loader',
cacheIdentifier: '2f4347b9'
}
},
/* config.module.rule('js').use('babel-loader') */
{
loader: 'babel-loader'
}
]
}

这里面的 use 配置在 babel-loader 之前配置了一个 cache-loader


{
loader: 'cache-loader',
options: {
cacheDirectory: '/Users/***/node_modules/.cache/babel-loader',
cacheIdentifier: '2f4347b9'
}
}

cache-loader 到底做什么的呢:

Caches the result of following loaders on disk (default) or in the database

它的使用中有一个示例:

Add this loader in front of other (expensive) loaders to cache the result on disk.

一般它会放置在 use 配置里面,而且是其他 loaders 的前面:


module.exports = {
module: {
rules: [
{
test: /\.ext$/,
use: [
'cache-loader',
...loaders
],
include: path.resolve('src')
}
]
}
}

那其实结果就很清晰了,写文件的就是它:

一开始通过 Set 来创建一个对象,后面还使用了 addhas


var directories = new Set();

它有一个函数 write,接受 3 个参数:

  • key
  • data
  • callback

function write(key, data, callback) {
var dirname = path.dirname(key);
var content = JSON.stringify(data); if (directories.has(dirname)) {
// for performance skip creating directory
fs.writeFile(key, content, 'utf-8', callback);
} else {
mkdirp(dirname, function (mkdirErr) {
if (mkdirErr) {
callback(mkdirErr);
return;
} directories.add(dirname); fs.writeFile(key, content, 'utf-8', callback);
});
}
}

这里创建目录用到了:mkdirp 来创建目录


var mkdirp = require('mkdirp');

然后通过 fs.writeFile 来写文件


var fs = require('fs');
fs.writeFile(key, content, 'utf-8', callback);

来源:https://segmentfault.com/a/1190000016244353

[Vue CLI 3] 插件开发中的 genCacheConfig 细节研究的更多相关文章

  1. Vue CLI 3开发中屏蔽的EsLint错误 (.eslintrc.js 在vue3+中 修改这个)

    1.关闭eslint校验有了eslint的校验,可以来规范开发人员的代码,是挺好的.但是有些像缩进.空格.空白行之类的规范,在开发过程中一直报错,未免太过于苛刻了.所以,我还是会选择关闭eslint校 ...

  2. 关于Vue.cli 脚手架环境中引入Bootstrap时,table表格样式缺失的解决办法

    Vue+bootstrap不能正常使用table的样式 环境:下载官网的本地bootstrap包,然后在vue 的index.html引入bootstrap的css和js环境 问题描述:1. vue里 ...

  3. [Vue CLI 3] 插件开发之 registerCommand 到底做了什么

    首先,我们看到在 package.json 中有 scripts 的定义: "scripts": { "serve": "vue-cli-servic ...

  4. 专访Vue作者尤雨溪:Vue CLI 3.0重构的原因

    1.为什么要对 Vue CLI 进行大规模修改? 尤雨溪认为旧版本的 Vue CLI 本质上只是从 GitHub 拉取模版,这种拉模版的方式有几个问题: (1) 在单个模版里面同时支持太多选项会导致模 ...

  5. Vue Cli 中使用 Karma / Chrome 执行样式相关单元测试

    在 GearCase 开源项目 中,我使用了 Vue Cli 的默认测试框架.因此和样式相关的东西,都无法进行测试.因为它并不类似于无头浏览器,而是存在于虚拟内存之中. 现状 在如下 button.s ...

  6. Vue CLI 3 中文文档

    翻译文档 文档翻译全貌 前言 之前写了一篇Vue CLI 3.x 版本的简单体验,当时文档还不全,具体的使用方法并不是很清楚,大概是2月7号,收到Vue CLI 3接近Beta版的提示,作者尤雨溪也讲 ...

  7. vue cli 中关于vue.config.js中chainWebpack的配置

    Vue CLI  的官方文档上写:调整webpack配置最简单的方式就是在vue.config.js中的configureWebpack选项提供一个对象. Vue CLI 内部的 webpack 配置 ...

  8. vue cli中的env详解

    前言 相信使用过 vueCli 开发项目的小伙伴有点郁闷,正常开发时会有三个接口环境(开发,测试,正式),但是 vueCli 只提供了两种 development,production(不包含 tes ...

  9. vue cli 3

    介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统 通过 @vue/cli 搭建交互式的项目脚手架. 通过 @vue/cli + @vue/cli-service-global 快 ...

随机推荐

  1. vue 关闭微信浏览器(返回路由为undefined时)

    参考:https://blog.csdn.net/KingJin_CSDN_/article/details/77050569 main.js: import router from './route ...

  2. stylus实现边框1px

    //1px的实现 border($border-width = 1px, $border-color = #ccc, $border-style = solid, $radius = ) // 为边框 ...

  3. C++中如何实现像Java中接口功能--C++抽象类(纯虚函数,虚函数)

    在Java中定义个接口,之后可以定义不同的类来实现接口,如果有个函数的参数为这个接口的话,就可以对各自的类做出不同的响应. 如: interface animal { public void info ...

  4. 【2019云栖大会】这一场,我们好好聊聊5G和边缘计算

    一年一度的科技盛会杭州云栖大会Apsara Conference就要来了9月25-27日数万名开发者将齐聚杭州云栖小镇共同探索人类科技演进的脉搏聚焦面向未来的创新.热点技术话题 5G和边缘计算是201 ...

  5. mysql中not in子查询不能为空

    转载自:https://blog.csdn.net/headingalong/article/details/77744755 错误sql delete from company_info where ...

  6. css3之属性选择器

    总体来看一下都有哪些选择器 1.属性选择器  1)[att*=val] 2)[att^=val] 3)[att$=val] 2.结构伪类选择器 3.UI伪类选择器 其中E:read-only伪类选择器 ...

  7. tensorflow中张量的理解

    自己通过网上查询的有关张量的解释,稍作整理. TensorFlow用张量这种数据结构来表示所有的数据.你可以把一个张量想象成一个n维的数组或列表.一个张量有一个静态类型和动态类型的维数.张量可以在图中 ...

  8. 在PyCharm中导入Numpy和Pygame模块 (win8.1)

    我用的是anaconda安装python3.6 已经在终端 pip install numpy 但是在pycharm运行程序出现错误:ImportError: No module named nump ...

  9. Leetcode220. Contains Duplicate III存在重复元素3

    给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ. 示例 1: 输入: ...

  10. LC327 Count of Range Number

    这一题,我们使用了分治法. 首先看时间复杂度为o(n^2),比较naïve的方法: 使用一个数组sum[],长度为原数组长度+1,每一个元素sum[i]为原数组中index0~i-1之和,显然当sum ...