monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

[Bug] Used exports are excluded with the "unused export" comment when using export * as ...

Open mifopen opened this issue 3 years ago • 1 comments

Reproducible in vscode.dev or in VS Code Desktop?

  • [X] Not reproducible in vscode.dev or VS Code Desktop

Reproducible in the monaco editor playground?

Monaco Editor Playground Code

No response

Actual Behavior

Steps to reproduce:

  1. Clone https://github.com/microsoft/monaco-editor
  2. cd samples/browser-esm-webpack-monaco-plugin
  3. Add file file.js with one line export * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
  4. Replace import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'; in index.js with import { monaco } from './file';
  5. NODE_ENV=production npm run build

Result: dist/main.bundle.js should contain _common_network_js__WEBPACK_IMPORTED_MODULE_8__/* .RemoteAuthorities.setPreferredWebSchema */ .WX.setPreferredWebSchema(/^https:/.test(window.location.href) ? 'https' : 'http'); but instead it has /* unused export .RemoteAuthorities.setPreferredWebSchema */ undefined(/^https:/.test(window.location.href) ? 'https' : 'http'); which cause a runtime error (you can open dist/index.html in browser to see)

You can switch between the old line in index.js and the new one to see the difference. Works similar in development mode.

Expected Behavior

Shouldn't exclude used exports

Additional Context

It's not the webpack bug https://github.com/webpack/webpack/issues/15346

mifopen avatar Feb 08 '22 16:02 mifopen

Also, I can confirm that changing

        ...pre.map((include) => `require(${stringifyRequest(include)});`),
        `module.exports = require(${stringifyRequest(`!!${remainingRequest}`)});`,
        ...post.map((include) => `require(${stringifyRequest(include)});`)

to

        ...pre.map((include) => `import(${stringifyRequest(include)});`),
        `import * as xxx from ${stringifyRequest(`!!${remainingRequest}`)};`,
        `export default xxx;`,
        ...post.map((include) => `import(${stringifyRequest(include)});`)

inside include.ts helps

mifopen avatar Feb 09 '22 09:02 mifopen