Skip to content

Commit c97b136

Browse files
authored
feat(build)!: Introduce exports section in package.json (#7822)
* fix(typings): Remove bogus .d.ts files; add new languages PR #3821 added .d.ts files for every file in msg/json/, but several of these are internal utility files rather than translations, and do not result in a langfile being output by create_messages.py when building langfiles. In the meantime we have added a few new languages that are being published but which have (until now) not had the corresponding type declarations. * feat(build)!: Add exports section to package.json Add an exports stanza to package.json, enumerating existing entrypoints in a new format. - The original main entrypoint, index.js, is removed since the exports section can point directly at node.js or browser.js. - No change made (yet) to other entrypoints (core, blocks, generators); these will be dealt with in a subsequent PR. - The msg/en entrypoint is included in the top-level package.json as an example; entries for all other languages created as part of the packageJSON package task. BREAKING CHANGE: The introduction of an exports stanza means that correctly-behaved tools (node.js, bundlers like webpack, etc.) will only allow importing of the specified entrypoints. Here is the full list of permitted entrypoints that can be imported or required: - blockly - blockly/core - blockly/blocks - blockly/dart - blockly/lua - blockly/javascript - blockly/php - blockly/python - blockly/msg/<lang>, for all supported language codes <lang> (e.g blockly/msg/en, blockly/msg/fr, blockly/msg/de, etc.) If you previously impored any other paths from the blockly package you will need to update your imports. Here are the most common paths that may have been used, and their correct replacements: | If you previously imported: | Import instead: | | -------------------------------- | -------------------------- | | blockly/index.js | blockly | | blockly/node.js | blockly | | blockly/browser.js | blockly | | blockly/blockly.min | This file should only be loaded as a <script>. | | blockly/core.js | blockly/core | | blockly/core-browser.js | blockly/core | | blockly/blockly_compressed.js | blockly/core | | blockly/blocks.js | blockly/blocks | | blockly/blocks_compressed.js | blockly/blocks | | blockly/dart.js | blockly/dart | | blockly/dart_compressed.js | blockly/dart | | blockly/lua.js | blockly/lua | | blockly/lua_compressed.js | blockly/lua | | blockly/javascript.js | blockly/javascript | | blockly/javascript_compressed.js | blockly/javascript | | blockly/php.js | blockly/php | | blockly/php_compressed.js | blockly/php | | blockly/python.js | blockly/python | | blockly/python_compressed.js | blockly/python | | blockly/msg/en.js | blockly/msg/en | * fix(build): Use package-paths (blockly/*) in wrapper imports Use 'blockly/core' instead of './core' when importing core into other wrappers (and similarly for other entries in package.json exports stanza), so that (e.g.) dist/javascript.js won't import dist/core.js (the node.js version that loads jsdom) when being loaded in a browser environment. This fixes an issue where blockly attempts to load jsdom even in browser environments because the browser stanza in package.json, which caused attempts to load core.js to load core-browser.js instead in browser environments, was removed in a previous commit. * refactor(build): Remove unnecessray wrappers Remove pointless wrapper modules that no longer server any purpose; use exports stanza in package.json to point directly to compiled chunks where possible. * refactor(build)!: Eliminate separate browser and node entrypoints Combine scripts/package/browser/index.js (becomes dist/browser.js) and scripts/package/node/index.js (becomes dist/node.js) into a single environment-agnostic index.js. BREAKING CHANGE: Historically, importing the main 'blockly' package would import 'blockly/core', 'blockly/blocks', 'blockly/en' and 'blockly/javascript' - and additionally, in node.js, also import 'blockly/dart', 'blockly/lua', 'blockly/php' and 'blockly/python'. Now the main 'blockly' package entrypoint never loads any of the generator modules. This change has been made because of changes to generator exports made in blockly v9.0.0 that make necessary to always separately import generator modules. Note that this change does not affect loading the blockly package via <script src="https://unpkg.com/blockly"; that continues to load to blockly.min.js, which includes javascript_compressed.js and (due to being loaded as a script) makes it available via Blockly.JavaScript. * refactor(build): Simplify core entrypoint wrapper for node.js Move scripts/package/node/core.js to scripts/package/core-node.js, and have it packaged as dist/core-node.js rather than dist/core.js - without a UMD wrapper, since it will always be loaded as a CJS module. * chore(build): Remove disused packageCommonJS helper * refactor(build): Use subpath pattern (wildcard) for msg/* exports Use a subpath pattern (wildcard) for the msg/* entrypoints, obviating the need for special handling in packageJSON. * fix(tests): Fix node tests run_node_test.js previously directly require()d the dist/blockly.js and dist/javascript.js wrapper module, which no longer exist. Change it to require('blockly-test') (and …blockly-test/javascript) and create a symlink ./node_modules/blocky-test -> dist/ to satisfy this. * fix(build): Add types: and default: entries to exports['./core'] In the 'blockly/core' export: - Replace the browser: entrypoint with a default: one. - Add a types: entrypoint for core.
1 parent 8fc439f commit c97b136

File tree

27 files changed

+179
-863
lines changed

27 files changed

+179
-863
lines changed

package.json

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,45 @@
5454
"test:compile:advanced": "gulp buildAdvancedCompilationTest --debug",
5555
"updateGithubPages": "npm ci && gulp gitUpdateGithubPages"
5656
},
57-
"main": "./index.js",
58-
"umd": "./blockly.min.js",
59-
"unpkg": "./blockly.min.js",
60-
"types": "./index.d.ts",
61-
"browser": {
62-
"./node.js": "./browser.js",
63-
"./core.js": "./core-browser.js",
64-
"./blockly-node.js": "./blockly.js"
57+
"exports": {
58+
".": {
59+
"types": "./index.d.ts",
60+
"umd": "./blockly.min.js",
61+
"default": "./index.js"
62+
},
63+
"./core": {
64+
"types": "./core.d.ts",
65+
"node": "./core-node.js",
66+
"default": "./blockly_compressed.js"
67+
},
68+
"./blocks": {
69+
"types": "./blocks.d.ts",
70+
"default": "./blocks_compressed.js"
71+
},
72+
"./dart": {
73+
"types": "./dart.d.ts",
74+
"default": "./dart_compressed.js"
75+
},
76+
"./lua": {
77+
"types": "./lua.d.ts",
78+
"default": "./lua_compressed.js"
79+
},
80+
"./javascript": {
81+
"types": "./javascript.d.ts",
82+
"default": "./javascript_compressed.js"
83+
},
84+
"./php": {
85+
"types": "./php.d.ts",
86+
"default": "./php_compressed.js"
87+
},
88+
"./python": {
89+
"types": "./python.d.ts",
90+
"default": "./python_compressed.js"
91+
},
92+
"./msg/*": {
93+
"types": "./msg/*.d.ts",
94+
"default": "./msg/*.js"
95+
}
6596
},
6697
"license": "Apache-2.0",
6798
"devDependencies": {

scripts/gulpfiles/build_tasks.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const closureCompiler = require('google-closure-compiler').gulp();
2323
const argv = require('yargs').argv;
2424
const {rimraf} = require('rimraf');
2525

26-
const {BUILD_DIR, RELEASE_DIR, TSC_OUTPUT_DIR, TYPINGS_BUILD_DIR} = require('./config');
26+
const {BUILD_DIR, LANG_BUILD_DIR, RELEASE_DIR, TSC_OUTPUT_DIR, TYPINGS_BUILD_DIR} = require('./config');
2727
const {getPackageJson} = require('./helper_tasks');
2828

2929
const {posixPath, quote} = require('../helpers');
@@ -350,8 +350,7 @@ this removal!
350350
*/
351351
function buildLangfiles(done) {
352352
// Create output directory.
353-
const outputDir = path.join(BUILD_DIR, 'msg');
354-
fs.mkdirSync(outputDir, {recursive: true});
353+
fs.mkdirSync(LANG_BUILD_DIR, {recursive: true});
355354

356355
// Run create_messages.py.
357356
let json_files = fs.readdirSync(path.join('msg', 'json'));
@@ -364,7 +363,7 @@ function buildLangfiles(done) {
364363
--source_synonym_file ${path.join('msg', 'json', 'synonyms.json')} \
365364
--source_constants_file ${path.join('msg', 'json', 'constants.json')} \
366365
--key_file ${path.join('msg', 'json', 'keys.json')} \
367-
--output_dir ${outputDir} \
366+
--output_dir ${LANG_BUILD_DIR} \
368367
--quiet ${json_files.join(' ')}`;
369368
execSync(createMessagesCmd, {stdio: 'inherit'});
370369

scripts/gulpfiles/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ exports.BUILD_DIR = 'build';
2626
// Directory to write typings output to.
2727
exports.TYPINGS_BUILD_DIR = path.join(exports.BUILD_DIR, 'declarations');
2828

29+
// Directory to write langfile output to.
30+
exports.LANG_BUILD_DIR = path.join(exports.BUILD_DIR, 'msg');
31+
2932
// Directory where typescript compiler output can be found.
3033
// Matches the value in tsconfig.json: outDir
3134
exports.TSC_OUTPUT_DIR = path.join(exports.BUILD_DIR, 'src');

0 commit comments

Comments
 (0)