This commit is contained in:
masnn committed 2020-04-08 21:43:41 +08:00
1 parent 9c45527522
commit 7dc31d02fa
15 files changed
+10521 -3

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
/* eslint-disable */
import yaml from 'js-yaml';
import PluginError from 'plugin-error';
import through from 'through2';
import path from 'path';
export default function generateLocales() {
function bufferContents(file, encoding, callback) {
if (file.isNull()) {
callback();
return;
}
if (file.isStream()) {
this.emit('error', new PluginError('gulpGenerateLocales', 'Stream not supported'));
callback();
return;
}
const doc = yaml.safeLoad(file.contents);
file.contents = Buffer.from(`window.LOCALES = ${JSON.stringify(doc, null, 2)}`);
file.path = path.join(
path.dirname(file.path),
`${path.basename(file.path, path.extname(file.path))}.js`
);
this.push(file);
callback();
}
return through.obj(bufferContents);
}
+35
View File
@@ -0,0 +1,35 @@
/* eslint-disable */
import PluginError from 'plugin-error';
import through from 'through2';
import fs from 'fs-extra';
export default function touch(mtime) {
async function touchFile(file) {
const fd = await fs.open(file.path, 'a');
try {
await fs.futimes(fd, mtime, mtime);
} finally {
await fs.close(fd);
}
}
function processStream(file, encoding, callback) {
if (file.isNull()) {
callback();
return;
}
if (file.isStream()) {
this.emit('error', new PluginError('gulpTouch', 'Stream not supported'));
callback();
return;
}
touchFile(file)
.catch(err => this.emit('error', err))
.then(() => {
this.push(file);
callback();
});
}
return through.obj(processStream);
}
@@ -0,0 +1,16 @@
/* eslint-disable */
export default class DummyOutputPlugin {
constructor(file) {
this.file = file;
}
apply(compiler) {
compiler.plugin('emit', (compilation, callback) => {
compilation.assets[this.file] = {
source: () => '',
size: () => 0,
};
callback();
});
}
}
@@ -0,0 +1,50 @@
/* eslint-disable */
const _ = require('lodash');
const crypto = require('crypto');
export default class StaticManifestPlugin {
constructor({ fileName, ignore }) {
this.fileName = fileName;
this.ignore = ignore;
}
apply(compiler) {
compiler.plugin('emit', (compilation, callback) => {
const stats = compilation.getStats().toJson();
const manifest = _(stats.assets)
.map((asset) => {
const { name } = asset;
// Skip files listed in ignore
if (_.some(this.ignore, ignorePattern => name.indexOf(ignorePattern) > -1)) {
return null;
}
// Skip calculating hash for names like ?xxxx
if (name.indexOf('?') > -1) {
return [
name.replace(/\?[\s\S]*/, ''), // origin name
name, // name with hash
];
}
// We need to calculate hash for the remaining files.
const source = compilation.assets[asset.name].source();
const shasum = crypto.createHash('sha1');
shasum.update(source);
const hash = shasum.digest('hex').substr(0, 10);
return [
name, // origin name
`${name}?${hash}`, // name with hash
];
})
.filter()
.orderBy(['0'])
.fromPairs()
.value();
const json = JSON.stringify(manifest, null, 2);
compilation.assets[this.fileName] = {
source: () => json,
size: () => json.length,
};
callback();
});
}
}