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

feat: add new @rsbuild/plugin-typed-css-modules package #2340

Merged
merged 3 commits into from
May 14, 2024
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
8 changes: 2 additions & 6 deletions e2e/cases/css/css-modules-dom/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { join, resolve } from 'node:path';
import { build, gotoPage } from '@e2e/helper';
import { expect, test } from '@playwright/test';
import { pluginReact } from '@rsbuild/plugin-react';
import { pluginTypedCSSModules } from '@rsbuild/plugin-typed-css-modules';
import { fse } from '@rsbuild/shared';

const fixtures = resolve(__dirname);
Expand All @@ -12,12 +13,7 @@ test('enableCssModuleTSDeclaration', async () => {

await build({
cwd: fixtures,
plugins: [pluginReact()],
rsbuildConfig: {
output: {
enableCssModuleTSDeclaration: true,
},
},
plugins: [pluginReact(), pluginTypedCSSModules()],
});

expect(
Expand Down
11 changes: 5 additions & 6 deletions e2e/cases/css/css-modules-ts-declaration/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join, resolve } from 'node:path';
import { build } from '@e2e/helper';
import { expect, test } from '@playwright/test';
import { pluginTypedCSSModules } from '@rsbuild/plugin-typed-css-modules';
import { fse } from '@rsbuild/shared';

const fixtures = __dirname;
Expand All @@ -18,13 +19,11 @@ test('should generator ts declaration correctly for css modules auto true', asyn

await build({
cwd: __dirname,
plugins: [pluginTypedCSSModules()],
rsbuildConfig: {
source: {
entry: { index: resolve(testDir, 'index.js') },
},
output: {
enableCssModuleTSDeclaration: true,
},
},
});

Expand All @@ -51,12 +50,12 @@ test('should generator ts declaration correctly for css modules auto function',

await build({
cwd: __dirname,
plugins: [pluginTypedCSSModules()],
rsbuildConfig: {
source: {
entry: { index: resolve(testDir, 'index.js') },
},
output: {
enableCssModuleTSDeclaration: true,
cssModules: {
auto: (path) => {
return path.endsWith('.less');
Expand All @@ -80,12 +79,12 @@ test('should generator ts declaration correctly for css modules auto Regexp', as

await build({
cwd: __dirname,
plugins: [pluginTypedCSSModules()],
rsbuildConfig: {
source: {
entry: { index: resolve(testDir, 'index.js') },
},
output: {
enableCssModuleTSDeclaration: true,
cssModules: {
auto: /\.module\./i,
},
Expand All @@ -107,12 +106,12 @@ test('should generator ts declaration correctly for custom exportLocalsConventio

await build({
cwd: __dirname,
plugins: [pluginTypedCSSModules()],
rsbuildConfig: {
source: {
entry: { index: resolve(testDir, 'index.js') },
},
output: {
enableCssModuleTSDeclaration: true,
cssModules: {
auto: /\.module\./i,
exportLocalsConvention: 'asIs',
Expand Down
28 changes: 26 additions & 2 deletions e2e/cases/stylus/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import { join, resolve } from 'node:path';
import { build } from '@e2e/helper';
import { expect, test } from '@playwright/test';
import { pluginStylus } from '@rsbuild/plugin-stylus';
import { pluginTypedCSSModules } from '@rsbuild/plugin-typed-css-modules';
import { fse } from '@rsbuild/shared';

const fixtures = __dirname;

const generatorTempDir = async (testDir: string) => {
await fse.emptyDir(testDir);
await fse.copy(join(fixtures, 'src'), testDir);

return () => fse.remove(testDir);
};

test('should compile stylus correctly with ts declaration', async () => {
const testDir = join(fixtures, 'test-temp-src-1');
const clear = await generatorTempDir(testDir);

test('should compile stylus correctly', async () => {
const rsbuild = await build({
cwd: __dirname,
plugins: [pluginStylus()],
plugins: [pluginStylus(), pluginTypedCSSModules()],
rsbuildConfig: {
source: {
entry: { index: resolve(testDir, 'index.js') },
},
},
});
const files = await rsbuild.unwrapOutputJSON();

Expand All @@ -15,4 +35,8 @@ test('should compile stylus correctly', async () => {
expect(content).toMatch(
/body{color:#f00;font:14px Arial,sans-serif}\.title-class-\w{6}{font-size:14px}/,
);

expect(fse.existsSync(join(testDir, './b.module.styl.d.ts'))).toBeTruthy();

await clear();
});
1 change: 1 addition & 0 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@rsbuild/plugin-vue": "workspace:*",
"@rsbuild/plugin-vue-jsx": "workspace:*",
"@rsbuild/plugin-vue2": "workspace:*",
"@rsbuild/plugin-typed-css-modules": "workspace:*",
"@rsbuild/plugin-vue2-jsx": "workspace:*",
"@rsbuild/plugin-yaml": "workspace:*",
"@rsbuild/shared": "workspace:*",
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
"less-loader": "12.2.0",
"on-finished": "2.4.1",
"open": "^8.4.0",
"line-diff": "2.1.1",
"postcss-load-config": "5.1.0",
"postcss-loader": "8.1.1",
"postcss-value-parser": "4.2.0",
Expand Down
4 changes: 0 additions & 4 deletions packages/core/prebundle.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ export default {
picocolors: '@rsbuild/shared/picocolors',
},
},
{
name: 'line-diff',
ignoreDts: true,
},
{
name: 'postcss-value-parser',
ignoreDts: true,
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ const getDefaultOutputConfig = (): NormalizedOutputConfig => ({
css: false,
},
filenameHash: true,
enableCssModuleTSDeclaration: false,
inlineScripts: false,
inlineStyles: false,
cssModules: {
Expand Down
12 changes: 0 additions & 12 deletions packages/core/src/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ export async function applyCSSRule({

// 1. Check user config
const enableExtractCSS = isUseCssExtract(config, target);
const enableCSSModuleTS = Boolean(config.output.enableCssModuleTSDeclaration);

// 2. Prepare loader options
const localIdentName = getCssModuleLocalIdentName(config, isProd);
Expand Down Expand Up @@ -306,17 +305,6 @@ export async function applyCSSRule({
.options(styleLoaderOptions)
.end();
}

// use css-modules-typescript-loader
if (enableCSSModuleTS && cssLoaderOptions.modules) {
rule
.use(CHAIN_ID.USE.CSS_MODULES_TS)
.loader(path.resolve(__dirname, '../rspack/cssModulesTypescriptLoader'))
.options({
modules: cssLoaderOptions.modules,
})
.end();
}
} else {
rule
.use(CHAIN_ID.USE.IGNORE_CSS)
Expand Down
79 changes: 0 additions & 79 deletions packages/core/tests/__snapshots__/css.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,84 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`plugin-css > should apply custom css-modules-typescript-loader when enableCssModuleTSDeclaration 1`] = `
{
"experiments": {
"css": false,
},
"module": {
"rules": [
{
"resolve": {
"preferRelative": true,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"use": [
{
"loader": "<ROOT>/node_modules/<PNPM_INNER>/@rspack/core/dist/builtin-plugin/css-extract/loader.js",
},
{
"loader": "<ROOT>/packages/core/src/rspack/cssModulesTypescriptLoader",
"options": {
"modules": {
"auto": true,
"exportLocalsConvention": "camelCase",
"localIdentName": "[path][name]__[local]-[hash:base64:6]",
"namedExport": false,
},
},
},
{
"loader": "<ROOT>/packages/core/compiled/css-loader",
"options": {
"importLoaders": 1,
"modules": {
"auto": true,
"exportLocalsConvention": "camelCase",
"localIdentName": "[path][name]__[local]-[hash:base64:6]",
"namedExport": false,
},
"sourceMap": false,
},
},
{
"loader": "<ROOT>/packages/core/compiled/postcss-loader",
"options": {
"postcssOptions": {
"config": false,
"plugins": [
{
"browsers": [
"chrome >= 87",
"edge >= 88",
"firefox >= 78",
"safari >= 14",
],
"info": [Function],
"options": {
"flexbox": "no-2009",
"overrideBrowserslist": [
"chrome >= 87",
"edge >= 88",
"firefox >= 78",
"safari >= 14",
],
},
"postcssPlugin": "autoprefixer",
"prepare": [Function],
},
],
},
"sourceMap": false,
},
},
],
},
],
},
}
`;

exports[`plugin-css > should override browserslist of autoprefixer when using output.overrideBrowserslist config 1`] = `
{
"experiments": {
Expand Down
13 changes: 0 additions & 13 deletions packages/core/tests/css.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,6 @@ describe('plugin-css', () => {
const bundlerConfigs = await rsbuild.initConfigs();
expect(bundlerConfigs[0]).toMatchSnapshot();
});

it('should apply custom css-modules-typescript-loader when enableCssModuleTSDeclaration', async () => {
const rsbuild = await createStubRsbuild({
plugins: [pluginCss()],
rsbuildConfig: {
output: {
enableCssModuleTSDeclaration: true,
},
},
});
const bundlerConfigs = await rsbuild.initConfigs();
expect(bundlerConfigs[0]).toMatchSnapshot();
});
});

describe('plugin-css injectStyles', () => {
Expand Down
21 changes: 21 additions & 0 deletions packages/plugin-typed-css-modules/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023-present Bytedance, Inc. and its affiliates.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
19 changes: 19 additions & 0 deletions packages/plugin-typed-css-modules/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<p align="center">
<a href="https://rsbuild.dev" target="blank"><img src="https://github.com/web-infra-dev/rsbuild/assets/7237365/84abc13e-b620-468f-a90b-dbf28e7e9427" alt="Rsbuild Logo" /></a>
</p>

# Rsbuild

The Rspack-based build tool. It's fast, out-of-the-box and extensible.

## Documentation

https://rsbuild.dev/

## Contributing

Please read the [Contributing Guide](https://github.com/web-infra-dev/rsbuild/blob/main/CONTRIBUTING.md).

## License

Rsbuild is [MIT licensed](https://github.com/web-infra-dev/rsbuild/blob/main/LICENSE).
13 changes: 13 additions & 0 deletions packages/plugin-typed-css-modules/modern.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { moduleTools } from '@modern-js/module-tools';
import { buildConfigWithMjs } from '../../scripts/modern.base.config';

export default {
plugins: [moduleTools()],
buildConfig: buildConfigWithMjs.map((config) => {
if (config.format === 'cjs') {
// add loader to entry
config.input = ['src/index.ts', 'src/loader.ts'];
}
return config;
}),
};
Loading
Loading