Skip to content

Commit

Permalink
Optimize build (#383)
Browse files Browse the repository at this point in the history
* Output ES modules from typescript

Webpack has some optimizations applied only when the input is using ES
modules, not CommonJS. Outputting ES modules from typescript (and
letting webpack deal with them) allows to apply these optimizations.

* Refactor the mocking of the dependency loader

Instead of replacing the exported object with a mock, which works only
because of TS outputting CommonJS code and so webpack not seeing ES6
semantic for the export, the tests are now mocking the different methods
of the object instead, preserving the object identity.
This makes them compatible with the semantic of ES6 named exports.

* Refactor the protocol to export an object with methods

Exporting individual methods as named exports does not allow mocking
these methods when using ES6 modules (as exports are referenced
directly).
As named exports were never imported directly, this has minimal impact
on the consuming code.

* Fix integration tests

Webpack does not allow using module.exports in an ES module. Things were
working fine before because TS modules were compiled to CommonJS before
reaching webpack. Now that webpack sees them as ES modules (allowing
more optimizations), the main entry point needs to be a CommonJS module
explicitly (and so not using TS).
  • Loading branch information
James Lees authored Sep 25, 2019
1 parent 6332937 commit b38d4eb
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 157 deletions.
8 changes: 0 additions & 8 deletions spec/javascripts/helpers/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ var Mocks = {
return request;
},

getDependencies: function() {
return {
load: jasmine.createSpy("load"),
getRoot: jasmine.createSpy("getRoot"),
getPath: jasmine.createSpy("getPath")
};
},

getJSONPSender: function() {
return {
send: jasmine.createSpy("send")
Expand Down
1 change: 1 addition & 0 deletions spec/javascripts/helpers/pusher_integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./pusher_integration_class').default;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Pusher from '../../../src/core/pusher';
import {ScriptReceiverFactory} from '../../../src/runtimes/web/dom/script_receiver_factory';

class PusherIntegration extends Pusher {
export default class PusherIntegration extends Pusher {

static Integration : any = {
ScriptReceivers: new ScriptReceiverFactory(
Expand All @@ -10,5 +10,3 @@ class PusherIntegration extends Pusher {
)}

}

module.exports = PusherIntegration;
2 changes: 1 addition & 1 deletion spec/javascripts/integration/index.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var TestEnv = require('testenv');

var testConfigs = getTestConfigs();

// We can ccess this 'env var' here because there's a webpack.DefinePlugin
// We can access this 'env var' here because there's a webpack.DefinePlugin
// overwriting this value with whatever is set at compile time
if (process.env.MINIMAL_INTEGRATION_TESTS) {
testConfigs = testConfigs.filter((config) => config.forceTLS && config.transport === "ws")
Expand Down
2 changes: 1 addition & 1 deletion spec/javascripts/unit/core/connection/connection_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Connection = require('core/connection/connection').default;
var Protocol = require('core/connection/protocol/protocol');
var Protocol = require('core/connection/protocol/protocol').default;
var Mocks = require("mocks");

describe("Connection", function() {
Expand Down
2 changes: 1 addition & 1 deletion spec/javascripts/unit/core/connection/handshake_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Handshake = require('core/connection/handshake').default;
var Protocol = require('core/connection/protocol/protocol');
var Protocol = require('core/connection/protocol/protocol').default;
var Connection = require('core/connection/connection').default;
var Mocks = require("mocks");

Expand Down
2 changes: 1 addition & 1 deletion spec/javascripts/unit/core/connection/protocol_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Protocol = require('core/connection/protocol/protocol');
var Protocol = require('core/connection/protocol/protocol').default;

describe("Protocol", function() {
describe("#decodeMessage", function() {
Expand Down
19 changes: 13 additions & 6 deletions spec/javascripts/unit/core/transports/transport_connection_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var Mocks = require("mocks");
var TransportConnection = require('core/transports/transport_connection').default;
var Collections = require('core/utils/collections');
var Timer = require('core/utils/timers').OneOffTimer;
var DependenciesModule = require('dom/dependencies');
var Dependencies = require('dom/dependencies').Dependencies;

describe("TransportConnection", function() {
function getTransport(hooks, key, options) {
Expand All @@ -21,13 +21,18 @@ describe("TransportConnection", function() {
var socket;
var timeline;
var transport;
var Dependencies, _Dependencies;
var _DependenciesBackup;

beforeEach(function() {
if (TestEnv === "web") {
_Dependencies = DependenciesModule.Dependencies;
DependenciesModule.Dependencies = Mocks.getDependencies();
Dependencies = DependenciesModule.Dependencies;
_DependenciesBackup = {
load: Dependencies.load,
getRoot: Dependencies.getRoot,
getPath: Dependencies.getPath
}
Dependencies.load = jasmine.createSpy("load")
Dependencies.getRoot = jasmine.createSpy("getRoot")
Dependencies.getPath = jasmine.createSpy("getPath")
}

timeline = Mocks.getTimeline();
Expand All @@ -53,7 +58,9 @@ describe("TransportConnection", function() {

afterEach(function(){
if (TestEnv === "web") {
DependenciesModule.Dependencies = _Dependencies;
Dependencies.load = _DependenciesBackup.load
Dependencies.getRoot = _DependenciesBackup.getRoot
Dependencies.getPath = _DependenciesBackup.getPath
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/core/connection/connection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Collections from '../utils/collections';
import {default as EventsDispatcher} from '../events/dispatcher';
import * as Protocol from './protocol/protocol';
import Protocol from './protocol/protocol';
import {PusherEvent} from './protocol/message-types';
import Logger from '../logger';
import TransportConnection from "../transports/transport_connection";
Expand Down
2 changes: 1 addition & 1 deletion src/core/connection/handshake/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Util from '../../util';
import * as Collections from '../../utils/collections';
import * as Protocol from '../protocol/protocol';
import Protocol from '../protocol/protocol';
import Connection from '../connection';
import TransportConnection from "../../transports/transport_connection";
import HandshakePayload from './handshake_payload';
Expand Down
Loading

0 comments on commit b38d4eb

Please sign in to comment.