Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Corrects CSS output being overwritten when .cjs ext used #919

Merged
merged 2 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/light-trainers-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'microbundle': patch
---

Fixes CSS output from being overwritten when also generating .cjs
23 changes: 11 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = {};

Expand Down Expand Up @@ -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'),
Comment on lines -485 to +487
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only regex that actually needed to be corrected. #802 missed this when adding in support, which is totally understandable given that the regex existed in 3 separate locations. Hopefully extracting this out makes it easier to handle.

minimize: options.compress,
sourceMap: options.sourcemap && options.css !== 'inline',
}),
Expand Down
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?)$/;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Names are hard... 😅

Certainly open to suggestions

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also (horrifyingly) supports the .cts and .mts extensions, as it looks like TS might head down that path