diff --git a/package.json b/package.json index cf338bfc0..92388b8ce 100644 --- a/package.json +++ b/package.json @@ -63,30 +63,37 @@ "./core": { "types": "./core.d.ts", "node": "./core-node.js", + "import": "./blockly.mjs", "default": "./blockly_compressed.js" }, "./blocks": { "types": "./blocks.d.ts", + "import": "./blocks.mjs", "default": "./blocks_compressed.js" }, "./dart": { "types": "./dart.d.ts", + "import": "./dart.mjs", "default": "./dart_compressed.js" }, "./lua": { "types": "./lua.d.ts", + "import": "./lua.mjs", "default": "./lua_compressed.js" }, "./javascript": { "types": "./javascript.d.ts", + "import": "./javascript.mjs", "default": "./javascript_compressed.js" }, "./php": { "types": "./php.d.ts", + "import": "./php.mjs", "default": "./php_compressed.js" }, "./python": { "types": "./python.d.ts", + "import": "./python.mjs", "default": "./python_compressed.js" }, "./msg/*": { diff --git a/scripts/gulpfiles/build_tasks.js b/scripts/gulpfiles/build_tasks.js index 0bdbb90f0..7172ebdbd 100644 --- a/scripts/gulpfiles/build_tasks.js +++ b/scripts/gulpfiles/build_tasks.js @@ -565,7 +565,10 @@ function buildCompiled() { } /** - * This task builds the shims used by the playgrounds and tests to + * This task builds the ESM wrappers used by the chunk "import" + * entrypoints declared in package.json. + * + * Also builds the shims used by the playgrounds and tests to * load Blockly in either compressed or uncompressed mode, creating * build/blockly.loader.mjs, blocks.loader.mjs, javascript.loader.mjs, * etc. @@ -583,7 +586,23 @@ async function buildShims() { // a shim to load the chunk either by importing the entrypoint // module or by loading the compiled script. await Promise.all(chunks.map(async (chunk) => { + // Get names of exports for chunk. const entryPath = path.posix.join(TSC_OUTPUT_DIR_POSIX, chunk.entry); + const exportedNames = Object.keys(await import(`../../${entryPath}`)); + + // Write ESM wrapper. + const cjsPath = `./${chunk.name}${COMPILED_SUFFIX}.js`; + const wrapperPath = path.join(RELEASE_DIR, `${chunk.name}.mjs`); + const importName = chunk.scriptExport.replace(/.*\./, ''); + + await fsPromises.writeFile(wrapperPath, + `import ${importName} from '${cjsPath}'; +export const { +${exportedNames.map((name) => ` ${name},`).join('\n')} +} = ${importName}; +`); + + // Write loading shim. const scriptPath = path.posix.join(RELEASE_DIR, `${chunk.name}${COMPILED_SUFFIX}.js`); const shimPath = path.join(BUILD_DIR, `${chunk.name}.loader.mjs`); @@ -591,14 +610,13 @@ async function buildShims() { chunk.parent ? `import ${quote(`./${chunk.parent.name}.loader.mjs`)};` : ''; - const exports = await import(`../../${entryPath}`); await fsPromises.writeFile(shimPath, `import {loadChunk} from '../tests/scripts/load.mjs'; ${parentImport} export const { -${Object.keys(exports).map((name) => ` ${name},`).join('\n')} +${exportedNames.map((name) => ` ${name},`).join('\n')} } = await loadChunk( ${quote(entryPath)}, ${quote(scriptPath)},