Skip to content

Commit 1c32799

Browse files
committed
chore: release v3.0.0
1 parent 49c5bd0 commit 1c32799

File tree

5 files changed

+63
-64
lines changed

5 files changed

+63
-64
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ The [internal `logger`](https://github.com/mrmckeb/typescript-plugin-css-modules
160160

161161
> If you use Webpack, note that tilde (`~`) imports not supported by Less and Sass natively.
162162
>
163-
> For Less users, this package exports a customRenderer that enables tilde imports: [`less-plugin-aliases`](https://github.com/dancon/less-plugin-aliases).
163+
> For Sass users: A custom importer has been implemented to resolve this as of v3.
164+
>
165+
> For Less users: This package exports a customRenderer that enables tilde imports: [`less-plugin-aliases`](https://github.com/dancon/less-plugin-aliases).
164166
165167
#### `customTemplate`
166168

@@ -198,11 +200,11 @@ The `classes` object represents all the classnames extracted from the CSS Module
198200

199201
#### `rendererOptions`
200202

201-
| Option | Default value | Description |
202-
| -------- | ------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
203-
| `less` | `{}` | Set [renderer options for Less](http://lesscss.org/usage/#less-options). |
204-
| `sass` | `{ enableWebpackTildeImports: true }` | Set [renderer options for Sass](https://sass-lang.com/documentation/js-api#options). The `enableWebpackTildeImports` property enables support for [Webpack's tilde-prefixed imports](https://webpack.js.org/loaders/css-loader/#import). |
205-
| `stylus` | `{}` | Set [renderer options for Stylus](https://stylus.bootcss.com/docs/js.html). |
203+
| Option | Default value | Description |
204+
| -------- | ------------- | ------------------------------------------------------------------------------------ |
205+
| `less` | `{}` | Set [renderer options for Less](http://lesscss.org/usage/#less-options). |
206+
| `sass` | `{}` | Set [renderer options for Sass](https://sass-lang.com/documentation/js-api#options). |
207+
| `stylus` | `{}` | Set [renderer options for Stylus](https://stylus.bootcss.com/docs/js.html). |
206208

207209
> For convenience, `includePaths` for Sass are extended, not replaced. The defaults are the path of the current file, and `'node_modules'`.
208210

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-plugin-css-modules",
3-
"version": "2.8.0",
3+
"version": "3.0.0",
44
"main": "lib/index.js",
55
"author": "Brody McKee <[email protected]>",
66
"license": "MIT",

src/helpers/getClasses.ts

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import path from 'path';
2-
import fs from 'fs';
32
import postcss from 'postcss';
43
import less from 'less';
54
import sass from 'sass';
65
import stylus from 'stylus';
76
import { extractICSS } from 'icss-utils';
87
import tsModule from 'typescript/lib/tsserverlibrary';
98
import { createMatchPath } from 'tsconfig-paths';
10-
import { Logger } from './logger';
9+
import { sassTildeImporter } from '../importers/sassTildeImporter';
1110
import { Options, CustomRenderer } from '../options';
11+
import { Logger } from './logger';
1212

1313
export const enum FileTypes {
1414
css = 'css',
@@ -28,50 +28,6 @@ export const getFileType = (fileName: string) => {
2828

2929
const getFilePath = (fileName: string) => path.dirname(fileName);
3030

31-
// Creates a sass importer which resolves Webpack-style tilde-imports
32-
const webpackTildeSupportingImporter: sass.Importer = (
33-
rawImportPath: string,
34-
source: string,
35-
) => {
36-
// We only care about tilde-prefixed imports that do not look like paths
37-
if (!rawImportPath.startsWith('~') || rawImportPath.startsWith('~/')) {
38-
return null;
39-
}
40-
41-
// Create subpathsWithExts such that it has entries of the form
42-
// node_modules/@foo/bar/baz.(scss|sass)
43-
// for an import of the form ~@foo/bar/baz(.(scss|sass))?
44-
const nodeModSubpath = path.join('node_modules', rawImportPath.substring(1));
45-
const subpathsWithExts: string[] = [];
46-
if (nodeModSubpath.endsWith('.scss') || nodeModSubpath.endsWith('.sass')) {
47-
subpathsWithExts.push(nodeModSubpath);
48-
} else {
49-
// Look for .scss first
50-
subpathsWithExts.push(`${nodeModSubpath}.scss`, `${nodeModSubpath}.sass`);
51-
}
52-
53-
// Climbs the filesystem tree until we get to the root, looking for the first
54-
// node_modules directory which has a matching module and filename.
55-
let prevDir = '';
56-
let dir = path.dirname(source);
57-
while (prevDir !== dir) {
58-
const searchPaths = subpathsWithExts.map((subpathWithExt) =>
59-
path.join(dir, subpathWithExt),
60-
);
61-
for (const searchPath of searchPaths) {
62-
if (fs.existsSync(searchPath)) {
63-
return { file: searchPath };
64-
}
65-
}
66-
prevDir = dir;
67-
dir = path.dirname(dir);
68-
}
69-
70-
// Returning null is not itself an error, it tells sass to instead try the
71-
// next import resolution method if one exists
72-
return null;
73-
};
74-
7531
export const getClasses = ({
7632
css,
7733
fileName,
@@ -116,8 +72,7 @@ export const getClasses = ({
11672
);
11773
} else if (fileType === FileTypes.scss || fileType === FileTypes.sass) {
11874
const filePath = getFilePath(fileName);
119-
const { includePaths, enableWebpackTildeImports, ...sassOptions } =
120-
rendererOptions.sass || {};
75+
const { includePaths, ...sassOptions } = rendererOptions.sass || {};
12176
const { baseUrl, paths } = compilerOptions;
12277
const matchPath =
12378
baseUrl && paths ? createMatchPath(path.resolve(baseUrl), paths) : null;
@@ -127,10 +82,7 @@ export const getClasses = ({
12782
return newUrl ? { file: newUrl } : null;
12883
};
12984

130-
const importers = [aliasImporter];
131-
if (enableWebpackTildeImports !== false) {
132-
importers.push(webpackTildeSupportingImporter);
133-
}
85+
const importers = [aliasImporter, sassTildeImporter];
13486

13587
transformedCss = sass
13688
.renderSync({

src/importers/sassTildeImporter.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
import sass from 'sass';
4+
5+
/**
6+
* Creates a sass importer which resolves Webpack-style tilde-imports.
7+
*/
8+
export const sassTildeImporter: sass.Importer = (
9+
rawImportPath: string,
10+
source: string,
11+
) => {
12+
// We only care about tilde-prefixed imports that do not look like paths.
13+
if (!rawImportPath.startsWith('~') || rawImportPath.startsWith('~/')) {
14+
return null;
15+
}
16+
17+
// Create subpathsWithExts such that it has entries of the form
18+
// node_modules/@foo/bar/baz.(scss|sass)
19+
// for an import of the form ~@foo/bar/baz(.(scss|sass))?
20+
const nodeModSubpath = path.join('node_modules', rawImportPath.substring(1));
21+
const subpathsWithExts: string[] = [];
22+
if (nodeModSubpath.endsWith('.scss') || nodeModSubpath.endsWith('.sass')) {
23+
subpathsWithExts.push(nodeModSubpath);
24+
} else {
25+
// Look for .scss first.
26+
subpathsWithExts.push(`${nodeModSubpath}.scss`, `${nodeModSubpath}.sass`);
27+
}
28+
29+
// Climbs the filesystem tree until we get to the root, looking for the first
30+
// node_modules directory which has a matching module and filename.
31+
let prevDir = '';
32+
let dir = path.dirname(source);
33+
while (prevDir !== dir) {
34+
const searchPaths = subpathsWithExts.map((subpathWithExt) =>
35+
path.join(dir, subpathWithExt),
36+
);
37+
for (const searchPath of searchPaths) {
38+
if (fs.existsSync(searchPath)) {
39+
return { file: searchPath };
40+
}
41+
}
42+
prevDir = dir;
43+
dir = path.dirname(dir);
44+
}
45+
46+
// Returning null is not itself an error, it tells sass to instead try the
47+
// next import resolution method if one exists
48+
return null;
49+
};

src/options.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@ export interface PostCssOptions {
1313
useConfig?: boolean;
1414
}
1515

16-
export interface ExtraSassOptions {
17-
enableWebpackTildeImports?: boolean;
18-
}
19-
2016
export interface RendererOptions {
2117
less?: Partial<Less.Options>;
22-
sass?: Partial<SassOptions> & ExtraSassOptions;
18+
sass?: Partial<SassOptions>;
2319
stylus?: Partial<StylusRenderOptions>;
2420
}
2521

0 commit comments

Comments
 (0)