Skip to content

Commit

Permalink
Avoid using path-browserify
Browse files Browse the repository at this point in the history
This is essentially unmaintained and broken [1].

[1] browserify/path-browserify#32
  • Loading branch information
badeball committed Sep 24, 2024
1 parent bcedb77 commit 3d4e42c
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 59 deletions.
8 changes: 6 additions & 2 deletions docs/source-maps.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ How to enable source maps for each bundler is shown below.

## esbuild

Source maps can be enabled using an optional argument to `createEsbuildPlugin()`, like seen below.
The process of ensuring that source maps are not only enabled, but also "pretty", is somewhat
cumbersome in lieu of [evanw/esbuild#2218](https://github.com/evanw/esbuild/issues/2218). Hence,
this is disabled by default until it has been sufficiently tested.

```js
const { defineConfig } = require("cypress");
const createBundler = require("@bahmutov/cypress-esbuild-preprocessor");
Expand All @@ -23,8 +28,7 @@ async function setupNodeEvents(on, config) {
on(
"file:preprocessor",
createBundler({
plugins: [createEsbuildPlugin(config)],
sourcemap: "inline"
plugins: [createEsbuildPlugin(config, { prettySourceMap: true })]
})
);

Expand Down
3 changes: 1 addition & 2 deletions features/experimental_source_map.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ Feature: experimental source map
on(
"file:preprocessor",
createBundler({
plugins: [createEsbuildPlugin(config)],
sourcemap: "inline"
plugins: [createEsbuildPlugin(config, { prettySourceMap: true })]
})
);
Expand Down
3 changes: 1 addition & 2 deletions features/reporters/usage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ Feature: usage report
on(
"file:preprocessor",
createBundler({
plugins: [createEsbuildPlugin(config)],
sourcemap: "inline"
plugins: [createEsbuildPlugin(config, { prettySourceMap: true })]
})
);
Expand Down
4 changes: 1 addition & 3 deletions lib/browser-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1172,8 +1172,6 @@ export default function createTests(
stepDefinitionPatterns: string[];
stepDefinitionPaths: string[];
},
projectRoot: string,
sourcesRelativeTo: string,
dryRun: boolean,
) {
const prng = random(seed.toString());
Expand All @@ -1183,7 +1181,7 @@ export default function createTests(
random: Array.from({ length: 16 }, () => Math.floor(prng() * 256)),
});

registry.finalize(newId, projectRoot, sourcesRelativeTo);
registry.finalize(newId);

const testFilter = createTestFilter(gherkinDocument, Cypress.env());

Expand Down
2 changes: 2 additions & 0 deletions lib/helpers/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,7 @@ export function maybeRetrievePositionFromSourceMap(): Position | undefined {
column: relevantFrame.getColumnNumber()!,
});

position.source = position.source.replace(/^webpack:\/\/\//, "");

return position;
}
31 changes: 4 additions & 27 deletions lib/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import parse from "@cucumber/tag-expressions";

import { IdGenerator } from "@cucumber/messages";

import path from "path-browserify";

import { assertAndReturn } from "./helpers/assertions";

import DataTable from "./data_table";
Expand Down Expand Up @@ -119,27 +117,7 @@ export class Registry {
this.parameterTypeRegistry = new ParameterTypeRegistry();
}

public finalize(
newId: IdGenerator.NewId,
projectRoot: string,
sourcesRelativeTo: string,
) {
const finalizePosition = (position?: Position) => {
if (position != null) {
console.log("Original source", position.source);

position.source = path.relative(
projectRoot,
path.join(
sourcesRelativeTo,
// Why does Webpack do this? I have no idea.
position.source.replace(/^webpack:\/\//, ""),
),
);
}
return position;
};

public finalize(newId: IdGenerator.NewId) {
for (const { description, implementation, position } of this
.preliminaryStepDefinitions) {
if (typeof description === "string") {
Expand All @@ -150,7 +128,7 @@ export class Registry {
this.parameterTypeRegistry,
),
implementation,
position: finalizePosition(position),
position,
});
} else {
this.stepDefinitions.push({
Expand All @@ -160,15 +138,14 @@ export class Registry {
this.parameterTypeRegistry,
),
implementation,
position: finalizePosition(position),
position,
});
}
}

for (const { position, ...preliminaryHook } of this.preliminaryHooks) {
for (const preliminaryHook of this.preliminaryHooks) {
this.caseHooks.push({
id: newId(),
position: finalizePosition(position),
...preliminaryHook,
});
}
Expand Down
7 changes: 1 addition & 6 deletions lib/subpath-entrypoints/browserify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ export default function transform(
try {
done(
null,
await compile(
configuration,
buffer.toString("utf8"),
filepath,
configuration.projectRoot,
),
await compile(configuration, buffer.toString("utf8"), filepath),
);

debug(`compiled ${filepath}`);
Expand Down
54 changes: 43 additions & 11 deletions lib/subpath-entrypoints/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,57 @@ import { assertAndReturn } from "../helpers/assertions";

export function createEsbuildPlugin(
configuration: Cypress.PluginConfigOptions,
options: { prettySourceMap: boolean } = { prettySourceMap: false },
): esbuild.Plugin {
return {
name: "feature",
setup(build) {
if (options.prettySourceMap) {
build.initialOptions.sourcemap = "external";
build.initialOptions.sourcesContent = false;

build.onEnd(async () => {
const outfile = assertAndReturn(
build.initialOptions.outfile,
"Expected an outfile",
);

const sourceMapLocation = outfile + ".map";

const sourceMap = JSON.parse(
(await fs.readFile(sourceMapLocation)).toString(),
);

/**
* In leu of https://github.com/evanw/esbuild/issues/2218.
*/
sourceMap.sources = sourceMap.sources.map((source: string) => {
return path.relative(
configuration.projectRoot,
path.normalize(path.join(path.dirname(outfile), source)),
);
});

await fs.rm(sourceMapLocation);

const encoded = Buffer.from(JSON.stringify(sourceMap)).toString(
"base64",
);

console.log(encoded.length);

await fs.appendFile(
outfile,
`//# sourceMappingURL=data:application/json;base64,${encoded}\n`,
);
});
}

build.onLoad({ filter: /\.feature$/ }, async (args) => {
const content = await fs.readFile(args.path, "utf8");

return {
contents: await compile(
configuration,
content,
args.path,
path.dirname(
assertAndReturn(
build.initialOptions.outfile,
"Expected to find 'outfile'",
),
),
),
contents: await compile(configuration, content, args.path),
loader: "js",
};
});
Expand Down
2 changes: 1 addition & 1 deletion lib/subpath-entrypoints/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function createRollupPlugin(
async transform(src: string, id: string) {
if (/\.feature$/.test(id)) {
return {
code: await compile(config, src, id, config.projectRoot),
code: await compile(config, src, id),
map: null,
};
}
Expand Down
2 changes: 1 addition & 1 deletion lib/subpath-entrypoints/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const loader: LoaderDefinition = function (data) {

const config: Cypress.PluginConfigOptions = this.query as any;

compile(config, data, this.resourcePath, config.projectRoot).then(
compile(config, data, this.resourcePath).then(
(result) => callback(null, result),
(error) => callback(error),
);
Expand Down
3 changes: 0 additions & 3 deletions lib/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export async function compile(
configuration: Cypress.PluginConfigOptions,
data: string,
uri: string,
sourcesRelativeTo: string,
) {
configuration = rebuildOriginalConfigObject(configuration);

Expand Down Expand Up @@ -145,8 +144,6 @@ export async function compile(
),
stepDefinitionPaths: stepDefinitionPaths.map(ensureRelativeToProjectRoot),
},
configuration.projectRoot,
sourcesRelativeTo,
preprocessor.dryRun,
];

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
"glob": "^10.4.5",
"is-path-inside": "^3.0.3",
"mocha": "^10.7.0",
"path-browserify": "^1.0.1",
"seedrandom": "^3.0.5",
"source-map": "^0.6.1",
"split": "^1.0.1",
Expand Down

0 comments on commit 3d4e42c

Please sign in to comment.