"build:file": "node build/bin/iconInit.js & node build/bin/build-entry.js & node build/bin/i18n.js & node build/bin/version.js"
node build/bin/i18n.js
// build/bin/i18n.js
'use strict';
var fs = require('fs');
var path = require('path');
var langConfig = require('../../examples/i18n/page.json'); // 语言配置文件
langConfig.forEach(lang => {
// 有目录读取目录信息,没有目录创建目录
try {
// 获取文件信息
fs.statSync(path.resolve(__dirname, `../../examples/pages/${ lang.lang }`)); // 同步提供有关文件信息
} catch (e) {
// 创建文件夹
fs.mkdirSync(path.resolve(__dirname, `../../examples/pages/${ lang.lang }`)); // 同步地创建目录
}
// 遍历写入文件
Object.keys(lang.pages).forEach(page => {
var templatePath = path.resolve(__dirname, `../../examples/pages/template/${ page }.tpl`);
var outputPath = path.resolve(__dirname, `../../examples/pages/${ lang.lang }/${ page }.vue`);
var content = fs.readFileSync(templatePath, 'utf8');
var pairs = lang.pages[page];
Object.keys(pairs).forEach(key => {
// 例如:匹配 <%= 1 %> 替换成展示内容
content = content.replace(new RegExp(`<%=\\s*${ key }\\s*>`, 'g'), pairs[key]);
});
// 将替换后的内容写入 outputPath 中
fs.writeFileSync(outputPath, content);
});
});
以上代码是国际化的过程,最终将会在
examples/pages/
目录中生成不同语言的内容。国际化具体内容请参照 国际化。pages里的组件都是页面级组件
node build/bin/version.js
// build/bin/version.js
var fs = require('fs');
var path = require('path');
var version = process.env.VERSION || require('../../package.json').version;
var content = { '1.4.13': '1.4', '2.0.11': '2.0', '2.1.0': '2.1', '2.2.2': '2.2', '2.3.9': '2.3', '2.4.11': '2.4', '2.5.4': '2.5', '2.6.3': '2.6', '2.7': '2.7.2' };
if (!content[version]) content[version] = '2.8';
// 写入版本号
fs.writeFileSync(path.resolve(__dirname, '../../examples/versions.json'), JSON.stringify(content));
一、学习 fs.statSync
- 先搭建环境
$ touch node-fs/index.js node-fs/1.js && mkdir node-fs/zh-CN
-
node-fs/index.js
文件内容:
const path = require('path');
const fs = require('fs');
// 同步获取 文件/目录信息
let dirStat = fs.statSync(path.resolve(__dirname, `./1.js`));
let fileStat = fs.statSync(path.resolve(__dirname, `./zh-CN`));
console.log('-----------------------------------------------------');
console.log('文件信息:fs.statSync(path)');
console.log(dirStat);
console.log('-----------------------------------------------------');
console.log('目录信息:fs.statSync(path)');
console.log(fileStat);
console.log('-----------------------------------------------------');
- 执行
node node-fs/index.js
输出:
-----------------------------------------------------
文件信息:fs.statSync(path)
Stats {
dev: 16777220,
mode: 33188,
nlink: 1,
uid: 1920153298,
gid: 672505530,
rdev: 0,
blksize: 4096,
ino: 12898608924,
size: 0,
blocks: 0,
atimeMs: 1559048984234.2095,
mtimeMs: 1559048984234.2095,
ctimeMs: 1559048984234.2095,
birthtimeMs: 1559048984234.2095,
atime: 2019-05-28T13:09:44.234Z,
mtime: 2019-05-28T13:09:44.234Z,
ctime: 2019-05-28T13:09:44.234Z,
birthtime: 2019-05-28T13:09:44.234Z }
-----------------------------------------------------
目录信息:fs.statSync(path)
Stats {
dev: 16777220,
mode: 16877,
nlink: 2,
uid: 1920153298,
gid: 672505530,
rdev: 0,
blksize: 4096,
ino: 12898608926,
size: 64,
blocks: 0,
atimeMs: 1559048984845.0688,
mtimeMs: 1559048984238.4143,
ctimeMs: 1559048984238.4143,
birthtimeMs: 1559048984238.4143,
atime: 2019-05-28T13:09:44.845Z,
mtime: 2019-05-28T13:09:44.238Z,
ctime: 2019-05-28T13:09:44.238Z,
birthtime: 2019-05-28T13:09:44.238Z }
-----------------------------------------------------
二、学习 fs.mkdirSync(path)
- 继续在
node-fs/index.js
文件中写入
fs.mkdirSync(path.resolve(__dirname, `./en-US`)); // 同步创建目录
- 执行
node index.js
发现在
node-fs
中多了一个en-US
目录
目录
- element源码学习一(package.json)
- element源码学习二(dev)
- element源码学习三(build:file)
- element源码学习四(build:file)
- element源码学习五(build:file)