Skip to content

Commit

Permalink
feat: support custom config
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j committed Feb 22, 2023
1 parent 38f2663 commit a657f3d
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 9 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,19 @@ the template (i.e. leave it untouched and won't process it).

You can then use a `// postcss-lit-disable-next-line` comment to silence the
warning.

## Custom babel options

You may customise the babel options via your `package.json`:

```json
{
"postcss-lit": {
"babelOptions": {
}
}
}
```

The available options are listed
[here](https://babeljs.io/docs/babel-parser#options).
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"dependencies": {
"@babel/generator": "^7.16.5",
"@babel/parser": "^7.16.2",
"@babel/traverse": "^7.16.0"
"@babel/traverse": "^7.16.0",
"lilconfig": "^2.0.6"
}
}
13 changes: 6 additions & 7 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {default as traverse, NodePath} from '@babel/traverse';
import {TaggedTemplateExpression} from '@babel/types';
import {createPlaceholder, hasDisableComment} from './util.js';
import {locationCorrectionWalker} from './locationCorrection.js';
import {getUserConfig} from './userConfig.js';

const configKey = 'postcss-lit';

/**
* Parses CSS from within tagged template literals in a JavaScript document
Expand All @@ -25,14 +28,10 @@ export const parse: Parser<Root | Document> = (
): Root | Document => {
const doc = new Document();
const sourceAsString = source.toString();
const userConfig = getUserConfig(configKey);

const ast = babelParse(sourceAsString, {
sourceType: 'unambiguous',
plugins: [
'typescript',
['decorators', {decoratorsBeforeExport: true}],
'jsx'
],
ranges: true
...userConfig.babelOptions
});
const extractedStyles = new Set<TaggedTemplateExpression>();

Expand Down
21 changes: 21 additions & 0 deletions src/test/parse_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {cwd, chdir} from 'process';
import {Root, Rule, Declaration, Comment, AtRule} from 'postcss';
import {assert} from 'chai';
import {createTestAst} from './util.js';
Expand Down Expand Up @@ -381,4 +382,24 @@ describe('parse', () => {

assert.equal(ast.nodes.length, 0);
});

it('should respect custom config', () => {
const currentDir = cwd();

chdir('./test/fixtures/custom-config');

try {
createTestAst(`
css\`
.foo { color: hotpink; }
\`;
const x = (<div></div>);
`);
assert.fail();
} catch (err) {
assert.equal((err as Error).name, 'SyntaxError');
} finally {
chdir(currentDir);
}
});
});
38 changes: 38 additions & 0 deletions src/userConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {lilconfigSync as lilconfig} from 'lilconfig';
import {ParserOptions} from '@babel/parser';

export interface UserConfig {
babelOptions: ParserOptions;
}

const defaultConfig: UserConfig = {
babelOptions: {
sourceType: 'unambiguous',
plugins: [
'typescript',
['decorators', {decoratorsBeforeExport: true}],
'jsx'
],
ranges: true
}
};

/**
* Gets the postcss-lit config from package.json if it exists
* @param {string} key Config key to search for
* @return {PackageConfig}
*/
export function getUserConfig(key: string): UserConfig {
const result = lilconfig(key, {
searchPlaces: ['package.json']
}).search();

const userConfig = result ? result.config : {};

return {
babelOptions: {
...defaultConfig.babelOptions,
...userConfig?.babelOptions
}
};
}
11 changes: 11 additions & 0 deletions test/fixtures/custom-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"private": true,
"name": "custom-config-in-postcss-lit",
"version": "1.0.0",
"main": "main.js",
"postcss-lit": {
"babelOptions": {
"plugins": []
}
}
}

0 comments on commit a657f3d

Please sign in to comment.