@jsenv/sourcemap 1.0.1 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/sourcemap",
3
- "version": "1.0.1",
3
+ "version": "1.0.4",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -8,19 +8,19 @@
8
8
  "directory": "packages/utils"
9
9
  },
10
10
  "publishConfig": {
11
- "access": "public",
12
- "registry": "https://registry.npmjs.org"
11
+ "access": "public"
13
12
  },
14
13
  "type": "module",
15
14
  "exports": {
16
- ".": "./src/main.js"
15
+ ".": "./src/main.js",
16
+ "./*": "./*"
17
17
  },
18
18
  "scripts": {
19
19
  "test": "node --conditions=development ./scripts/test.mjs"
20
20
  },
21
21
  "dependencies": {
22
- "@jsenv/urls": "1.2.6",
22
+ "@jsenv/urls": "1.2.7",
23
23
  "magic-string": "0.26.2",
24
- "source-map": "0.7.4"
24
+ "source-map-js": "1.0.2"
25
25
  }
26
- }
26
+ }
package/scripts/test.mjs CHANGED
@@ -1,15 +1,15 @@
1
- import { executeTestPlan, nodeProcess } from "@jsenv/core"
1
+ import { executeTestPlan, nodeWorkerThread } from "@jsenv/core"
2
2
 
3
3
  await executeTestPlan({
4
4
  rootDirectoryUrl: new URL("../", import.meta.url),
5
5
  testPlan: {
6
6
  "**/*.test.mjs": {
7
7
  node: {
8
- runtime: nodeProcess,
8
+ runtime: nodeWorkerThread,
9
9
  },
10
10
  },
11
11
  },
12
12
  failFast: process.argv.includes("--workspace"),
13
13
  completedExecutionLogMerging: process.argv.includes("--workspace"),
14
- coverage: process.argv.includes("--coverage"),
14
+ coverageEnabled: process.argv.includes("--coverage"),
15
15
  })
@@ -1,6 +1,4 @@
1
- import { startsWithWindowsDriveLetter, windowsFilePathToUrl } from "@jsenv/urls"
2
-
3
- import { createDetailedMessage } from "./detailed_message.js"
1
+ import { remapSourcePosition } from "./remap_source_position.js"
4
2
 
5
3
  export const remapCallSite = async (
6
4
  callSite,
@@ -18,13 +16,12 @@ export const remapCallSite = async (
18
16
  const line = callSite.getLineNumber()
19
17
  const column = callSite.getColumnNumber() - 1
20
18
  const originalPosition = await remapSourcePosition({
21
- source,
19
+ url: resolveFile(source),
22
20
  line,
23
21
  column,
24
22
  resolveFile,
25
23
  urlToSourcemapConsumer,
26
24
  readErrorStack,
27
- onFailure,
28
25
  })
29
26
 
30
27
  const callSiteClone = cloneCallSite(callSite)
@@ -196,12 +193,11 @@ const remapEvalOrigin = async (
196
193
  const line = Number(topLevelEvalMatch[3])
197
194
  const column = topLevelEvalMatch[4] - 1
198
195
  const originalPosition = await remapSourcePosition({
199
- source,
196
+ url: resolveFile(source),
200
197
  line,
201
198
  column,
202
199
  resolveFile,
203
200
  urlToSourcemapConsumer,
204
- onFailure,
205
201
  })
206
202
  return `eval at ${topLevelEvalMatch[1]} (${originalPosition.source}:${
207
203
  originalPosition.line
@@ -222,91 +218,3 @@ const remapEvalOrigin = async (
222
218
  // Make sure we still return useful information if we didn't find anything
223
219
  return origin
224
220
  }
225
-
226
- const remapSourcePosition = async ({
227
- source,
228
- line,
229
- column,
230
- resolveFile,
231
- urlToSourcemapConsumer,
232
- readErrorStack,
233
- onFailure,
234
- }) => {
235
- const position = { source, line, column }
236
-
237
- const url = sourceToUrl(source, { resolveFile })
238
- if (!url) return position
239
-
240
- const sourceMapConsumer = await urlToSourcemapConsumer(url)
241
-
242
- if (!sourceMapConsumer) return position
243
-
244
- try {
245
- const originalPosition = sourceMapConsumer.originalPositionFor(position)
246
-
247
- // Only return the original position if a matching line was found. If no
248
- // matching line is found then we return position instead, which will cause
249
- // the stack trace to print the path and line for the compiled file. It is
250
- // better to give a precise location in the compiled file than a vague
251
- // location in the original file.
252
- const originalSource = originalPosition.source
253
-
254
- if (originalSource === null) return position
255
- originalPosition.source = resolveFile(originalSource, url, {
256
- type: "file-original",
257
- })
258
-
259
- return originalPosition
260
- } catch (e) {
261
- onFailure(
262
- createDetailedMessage(`error while remapping position.`, {
263
- ["error stack"]: readErrorStack(e),
264
- ["source"]: source,
265
- ["line"]: line,
266
- ["column"]: column,
267
- }),
268
- )
269
- return position
270
- }
271
- }
272
-
273
- const sourceToUrl = (source, { resolveFile }) => {
274
- if (startsWithScheme(source)) {
275
- return source
276
- }
277
-
278
- // linux filesystem path
279
- if (source[0] === "/") {
280
- return resolveFile(source)
281
- }
282
-
283
- // be careful, due to babel or something like that we might receive paths like
284
- // C:/directory/file.js (without backslashes we would expect on windows)
285
- // In that case we consider C: is the signe we are on windows
286
- // And I avoid to rely on process.platform === "win32" because this file might be executed in chrome
287
- if (startsWithWindowsDriveLetter(source)) {
288
- return windowsFilePathToUrl(source)
289
- }
290
-
291
- // I don't think we will ever encounter relative file in the stack trace
292
- // but if it ever happens we are safe :)
293
- if (source.slice(0, 2) === "./" || source.slice(0, 3) === "../") {
294
- return resolveFile(source)
295
- }
296
-
297
- // we have received a "bare specifier" for the source
298
- // it happens for internal/process/task_queues.js for instance
299
- // if we do return resolveFile(source) it will be converted to
300
- // file:///C:/project-directory/internal/process/task_queues.js in node
301
- // and
302
- // http://domain.com/internal/process/task_queues.js
303
- // but the file will certainly be a 404
304
- // and if not it won't be the right file anyway
305
- // for now we assume "bare specifier" in the stack trace
306
- // are internal files that are pointless to try to remap
307
- return null
308
- }
309
-
310
- const startsWithScheme = (string) => {
311
- return /^[a-zA-Z]{2,}:/.test(string)
312
- }
@@ -0,0 +1,46 @@
1
+ import { createDetailedMessage } from "./detailed_message.js"
2
+
3
+ export const remapSourcePosition = async ({
4
+ url,
5
+ line,
6
+ column,
7
+ resolveFile,
8
+ urlToSourcemapConsumer,
9
+ readErrorStack = (e) => e.stack,
10
+ }) => {
11
+ const position = { url, line, column }
12
+ const sourceMapConsumer = await urlToSourcemapConsumer(url)
13
+
14
+ if (!sourceMapConsumer) {
15
+ return position
16
+ }
17
+
18
+ try {
19
+ const originalPosition = sourceMapConsumer.originalPositionFor(position)
20
+
21
+ // Only return the original position if a matching line was found. If no
22
+ // matching line is found then we return position instead, which will cause
23
+ // the stack trace to print the path and line for the compiled file. It is
24
+ // better to give a precise location in the compiled file than a vague
25
+ // location in the original file.
26
+ const originalSource = originalPosition.source
27
+ if (originalSource === null) {
28
+ return position
29
+ }
30
+ return {
31
+ url: resolveFile(originalSource, url),
32
+ line: originalPosition.line,
33
+ column: originalPosition.column,
34
+ }
35
+ } catch (e) {
36
+ console.warn(
37
+ createDetailedMessage(`error while remapping position.`, {
38
+ ["error stack"]: readErrorStack(e),
39
+ ["url"]: url,
40
+ ["line"]: line,
41
+ ["column"]: column,
42
+ }),
43
+ )
44
+ return position
45
+ }
46
+ }
@@ -1,15 +1,10 @@
1
1
  import { requireSourcemap } from "./require_sourcemap.js"
2
2
 
3
3
  // https://github.com/mozilla/source-map#sourcemapconsumerprototypeoriginalpositionforgeneratedposition
4
- export const getOriginalPosition = async ({
5
- sourcemap,
6
- line,
7
- column,
8
- bias,
9
- }) => {
4
+ export const getOriginalPosition = ({ sourcemap, line, column, bias }) => {
10
5
  const { SourceMapConsumer } = requireSourcemap()
11
6
 
12
- const sourceMapConsumer = await new SourceMapConsumer(sourcemap)
7
+ const sourceMapConsumer = new SourceMapConsumer(sourcemap)
13
8
  const originalPosition = sourceMapConsumer.originalPositionFor({
14
9
  line,
15
10
  column,
@@ -4,6 +4,6 @@ const require = createRequire(import.meta.url)
4
4
  // consider using https://github.com/7rulnik/source-map-js
5
5
 
6
6
  export const requireSourcemap = () => {
7
- const namespace = require("source-map")
7
+ const namespace = require("source-map-js")
8
8
  return namespace
9
9
  }
@@ -6,7 +6,7 @@ import { requireSourcemap } from "./require_sourcemap.js"
6
6
 
7
7
  const { SourceMapConsumer, SourceMapGenerator } = requireSourcemap()
8
8
 
9
- export const composeTwoSourcemaps = async (firstSourcemap, secondSourcemap) => {
9
+ export const composeTwoSourcemaps = (firstSourcemap, secondSourcemap) => {
10
10
  if (!firstSourcemap && !secondSourcemap) {
11
11
  return null
12
12
  }
@@ -17,8 +17,8 @@ export const composeTwoSourcemaps = async (firstSourcemap, secondSourcemap) => {
17
17
  return firstSourcemap
18
18
  }
19
19
  const sourcemapGenerator = new SourceMapGenerator()
20
- const firstSourcemapConsumer = await new SourceMapConsumer(firstSourcemap)
21
- const secondSourcemapConsumer = await new SourceMapConsumer(secondSourcemap)
20
+ const firstSourcemapConsumer = new SourceMapConsumer(firstSourcemap)
21
+ const secondSourcemapConsumer = new SourceMapConsumer(secondSourcemap)
22
22
  const firstMappings = readMappings(firstSourcemapConsumer)
23
23
  firstMappings.forEach((mapping) => {
24
24
  sourcemapGenerator.addMapping(mapping)
File without changes