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

feat(core): support swc's ignoreDynamic option #745

Merged
merged 2 commits into from
Jan 20, 2024

Conversation

SukkaW
Copy link
Contributor

@SukkaW SukkaW commented Dec 10, 2023

Add the ignoreDynamic option to the @swc-node/core, and the SWC_NODE_IGNORE_DYNAMIC env option.


Given the following the CLI written in TypeScript:

#!/usr/bin/env node

import { arg } from 'arg'

async function main() {
  // some CLI stuff
  // ...

  arg();

  const { method } = await import('some-pure-esm-module');

  // other stuff
}

main().catch(exit)

SWC emits the following output when transpiling it into CommonJ,:

#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
const _arg = require("arg");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
async function main() {
    (0, _arg.arg)();
    const { method } = await Promise.resolve().then(()=>_interop_require_wildcard(require("some-pure-esm-module")));
}
main().catch(exit);

Notice that the import { arg } from 'arg' is transformed into const _arg = require("arg");, but the await import('some-pure-esm-module') is transformed into await Promise.resolve().then(()=>_interop_require_wildcard(require("some-pure-esm-module"))).

The SWC does provide an option to keep transforming import { arg } from 'arg' is transformed into const _arg = require("arg"); while retaining the await import('some-pure-esm-module'): module.ignoreDynamic. This option is handy when writing Node.js CLI with TypeScript.

The PR adds the ignoreDynamic option support for the swc-node:

SWC_NODE_IGNORE_DYNAMIC=true node -r @swc-node/register src/bin/index.ts

I want to make options.ignoreDynamic enabled by default in @swc-node/register, but I am afraid this could introduce a breaking change. Thus I add SWC_NODE_IGNORE_DYNAMIC=true.

@Brooooooklyn Brooooooklyn merged commit 0e8eca2 into swc-project:master Jan 20, 2024
3 checks passed
austinbiggs referenced this pull request in austinbiggs/ivy Feb 3, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@swc-node/register](https://togithub.com/swc-project/swc-node) |
[`1.6.8` ->
`1.8.0`](https://renovatebot.com/diffs/npm/@swc-node%2fregister/1.6.8/1.8.0)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/@swc-node%2fregister/1.8.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@swc-node%2fregister/1.8.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@swc-node%2fregister/1.6.8/1.8.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@swc-node%2fregister/1.6.8/1.8.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>swc-project/swc-node (@&#8203;swc-node/register)</summary>

###
[`v1.8.0`](https://togithub.com/swc-project/swc-node/releases/tag/%40swc-node/register%401.8.0)

[Compare
Source](https://togithub.com/swc-project/swc-node/compare/@swc-node/register@1.6.8...@swc-node/register@1.8.0)

#### What's Changed

- fix(core): remove `jsc.module.noInterop` when it's not supported by
[@&#8203;kdy1](https://togithub.com/kdy1) in
[https://github.com/swc-project/swc-node/pull/733](https://togithub.com/swc-project/swc-node/pull/733)
- chore: fix README for install, build, and test by
[@&#8203;devjiwonchoi](https://togithub.com/devjiwonchoi) in
[https://github.com/swc-project/swc-node/pull/728](https://togithub.com/swc-project/swc-node/pull/728)
- feat(register): @&#8203;swc-node/register/esm use TypeScript resolver
by [@&#8203;cm-ayf](https://togithub.com/cm-ayf) in
[https://github.com/swc-project/swc-node/pull/727](https://togithub.com/swc-project/swc-node/pull/727)
- chore(deps-dev): bump
[@&#8203;babel/traverse](https://togithub.com/babel/traverse) from
7.22.10 to 7.23.2 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/swc-project/swc-node/pull/735](https://togithub.com/swc-project/swc-node/pull/735)
- Fix
[#&#8203;716](https://togithub.com/swc-project/swc-node/issues/716) by
[@&#8203;eliellis](https://togithub.com/eliellis) in
[https://github.com/swc-project/swc-node/pull/725](https://togithub.com/swc-project/swc-node/pull/725)
- chore(deps-dev): bump axios from 1.3.5 to 1.6.1 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/swc-project/swc-node/pull/738](https://togithub.com/swc-project/swc-node/pull/738)
- fix(core): respect useDefineForClassFields tsconfig value by
[@&#8203;dsyddall](https://togithub.com/dsyddall) in
[https://github.com/swc-project/swc-node/pull/740](https://togithub.com/swc-project/swc-node/pull/740)
- fix(core): respect sourceMaps swcrc value by
[@&#8203;dsyddall](https://togithub.com/dsyddall) in
[https://github.com/swc-project/swc-node/pull/742](https://togithub.com/swc-project/swc-node/pull/742)
- feat: add esm-register for node>20.6 by
[@&#8203;yeliex](https://togithub.com/yeliex) in
[https://github.com/swc-project/swc-node/pull/748](https://togithub.com/swc-project/swc-node/pull/748)
- chore(deps-dev): bump follow-redirects from 1.15.2 to 1.15.4 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/swc-project/swc-node/pull/747](https://togithub.com/swc-project/swc-node/pull/747)
- feat(core): support swc's `ignoreDynamic` option by
[@&#8203;SukkaW](https://togithub.com/SukkaW) in
[https://github.com/swc-project/swc-node/pull/745](https://togithub.com/swc-project/swc-node/pull/745)
- fix: fix import ts from node_modules by
[@&#8203;yeliex](https://togithub.com/yeliex) in
[https://github.com/swc-project/swc-node/pull/744](https://togithub.com/swc-project/swc-node/pull/744)
- Mention --enable-source-maps in README.md for esm projects. by
[@&#8203;Xinkai](https://togithub.com/Xinkai) in
[https://github.com/swc-project/swc-node/pull/737](https://togithub.com/swc-project/swc-node/pull/737)
- feat: upgrade dependencies by
[@&#8203;Brooooooklyn](https://togithub.com/Brooooooklyn) in
[https://github.com/swc-project/swc-node/pull/751](https://togithub.com/swc-project/swc-node/pull/751)

#### New Contributors

- [@&#8203;devjiwonchoi](https://togithub.com/devjiwonchoi) made their
first contribution in
[https://github.com/swc-project/swc-node/pull/728](https://togithub.com/swc-project/swc-node/pull/728)
- [@&#8203;cm-ayf](https://togithub.com/cm-ayf) made their first
contribution in
[https://github.com/swc-project/swc-node/pull/727](https://togithub.com/swc-project/swc-node/pull/727)
- [@&#8203;eliellis](https://togithub.com/eliellis) made their first
contribution in
[https://github.com/swc-project/swc-node/pull/725](https://togithub.com/swc-project/swc-node/pull/725)
- [@&#8203;dsyddall](https://togithub.com/dsyddall) made their first
contribution in
[https://github.com/swc-project/swc-node/pull/740](https://togithub.com/swc-project/swc-node/pull/740)
- [@&#8203;SukkaW](https://togithub.com/SukkaW) made their first
contribution in
[https://github.com/swc-project/swc-node/pull/745](https://togithub.com/swc-project/swc-node/pull/745)
- [@&#8203;Xinkai](https://togithub.com/Xinkai) made their first
contribution in
[https://github.com/swc-project/swc-node/pull/737](https://togithub.com/swc-project/swc-node/pull/737)

**Full Changelog**:
https://github.com/swc-project/swc-node/compare/[@&#8203;swc-node/core](https://togithub.com/swc-node/core)[@&#8203;1](https://togithub.com/1).10.5...[@&#8203;swc-node/register](https://togithub.com/swc-node/register)[@&#8203;1](https://togithub.com/1).8.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/austinbiggs/ivy).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xNTMuMiIsInVwZGF0ZWRJblZlciI6IjM3LjE1My4yIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->
tsang-bot bot referenced this pull request in tsangste/nx-package-test Feb 6, 2024
This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@swc-node/register](https://togithub.com/swc-project/swc-node) |
[`1.6.8` ->
`1.8.0`](https://renovatebot.com/diffs/npm/@swc-node%2fregister/1.6.8/1.8.0)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/@swc-node%2fregister/1.8.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@swc-node%2fregister/1.8.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@swc-node%2fregister/1.6.8/1.8.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@swc-node%2fregister/1.6.8/1.8.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
| [@swc/cli](https://togithub.com/swc-project/pkgs)
([source](https://togithub.com/node ./bin)) | [`0.3.5` ->
`0.3.6`](https://renovatebot.com/diffs/npm/@swc%2fcli/0.3.5/0.3.6) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/@swc%2fcli/0.3.6?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@swc%2fcli/0.3.6?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@swc%2fcli/0.3.5/0.3.6?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@swc%2fcli/0.3.5/0.3.6?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>swc-project/swc-node (@&#8203;swc-node/register)</summary>

###
[`v1.8.0`](https://togithub.com/swc-project/swc-node/releases/tag/%40swc-node/register%401.8.0)

[Compare
Source](https://togithub.com/swc-project/swc-node/compare/@swc-node/register@1.6.8...@swc-node/register@1.8.0)

#### What's Changed

- fix(core): remove `jsc.module.noInterop` when it's not supported by
[@&#8203;kdy1](https://togithub.com/kdy1) in
[https://github.com/swc-project/swc-node/pull/733](https://togithub.com/swc-project/swc-node/pull/733)
- chore: fix README for install, build, and test by
[@&#8203;devjiwonchoi](https://togithub.com/devjiwonchoi) in
[https://github.com/swc-project/swc-node/pull/728](https://togithub.com/swc-project/swc-node/pull/728)
- feat(register): @&#8203;swc-node/register/esm use TypeScript resolver
by [@&#8203;cm-ayf](https://togithub.com/cm-ayf) in
[https://github.com/swc-project/swc-node/pull/727](https://togithub.com/swc-project/swc-node/pull/727)
- chore(deps-dev): bump
[@&#8203;babel/traverse](https://togithub.com/babel/traverse) from
7.22.10 to 7.23.2 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/swc-project/swc-node/pull/735](https://togithub.com/swc-project/swc-node/pull/735)
- Fix
[#&#8203;716](https://togithub.com/swc-project/swc-node/issues/716) by
[@&#8203;eliellis](https://togithub.com/eliellis) in
[https://github.com/swc-project/swc-node/pull/725](https://togithub.com/swc-project/swc-node/pull/725)
- chore(deps-dev): bump axios from 1.3.5 to 1.6.1 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/swc-project/swc-node/pull/738](https://togithub.com/swc-project/swc-node/pull/738)
- fix(core): respect useDefineForClassFields tsconfig value by
[@&#8203;dsyddall](https://togithub.com/dsyddall) in
[https://github.com/swc-project/swc-node/pull/740](https://togithub.com/swc-project/swc-node/pull/740)
- fix(core): respect sourceMaps swcrc value by
[@&#8203;dsyddall](https://togithub.com/dsyddall) in
[https://github.com/swc-project/swc-node/pull/742](https://togithub.com/swc-project/swc-node/pull/742)
- feat: add esm-register for node>20.6 by
[@&#8203;yeliex](https://togithub.com/yeliex) in
[https://github.com/swc-project/swc-node/pull/748](https://togithub.com/swc-project/swc-node/pull/748)
- chore(deps-dev): bump follow-redirects from 1.15.2 to 1.15.4 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/swc-project/swc-node/pull/747](https://togithub.com/swc-project/swc-node/pull/747)
- feat(core): support swc's `ignoreDynamic` option by
[@&#8203;SukkaW](https://togithub.com/SukkaW) in
[https://github.com/swc-project/swc-node/pull/745](https://togithub.com/swc-project/swc-node/pull/745)
- fix: fix import ts from node_modules by
[@&#8203;yeliex](https://togithub.com/yeliex) in
[https://github.com/swc-project/swc-node/pull/744](https://togithub.com/swc-project/swc-node/pull/744)
- Mention --enable-source-maps in README.md for esm projects. by
[@&#8203;Xinkai](https://togithub.com/Xinkai) in
[https://github.com/swc-project/swc-node/pull/737](https://togithub.com/swc-project/swc-node/pull/737)
- feat: upgrade dependencies by
[@&#8203;Brooooooklyn](https://togithub.com/Brooooooklyn) in
[https://github.com/swc-project/swc-node/pull/751](https://togithub.com/swc-project/swc-node/pull/751)

#### New Contributors

- [@&#8203;devjiwonchoi](https://togithub.com/devjiwonchoi) made their
first contribution in
[https://github.com/swc-project/swc-node/pull/728](https://togithub.com/swc-project/swc-node/pull/728)
- [@&#8203;cm-ayf](https://togithub.com/cm-ayf) made their first
contribution in
[https://github.com/swc-project/swc-node/pull/727](https://togithub.com/swc-project/swc-node/pull/727)
- [@&#8203;eliellis](https://togithub.com/eliellis) made their first
contribution in
[https://github.com/swc-project/swc-node/pull/725](https://togithub.com/swc-project/swc-node/pull/725)
- [@&#8203;dsyddall](https://togithub.com/dsyddall) made their first
contribution in
[https://github.com/swc-project/swc-node/pull/740](https://togithub.com/swc-project/swc-node/pull/740)
- [@&#8203;SukkaW](https://togithub.com/SukkaW) made their first
contribution in
[https://github.com/swc-project/swc-node/pull/745](https://togithub.com/swc-project/swc-node/pull/745)
- [@&#8203;Xinkai](https://togithub.com/Xinkai) made their first
contribution in
[https://github.com/swc-project/swc-node/pull/737](https://togithub.com/swc-project/swc-node/pull/737)

**Full Changelog**:
https://github.com/swc-project/swc-node/compare/[@&#8203;swc-node/core](https://togithub.com/swc-node/core)[@&#8203;1](https://togithub.com/1).10.5...[@&#8203;swc-node/register](https://togithub.com/swc-node/register)[@&#8203;1](https://togithub.com/1).8.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config help](https://togithub.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xNzEuMCIsInVwZGF0ZWRJblZlciI6IjM3LjE3Mi4yIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCJ9-->

Co-authored-by: tsang-bot[bot] <146107447+tsang-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants