0.6.0 • Published 8 months ago

@masknet/static-module-record-swc v0.6.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

@masknet/static-module-record-swc

WARNING: This package currently does not follow the Semantic Versioning because the original standard is still developing. Minor version might include breaking changes!

A @swc/core plugin to convert an ES Module into a VirtualModuleSource.

Usage

Add it in @swc/core config

const config = {
    jsc: {
        experimental: {
            plugins: [
                [
                    '@masknet/static-module-record-swc',
                    // see "config" section below
                    {},
                ],
            ],
        },
    },
}

Configs

config.template

Specify the emit template

config.template.type: "export-default"

This is the default option.

[
    "@masknet/static-module-record-swc",
    {
        "template": {
            "type": "export-default"
        }
    }
]

It will convert code

import { writeFile } from 'node:fs/promises'

export const url = new URL('./here.txt', import.meta.url)
await writeFile(url, 'Hello World')

into this file

config.template.type: "eval"

[
    "@masknet/static-module-record-swc",
    {
        "template": {
            "type": "eval"
        }
    }
]

It will convert code

import { writeFile } from 'node:fs/promises'

export const url = new URL('./here.txt', import.meta.url)
await writeFile(url, 'Hello World')

into this file

config.template.type: "callback"

[
    "@masknet/static-module-record-swc",
    { "template": { "type": "callback", "callback": "__register", "firstArg": "/index.js" } }
]

It will convert code

import { writeFile } from 'node:fs/promises'

export const url = new URL('./here.txt', import.meta.url)
await writeFile(url, 'Hello World')

into this file

config.template.type: "callback-cwd"

It is similar to callback but it will try to infer the file name to URI as the first argument of the callback.

Let's take /home/jack/aot-ses/packages/static-module-record-swc/tests/fixtures/example-callback-infer.js as an example.

[
    "@masknet/static-module-record-swc",
    {
        "template": {
            "type": "callback-cwd",
            "callback": "__register",
            "cwd": "/home/jack/aot-ses/packages/static-module-record-swc/"
        }
    }
]

Warning: due to the limitation of the swc plugin system, we need a cwd to resolve the file name into URI. This cwd must contain all input files, otherwise, it will panic.

import { writeFile } from 'node:fs/promises'

export const url = new URL('./here.txt', import.meta.url)
await writeFile(url, 'Hello World')

into this file

0.6.0

8 months ago

0.5.0

1 year ago

0.3.5

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.0

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago

1.0.0

2 years ago