creevey
Version:
Cross-browser screenshot testing tool for Storybook with fancy UI Runner
88 lines (68 loc) • 3.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var _path = _interopRequireDefault(require("path"));
var _helpers = require("./helpers");
var _logger = require("../../logger");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
let lastStoryFile = null;
const stories = new Set();
const reexportedStories = new Map();
function _default() {
return {
pre() {
var _this$opts$parents;
const parents = (_this$opts$parents = this.opts.parents()) !== null && _this$opts$parents !== void 0 ? _this$opts$parents : [];
const story = this.opts.story();
this.resourcePath = this.filename;
this.fileType = _helpers.FileType.Invalid; // eslint-disable-next-line @typescript-eslint/no-explicit-any
this.visitedTopPaths = new Set();
this.visitedBindings = new Set();
this.reexportedStories = reexportedStories;
if (story && this.filename.startsWith(story) || parents.find(parent => this.reexportedStories.has(parent))) {
this.fileType = _helpers.FileType.Story;
this.isMDX = _path.default.parse(this.filename).ext == '.mdx';
lastStoryFile = this.filename;
stories.add(this.filename);
} else if (this.filename.startsWith(this.opts.preview)) this.fileType = _helpers.FileType.Preview;else if (lastStoryFile && this.opts.debug) {
_logger.logger.warn('Trying to transform possible non-story file', this.resourcePath, 'Please check the', lastStoryFile);
lastStoryFile = null; // TODO Add link to docs, how creevey works and what user should do in this situation
}
},
visitor: { ..._helpers.commonVisitor,
ExportAllDeclaration(path) {
if (this.fileType == _helpers.FileType.Story && typeof _helpers.storyVisitor.ExportAllDeclaration == 'function') {
_helpers.storyVisitor.ExportAllDeclaration.call(this, path, this);
}
},
ExportDefaultDeclaration(path) {
if (this.fileType == _helpers.FileType.Story && typeof _helpers.storyVisitor.ExportDefaultDeclaration == 'function') {
_helpers.storyVisitor.ExportDefaultDeclaration.call(this, path, this);
}
},
ExportNamedDeclaration(path) {
if (this.fileType == _helpers.FileType.Preview && typeof _helpers.previewVisitor.ExportNamedDeclaration == 'function') {
_helpers.previewVisitor.ExportNamedDeclaration.call(this, path, this);
}
if (this.fileType == _helpers.FileType.Story && typeof _helpers.storyVisitor.ExportNamedDeclaration == 'function') {
_helpers.storyVisitor.ExportNamedDeclaration.call(this, path, this);
}
},
CallExpression(path) {
if (this.fileType == _helpers.FileType.Preview && typeof _helpers.previewVisitor.CallExpression == 'function') {
_helpers.previewVisitor.CallExpression.call(this, path, this);
}
if (this.fileType == _helpers.FileType.Story && typeof _helpers.storyVisitor.CallExpression == 'function') {
_helpers.storyVisitor.CallExpression.call(this, path, this);
}
},
FunctionDeclaration(path) {
if (this.isMDX && typeof _helpers.mdxVisitor.FunctionDeclaration == 'function') {
_helpers.mdxVisitor.FunctionDeclaration.call(this, path, this);
}
}
}
};
}