Skip to content

Commit

Permalink
Transferables (ampproject#361)
Browse files Browse the repository at this point in the history
* Started thoughts on Buffers
* Added Nodes and Removed Nodes in structure
* Uint16Array representation
* Move all transmissions to a single helper method.
* Mutators implemented
* Continued progress toward working
* Performance testing
* Working
* Fixes for mutation observer tests
* Tests pass now.
* Remove unused comment
* Move to replacement TransferrableNode
* Fix forEach
* Private for no reason
* Repair SVG Element creation
* Continue moving transfer to updated location and remove unused items
* Dont modify tsconfig automatically VSCode :(
* Refactored debugger to be inline with Command Executors
* Ensure correct number of print methods from CommandExecutors are removed during build process.
* Fix up comment on Rollup Plugin
* Removed replacement directory, merged back to transfer
* No need for these helpers
* Pin rollup-plugin-replace
* Continued merging long tasks and added tests for Transferable format
* Add Long Task API in worker thread
* Main thread tests and Long Task tests now pass
* Format message for property range replacement
* Last cleanup for ObjectExpression replacement
* Long task demo
* Reduce low priority changes
* Property names cannot be invalid across bridge
* Fix binding error in Element.innerHTML setter
* Remove shorthand not relevant to review
* Use Local Name for Elements, restore comment support in node creation
* Only collect information to transmit to main thread after the DOM enters the Mutation Phase
* Memory info for dbmon
* HFS+ still doesn't understand capitalization of filenames
* Fix Windows build issue with new Emitter
* Serial for cb tests requiring a document
* Fix for ampproject#371, remove a slice of the childNodes
* Made test for removing child node elements a little more through
* Made test for removing child node elements a little more through
* Run Prettier on all .js files
* Remove +1 on end positions within a Transferable
* Element.getBoundingClientRectAsync now removes its event listener
* Make WorkerContext.worker readonly
* Remove unused statement
* Remove unused callbacks on WorkerDOMConfiguration
* Fix comment on upgrade method
* Fix for ampproject#371, remove a slice of the childNodes (ampproject#383)
* Fix for ampproject#371, remove a slice of the childNodes
* Made test for removing child node elements a little more through
  • Loading branch information
kristoferbaxter authored Mar 27, 2019
1 parent 6546d78 commit 7db5e34
Show file tree
Hide file tree
Showing 90 changed files with 3,418 additions and 1,855 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ yarn-error.log
yarn.lock
package-lock.json
demo/build/
demo/example/
dist/*
5 changes: 1 addition & 4 deletions config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,4 @@
import MainThreadBuilds from './rollup.main-thread.js';
import WorkerThreadBuilds from './rollup.worker-thread.js';

export default [
...MainThreadBuilds,
...WorkerThreadBuilds,
];
export default [...MainThreadBuilds, ...WorkerThreadBuilds];
104 changes: 69 additions & 35 deletions config/rollup.main-thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
import resolve from 'rollup-plugin-node-resolve';
import compiler from '@ampproject/rollup-plugin-closure-compiler';
import { terser } from 'rollup-plugin-terser';
import {babelPlugin} from './rollup.plugins.js';
import {MINIFY_BUNDLE_VALUE, DEBUG_BUNDLE_VALUE} from './rollup.utils.js';
import replace from 'rollup-plugin-replace';
import { babelPlugin, removeDebugCommandExecutors } from './rollup.plugins.js';
import { MINIFY_BUNDLE_VALUE, DEBUG_BUNDLE_VALUE } from './rollup.utils.js';

const ESModules = [
{
Expand All @@ -29,13 +30,17 @@ const ESModules = [
sourcemap: true,
},
plugins: [
removeDebugCommandExecutors(),
replace({
DEBUG_ENABLED: false,
}),
babelPlugin({
transpileToES5: false,
allowConsole: DEBUG_BUNDLE_VALUE,
}),
MINIFY_BUNDLE_VALUE ? compiler() : null,
MINIFY_BUNDLE_VALUE ? terser() : null,
].filter(Boolean)
].filter(Boolean),
},
{
input: 'output/main-thread/index.js',
Expand All @@ -45,11 +50,15 @@ const ESModules = [
sourcemap: true,
},
plugins: [
removeDebugCommandExecutors(),
replace({
DEBUG_ENABLED: false,
}),
babelPlugin({
transpileToES5: false,
allowConsole: DEBUG_BUNDLE_VALUE,
}),
].filter(Boolean)
].filter(Boolean),
},
{
input: 'output/main-thread/index.safe.js',
Expand All @@ -60,13 +69,17 @@ const ESModules = [
},
plugins: [
resolve(),
removeDebugCommandExecutors(),
replace({
DEBUG_ENABLED: false,
}),
babelPlugin({
transpileToES5: false,
allowConsole: DEBUG_BUNDLE_VALUE,
}),
MINIFY_BUNDLE_VALUE ? compiler() : null,
MINIFY_BUNDLE_VALUE ? terser() : null,
].filter(Boolean)
].filter(Boolean),
},
{
input: 'output/main-thread/index.safe.js',
Expand All @@ -77,11 +90,15 @@ const ESModules = [
},
plugins: [
resolve(),
removeDebugCommandExecutors(),
replace({
DEBUG_ENABLED: false,
}),
babelPlugin({
transpileToES5: false,
allowConsole: DEBUG_BUNDLE_VALUE,
}),
].filter(Boolean)
].filter(Boolean),
},
];

Expand All @@ -95,13 +112,17 @@ const IIFEModules = [
sourcemap: true,
},
plugins: [
removeDebugCommandExecutors(),
replace({
DEBUG_ENABLED: false,
}),
babelPlugin({
transpileToES5: true,
allowConsole: DEBUG_BUNDLE_VALUE,
}),
MINIFY_BUNDLE_VALUE ? compiler() : null,
MINIFY_BUNDLE_VALUE ? terser() : null,
].filter(Boolean)
].filter(Boolean),
},
{
input: 'output/main-thread/index.js',
Expand All @@ -112,11 +133,15 @@ const IIFEModules = [
sourcemap: true,
},
plugins: [
removeDebugCommandExecutors(),
replace({
DEBUG_ENABLED: false,
}),
babelPlugin({
transpileToES5: true,
allowConsole: DEBUG_BUNDLE_VALUE,
}),
].filter(Boolean)
].filter(Boolean),
},
{
input: 'output/main-thread/index.safe.js',
Expand All @@ -128,13 +153,17 @@ const IIFEModules = [
},
plugins: [
resolve(),
removeDebugCommandExecutors(),
replace({
DEBUG_ENABLED: false,
}),
babelPlugin({
transpileToES5: true,
allowConsole: DEBUG_BUNDLE_VALUE,
}),
MINIFY_BUNDLE_VALUE ? compiler() : null,
MINIFY_BUNDLE_VALUE ? terser() : null,
].filter(Boolean)
].filter(Boolean),
},
{
input: 'output/main-thread/index.safe.js',
Expand All @@ -146,36 +175,41 @@ const IIFEModules = [
},
plugins: [
resolve(),
removeDebugCommandExecutors(),
replace({
DEBUG_ENABLED: false,
}),
babelPlugin({
transpileToES5: true,
allowConsole: DEBUG_BUNDLE_VALUE,
}),
].filter(Boolean)
}
].filter(Boolean),
},
];

const debugModules = DEBUG_BUNDLE_VALUE ? [
{
input: 'output/main-thread/index.js',
output: {
file: 'dist/debug.index.js',
format: 'iife',
name: 'MainThread',
sourcemap: true,
},
plugins: [
babelPlugin({
transpileToES5: false,
allowConsole: true,
}),
MINIFY_BUNDLE_VALUE ? compiler() : null,
MINIFY_BUNDLE_VALUE ? terser() : null,
].filter(Boolean)
}
] : [];
const debugModules = DEBUG_BUNDLE_VALUE
? [
{
input: 'output/main-thread/index.js',
output: {
file: 'dist/debug.index.js',
format: 'iife',
name: 'MainThread',
sourcemap: true,
},
plugins: [
replace({
DEBUG_ENABLED: true,
}),
babelPlugin({
transpileToES5: false,
allowConsole: true,
}),
MINIFY_BUNDLE_VALUE ? compiler() : null,
MINIFY_BUNDLE_VALUE ? terser() : null,
].filter(Boolean),
},
]
: [];

export default [
...ESModules,
...IIFEModules,
...debugModules,
];
export default [...ESModules, ...IIFEModules, ...debugModules];
102 changes: 85 additions & 17 deletions config/rollup.plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@

import babel from 'rollup-plugin-babel';
import MagicString from 'magic-string';
import fs from 'fs';
import path from 'path';
const walk = require('acorn-walk');

/**
* Invoke Babel on source, with some configuration.
* @param {object} config, two keys transpileToES5, and allowConsole
* @param {object} config, two keys transpileToES5, and allowConsole
* - transpileToES5 Should we transpile down to ES5 or features supported by `module` capable browsers?
* - allowConsole Should we allow `console` methods in the output?
* - allowPostMessage Should we allow postMessage to/from the Worker?
*/
export function babelPlugin({transpileToES5, allowConsole = false, allowPostMessage = true}) {
export function babelPlugin({ transpileToES5, allowConsole = false, allowPostMessage = true }) {
const targets = transpileToES5 ? { browsers: ['last 2 versions', 'ie >= 11', 'safari >= 7'] } : { esmodules: true };
const exclude = allowConsole ? ['error', 'warn', 'trace', 'info', 'log', 'time', 'timeEnd'] : [];

Expand All @@ -44,19 +46,24 @@ export function babelPlugin({transpileToES5, allowConsole = false, allowPostMess
plugins: [
['@babel/plugin-proposal-object-rest-spread'],
['@babel/proposal-class-properties'],
['babel-plugin-minify-replace', {
'replacements': [{
'identifierName': '__ALLOW_POST_MESSAGE__',
'replacement': {
'type': 'booleanLiteral',
'value': allowPostMessage
}
}]
}],
[
'babel-plugin-minify-replace',
{
replacements: [
{
identifierName: '__ALLOW_POST_MESSAGE__',
replacement: {
type: 'booleanLiteral',
value: allowPostMessage,
},
},
],
},
],
['babel-plugin-transform-remove-console', { exclude }],
],
});
};
}

/**
* RollupPlugin that removes the testing document singleton from output source.
Expand All @@ -69,25 +76,86 @@ export function removeTestingDocument() {
buildStart() {
context = this;
},
renderChunk: async (code) => {
async renderChunk(code) {
const source = new MagicString(code);
const program = context.parse(code, { ranges: true });

walk.simple(program, {
VariableDeclarator(node) {
if (node.id && node.id.type === 'Identifier' && node.id.name && node.id.name === 'documentForTesting') {
const range = node.range;
if (range) {
if (node.range) {
source.overwrite(node.range[0], node.range[1], 'documentForTesting = undefined');
}
}
},
});


return {
code: source.toString(),
map: source.generateMap(),
};
},
};
}

/**
* Formats valid output for trimmed ObjectExpressions.
* @param {string} code
* @param {Array<Array<number, number>>} validPropertyRanges
*/
const outputPropertyRange = (code, validPropertyRanges) =>
`{
${validPropertyRanges.map((range, index) => `${index > 0 ? '\n\t\t' : ''}${code.substring(range[0], range[1])}`)}
}`;

/**
* RollupPlugin that removes the debugging printers from CommandExecutors.
*/
export function removeDebugCommandExecutors() {
let context;
let toDiscover;

return {
name: 'remove-debug-command-executors',
buildStart(options) {
context = this;
toDiscover = fs
.readdirSync(path.join(path.dirname(options.input), 'commands'))
.filter(file => path.extname(file) !== '.map' && path.basename(file, '.js') !== 'interface').length;
},
async renderChunk(code) {
const source = new MagicString(code);
const program = context.parse(code, { ranges: true });

walk.simple(program, {
ObjectExpression(node) {
const propertyNames = (node.properties && node.properties.map(property => property.key.name)) || [];
const validPropertyRanges = [];

if (propertyNames.includes('execute') && propertyNames.includes('print')) {
for (const property of node.properties) {
if (property.key.type === 'Identifier') {
if (property.key.name === 'print') {
toDiscover--;
} else {
validPropertyRanges.push([property.range[0], property.range[1]]);
}
}
}

source.overwrite(node.range[0], node.range[1], outputPropertyRange(code, validPropertyRanges));
}
},
});

if (toDiscover > 0) {
context.warn(`${toDiscover} CommandExecutors were not found during compilation.`);
}

return {
code: source.toString(),
map: source.generateMap(),
};
},
};
}
}
2 changes: 1 addition & 1 deletion config/rollup.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ const { DEBUG_BUNDLE = false, MINIFY_BUNDLE = false, COMPRESS_BUNDLE = false } =

export let DEBUG_BUNDLE_VALUE = DEBUG_BUNDLE === 'true';
export let MINIFY_BUNDLE_VALUE = MINIFY_BUNDLE === 'true';
export let COMPRESS_BUNDLE_VALUE = COMPRESS_BUNDLE === 'true';
export let COMPRESS_BUNDLE_VALUE = COMPRESS_BUNDLE === 'true';
Loading

0 comments on commit 7db5e34

Please sign in to comment.