@badeball/cypress-cucumber-preprocessor
Version:
[](https://github.com/badeball/cypress-cucumber-preprocessor/actions/workflows/build.yml) [ • 5.2 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createEsbuildPlugin = createEsbuildPlugin;
const promises_1 = __importDefault(require("node:fs/promises"));
const node_path_1 = __importDefault(require("node:path"));
const template_1 = require("../template");
const assertions_1 = require("../helpers/assertions");
const debug_1 = __importDefault(require("../helpers/debug"));
const debug = debug_1.default.extend("esbuild");
const toPosix = (location) => node_path_1.default.sep === "\\" ? location.replaceAll("\\", "/") : location;
function createEsbuildPlugin(configuration, options = { prettySourceMap: false }) {
return {
name: "feature",
setup(build) {
if (options.prettySourceMap) {
build.initialOptions.sourcemap = "external";
build.onEnd(async () => {
var _a;
const outfile = (0, assertions_1.ensure)(build.initialOptions.outfile, "Expected an outfile");
const sourceMapLocation = outfile + ".map";
const sourceMap = JSON.parse((await promises_1.default.readFile(sourceMapLocation)).toString());
const lastSource = sourceMap.sources[sourceMap.sources.length - 1];
// The sources property contains posix paths, even on windows.
const startsWith = toPosix(node_path_1.default.relative(node_path_1.default.dirname(outfile), configuration.projectRoot));
const needPrettify = (_a = lastSource === null || lastSource === void 0 ? void 0 : lastSource.startsWith(startsWith)) !== null && _a !== void 0 ? _a : false;
debug("startsWith", startsWith);
debug("last source", lastSource);
debug("project root", configuration.projectRoot);
debug("outfile", outfile);
debug("sources before", sourceMap.sources.slice(-5));
/**
* There are numerous issues regarding the sources property in esbuild, particularly when
* different drives are involved (which is the case on Github actions, when using
* Windows).
*
* - https://github.com/evanw/esbuild/issues/3460
* - https://github.com/evanw/esbuild/issues/3183
* - https://github.com/evanw/esbuild/issues/2595
* - https://github.com/evanw/esbuild/issues/2218
* - https://github.com/evanw/esbuild/issues/1699
* - https://github.com/evanw/esbuild/pull/1234
*
* I originally thought that my issue (#2218) was the only thing standing in the way, but
* it turns out there's more. The stack traces simply aren't going to be good when
* bundling using esbuild. However, that doesn't matter for the purpose of the usage
* reporter - only the last sources, IE. the one pointing to the users project need
* to be correct.
*/
if (needPrettify) {
debug("esbuild: prettifying sources");
sourceMap.sources = sourceMap.sources.map((source) => {
return node_path_1.default.relative(configuration.projectRoot, node_path_1.default.normalize(node_path_1.default.join(node_path_1.default.dirname(outfile), source)));
});
}
else {
debug("esbuild: using original sources");
}
debug("sources after", sourceMap.sources.slice(-5));
await promises_1.default.rm(sourceMapLocation);
const encoded = Buffer.from(JSON.stringify(sourceMap)).toString("base64");
/**
* Why `${"sourceMappingURL"}` you may ask. This is so esbuild doesn't crap itself upon
* errors, where it would search for source maps and find THIS code line, which is not a
* valid source map (obviously).
*
* Without this, esbuild would error with "Unexpected token z in JSON at position 0" every
* time an error occurred during build time.
*/
await promises_1.default.appendFile(outfile, `//# ${"sourceMappingURL"}=data:application/json;base64,${encoded}\n`);
});
}
build.onLoad({ filter: /\.feature$/ }, async (args) => {
const content = await promises_1.default.readFile(args.path, "utf8");
return {
contents: await (0, template_1.compile)(configuration, content, args.path),
loader: "js",
};
});
},
};
}
exports.default = createEsbuildPlugin;