Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use relative file location in source maps with relativeSourceLocation enabled #5464

Merged
merged 5 commits into from Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion __tests__/plugin.test.ts
@@ -1,6 +1,6 @@
import { html } from 'code-tag';
import plugin from '../plugin';
import { TransformOptions, transformSync } from '@babel/core';
import { BabelFileResult, TransformOptions, transformSync } from '@babel/core';
import traverse from '@babel/traverse';
import { strict as assert } from 'assert';
import '../plugin/jestUtils';
Expand Down Expand Up @@ -97,6 +97,23 @@ describe('babel plugin', () => {
);
});

it('uses relative source location when `relativeSourceLocation` is set to `true`', () => {
process.env.REANIMATED_JEST_SHOULD_MOCK_SOURCE_MAP = '0'; // don't mock source maps
const input = html`<script>
function foo() {
'worklet';
var foo = 'bar';
}
</script>`;

const { code } = runPlugin(input, undefined, {
relativeSourceLocation: true,
});

const matches = code?.match(new RegExp(`..${MOCK_LOCATION}`, 'g'));
expect(matches).toHaveLength(2);
});

it('removes comments from worklets', () => {
const input = html`<script>
const f = () => {
Expand Down
3 changes: 2 additions & 1 deletion plugin/build/plugin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion plugin/src/makeWorklet.ts
Expand Up @@ -107,7 +107,7 @@ export function makeWorklet(
? functionExpression(null, clone.params, clone.body)
: clone;

const [funString, sourceMapString] = buildWorkletString(
let [funString, sourceMapString] = buildWorkletString(
transformed.ast,
variables,
functionName,
Expand Down Expand Up @@ -155,6 +155,11 @@ export function makeWorklet(
let location = state.file.opts.filename;
if (state.opts.relativeSourceLocation) {
location = relative(state.cwd, location);
// It seems there is no designated option to use relative paths in generated sourceMap
sourceMapString = sourceMapString?.replace(
state.file.opts.filename,
location
);
}

initDataObjectExpression.properties.push(
Expand Down