Skip to content

Commit 28b87cf

Browse files
Jasper De Moordevongovett
authored andcommitted
Fix sourcemap reference in JS output (#1148)
1 parent 9f3f30a commit 28b87cf

5 files changed

Lines changed: 64 additions & 7 deletions

File tree

src/packagers/JSPackager.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,16 @@ class JSPackager extends Packager {
205205

206206
await this.write('},{},' + JSON.stringify(entry) + ')');
207207
if (this.options.sourceMaps) {
208-
// Add source map url
209-
await this.write(
210-
`\n//# sourceMappingURL=${urlJoin(
211-
this.options.publicURL,
212-
path.basename(this.bundle.name, '.js') + '.map'
213-
)}`
214-
);
208+
// Add source map url if a map bundle exists
209+
let mapBundle = this.bundle.siblingBundlesMap.get('map');
210+
if (mapBundle) {
211+
await this.write(
212+
`\n//# sourceMappingURL=${urlJoin(
213+
this.options.publicURL,
214+
path.basename(mapBundle.name)
215+
)}`
216+
);
217+
}
215218
}
216219
await this.dest.end();
217220
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"some": "data",
3+
"more": {
4+
"data": "value"
5+
}
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<script src="./index.js"></script>
4+
</head>
5+
<body>
6+
Hello!
7+
</body>
8+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './data.json';

test/sourcemaps.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,43 @@ describe('sourcemaps', function() {
148148
assert.equal(typeof output, 'function');
149149
assert.equal(output(), 14);
150150
});
151+
152+
it('should create a valid sourcemap reference for a child bundle', async function() {
153+
let b = await bundle(
154+
__dirname + '/integration/sourcemap-reference/index.html'
155+
);
156+
157+
assertBundleTree(b, {
158+
name: 'index.html',
159+
assets: ['index.html'],
160+
childBundles: [
161+
{
162+
type: 'js',
163+
assets: ['index.js', 'data.json'],
164+
childBundles: [
165+
{
166+
type: 'map'
167+
}
168+
]
169+
}
170+
]
171+
});
172+
173+
let jsOutput = fs
174+
.readFileSync(Array.from(b.childBundles)[0].name)
175+
.toString();
176+
177+
let sourcemapReference = path.join(
178+
__dirname,
179+
'/dist/',
180+
jsOutput.substring(jsOutput.lastIndexOf('//# sourceMappingURL') + 22)
181+
);
182+
assert(
183+
fs.existsSync(path.join(sourcemapReference)),
184+
'referenced sourcemap should exist'
185+
);
186+
187+
let map = fs.readFileSync(path.join(sourcemapReference)).toString();
188+
mapValidator(jsOutput, map);
189+
});
151190
});

0 commit comments

Comments
 (0)