From cab0cfbacdfd8f568c387b57bebf9cd95c13c6da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kus=20Ca=CC=81mara?= Date: Thu, 31 Dec 2020 17:18:58 +0100 Subject: [PATCH] feat: use .css as default extension for test option --- README.md | 2 +- src/index.spec.ts | 30 ++++++++++++++++++++++++++++-- src/index.ts | 2 +- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a8eef46..ec24ed4 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ var style2 = css` Regular expression or function which test importee for being parsed as style file (css). Type: `RegExp | Function` -Default: `() => false` +Default: `/\.css$/` Example: `/\.css$/` only `.css` imports will be parsed #### postcss diff --git a/src/index.spec.ts b/src/index.spec.ts index 26e55aa..d09a32f 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -7,7 +7,7 @@ import plugin, { PluginOptions } from '.'; function run(source: string, options?: Partial, ...plugins_: any[]) { const { code } = transform(source, { filename: 'test.ts', - plugins: [[plugin, { test: /\.css$/, ...(options || {}) }], ...plugins_], + plugins: [[plugin, { ...(options || {}) }], ...plugins_], })!; return code; } @@ -71,7 +71,7 @@ it('side effect import', () => { expect(result).toEqual(expect.stringMatching(`import 'style.css';`)); }); -it('unknow file should be touched', () => { +it('unknow file should not be touched', () => { const result = run( stripIndents` import style from 'style.vue'; @@ -83,6 +83,32 @@ it('unknow file should be touched', () => { expect(result).toEqual(expect.stringMatching(`import style from 'style.vue'`)); }); +it('file with matched extension is transformed', () => { + const result = run( + stripIndents` + import style from 'style.pcss'; + `, + { + readFileSync: () => 'a {}', + test: /\.p?css$/, + }, + ); + expect(result).toEqual(expect.stringMatching(`const style = "a {}";`)); +}); + +it('function as test option is executed', () => { + const result = run( + stripIndents` + import style from 'style.pcss'; + `, + { + readFileSync: () => 'a {}', + test: (file) => file.includes('.pcss'), + }, + ); + expect(result).toEqual(expect.stringMatching(`const style = "a {}";`)); +}); + it('tagged template expression', () => { const result = run( stripIndents` diff --git a/src/index.ts b/src/index.ts index cc16780..4a1ae03 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import { dirname, resolve } from 'path'; import { getCss } from './getcss'; const defaultOptions = { - test: (() => false) as RegExp | Function, + test: /\.css$/ as RegExp | Function, readFileSync: undefined as Function | undefined, postcss: undefined as undefined | string | boolean, tagged: undefined as undefined | [string, string],