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: support decorators with CSS side-effects #5305

Merged
merged 1 commit into from
Jan 30, 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/honest-crabs-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Support decorators in files using CSS side-effect imports
251 changes: 251 additions & 0 deletions packages/remix-dev/__tests__/cssSideEffectsPlugin-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,255 @@ describe("addSuffixToCssSideEffectImports", () => {
`);
});
});

describe("parser support for language features", () => {
function languageFeaturesFixture(options: { ts: boolean; jsx: boolean }) {
let tsLanguageFeatures = dedent`
// TS
const exampleSatisfies = 'satisfies' satisfies string;
enum ExampleEnum {
Foo,
Bar,
Baz,
}
`;

let jsxLanguageFeatures = dedent`
// JSX
const ExampleComponent = () => <div>JSX element</div>;
`;

let jsLanguageFeatures = dedent`
// JS
const topLevelAwait = await Promise.resolve('top level await');
function classDecorator(target) {
return target;
}
function methodDecorator(target) {
return target;
}
@classDecorator
class ExampleClass {
#privateField;
#privateFieldWithInitializer = 'private field with initializer';
#privateMethod() {
return 'private method';
}
@methodDecorator
decoratedMethod() {
return 'decorated method';
}
}
const numericSeparator = 1_000_000;
const nullishCoalescing = null ?? 'nullish coalescing';
const optionalChaining = (['optional', 'chaining'])?.join?.(' ');
let optionalCatchBinding;
try {
optionalCatchBinding = error();
} catch {
optionalCatchBinding = 'optional catch binding';
}
export async function* asyncGenerator() {
yield await Promise.resolve('async generator');
}
`;

return [
'require("./foo.css")',
...(options.ts ? [tsLanguageFeatures] : []),
...(options.jsx ? [jsxLanguageFeatures] : []),
jsLanguageFeatures,
].join("\n\n");
}

test("JS language features", () => {
let code = languageFeaturesFixture({ ts: false, jsx: false });

expect(addSuffixToCssSideEffectImports("js", code))
.toMatchInlineSnapshot(`
"require(\\"./foo.css?__remix_sideEffect__\\");

// JS
const topLevelAwait = await Promise.resolve('top level await');
function classDecorator(target) {
return target;
}
function methodDecorator(target) {
return target;
}
@classDecorator class
ExampleClass {
#privateField;
#privateFieldWithInitializer = 'private field with initializer';
#privateMethod() {
return 'private method';
}
@methodDecorator
decoratedMethod() {
return 'decorated method';
}
}
const numericSeparator = 1_000_000;
const nullishCoalescing = null ?? 'nullish coalescing';
const optionalChaining = ['optional', 'chaining']?.join?.(' ');
let optionalCatchBinding;
try {
optionalCatchBinding = error();
} catch {
optionalCatchBinding = 'optional catch binding';
}
export async function* asyncGenerator() {
yield await Promise.resolve('async generator');
}"
`);
});

test("JSX language features", () => {
let code = languageFeaturesFixture({ ts: false, jsx: true });

expect(addSuffixToCssSideEffectImports("jsx", code))
.toMatchInlineSnapshot(`
"require(\\"./foo.css?__remix_sideEffect__\\");

// JSX
const ExampleComponent = () => <div>JSX element</div>;

// JS
const topLevelAwait = await Promise.resolve('top level await');
function classDecorator(target) {
return target;
}
function methodDecorator(target) {
return target;
}
@classDecorator class
ExampleClass {
#privateField;
#privateFieldWithInitializer = 'private field with initializer';
#privateMethod() {
return 'private method';
}
@methodDecorator
decoratedMethod() {
return 'decorated method';
}
}
const numericSeparator = 1_000_000;
const nullishCoalescing = null ?? 'nullish coalescing';
const optionalChaining = ['optional', 'chaining']?.join?.(' ');
let optionalCatchBinding;
try {
optionalCatchBinding = error();
} catch {
optionalCatchBinding = 'optional catch binding';
}
export async function* asyncGenerator() {
yield await Promise.resolve('async generator');
}"
`);
});

test("TS language features", () => {
let code = languageFeaturesFixture({ ts: true, jsx: false });

expect(addSuffixToCssSideEffectImports("tsx", code))
.toMatchInlineSnapshot(`
"require(\\"./foo.css?__remix_sideEffect__\\");

// TS
const exampleSatisfies = ('satisfies' satisfies string);
enum ExampleEnum {
Foo,
Bar,
Baz,
}

// JS
const topLevelAwait = await Promise.resolve('top level await');
function classDecorator(target) {
return target;
}
function methodDecorator(target) {
return target;
}
@classDecorator class
ExampleClass {
#privateField;
#privateFieldWithInitializer = 'private field with initializer';
#privateMethod() {
return 'private method';
}
@methodDecorator
decoratedMethod() {
return 'decorated method';
}
}
const numericSeparator = 1_000_000;
const nullishCoalescing = null ?? 'nullish coalescing';
const optionalChaining = ['optional', 'chaining']?.join?.(' ');
let optionalCatchBinding;
try {
optionalCatchBinding = error();
} catch {
optionalCatchBinding = 'optional catch binding';
}
export async function* asyncGenerator() {
yield await Promise.resolve('async generator');
}"
`);
});

test("TSX language features", () => {
let code = languageFeaturesFixture({ ts: true, jsx: true });

expect(addSuffixToCssSideEffectImports("tsx", code))
.toMatchInlineSnapshot(`
"require(\\"./foo.css?__remix_sideEffect__\\");

// TS
const exampleSatisfies = ('satisfies' satisfies string);
enum ExampleEnum {
Foo,
Bar,
Baz,
}

// JSX
const ExampleComponent = () => <div>JSX element</div>;

// JS
const topLevelAwait = await Promise.resolve('top level await');
function classDecorator(target) {
return target;
}
function methodDecorator(target) {
return target;
}
@classDecorator class
ExampleClass {
#privateField;
#privateFieldWithInitializer = 'private field with initializer';
#privateMethod() {
return 'private method';
}
@methodDecorator
decoratedMethod() {
return 'decorated method';
}
}
const numericSeparator = 1_000_000;
const nullishCoalescing = null ?? 'nullish coalescing';
const optionalChaining = ['optional', 'chaining']?.join?.(' ');
let optionalCatchBinding;
try {
optionalCatchBinding = error();
} catch {
optionalCatchBinding = 'optional catch binding';
}
export async function* asyncGenerator() {
yield await Promise.resolve('async generator');
}"
`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ export const cssSideEffectImportsPlugin = ({
};
};

const additionalLanguageFeatures: ParserOptions["plugins"] = ["decorators"];

const babelPluginsForLoader: Record<Loader, ParserOptions["plugins"]> = {
js: [],
jsx: ["jsx"],
ts: ["typescript"],
tsx: ["typescript", "jsx"],
js: [...additionalLanguageFeatures],
jsx: ["jsx", ...additionalLanguageFeatures],
ts: ["typescript", ...additionalLanguageFeatures],
tsx: ["typescript", "jsx", ...additionalLanguageFeatures],
};

const cache = new LRUCache<string, string>({ max: 1000 });
Expand Down