Skip to content

Commit

Permalink
Merge branch 'master' into scope-hoisting-destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Mar 12, 2019
2 parents aa00645 + 161cd27 commit 414f426
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 11 deletions.
34 changes: 34 additions & 0 deletions packages/core/integration-tests/test/hmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,40 @@ describe('hmr', function() {
assert(spy.calledOnce);
});

it('should trigger a page reload when a new bundle is created', async function() {
await ncp(
path.join(__dirname, '/integration/hmr-new-bundle'),
path.join(__dirname, '/input')
);

b = bundler(path.join(__dirname, '/input/index.html'), {
watch: true,
hmr: true
});
let bundle = await b.bundle();

let ctx = await run([...bundle.childBundles][0], {}, {require: false});
let spy = sinon.spy(ctx.location, 'reload');

await sleep(50);
assert(spy.notCalled);

await sleep(100);
fs.writeFile(
path.join(__dirname, '/input/index.js'),
'import "./index.css"'
);

await nextEvent(b, 'bundled');
assert(spy.calledOnce);

let contents = await fs.readFile(
path.join(__dirname, '/dist/index.html'),
'utf8'
);
assert(contents.includes('.css'));
});

it('should log emitted errors and show an error overlay', async function() {
await ncp(
path.join(__dirname, '/integration/commonjs'),
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!doctype html>
<html>
<body>
<script src="./index.js"></script>
</body>
</html>
Empty file.
6 changes: 4 additions & 2 deletions packages/core/parcel-bundler/src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ class Bundler extends EventEmitter {
}

// Generate the final bundle names, and replace references in the built assets.
let numBundles = this.bundleNameMap ? this.bundleNameMap.size : 0;
this.bundleNameMap = this.mainBundle.getBundleNameMap(
this.options.contentHash
);
Expand All @@ -313,16 +314,17 @@ class Bundler extends EventEmitter {
}

// Emit an HMR update if this is not the initial bundle.
let bundlesChanged = numBundles !== this.bundleNameMap.size;
if (this.hmr && !isInitialBundle) {
this.hmr.emitUpdate(changedAssets);
this.hmr.emitUpdate(changedAssets, bundlesChanged);
}

logger.progress(`Packaging...`);

// Package everything up
this.bundleHashes = await this.mainBundle.package(
this,
this.bundleHashes
bundlesChanged ? null : this.bundleHashes
);

// Unload any orphaned assets
Expand Down
4 changes: 2 additions & 2 deletions packages/core/parcel-bundler/src/HMRServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ class HMRServer {
this.broadcast(this.unresolvedError);
}

emitUpdate(assets) {
emitUpdate(assets, reload = false) {
if (this.unresolvedError) {
this.unresolvedError = null;
this.broadcast({
type: 'error-resolved'
});
}

const shouldReload = assets.some(asset => asset.hmrPageReload);
const shouldReload = reload || assets.some(asset => asset.hmrPageReload);
if (shouldReload) {
this.broadcast({
type: 'reload'
Expand Down
13 changes: 8 additions & 5 deletions packages/core/parcel-bundler/src/SourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,9 @@ class SourceMap {
generatedPosition.line,
generatedPosition.column
);

let mapping = this.mappings[index];
if (!mapping) {
if (!mapping || !mapping.original) {
return null;
}

Expand All @@ -317,11 +318,13 @@ class SourceMap {
originalPosition.column,
'original'
);

let mapping = this.mappings[index];
return {
source: this.mappings[index].source,
name: this.mappings[index].name,
line: this.mappings[index].generated.line,
column: this.mappings[index].generated.column
source: mapping.source,
name: mapping.name,
line: mapping.generated.line,
column: mapping.generated.column
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel-bundler/src/assets/JSAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class JSAsset extends Asset {
mightHaveDependencies() {
return (
this.isAstDirty ||
!/.js$/.test(this.name) ||
!/\.js$/.test(this.name) ||
IMPORT_RE.test(this.contents) ||
GLOBAL_RE.test(this.contents) ||
SW_RE.test(this.contents) ||
Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel-bundler/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ async function bundle(main, command) {
const server = await bundler.serve(port, command.https, command.host);
if (server && command.open) {
await require('./utils/openInBrowser')(
`${command.https ? 'https' : 'http'}://localhost:${
`${command.https ? 'https' : 'http'}://${command.host || 'localhost'}:${
server.address().port
}`,
command.open
Expand Down

0 comments on commit 414f426

Please sign in to comment.