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

support remove type only import like tsc #756

Closed
ghost opened this issue Apr 22, 2020 · 4 comments
Closed

support remove type only import like tsc #756

ghost opened this issue Apr 22, 2020 · 4 comments

Comments

@ghost
Copy link

ghost commented Apr 22, 2020

support remove type only import like tsc

tsc example

import {funcA} from "./a"

export const read = () => {
    
}

now use tsc with cmd: tsc -m "amd" -t "es2015" src/test.ts

we get the code:

// Note:  the a module is removed in output code

define(["require", "exports"], function (require, exports) {
    "use strict";
    
    Object.defineProperty(exports, "__esModule", { value: true });
    
    exports.read = () => {
    };
    
});

swc example

const swc = require("@swc/core");
const {readFileSync} = require("fs");


let srcPath = "src/test.ts";

let {code} = swc.transformFileSync(srcPath, {
    minify: false,

    module: {
        type: "amd", // "amd", "commonjs"
        moduleId: srcPath.slice(0, srcPath.lastIndexOf(".")),
    },
    
    jsc: {
        target: "es2015", //  "es3" | "es5" | "es2015" | "es2016" | "es2017" | "es2018" | "es2019"
        
        parser: {
            syntax: "typescript",
            decorators: true,
            dynamicImport: true,
        },
    }
});

console.log(code);

run swc, we get code as follow, note the a module already exist.

define('src/test', [
    'exports',
    './a'
], function(_exports, _a) {
    'use strict';
    Object.defineProperty(_exports, '__esModule', {
        value: true
    });
    _exports.read = void 0;
    var read = function() {
    };
    _exports.read = read;
});

typescript reference, Module side-effects

https://github.com/microsoft/TypeScript/wiki/FAQ#Modules

https://www.typescriptlang.org/docs/handbook/modules.html

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-exports

@ghost
Copy link
Author

ghost commented Apr 22, 2020

swc version:

"@swc/cli": "^0.1.24",
"@swc/core": "^1.1.39",

@kdy1
Copy link
Member

kdy1 commented Apr 30, 2020

How can we know if funcA is type-only import?
In javascript, an import statement can have side effects.

If you use funcA only in type position, swc treat the import as type-only import and will remove it.

@kdy1 kdy1 closed this as completed May 1, 2020
@nayeemrmn
Copy link
Contributor

Ref denoland/deno#7413 (comment).

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 26, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants