Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 46 additions & 16 deletions scripts/gulpfiles/build_tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,25 @@ const NAMESPACE_OBJECT = '$';
/**
* A list of chunks. Order matters: later chunks can depend on
* earlier ones, but not vice-versa. All chunks are assumed to depend
* on the first chunk.
* on the first chunk. Properties are as follows:
*
* - .name: the name of the chunk. Used to label it when describing
* it to Closure Compiler and forms the prefix of filename the chunk
* will be written to.
* - .entry: the source .js file which is the entrypoint for the
* chunk.
* - .exports: a variable or property that will (prefixed with
* NAMESPACE_OBJECT) be returned from the factory function and which
* (sans prefix) will be set in the global scope to that returned
* value if the module is loaded in a browser.
* - .importAs: the name that this chunk's exports object will be
* given when passed to the factory function of other chunks that
* depend on it. (Needs to be distinct from .exports since (e.g.)
* "Blockly.blocks.all" is not a valid variable name.)
* - .factoryPreamble: code to override the default wrapper factory
* function preamble.
* - .factoryPostamble: code to override the default wrapper factory
* function postabmle.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL postamble is a word. Thanks!

Copy link
Collaborator Author

@cpcallen cpcallen Dec 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually had to check that myself. I was very glad to discover that it is.

*
* The function getChunkOptions will, after running
* closure-calculate-chunks, update each chunk to add the following
Expand All @@ -81,37 +99,49 @@ const chunks = [
{
name: 'blockly',
entry: 'core/requires.js',
namespace: 'Blockly',
wrapperSetup: `const ${NAMESPACE_OBJECT}={};`,
wrapperCleanup:
exports: 'Blockly',
importAs: 'Blockly',
factoryPreamble: `const ${NAMESPACE_OBJECT}={};`,
factoryPostamble:
`${NAMESPACE_OBJECT}.Blockly.internal_=${NAMESPACE_OBJECT};`,
}, {
name: 'blocks',
entry: 'blocks/all.js',
namespace: 'Blockly.blocks',
exports: 'Blockly.Blocks',
importAs: 'BlocklyBlocks',
}, {
name: 'javascript',
entry: 'generators/javascript/all.js',
namespace: 'Blockly.JavaScript',
exports: 'Blockly.JavaScript',
}, {
name: 'python',
entry: 'generators/python/all.js',
namespace: 'Blockly.Python',
exports: 'Blockly.Python',
}, {
name: 'php',
entry: 'generators/php/all.js',
namespace: 'Blockly.PHP',
exports: 'Blockly.PHP',
}, {
name: 'lua',
entry: 'generators/lua/all.js',
namespace: 'Blockly.Lua',
exports: 'Blockly.Lua',
}, {
name: 'dart',
entry: 'generators/dart/all.js',
namespace: 'Blockly.Dart',
exports: 'Blockly.Dart',
}
];

/**
* The default factory function premable.
*/
const FACTORY_PREAMBLE = `const ${NAMESPACE_OBJECT}=Blockly.internal_;`;

/**
* The default factory function postamble.
*/
const FACTORY_POSTAMBLE = '';

const licenseRegex = `\\/\\*\\*
\\* @license
\\* (Copyright \\d+ (Google LLC|Massachusetts Institute of Technology))
Expand Down Expand Up @@ -283,8 +313,8 @@ function chunkWrapper(chunk) {
const amdDeps = fileNames.join(', ');
const cjsDeps = fileNames.map(f => `require(${f})`).join(', ');
const browserDeps =
chunk.dependencies.map(d => `root.${d.namespace}`).join(', ');
const imports = chunk.dependencies.map(d => d.namespace).join(', ');
chunk.dependencies.map(d => `root.${d.exports}`).join(', ');
const imports = chunk.dependencies.map(d => d.importAs).join(', ');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every chunk previously had a namespace property, but not all chunks have an importAs property (in fact, only one does). Is that an issue?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only chunks which are depended upon by other chunks need an importAs, and at the moment only the first chunk is depended upon by any others.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Arguably I should add a robustness check here, such that if it discovers a chunk that has become a dependancy but has no importAs it will complain. Feel free to open a bug about that if you like…)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Then this works fine here, but it's fragile.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll file a low priority bug after I finish this review

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reading more: I expect that this code will continue to change into next quarter, particularly when we mess around with entry points, so I'll hold off on that bug.

return `// Do not edit this file; automatically generated.

/* eslint-disable */
Expand All @@ -294,13 +324,13 @@ function chunkWrapper(chunk) {
} else if (typeof exports === 'object') { // Node.js
module.exports = factory(${cjsDeps});
} else { // Browser
root.${chunk.namespace} = factory(${browserDeps});
root.${chunk.exports} = factory(${browserDeps});
}
}(this, function(${imports}) {
${chunk.wrapperSetup || `const ${NAMESPACE_OBJECT}=Blockly.internal_;`}
${chunk.factoryPreamble || FACTORY_PREAMBLE}
%output%
${chunk.wrapperCleanup || ''}
return ${NAMESPACE_OBJECT}.${chunk.namespace};
${chunk.factoryPostamble || FACTORY_POSTAMBLE}
return ${NAMESPACE_OBJECT}.${chunk.exports};
}));
`;
};
Expand Down
4 changes: 2 additions & 2 deletions scripts/gulpfiles/package_tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ function packageBlockly() {
*/
function packageBlocks() {
return gulp.src('scripts/package/blocks.js')
.pipe(packageUMD('Blockly.Blocks', [{
name: 'Blockly',
.pipe(packageUMD('BlocklyBlocks', [{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you keep the scripts/package/thing.js files, while emptying them of code, instead of just cutting that step off the pipeline entirely? Is there a benefit to starting the pipeline with an actual file, or is it just serving to inject the license and comment about the wrapper?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly to try to not break anything, TBH, but yes: the documentation in the resulting files in dist/ is helpful (and this improved documentation might have helped me considerably when I started trying to understand this mess some months ago).

I eventually think most of this should go away:

  • Why not just copy build/blockly_compressed.js to dist/blocklyjs instead of having a separate wrapper module?
  • Even where a wrapper module is needed, these ones are all so simple that having a build pipeline with a separate build function to put a UMD wrapper around each of module is absurdly heavyweight. Surely we could just put pre-UMD-wrapped static files in scripts/package and just copy them into dist/ unmodified?

But in the mean time I figured that feeding an effectively-empty file to the existing pipeline was the safest thing to do for the time being.

name: 'BlocklyBlocks',
amd: './blocks_compressed',
cjs: './blocks_compressed',
}]))
Expand Down
5 changes: 1 addition & 4 deletions scripts/package/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@
*/

/**
* @fileoverview Blockly module.
* @fileoverview Blockly module; just a wrapper for blockly_compressed.js.
*/

/* eslint-disable */
'use strict';
7 changes: 1 addition & 6 deletions scripts/package/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@
*/

/**
* @fileoverview Blockly Blocks module.
* @fileoverview Blockly blocks module; just a wrapper for blocks_compressed.js.
*/

/* eslint-disable */
'use strict';

Blockly.Blocks = {};
1 change: 0 additions & 1 deletion scripts/package/browser/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

// Add a helper method to set the Blockly locale.
Blockly.setLocale = function (locale) {
Blockly.Msg = Blockly.Msg || {};
Object.keys(locale).forEach(function (k) {
Blockly.Msg[k] = locale[k];
});
Expand Down
7 changes: 0 additions & 7 deletions scripts/package/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,3 @@

// Include the EN Locale by default.
Blockly.setLocale(En);

Blockly.Blocks = Blockly.Blocks || {};
Object.keys(BlocklyBlocks).forEach(function (k) {
Blockly.Blocks[k] = BlocklyBlocks[k];
});

Blockly.JavaScript = BlocklyJS;
7 changes: 1 addition & 6 deletions scripts/package/dart.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@
*/

/**
* @fileoverview Dart Generator module.
* @fileoverview Dart generator module; just a wrapper for dart_compressed.js.
*/

/* eslint-disable */
'use strict';

Blockly.Dart = BlocklyDart;
7 changes: 3 additions & 4 deletions scripts/package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*/

/**
* @fileoverview Blockly module.
* @fileoverview Blockly module; this is a wrapper which selects
* either browser.js or node.js, depending on which environment we
* are running in.
*/

/* eslint-disable */
'use strict';
8 changes: 2 additions & 6 deletions scripts/package/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
*/

/**
* @fileoverview JavaScript Generator module.
* @fileoverview JavaScript Generator module; just a wrapper for
* javascript_compressed.js.
*/

/* eslint-disable */
'use strict';

Blockly.JavaScript = BlocklyJavaScript;
7 changes: 1 addition & 6 deletions scripts/package/lua.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@
*/

/**
* @fileoverview Lua Generator module.
* @fileoverview Lua generator module; just a wrapper for lua_compressed.js.
*/

/* eslint-disable */
'use strict';

Blockly.Lua = BlocklyLua;
1 change: 0 additions & 1 deletion scripts/package/node/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

// Add a helper method to set the Blockly locale.
Blockly.setLocale = function (locale) {
Blockly.Msg = Blockly.Msg || {};
Object.keys(locale).forEach(function (k) {
Blockly.Msg[k] = locale[k];
});
Expand Down
15 changes: 0 additions & 15 deletions scripts/package/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,3 @@

// Include the EN Locale by default.
Blockly.setLocale(En);

Blockly.Blocks = Blockly.Blocks || {};
Object.keys(BlocklyBlocks).forEach(function (k) {
Blockly.Blocks[k] = BlocklyBlocks[k];
});

Blockly.JavaScript = BlocklyJS;

Blockly.Python = BlocklyPython;

Blockly.Lua = BlocklyLua;

Blockly.PHP = BlocklyPHP;

Blockly.Dart = BlocklyDart;
7 changes: 1 addition & 6 deletions scripts/package/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@
*/

/**
* @fileoverview PHP Generator module.
* @fileoverview PHP generator module; just a wrapper for php_compressed.js.
*/

/* eslint-disable */
'use strict';

Blockly.PHP = BlocklyPHP;
8 changes: 2 additions & 6 deletions scripts/package/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
*/

/**
* @fileoverview Python Generator module.
* @fileoverview Python generator module; just a wrapper for
* python_compressed.js.
*/

/* eslint-disable */
'use strict';

Blockly.Python = BlocklyPython;