Created
June 11, 2024 05:37
-
-
Save whoisYeshua/e85144970276a993028be54d7516e6f9 to your computer and use it in GitHub Desktop.
Build ESM to UMD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { transformFile } from '@swc/core' | |
import { writeFile } from 'node:fs/promises' | |
import { resolve } from 'node:path' | |
/** @type {import('@swc/core').Options} */ | |
const baseSwcOptions = { | |
jsc: { | |
target: 'es2022', | |
parser: { | |
syntax: 'ecmascript', | |
jsx: false, | |
}, | |
}, | |
module: { | |
type: 'umd', | |
strict: false, | |
strictMode: true, | |
lazy: false, | |
noInterop: false, | |
}, | |
} | |
/** | |
* Трансформация ESM сборки в UMD | |
* @param {import('@swc/core').Options} extrnalSwcOptions | |
* @param {string} inputFile - путь до `entry` файла с ESM-сборкой. По умолчанию: `lib/index.js` | |
* @param {string} outputFile - `output`-путь для трансформированного файла. По умолчанию: `lib/index.umd.js` | |
*/ | |
export const buildUmd = async ( | |
extrnalSwcOptions = {}, | |
inputFile = 'lib/index.js', | |
outputFile = 'lib/index.umd.js' | |
) => { | |
try { | |
console.log('UMD Transform start') | |
const fullInputFilePath = resolve(inputFile) | |
const fullOutputFilePath = resolve(outputFile) | |
const swcOptions = { ...baseSwcOptions, ...extrnalSwcOptions } | |
const result = await transformFile(fullInputFilePath, swcOptions) | |
await writeFile(fullOutputFilePath, result.code) | |
console.log(`UMD File Transform success: ${fullOutputFilePath}`) | |
} catch (error) { | |
console.error('UMD Error transforming file:\n', error) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment