diff --git a/package.json b/package.json index f71c263b..e5b272b8 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "untyped": "^1.4.2" }, "devDependencies": { + "@babel/plugin-transform-class-properties": "^7.24.6", "@types/node": "^20.14.1", "@vitest/coverage-v8": "^1.6.0", "changelogen": "^0.5.5", @@ -63,8 +64,8 @@ "eslint-config-unjs": "^0.3.2", "prettier": "^3.3.0", "typescript": "^5.4.5", - "vitest": "^1.6.0", - "unbuild": "workspace:." + "unbuild": "workspace:.", + "vitest": "^1.6.0" }, "peerDependencies": { "typescript": "^5.4.5" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b3d7032c..5f4ac94a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -84,6 +84,9 @@ importers: specifier: ^1.4.2 version: 1.4.2 devDependencies: + '@babel/plugin-transform-class-properties': + specifier: ^7.24.6 + version: 7.24.6(@babel/core@7.24.6) '@types/node': specifier: ^20.14.1 version: 20.14.1 @@ -134,10 +137,20 @@ packages: resolution: {integrity: sha512-S7m4eNa6YAPJRHmKsLHIDJhNAGNKoWNiWefz1MBbpnt8g9lvMDl1hir4P9bo/57bQEmuwEhnRU/AMWsD0G/Fbg==} engines: {node: '>=6.9.0'} + '@babel/helper-annotate-as-pure@7.24.6': + resolution: {integrity: sha512-DitEzDfOMnd13kZnDqns1ccmftwJTS9DMkyn9pYTxulS7bZxUxpMly3Nf23QQ6NwA4UB8lAqjbqWtyvElEMAkg==} + engines: {node: '>=6.9.0'} + '@babel/helper-compilation-targets@7.24.6': resolution: {integrity: sha512-VZQ57UsDGlX/5fFA7GkVPplZhHsVc+vuErWgdOiysI9Ksnw0Pbbd6pnPiR/mmJyKHgyIW0c7KT32gmhiF+cirg==} engines: {node: '>=6.9.0'} + '@babel/helper-create-class-features-plugin@7.24.6': + resolution: {integrity: sha512-djsosdPJVZE6Vsw3kk7IPRWethP94WHGOhQTc67SNXE0ZzMhHgALw8iGmYS0TD1bbMM0VDROy43od7/hN6WYcA==} + engines: {node: '>=6.9.0'} + peerDependencies: + '@babel/core': ^7.0.0 + '@babel/helper-environment-visitor@7.24.6': resolution: {integrity: sha512-Y50Cg3k0LKLMjxdPjIl40SdJgMB85iXn27Vk/qbHZCFx/o5XO3PSnpi675h1KEmmDb6OFArfd5SCQEQ5Q4H88g==} engines: {node: '>=6.9.0'} @@ -150,6 +163,10 @@ packages: resolution: {integrity: sha512-SF/EMrC3OD7dSta1bLJIlrsVxwtd0UpjRJqLno6125epQMJ/kyFmpTT4pbvPbdQHzCHg+biQ7Syo8lnDtbR+uA==} engines: {node: '>=6.9.0'} + '@babel/helper-member-expression-to-functions@7.24.6': + resolution: {integrity: sha512-OTsCufZTxDUsv2/eDXanw/mUZHWOxSbEmC3pP8cgjcy5rgeVPWWMStnv274DV60JtHxTk0adT0QrCzC4M9NWGg==} + engines: {node: '>=6.9.0'} + '@babel/helper-module-imports@7.24.6': resolution: {integrity: sha512-a26dmxFJBF62rRO9mmpgrfTLsAuyHk4e1hKTUkD/fcMfynt8gvEKwQPQDVxWhca8dHoDck+55DFt42zV0QMw5g==} engines: {node: '>=6.9.0'} @@ -160,10 +177,28 @@ packages: peerDependencies: '@babel/core': ^7.0.0 + '@babel/helper-optimise-call-expression@7.24.6': + resolution: {integrity: sha512-3SFDJRbx7KuPRl8XDUr8O7GAEB8iGyWPjLKJh/ywP/Iy9WOmEfMrsWbaZpvBu2HSYn4KQygIsz0O7m8y10ncMA==} + engines: {node: '>=6.9.0'} + + '@babel/helper-plugin-utils@7.24.6': + resolution: {integrity: sha512-MZG/JcWfxybKwsA9N9PmtF2lOSFSEMVCpIRrbxccZFLJPrJciJdG/UhSh5W96GEteJI2ARqm5UAHxISwRDLSNg==} + engines: {node: '>=6.9.0'} + + '@babel/helper-replace-supers@7.24.6': + resolution: {integrity: sha512-mRhfPwDqDpba8o1F8ESxsEkJMQkUF8ZIWrAc0FtWhxnjfextxMWxr22RtFizxxSYLjVHDeMgVsRq8BBZR2ikJQ==} + engines: {node: '>=6.9.0'} + peerDependencies: + '@babel/core': ^7.0.0 + '@babel/helper-simple-access@7.24.6': resolution: {integrity: sha512-nZzcMMD4ZhmB35MOOzQuiGO5RzL6tJbsT37Zx8M5L/i9KSrukGXWTjLe1knIbb/RmxoJE9GON9soq0c0VEMM5g==} engines: {node: '>=6.9.0'} + '@babel/helper-skip-transparent-expression-wrappers@7.24.6': + resolution: {integrity: sha512-jhbbkK3IUKc4T43WadP96a27oYti9gEf1LdyGSP2rHGH77kwLwfhO7TgwnWvxxQVmke0ImmCSS47vcuxEMGD3Q==} + engines: {node: '>=6.9.0'} + '@babel/helper-split-export-declaration@7.24.6': resolution: {integrity: sha512-CvLSkwXGWnYlF9+J3iZUvwgAxKiYzK3BWuo+mLzD/MDGOZDj7Gq8+hqaOkMxmJwmlv0iu86uH5fdADd9Hxkymw==} engines: {node: '>=6.9.0'} @@ -193,6 +228,12 @@ packages: engines: {node: '>=6.0.0'} hasBin: true + '@babel/plugin-transform-class-properties@7.24.6': + resolution: {integrity: sha512-j6dZ0Z2Z2slWLR3kt9aOmSIrBvnntWjMDN/TVcMPxhXMLmJVqX605CBRlcGI4b32GMbfifTEsdEjGjiE+j/c3A==} + engines: {node: '>=6.9.0'} + peerDependencies: + '@babel/core': ^7.0.0-0 + '@babel/standalone@7.24.6': resolution: {integrity: sha512-ch8nbtobUPLvSLKdG2s8pVAqS1zUc+mt7UE9k8/xpupvETbAFOaoqo0QcpgVD/f0xkMkbUnqedVY5eeVWOqtjw==} engines: {node: '>=6.9.0'} @@ -2463,6 +2504,10 @@ snapshots: '@jridgewell/trace-mapping': 0.3.25 jsesc: 2.5.2 + '@babel/helper-annotate-as-pure@7.24.6': + dependencies: + '@babel/types': 7.24.6 + '@babel/helper-compilation-targets@7.24.6': dependencies: '@babel/compat-data': 7.24.6 @@ -2471,6 +2516,19 @@ snapshots: lru-cache: 5.1.1 semver: 6.3.1 + '@babel/helper-create-class-features-plugin@7.24.6(@babel/core@7.24.6)': + dependencies: + '@babel/core': 7.24.6 + '@babel/helper-annotate-as-pure': 7.24.6 + '@babel/helper-environment-visitor': 7.24.6 + '@babel/helper-function-name': 7.24.6 + '@babel/helper-member-expression-to-functions': 7.24.6 + '@babel/helper-optimise-call-expression': 7.24.6 + '@babel/helper-replace-supers': 7.24.6(@babel/core@7.24.6) + '@babel/helper-skip-transparent-expression-wrappers': 7.24.6 + '@babel/helper-split-export-declaration': 7.24.6 + semver: 6.3.1 + '@babel/helper-environment-visitor@7.24.6': {} '@babel/helper-function-name@7.24.6': @@ -2482,6 +2540,10 @@ snapshots: dependencies: '@babel/types': 7.24.6 + '@babel/helper-member-expression-to-functions@7.24.6': + dependencies: + '@babel/types': 7.24.6 + '@babel/helper-module-imports@7.24.6': dependencies: '@babel/types': 7.24.6 @@ -2495,10 +2557,27 @@ snapshots: '@babel/helper-split-export-declaration': 7.24.6 '@babel/helper-validator-identifier': 7.24.6 + '@babel/helper-optimise-call-expression@7.24.6': + dependencies: + '@babel/types': 7.24.6 + + '@babel/helper-plugin-utils@7.24.6': {} + + '@babel/helper-replace-supers@7.24.6(@babel/core@7.24.6)': + dependencies: + '@babel/core': 7.24.6 + '@babel/helper-environment-visitor': 7.24.6 + '@babel/helper-member-expression-to-functions': 7.24.6 + '@babel/helper-optimise-call-expression': 7.24.6 + '@babel/helper-simple-access@7.24.6': dependencies: '@babel/types': 7.24.6 + '@babel/helper-skip-transparent-expression-wrappers@7.24.6': + dependencies: + '@babel/types': 7.24.6 + '@babel/helper-split-export-declaration@7.24.6': dependencies: '@babel/types': 7.24.6 @@ -2525,6 +2604,12 @@ snapshots: dependencies: '@babel/types': 7.24.6 + '@babel/plugin-transform-class-properties@7.24.6(@babel/core@7.24.6)': + dependencies: + '@babel/core': 7.24.6 + '@babel/helper-create-class-features-plugin': 7.24.6(@babel/core@7.24.6) + '@babel/helper-plugin-utils': 7.24.6 + '@babel/standalone@7.24.6': {} '@babel/template@7.24.6': diff --git a/src/builder/rollup.ts b/src/builder/rollup.ts index 2735662b..9a605525 100644 --- a/src/builder/rollup.ts +++ b/src/builder/rollup.ts @@ -53,6 +53,9 @@ const DEFAULT_EXTENSIONS = [ export async function rollupBuild(ctx: BuildContext) { if (ctx.options.stub) { const jitiPath = await resolvePath("jiti", { url: import.meta.url }); + const babelPlugins = ctx.options.stubOptions.jiti.transformOptions?.babel + ?.plugins as any; + const importedBabelPlugins: Array = []; const serializedJitiOptions = JSON.stringify( { ...ctx.options.stubOptions.jiti, @@ -60,9 +63,41 @@ export async function rollupBuild(ctx: BuildContext) { ...resolveAliases(ctx), ...ctx.options.stubOptions.jiti.alias, }, + transformOptions: { + ...ctx.options.stubOptions.jiti.transformOptions, + babel: { + ...ctx.options.stubOptions.jiti.transformOptions?.babel, + plugins: "__$BABEL_PLUGINS", + }, + }, }, null, 2, + ).replace( + '"__$BABEL_PLUGINS"', + Array.isArray(babelPlugins) + ? "[" + + babelPlugins + .map((plugin: string | Array, i) => { + if (Array.isArray(plugin)) { + const [name, ...args] = plugin; + importedBabelPlugins.push(name); + return ( + `[` + + [ + `plugin${i}`, + ...args.map((val) => JSON.stringify(val)), + ].join(", ") + + "]" + ); + } else { + importedBabelPlugins.push(plugin); + return `plugin${i}`; + } + }) + .join(",") + + "]" + : "[]", ); for (const entry of ctx.options.entries.filter( @@ -97,6 +132,10 @@ export async function rollupBuild(ctx: BuildContext) { shebang + [ `const jiti = require(${JSON.stringify(jitiPath)})`, + ...importedBabelPlugins.map( + (plugin, i) => + `const plugin${i} = require(${JSON.stringify(plugin)})`, + ), "", `const _jiti = jiti(null, ${serializedJitiOptions})`, "", @@ -127,6 +166,9 @@ export async function rollupBuild(ctx: BuildContext) { shebang + [ `import jiti from ${JSON.stringify(pathToFileURL(jitiPath).href)};`, + ...importedBabelPlugins.map( + (plugin, i) => `import plugin${i} from ${JSON.stringify(plugin)}`, + ), "", `const _jiti = jiti(null, ${serializedJitiOptions})`, "", diff --git a/test/fixture/build.config.ts b/test/fixture/build.config.ts index f522fcc4..d7064b5c 100644 --- a/test/fixture/build.config.ts +++ b/test/fixture/build.config.ts @@ -21,6 +21,15 @@ export default defineBuildConfig([ }, { input: "src/schema", builder: "untyped" }, ], + stubOptions: { + jiti: { + transformOptions: { + babel: { + plugins: [["@babel/plugin-transform-class-properties"]], + }, + }, + }, + }, }, // Minified with sourcemaps {