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

Add support for mjs and cjs extensions with CSS imports #5564

Merged
merged 7 commits into from
Mar 14, 2023
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/css-side-effect-import-extensions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Add support for `.mjs` and `.cjs` extensions when detecting CSS side-effect imports
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
- derekr
- derenge
- developit
- devongovett
- dgurns
- dhargitai
- dhmacs
Expand Down
14 changes: 9 additions & 5 deletions integration/css-side-effect-imports-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
createFixture,
css,
js,
json,
} from "./helpers/create-fixture";

const TEST_PADDING_VALUE = "20px";
Expand Down Expand Up @@ -75,7 +76,7 @@ test.describe("CSS side-effect imports", () => {
`,
"app/routes/basic-side-effect-test.jsx": js`
import "../basicSideEffect/styles.css";

export default function() {
return (
<div data-testid="basic-side-effect" className="basicSideEffect">
Expand Down Expand Up @@ -104,7 +105,7 @@ test.describe("CSS side-effect imports", () => {
`,
"app/routes/root-relative-test.jsx": js`
import "~/rootRelative/styles.css";

export default function() {
return (
<div data-testid="root-relative" className="rootRelative">
Expand Down Expand Up @@ -139,7 +140,7 @@ test.describe("CSS side-effect imports", () => {
`,
"app/routes/image-urls-test.jsx": js`
import "../imageUrls/styles.css";

export default function() {
return (
<div data-testid="image-urls" className="imageUrls">
Expand Down Expand Up @@ -179,7 +180,7 @@ test.describe("CSS side-effect imports", () => {
`,
"app/routes/root-relative-image-urls-test.jsx": js`
import "../rootRelativeImageUrls/styles.css";

export default function() {
return (
<div data-testid="root-relative-image-urls" className="rootRelativeImageUrls">
Expand Down Expand Up @@ -252,7 +253,7 @@ test.describe("CSS side-effect imports", () => {
padding: ${TEST_PADDING_VALUE};
}
`,
"node_modules/@test-package/esm/index.js": js`
"node_modules/@test-package/esm/index.mjs": js`
import React from 'react';
import './styles.css';

Expand All @@ -267,6 +268,9 @@ test.describe("CSS side-effect imports", () => {
);
};
`,
"node_modules/@test-package/esm/package.json": json({
exports: './index.mjs'
}),
"app/routes/esm-package-test.jsx": js`
import { Test } from "@test-package/esm";
export default function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ export function isCssSideEffectImportPath(path: string): boolean {
return cssSideEffectFilter.test(path);
}

const loaders = ["js", "jsx", "ts", "tsx"] as const;
const allJsFilesFilter = new RegExp(`\\.(${loaders.join("|")})$`);
const extensions = ["js", "jsx", "ts", "tsx", "mjs", "cjs"] as const;
const allJsFilesFilter = new RegExp(`\\.(${extensions.join("|")})$`);

type Loader = typeof loaders[number];
type Extension = `.${Loader}`;
type Loader = 'js' | 'jsx' | 'ts' | 'tsx';
type Extension = `.${typeof extensions[number]}`;

const loaderForExtension: Record<Extension, Loader> = {
".js": "js",
".jsx": "jsx",
".ts": "ts",
".tsx": "tsx",
".mjs": "js",
".cjs": "js"
};

/**
Expand Down