From 530b9ea2055af62721831605a3616cbe722bf3fb Mon Sep 17 00:00:00 2001 From: Sung Ye In <66503450+syi0808@users.noreply.github.com> Date: Fri, 12 Jul 2024 03:08:11 +0900 Subject: [PATCH] fix(utils): setup extension by format in getRollupOutput (#244) BREAKING CHANGE: Built files now have `.mjs` or `.cjs` for ESM and CJS respectively. You may need to update your exports configuration for your library. --- src/__tests__/utils.js | 14 ++++++++++++++ src/utils.js | 7 ++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/__tests__/utils.js b/src/__tests__/utils.js index c658fbc0..f07d5c91 100644 --- a/src/__tests__/utils.js +++ b/src/__tests__/utils.js @@ -190,3 +190,17 @@ function mockCosmiconfig(result = null) { cosmiconfigSync.mockImplementationOnce(() => ({search: () => result})) } + +test.each([ + {format: 'cjs', extension: '.cjs'}, + {format: 'esm', extension: '.mjs'}, + {format: 'umd', extension: '.js'}, + {format: 'amd', extension: '.js'}, +])( + 'file extension in rollupOutput with $format should be $extension', + ({format, extension}) => { + expect( + require('../utils').getRollupOutput(format).filename.endsWith(extension), + ).toBeTruthy() + }, +) diff --git a/src/utils.js b/src/utils.js index 0f761e57..a515cead 100644 --- a/src/utils.js +++ b/src/utils.js @@ -210,12 +210,17 @@ function getRollupInputs() { function getRollupOutput(format = process.env.BUILD_FORMAT) { const minify = parseEnv('BUILD_MINIFY', false) const filenameSuffix = process.env.BUILD_FILENAME_SUFFIX || '' + const ext = + { + esm: '.mjs', + cjs: '.cjs', + }[format] || '.js' const filename = [ pkg.name, filenameSuffix, `.${format}`, minify ? '.min' : null, - '.js', + ext, ] .filter(Boolean) .join('')