forked from badeball/cypress-cucumber-preprocessor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browserify.ts
97 lines (78 loc) · 2.13 KB
/
browserify.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { PassThrough, Transform, TransformCallback } from "stream";
import { EventEmitter } from "events";
import browserify from "@cypress/browserify-preprocessor";
import { ICypressConfiguration } from "@badeball/cypress-configuration";
import debug from "./lib/debug";
import { compile } from "./lib/template";
export function transform(
configuration: ICypressConfiguration,
filepath: string
) {
if (!filepath.match(".feature$")) {
return new PassThrough();
}
debug(`compiling ${filepath}`);
let buffer = Buffer.alloc(0);
return new Transform({
transform(chunk, encoding, done) {
buffer = Buffer.concat([buffer, chunk]);
done();
},
async flush(done: TransformCallback) {
try {
done(
null,
await compile(configuration, buffer.toString("utf8"), filepath)
);
debug(`compiled ${filepath}`);
} catch (e: any) {
done(e);
}
},
});
}
// https://docs.cypress.io/api/plugins/preprocessors-api.html#File-object
type ICypressPreprocessorFile = EventEmitter & {
filePath: string;
outputPath: string;
shouldWatch: boolean;
};
function preprendTransformerToOptions(
configuration: ICypressConfiguration,
options: any
) {
let wrappedTransform;
if (
!options.browserifyOptions ||
!Array.isArray(options.browserifyOptions.transform)
) {
wrappedTransform = [transform.bind(null, configuration)];
} else {
wrappedTransform = [
transform.bind(null, configuration),
...options.browserifyOptions.transform,
];
}
return {
...options,
browserifyOptions: {
...(options.browserifyOptions || {}),
transform: wrappedTransform,
},
};
}
export function preprocessor(
configuration: ICypressConfiguration,
options = browserify.defaultOptions,
{ prependTransform = true }: { prependTransform?: boolean } = {}
) {
if (prependTransform) {
options = preprendTransformerToOptions(configuration, options);
}
return function (file: ICypressPreprocessorFile) {
return browserify(options)(file);
};
}
export { ICypressConfiguration };
export { compile };
export default preprocessor;