Skip to content

Latest commit

 

History

History
183 lines (139 loc) · 3.45 KB

README.md

File metadata and controls

183 lines (139 loc) · 3.45 KB

@builder/swc

swc version is 0.181.2

npm package

npm downloads

Custom swc for ice.js and rax-app.

Customization is achieved through the pass hook of swc.

Platform Support

  • Windows x64
  • Windows aarch64
  • Window ia32
  • macOS x64
  • macOS aarch64
  • Linux aarch64 gnu
  • Linux aarch64 musl
  • Linux x64 gnu
  • Linux x64 musl

API

transformSync(code: string, options)

Return { code: string, map?: string }

Synchronous transform code. You can pass @swc/core options directly.

import { transformSync } from '@builder/swc';

const { code, map } = transformSync('var a = 1;', {
  jsc: {
    parser: {
      syntax: "ecmascript",
    },
    transform: {},
  },
});

options.keepPlatform

Transform the variables which exported by universal-env to bool. The output code will remove other platform code by compressor.

import { transformSync } from '@builder/swc';

// Case 1: import specifier
// Input
var input = `
import { isWeb, isWeex } from 'universal-env';

if (isWeb) {
  console.log('This is web code');
} else {
  console.log('This is weex code');
}
`;

var { code, map } = transformSync(input, {
  jsc: {
    parser: {
      syntax: "ecmascript",
    },
    transform: {},
  },
  keepPlatform: 'web',
});

console.log(code);
/* The output code is:
var isWeb = true;
var isWeex = false;

if (isWeb) {
  console.log('This is web code');
} else {
  console.log('This is weex code');
}
*/

// Case 2: import namespace specifier
// Input
var input = `
import * as env from 'universal-env';

if (env.isWeb) {
  console.log('This is web code');
} else {
  console.log('This is weex code');
}
`;

var { code, map } = transformSync(input, {
  jsc: {
    parser: {
      syntax: "ecmascript",
    },
    transform: {},
  },
  keepPlatform: 'web',
});

console.log(code);
/* The output code is:
var env = {
  isWeb: true
};

if (env.isWeb) {
  console.log('This is web code');
} else {
  console.log('This is weex code');
}
*/

transform(code: string, options)

Return Promise<{ code: string, map?: string }>

Asynchronous transform code. The options is the same as transformSync.

import { transformSync } from '@builder/swc';

const { code, map } = await transformSync(input, {
  jsc: {
    parser: {
      syntax: "ecmascript",
    },
    transform: {},
  },
  keepPlatform: 'web',
});

minify

Return Promise<{ code: string, map?: string }>

It is the same as @swc/core.

This API is asynchronous and all of parsing, minification, and code generation will be done in background thread.

import { minify } from "@builder/swc";

const { code, map } = await minify(
  "import foo from '@src/app'; console.log(foo)",
  {
    compress: false,
    mangle: true,
  }
);

expect(code).toMatchInlineSnapshot(`"import a from'@src/app';console.log(a);"`);

minifySync

{ code: string, map?: string }

It is the same as @swc/core.

import { minifySync } from "@builder/swc";

const { code, map } = minifySync(
  "import foo from '@src/app'; console.log(foo)",
  {
    compress: false,
    mangle: true,
  }
);

expect(code).toMatchInlineSnapshot(`"import a from'@src/app';console.log(a);"`);