@areslabs/alita-core
Version:
alita-core
79 lines (60 loc) • 3.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _cacheImageInfos = require("../util/cacheImageInfos");
var npath = _interopRequireWildcard(require("path"));
var fse = _interopRequireWildcard(require("fs-extra"));
var _configure = _interopRequireDefault(require("../configure"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* //TODO 改为webpack打包之后,是否还需要手动copoy 图片文件??
*/
class ExtractImageFilesPlugin {
apply(compiler) {
compiler.hooks.done.tap('ExtractImageFilesPlugin', () => {
const {
usedImages,
outFiles
} = (0, _cacheImageInfos.getImageInfos)();
const usedImageArr = Array.from(usedImages);
for (let i = 0; i < usedImageArr.length; i++) {
const imagePath = usedImageArr[i];
if (outFiles[imagePath]) {
continue;
}
const realPath = getRealImagePath(imagePath);
const targetPath = imagePath.replace(_configure.default.inputFullpath, _configure.default.outputFullpath).replace('node_modules', 'npm');
fse.copySync(realPath, targetPath);
outFiles[imagePath] = targetPath;
} // TODO 图片的移除??
/*const fileKeys = Object.keys(outFiles)
console.log('usedImages:', usedImages)
for(let i = 0; i < fileKeys.length; i ++ ) {
const fk = fileKeys[i]
if (!usedImages.has(fk)) {
console.log('fk:', fk)
fse.removeSync(outFiles[fk])
delete outFiles[fk]
}
}
resetUsedImages()*/
});
}
}
exports.default = ExtractImageFilesPlugin;
function getRealImagePath(imagePath) {
let sourcePath = null;
const extname = npath.extname(imagePath);
if (fse.existsSync(imagePath.replace(extname, `@3x${extname}`))) {
sourcePath = imagePath.replace(extname, `@3x${extname}`);
} else if (fse.existsSync(imagePath.replace(extname, `@2x${extname}`))) {
sourcePath = imagePath.replace(extname, `@2x${extname}`);
} else {
sourcePath = imagePath;
}
return sourcePath;
}