Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import message files with filename that contain hyphens in v11 #8174

Closed
1 task done
devilhyt opened this issue May 24, 2024 · 0 comments · Fixed by #8179
Closed
1 task done

Cannot import message files with filename that contain hyphens in v11 #8174

devilhyt opened this issue May 24, 2024 · 0 comments · Fixed by #8179
Assignees
Labels
issue: bug Describes why the code or behaviour is wrong

Comments

@devilhyt
Copy link
Contributor

Check for duplicates

  • I have searched for similar issues before opening a new one.

Description

An error occurs when importing message files with filenames that contain hyphens (en-gb, zh-hant, etc).

In v11, a script for building message files was updated (#8091). The code below reveals that we directly use the filenames from msg/json/*.json to name the default export object.

/**
* This task builds the ESM wrappers used by the langfiles "import"
* entrypoints declared in package.json.
*/
async function buildLangfileShims() {
// Create output directory.
fs.mkdirSync(path.join(RELEASE_DIR, 'msg'), {recursive: true});
// Get the names of the exports from the langfile by require()ing
// msg/messages.js and letting it mutate the (global) Blockly.Msg.
// (We have to do it this way because messages.js is a script and
// not a CJS module with exports.)
globalThis.Blockly = {Msg: {}};
require('../../msg/messages.js');
const exportedNames = Object.keys(globalThis.Blockly.Msg);
delete globalThis.Blockly;
await Promise.all(getLanguages().map(async (lang) => {
// Write an ESM wrapper that imports the CJS module and re-exports
// its named exports.
const cjsPath = `./${lang}.js`;
const wrapperPath = path.join(RELEASE_DIR, 'msg', `${lang}.mjs`);
await fsPromises.writeFile(wrapperPath,
`import ${lang} from '${cjsPath}';
export const {
${exportedNames.map((name) => ` ${name},`).join('\n')}
} = ${lang};
`);
}));
}

However, hyphens are not legal in javascript names, which seems to be the root cause of the issue.

Reproduction steps

  1. Get Blockly through Create-package script.
  2. Import message files in index.js (ex: en-gb).
    import * as enGb from 'blockly/msg/en-gb';

Stack trace

ERROR in ./node_modules/blockly/msg/en-gb.mjs 1:9
Module parse failed: Unexpected token (1:9)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> import en-gb from './en-gb.js';
| export const {
|   LOGIC_HUE,
 @ ./src/index.js 8:0-42 16:18-22

Screenshots

No response

Browsers

No response

@devilhyt devilhyt added issue: bug Describes why the code or behaviour is wrong issue: triage Issues awaiting triage by a Blockly team member labels May 24, 2024
@BeksOmega BeksOmega assigned cpcallen and BeksOmega and unassigned cpcallen May 24, 2024
@BeksOmega BeksOmega removed the issue: triage Issues awaiting triage by a Blockly team member label May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Describes why the code or behaviour is wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants