diff --git a/.changeset/light-trainers-obey.md b/.changeset/light-trainers-obey.md new file mode 100644 index 00000000..1ad70f93 --- /dev/null +++ b/.changeset/light-trainers-obey.md @@ -0,0 +1,5 @@ +--- +'microbundle': patch +--- + +Fixes CSS output from being overwritten when also generating .cjs diff --git a/src/index.js b/src/index.js index 62a160da..f47a6dda 100644 --- a/src/index.js +++ b/src/index.js @@ -20,7 +20,14 @@ import typescript from 'rollup-plugin-typescript2'; import json from '@rollup/plugin-json'; import OMT from '@surma/rollup-plugin-off-main-thread'; import logError from './log-error'; -import { isDir, isFile, stdout, isTruthy, removeScope } from './utils'; +import { + EXTENSION, + isDir, + isFile, + isTruthy, + stdout, + removeScope, +} from './utils'; import { getSizeInfo } from './lib/compressed-size'; import { normalizeMinifyOptions } from './lib/terser'; import { @@ -279,17 +286,12 @@ function getMain({ options, entry, format }) { let mainNoExtension = options.output; if (options.multipleEntries) { - let name = entry.match( - /([\\/])index(\.(umd|cjs|es|m))?\.(mjs|cjs|[tj]sx?)$/, - ) + let name = entry.match(new RegExp(/([\\/])index/.source + EXTENSION.source)) ? mainNoExtension : entry; mainNoExtension = resolve(dirname(mainNoExtension), basename(name)); } - mainNoExtension = mainNoExtension.replace( - /(\.(umd|cjs|es|m))?\.(mjs|cjs|[tj]sx?)$/, - '', - ); + mainNoExtension = mainNoExtension.replace(EXTENSION, ''); const mainsByFormat = {}; @@ -482,10 +484,7 @@ function createConfig(options, entry, format, writeMeta) { extract: !!writeMeta && options.css !== 'inline' && - options.output.replace( - /(\.(umd|cjs|es|m))?\.(mjs|[tj]sx?)$/, - '.css', - ), + options.output.replace(EXTENSION, '.css'), minimize: options.compress, sourceMap: options.sourcemap && options.css !== 'inline', }), diff --git a/src/utils.js b/src/utils.js index dbb5e82f..aa3b8449 100644 --- a/src/utils.js +++ b/src/utils.js @@ -43,3 +43,5 @@ export function safeVariableName(name) { const identifier = normalized.replace(INVALID_ES3_IDENT, ''); return camelCase(identifier); } + +export const EXTENSION = /(\.(umd|cjs|es|m))?\.([cm]?[tj]sx?)$/;