Skip to content

Commit

Permalink
increment "nextSourcesIndex" correctly (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 6, 2021
1 parent a06c241 commit 4fefb14
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

* Minification now takes advantage of the left-associativity of certain operators. This means `a && (b && c)` turns into `a && b && c`.

* Fix issues with source maps ([#638][https://github.com/evanw/esbuild/issues/638])

* Generated source maps were incorrect when an input file had a nested source map (i.e. contained a valid `//# sourceMappingURL=` comment) and the input source map had more than one source file. This regression was introduced by an optimization in version 0.8.25 that parallelizes the generation of certain internal source map data structures. The index into the generated `sources` array was incorrectly incremented by 1 for every input file instead of by the number of sources in the input source map. This issue has been fixed and now has test coverage.

## 0.8.29

* Allow entry points outside of the `outbase` directory ([#634](https://github.com/evanw/esbuild/issues/634))
Expand Down
3 changes: 2 additions & 1 deletion internal/bundler/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4291,7 +4291,6 @@ func (c *linkerContext) generateSourceMapForChunk(
continue
}
sourceIndexToSourcesIndex[result.sourceIndex] = nextSourcesIndex
nextSourcesIndex++
file := &c.files[result.sourceIndex]

// Simple case: no nested source map
Expand All @@ -4305,6 +4304,7 @@ func (c *linkerContext) generateSourceMapForChunk(
prettyPath: file.source.PrettyPath,
quotedContents: quotedContents,
})
nextSourcesIndex++
continue
}

Expand Down Expand Up @@ -4332,6 +4332,7 @@ func (c *linkerContext) generateSourceMapForChunk(
quotedContents: quotedContents,
})
}
nextSourcesIndex += len(sm.Sources)
}

// Write the sources
Expand Down
32 changes: 32 additions & 0 deletions scripts/package-lock.json

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

2 changes: 2 additions & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"dependencies": {
"@types/node": "14.14.6",
"fuse.js": "3.2.0",
"js-yaml": "3.14.0",
"react": "17.0.1",
"rimraf": "3.0.2",
"source-map": "0.7.3",
"typescript": "4.0.5",
Expand Down
25 changes: 24 additions & 1 deletion scripts/verify-source-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,23 @@ const toSearchPartialMappings = {
entry: 'entry.js',
}

const testCaseComplex = {
// "fuse.js" is included because it has a nested source map of some complexity.
// "react" is included after that because it's a big blob of code and helps
// make sure stuff after a nested source map works ok.
'entry.js': `
import Fuse from 'fuse.js'
import * as React from 'react'
console.log(Fuse, React)
`,
}

const toSearchComplex = {
'Score average:': '../../node_modules/fuse.js/dist/webpack:/src/index.js',
'0123456789': '../../node_modules/object-assign/index.js',
'forceUpdate': '../../node_modules/react/cjs/react.production.min.js',
};

async function check(kind, testCase, toSearch, { flags, entryPoints, crlf }) {
let failed = 0

Expand Down Expand Up @@ -297,7 +314,8 @@ async function check(kind, testCase, toSearch, { flags, entryPoints, crlf }) {
recordCheck(source === inSource, `expected: ${inSource} observed: ${source}`)

const inJs = map.sourceContentFor(source)
const inIndex = inJs.indexOf(`"${id}"`)
let inIndex = inJs.indexOf(`"${id}"`)
if (inIndex < 0) inIndex = inJs.indexOf(`'${id}'`)
if (inIndex < 0) throw new Error(`Failed to find "${id}" in input`)
const inLines = inJs.slice(0, inIndex).split('\n')
const inLine = inLines.length
Expand Down Expand Up @@ -431,6 +449,11 @@ async function main() {
entryPoints: ['a.js'],
crlf,
}),
check('complex' + suffix, testCaseComplex, toSearchComplex, {
flags: flags.concat('--outfile=out.js', '--bundle', '--define:process.env.NODE_ENV="production"'),
entryPoints: ['entry.js'],
crlf,
}),
)
}
}
Expand Down

0 comments on commit 4fefb14

Please sign in to comment.