Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
26 changes: 14 additions & 12 deletions packages/ice/src/esbuild/cssModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import temp from 'temp';
import cssModules from '@ice/bundles/compiled/postcss-modules/index.js';
import { less, sass, postcss } from '@ice/bundles';
import type { Plugin, PluginBuild, OnResolveArgs, OnResolveResult, OnLoadArgs, OnLoadResult } from 'esbuild';
import type { Plugin, PluginBuild, OnResolveArgs, OnLoadArgs, OnLoadResult } from 'esbuild';
import { resolveId } from '../service/analyze.js';
import type { AliasWithEmpty } from '../service/analyze.js';

const cssModulesStyleFilter = /\.module\.(css|sass|scss|less)$/;
const STYLE_HANDLER_NAMESPACE = 'style-handler-namespace';

type GenerateScopedNameFunction = (name: string, filename: string, css: string) => string;

interface PluginOptions {
alias: AliasWithEmpty;
/** extract css files, default is true */
extract?: false;
/** css classname identifier default is `[hash:base64]` */
Expand All @@ -24,23 +27,22 @@
return {
name: 'esbuild-css-modules',
setup: async (build: PluginBuild) => {
build.onResolve({ filter: cssModulesStyleFilter }, onResolve);
build.onResolve({ filter: cssModulesStyleFilter }, (args: OnResolveArgs) => {
const { resolveDir } = args;
const resolvePath = !args.path.startsWith('.') ? resolveId(args.path, options.alias) : '';

Check warning on line 32 in packages/ice/src/esbuild/cssModules.ts

View workflow job for this annotation

GitHub Actions / build (18.x, windows-latest)

Unexpected negated condition

Check warning on line 32 in packages/ice/src/esbuild/cssModules.ts

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

Unexpected negated condition

Check warning on line 32 in packages/ice/src/esbuild/cssModules.ts

View workflow job for this annotation

GitHub Actions / build (16.x, windows-latest)

Unexpected negated condition

Check warning on line 32 in packages/ice/src/esbuild/cssModules.ts

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

Unexpected negated condition
const absolutePath = path.resolve(resolveDir, resolvePath && typeof resolvePath === 'string' ? resolvePath : args.path);
// Generate css and put it in the `STYLE_HANDLER_NAMESPACE` namespace to handle css file
return {
path: absolutePath,
namespace: STYLE_HANDLER_NAMESPACE,
};
});

build.onLoad({ filter: /.*/, namespace: STYLE_HANDLER_NAMESPACE }, onStyleLoad(options));
},
};
};

async function onResolve(args: OnResolveArgs): Promise<OnResolveResult> {
const { resolveDir } = args;
const absolutePath = path.resolve(resolveDir, args.path);
// Generate css and put it in the `STYLE_HANDLER_NAMESPACE` namespace to handle css file
return {
path: absolutePath,
namespace: STYLE_HANDLER_NAMESPACE,
};
}

/**
* parse less/scss/css-modules to css
*/
Expand Down
1 change: 1 addition & 0 deletions packages/ice/src/service/ServerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class ServerRunner extends Runner {
}),
server?.ignores && ignorePlugin(server.ignores),
cssModulesPlugin({
alias,
extract: false,
generateLocalIdentName: function (name: string, fileName: string) {
return getCSSModuleIdent({
Expand Down
2 changes: 1 addition & 1 deletion packages/ice/src/service/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { createLogger } from '../utils/logger.js';
const logger = createLogger('scan-modules');

type Alias = Record<string, string>;
type AliasWithEmpty = Record<string, string | false>;
export type AliasWithEmpty = Record<string, string | false>;

interface Options {
parallel?: number;
Expand Down
1 change: 1 addition & 0 deletions packages/ice/src/service/preBundleDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export async function bundleDeps(options:
emptyCSSPlugin(),
externalPlugin({ ignores, format: 'esm', externalDependencies: false }),
cssModulesPlugin({
alias,
extract: false,
generateLocalIdentName: function (name: string, fileName: string) {
return getCSSModuleIdent({
Expand Down
1 change: 1 addition & 0 deletions packages/ice/src/service/serverCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
}),
server?.ignores && ignorePlugin(server.ignores),
cssModulesPlugin({
alias,
extract: false,
generateLocalIdentName: function (name: string, fileName: string) {
// Compatible with webpack css-loader.
Expand Down Expand Up @@ -251,7 +252,7 @@
} else {
switch (server.bundler) {
case 'webpack':
const webpackServerCompiler = new WebpackServerCompiler({

Check warning on line 255 in packages/ice/src/service/serverCompiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x, windows-latest)

Unexpected lexical declaration in case block

Check warning on line 255 in packages/ice/src/service/serverCompiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

Unexpected lexical declaration in case block

Check warning on line 255 in packages/ice/src/service/serverCompiler.ts

View workflow job for this annotation

GitHub Actions / build (16.x, windows-latest)

Unexpected lexical declaration in case block

Check warning on line 255 in packages/ice/src/service/serverCompiler.ts

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

Unexpected lexical declaration in case block
...buildOptions,
externals,
compileIncludes: task.config.compileIncludes,
Expand Down
Loading