Skip to content

Commit

Permalink
feat: use .css as default extension for test option
Browse files Browse the repository at this point in the history
  • Loading branch information
kcmr committed Dec 31, 2020
1 parent c2d9e2b commit cab0cfb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 28 additions & 2 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import plugin, { PluginOptions } from '.';
function run(source: string, options?: Partial<PluginOptions>, ...plugins_: any[]) {
const { code } = transform(source, {
filename: 'test.ts',
plugins: [[plugin, { test: /\.css$/, ...(options || {}) }], ...plugins_],
plugins: [[plugin, { ...(options || {}) }], ...plugins_],
})!;
return code;
}
Expand Down Expand Up @@ -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';
Expand All @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit cab0cfb

Please sign in to comment.