Skip to content

rewriteRelativeImportExtensions not working with inline await import #11360

@jsteelz

Description

@jsteelz

Describe the bug

swc is leaving a trailing _ts_rewrite_relative_import_extension wrapper on await import statements when using rewriteRelativeImportExtensions: true option in jsc.

This is not expected for me and predictably causes ReferenceError: _ts_rewrite_relative_import_extension is not defined when running output code.

Please excuse me if I've mis-configured or missed something.

Input code

import path from 'path';
import fs from 'fs-extra';

export async function loadNode(file: string) {
  const pathToFile = path.resolve('base', `${file}.ts`);
  if (await fs.pathExists(pathToFile)) {
    const { default: _default, ...rest } = await import(pathToFile);
    return rest;
  }
  return {};
}

Config

{
  "$schema": "https://swc.rs/schema.json",
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true,
      "dynamicImport": true
    },
    "rewriteRelativeImportExtensions": true,
    "target": "es2024",
  },
  "module": {
    "type": "es6"
  },
  "sourceMaps": true,
  "isModule": true
}

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.13.20&code=H4sIAAAAAAAAA02PMU%2FDMBCFd%2F%2BKG5CSSK3ZGzHCiBjYW5OcwVJqV74LDbL83%2FHFKWU7v%2FfufWd3voTIcDH8BTaGMzQyNr1y1bC0yZb2uHA0dyt9GMK3ks5bRD%2BKspcCzdQohcsaNPTjB7CzH9gFD1Mw42sYsbVuwgMQR%2Bc%2FO0gKYAie6jHv4aW48LQ%2BdEQK0ze2N%2BIOTg9J1nMBnbq%2BrDoLrbkaJydrWXpeHDG197KuMm6UBCNaM098gOM27UBrYTHkQq5t9bP%2Fa%2Fq1JCLP0YOkRcjqT0q5V1n9AkCRo3tZAQAA&config=H4sIAAAAAAAAA02OOw7CQAxEe04RWZSIoAhR0FNQ0HCD1cbAouxHtgNEUe7OfiDQ2Z434xkXVQVL1je0CvYV3EQC7%2Buan3pNXBdhfWfvYJXQO%2BuIjXGMS1DESPMeLzw4Ua8UJENA1mSCZGNWhZN0UR3jfGsHp6zRRxs8SVSFeszaVBAgfJIRPGOnxDywgIeXoGPjHX8sH1gUXTHFAHKzabb5d04C69u%2Bw1%2F5VLCAO5gh9j1pPKnwnwuGT19zrjct3tn52CI3AQAA

SWC Info output

No response

Expected behavior

The await import statement should be left as is, or if transforming is done, there should not be a leftover _ts_rewrite_relative_import_extension in the output.

Actual behavior

_ts_rewrite_relative_import_extension is left in the output, but this is never defined.

Version

1.13.20

Additional context

Originally encountered on 1.15.3, but swc-playground indicates it is still occurring in latest.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions