Skip to content

Commit

Permalink
Drop Versa Lite specific polyfills, 3.x SDK support (#132)
Browse files Browse the repository at this point in the history
Signed-off-by: Liam McLoughlin <lmcloughlin@fitbit.com>
  • Loading branch information
Hexxeh committed Oct 29, 2019
1 parent ab8fa89 commit 3765ca7
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 80 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"@fitbit/pngjs": "^3.3.3-fitbit.4",
"chalk": "^2.4.2",
"dateformat": "^3.0.3",
"drop-stream": "^1.0.0",
"elfy": "^0.1.0",
"events-intercept": "^2.0.0",
"fp-ts": "^2.0.5",
Expand All @@ -82,6 +83,7 @@
"magic-string": "^0.25.3",
"merge-stream": "^2.0.0",
"multistream": "^4.0.0",
"playback-stream": "^1.0.0",
"plugin-error": "^1.0.1",
"pofile": "^1.1.0",
"pumpify": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/__mocks__/packageVersion.const.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '3.1.0';
export default '4.0.0';
10 changes: 5 additions & 5 deletions src/__snapshots__/appPackageManifest.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Object {
"manifestVersion": 6,
"requestedPermissions": Array [],
"sdkVersion": Object {
"deviceApi": "4.0.0",
"deviceApi": "5.0.0",
},
"sourceMaps": Object {
"device": Object {
Expand Down Expand Up @@ -47,8 +47,8 @@ Object {
"manifestVersion": 6,
"requestedPermissions": Array [],
"sdkVersion": Object {
"companionApi": "2.1.0",
"deviceApi": "4.0.0",
"companionApi": "3.0.0",
"deviceApi": "5.0.0",
},
"sourceMaps": Object {
"companion": "sourceMaps/companion/companion.js.json",
Expand Down Expand Up @@ -100,7 +100,7 @@ Object {
"manifestVersion": 6,
"requestedPermissions": Array [],
"sdkVersion": Object {
"companionApi": "2.1.0",
"companionApi": "3.0.0",
},
"sourceMaps": Object {
"companion": "sourceMaps/companion/companion.js.json",
Expand Down Expand Up @@ -132,7 +132,7 @@ Object {
"manifestVersion": 6,
"requestedPermissions": Array [],
"sdkVersion": Object {
"deviceApi": "4.0.0",
"deviceApi": "5.0.0",
},
"sourceMaps": Object {
"device": Object {
Expand Down
8 changes: 0 additions & 8 deletions src/__snapshots__/compile.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ console.log(A)
"
`;

exports[`applies specified polyfills 1`] = `
"\\"use strict\\"
var e=require(\\"cbor\\")
const o=false
console.log(o,e.decode)
"
`;

exports[`build fails when a non-existent relative import is specified 1`] = `"Could not resolve './__doesNotExist.js' from src/__test__/compile/relativeImportNotFound.js"`;

exports[`build fails when an absolute import is specified 1`] = `/foo.js is imported by <cwd>/src/__test__/compile/absoluteImport.js, but absolute imports are disallowed`;
Expand Down
16 changes: 8 additions & 8 deletions src/__snapshots__/componentManifest.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ PluginError {

exports[`when there is a companion entry point present builds a companion manifest 1`] = `
Object {
"apiVersion": "2.1.0",
"apiVersion": "3.0.0",
"buildId": "0x0f75775f470c1585",
"bundleDate": "2018-06-27T00:00:00.000Z",
"companion": Object {
Expand All @@ -97,7 +97,7 @@ PluginError {

exports[`when there is a companion entry point present sets app cluster storage related fields if configured 1`] = `
Object {
"apiVersion": "2.1.0",
"apiVersion": "3.0.0",
"appClusters": Array [
"a.storage.group",
],
Expand All @@ -116,7 +116,7 @@ Object {

exports[`when there is a companion entry point present when there is a settings entry point present builds a companion manifest with settings 1`] = `
Object {
"apiVersion": "2.1.0",
"apiVersion": "3.0.0",
"buildId": "0x0f75775f470c1585",
"bundleDate": "2018-06-27T00:00:00.000Z",
"companion": Object {
Expand All @@ -134,7 +134,7 @@ Object {

exports[`when there is a device entry point present builds a device manifest for a clock 1`] = `
Object {
"apiVersion": "4.0.0",
"apiVersion": "5.0.0",
"appManifestVersion": 1,
"appType": "clockface",
"buildId": "0x0f75775f470c1585",
Expand All @@ -158,7 +158,7 @@ Object {

exports[`when there is a device entry point present builds a device manifest for an app 1`] = `
Object {
"apiVersion": "4.0.0",
"apiVersion": "5.0.0",
"appManifestVersion": 1,
"appType": "app",
"buildId": "0x0f75775f470c1585",
Expand All @@ -182,13 +182,13 @@ Object {
}
`;

exports[`when there is a device entry point present when there are compiled language files ensures the default language en-US is the first key in the i18n object 1`] = `"{\\"appManifestVersion\\":1,\\"main\\":\\"device/index.js\\",\\"svgMain\\":\\"resources/index.gui\\",\\"svgWidgets\\":\\"resources/widgets.gui\\",\\"appType\\":\\"clockface\\",\\"apiVersion\\":\\"4.0.0\\",\\"buildId\\":\\"0x0f75775f470c1585\\",\\"bundleDate\\":\\"2018-06-27T00:00:00.000Z\\",\\"uuid\\":\\"b4ae822e-eca9-4fcb-8747-217f2a1f53a1\\",\\"name\\":\\"My App\\",\\"requestedPermissions\\":[],\\"i18n\\":{\\"en-us\\":{\\"name\\":\\"My App\\",\\"resources\\":\\"lang/english\\"},\\"fr-fr\\":{\\"name\\":\\"Mon application\\"},\\"es-es\\":{\\"resources\\":\\"spanish/language\\"}}}"`;
exports[`when there is a device entry point present when there are compiled language files ensures the default language en-US is the first key in the i18n object 1`] = `"{\\"appManifestVersion\\":1,\\"main\\":\\"device/index.js\\",\\"svgMain\\":\\"resources/index.gui\\",\\"svgWidgets\\":\\"resources/widgets.gui\\",\\"appType\\":\\"clockface\\",\\"apiVersion\\":\\"5.0.0\\",\\"buildId\\":\\"0x0f75775f470c1585\\",\\"bundleDate\\":\\"2018-06-27T00:00:00.000Z\\",\\"uuid\\":\\"b4ae822e-eca9-4fcb-8747-217f2a1f53a1\\",\\"name\\":\\"My App\\",\\"requestedPermissions\\":[],\\"i18n\\":{\\"en-us\\":{\\"name\\":\\"My App\\",\\"resources\\":\\"lang/english\\"},\\"fr-fr\\":{\\"name\\":\\"Mon application\\"},\\"es-es\\":{\\"resources\\":\\"spanish/language\\"}}}"`;

exports[`when there is a device entry point present when there are compiled language files ensures the default language es-ES is the first key in the i18n object 1`] = `"{\\"appManifestVersion\\":1,\\"main\\":\\"device/index.js\\",\\"svgMain\\":\\"resources/index.gui\\",\\"svgWidgets\\":\\"resources/widgets.gui\\",\\"appType\\":\\"clockface\\",\\"apiVersion\\":\\"4.0.0\\",\\"buildId\\":\\"0x0f75775f470c1585\\",\\"bundleDate\\":\\"2018-06-27T00:00:00.000Z\\",\\"uuid\\":\\"b4ae822e-eca9-4fcb-8747-217f2a1f53a1\\",\\"name\\":\\"My App\\",\\"requestedPermissions\\":[],\\"i18n\\":{\\"es-es\\":{\\"resources\\":\\"spanish/language\\"},\\"en-us\\":{\\"name\\":\\"My App\\",\\"resources\\":\\"lang/english\\"},\\"fr-fr\\":{\\"name\\":\\"Mon application\\"}}}"`;
exports[`when there is a device entry point present when there are compiled language files ensures the default language es-ES is the first key in the i18n object 1`] = `"{\\"appManifestVersion\\":1,\\"main\\":\\"device/index.js\\",\\"svgMain\\":\\"resources/index.gui\\",\\"svgWidgets\\":\\"resources/widgets.gui\\",\\"appType\\":\\"clockface\\",\\"apiVersion\\":\\"5.0.0\\",\\"buildId\\":\\"0x0f75775f470c1585\\",\\"bundleDate\\":\\"2018-06-27T00:00:00.000Z\\",\\"uuid\\":\\"b4ae822e-eca9-4fcb-8747-217f2a1f53a1\\",\\"name\\":\\"My App\\",\\"requestedPermissions\\":[],\\"i18n\\":{\\"es-es\\":{\\"resources\\":\\"spanish/language\\"},\\"en-us\\":{\\"name\\":\\"My App\\",\\"resources\\":\\"lang/english\\"},\\"fr-fr\\":{\\"name\\":\\"Mon application\\"}}}"`;

exports[`when there is a device entry point present when there are compiled language files sets the i18n[lang].resources key for language files that pass through 1`] = `
Object {
"apiVersion": "4.0.0",
"apiVersion": "5.0.0",
"appManifestVersion": 1,
"appType": "clockface",
"buildId": "0x0f75775f470c1585",
Expand Down
13 changes: 0 additions & 13 deletions src/buildTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import lodash from 'lodash';
import semver from 'semver';

import sdkVersion from './sdkVersion';
import { PolyfillMap } from './plugins/polyfill';

export interface BuildTargetDescriptor {
displayName: string;
platform: string[];
resourceFilterTag: string;
polyfills?: PolyfillMap;
maxDeviceBundleSize?: number; // in bytes
minSDKVersion?: string;
}
Expand All @@ -30,17 +28,6 @@ const baseBuildTargets: { [platform: string]: BuildTargetDescriptor } = {
resourceFilterTag: '300x300',
minSDKVersion: '3.1.0',
maxDeviceBundleSize: 3145728,
polyfills: {
barometer: 'export var Barometer = undefined; export default Barometer;',
gyroscope: 'export var Gyroscope = undefined; export default Gyroscope;',
orientation:
'export var OrientationSensor = undefined; export default OrientationSensor;',
'user-activity': `
export { today, goals, default } from 'user-activity';
import { today } from 'user-activity';
Object.defineProperty(today.local, 'elevationGain', {});
`,
},
},
mira: {
displayName: 'Fitbit Versa 2',
Expand Down
12 changes: 0 additions & 12 deletions src/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ function compileFile(
allowUnknownExternals = undefined as boolean | undefined,
component = ComponentType.COMPANION,
outputDir = undefined as string | undefined,
polyfills = {},
} = {},
expectFilename?: string,
) {
Expand All @@ -53,7 +52,6 @@ function compileFile(
component,
allowUnknownExternals,
outputDir,
polyfills,
entryPoint: testResourcePath(filename),
onDiagnostic: mockDiagnosticHandler,
defaultLanguage: 'en-US',
Expand Down Expand Up @@ -211,16 +209,6 @@ it('emits sourcemaps with source paths relative to the project root', async () =
expect(file.sourceMap.sources).toMatchSnapshot();
});

it('applies specified polyfills', () => {
return expect(
compileFile('polyfill.js', {
polyfills: {
cbor: "export * from 'cbor'; export const encode = false;",
},
}).then(getVinylContents),
).resolves.toMatchSnapshot();
});

it('emits multiple chunks when dynamic import are used', async () => {
const buildStream = compile({
component: ComponentType.DEVICE,
Expand Down
6 changes: 1 addition & 5 deletions src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import sdkVersion from './sdkVersion';
import forbidAbsoluteImport from './plugins/forbidAbsoluteImport';
import i18nPolyfill from './plugins/i18nPolyfill';
import platformExternals from './plugins/platformExternals';
import polyfill, { PolyfillMap } from './plugins/polyfill';
import polyfill from './plugins/polyfill';
import polyfillDevice from './plugins/polyfillDevice';
import resourceImports from './plugins/resourceImports';
import workaroundRequireScope from './plugins/workaroundRequireScope';
Expand Down Expand Up @@ -50,24 +50,20 @@ export default function compile({
defaultLanguage,
allowUnknownExternals = false,
onDiagnostic = logDiagnosticToConsole,
polyfills = {},
}: {
component: ComponentType;
entryPoint: string;
outputDir?: string;
defaultLanguage: string;
allowUnknownExternals?: boolean;
onDiagnostic?: DiagnosticHandler;
polyfills?: PolyfillMap;
}) {
const ecma = component !== ComponentType.DEVICE ? 6 : 5;
const { translationsGlob } = componentTargets[component];
return rollupToVinyl(
{
input: entryPoint,
plugins: [
// Polyfills must come before platform externals
polyfill(polyfills),
...pluginIf(component === ComponentType.DEVICE, polyfillDevice),
...pluginIf(component !== ComponentType.DEVICE, () =>
polyfill(i18nPolyfill(translationsGlob, defaultLanguage)),
Expand Down
41 changes: 24 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import fs from 'fs';
import { Readable, Stream } from 'stream';

import dropStream from 'drop-stream';
import lazystream from 'lazystream';
import mergeStream from 'merge-stream';
import multistream from 'multistream';
import playbackStream from 'playback-stream';
import PluginError from 'plugin-error';
import pumpify from 'pumpify';
import simpleRandom from 'simple-random';
Expand Down Expand Up @@ -32,7 +34,6 @@ import filterResourceTag from './filterResourceTag';
import findEntryPoint from './findEntryPoint';
import nativeComponents from './nativeComponents';
import pluginError from './util/pluginError';
import { PolyfillMap } from './plugins/polyfill';
import ProjectConfiguration, {
normalizeProjectConfig,
validate,
Expand Down Expand Up @@ -145,13 +146,11 @@ export function loadProjectConfig({
export function buildComponent({
projectConfig,
component,
polyfills = {},
onDiagnostic = logDiagnosticToConsole,
}: {
projectConfig: ProjectConfiguration;
component: ComponentType;
onDiagnostic?: DiagnosticHandler;
polyfills?: PolyfillMap;
}) {
const { inputs, outputDir, notFoundIsFatal } = componentTargets[component];

Expand All @@ -170,7 +169,6 @@ export function buildComponent({
onDiagnostic,
entryPoint,
outputDir,
polyfills,
allowUnknownExternals: projectConfig.enableProposedAPI,
defaultLanguage: projectConfig.defaultLanguage,
}),
Expand Down Expand Up @@ -205,17 +203,32 @@ export function buildDeviceComponents({
buildId: string;
onDiagnostic?: DiagnosticHandler;
}) {
const deviceJSPipeline: Stream[] = [
// TODO: remove is-defined assertion ('!')
buildComponent({
projectConfig,
onDiagnostic,
component: ComponentType.DEVICE,
})!,
];

const processedJS = new playbackStream({ objectMode: true });
deviceJSPipeline.push(processedJS);

return multistream.obj([
// Sequence the build process: wait until compilation finishes
// before building the resources for each component.
new pumpify.obj(
...deviceJSPipeline,
// We don't want to send the JS file downstream directly. It will
// be played back into the individual device component pipelines.
dropStream.obj(),
),
...projectConfig.buildTargets.map((family) =>
lazyObjectReadable(() => {
const {
platform,
displayName,
polyfills,
maxDeviceBundleSize,
} = buildTargets[family];
const { platform, displayName, maxDeviceBundleSize } = buildTargets[
family
];
onDiagnostic({
messageText: `Building app for ${displayName}`,
category: DiagnosticCategory.Message,
Expand All @@ -228,13 +241,7 @@ export function buildDeviceComponents({
return new pumpify.obj(
mergeStream(
new pumpify.obj(
// TODO: remove is-defined assertion ('!')
buildComponent({
projectConfig,
onDiagnostic,
polyfills,
component: ComponentType.DEVICE,
})!,
processedJS.newReadableSide({ objectMode: true }),
sourceMap.collector(ComponentType.DEVICE, family),
),
new pumpify.obj(
Expand Down
7 changes: 0 additions & 7 deletions src/sdkVersion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ it('throws if package version has no known mapping', () => {
expect(() => apiVersions({}, '1000.0.0')).toThrowErrorMatchingSnapshot();
});

it('provides a mapping for SDKv3.1', () => {
expect(apiVersions({}, '3.1.0')).toEqual({
deviceApi: '4.0.0',
companionApi: '2.1.0',
});
});

it('provides a mapping for SDKv4.0', () => {
expect(apiVersions({}, '4.0.0')).toEqual({
deviceApi: '5.0.0',
Expand Down
3 changes: 0 additions & 3 deletions src/sdkVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ export function apiVersions(
if (enableProposedAPI) return { deviceApi: '*', companionApi: '*' };

const { major, minor } = sdkVersion(toolchainVersion);
if (major === 3 && minor === 1) {
return { deviceApi: '4.0.0', companionApi: '2.1.0' };
}
if (major === 4 && minor === 0) {
return { deviceApi: '5.0.0', companionApi: '3.0.0' };
}
Expand Down
7 changes: 7 additions & 0 deletions src/types/drop-stream/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as stream from 'stream';

declare class DropStream extends stream.Transform {
static obj(options?: stream.TransformOptions): DropStream;
}

export = DropStream;
7 changes: 7 additions & 0 deletions src/types/playback-stream/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as stream from 'stream';

declare class PlaybackStream extends stream.Transform {
newReadableSide(opts?: stream.TransformOptions): stream.PassThrough;
}

export = PlaybackStream;
Loading

0 comments on commit 3765ca7

Please sign in to comment.