css-codemod
Advanced tools
Comparing version 0.0.0-pr.6.1.0e4d157 to 0.0.0-pr.7.1.f4c705a
@@ -29,6 +29,7 @@ #!/usr/bin/env node | ||
var createAPIParse = ({ | ||
parser | ||
parser, | ||
plugins = [] | ||
}) => { | ||
const parse = (source) => { | ||
const result = (0, import_postcss.default)().process(source, { | ||
const result = (0, import_postcss.default)(plugins).process(source, { | ||
from: void 0, | ||
@@ -50,6 +51,7 @@ parser | ||
var createAPI = ({ | ||
parser | ||
parser, | ||
plugins | ||
} = {}) => { | ||
const api = { | ||
parse: createAPIParse({ parser }) | ||
parse: createAPIParse({ parser, plugins }) | ||
}; | ||
@@ -101,3 +103,4 @@ return api; | ||
const parser = mod.parser; | ||
return { transform, parser }; | ||
const plugins = mod.plugins; | ||
return { transform, parser, plugins }; | ||
} catch (err) { | ||
@@ -111,5 +114,5 @@ console.error(`An error occurred loading the transform file. Verify "${filepath}" exists.`); | ||
var perform = async (options) => { | ||
const { transform, parser } = await loadTransform(options.transform); | ||
const { transform, parser, plugins } = await loadTransform(options.transform); | ||
const files = getAllFilesToTransform(options.files); | ||
const api = createAPI({ parser }); | ||
const api = createAPI({ parser, plugins }); | ||
files.map((file) => { | ||
@@ -116,0 +119,0 @@ const fileInfo = getFileInfo(file); |
{ | ||
"version": "0.0.0-pr.6.1.0e4d157", | ||
"version": "0.0.0-pr.7.1.f4c705a", | ||
"license": "MIT", | ||
@@ -50,2 +50,3 @@ "name": "css-codemod", | ||
"husky": "^7.0.4", | ||
"postcss-calc": "^8.2.3", | ||
"postcss-scss": "^4.0.3", | ||
@@ -52,0 +53,0 @@ "rimraf": "^3.0.2", |
@@ -5,2 +5,7 @@ # :snake: css-codemod | ||
Powered by [PostCSS](https://postcss.org) so the existing tooling and community can be leveraged. | ||
- Any [PostCSS syntax parser and stringifier](https://github.com/postcss/postcss/blob/main/docs/syntax.md) can be added. | ||
- Any [PostCSS plugin](https://github.com/postcss/postcss/blob/main/docs/plugins.md) can be added, | ||
## Install | ||
@@ -76,2 +81,6 @@ | ||
}; | ||
// Optionally defined a named `parser` export to configure the PostCSS parser. | ||
// Docs: https://postcss.org/api/#parser | ||
// export const parser = ...; | ||
``` | ||
@@ -112,2 +121,3 @@ | ||
import { Transform } from 'css-codemod'; | ||
import { parse, stringify } from 'postcss-scss'; | ||
@@ -135,4 +145,12 @@ export const transform: Transform = (fileInfo, api) => { | ||
// Since a string is returned this will be written back to the file. | ||
return root.toString(); | ||
// Note: in this example the `postcss-scss` package is used to add | ||
// SCSS syntax support. The stringifier is passed when we call `toString` to | ||
// re-output valid SCSS syntax. | ||
return root.toString(stringify); | ||
}; | ||
// Note: in this example the `postcss-scss` package is used to add SCSS syntax support. | ||
// This configures PostCSS to correctly parse SCSS syntax. | ||
// Docs: https://postcss.org/api/#parser | ||
export const parser = parse; | ||
``` | ||
@@ -139,0 +157,0 @@ |
@@ -1,2 +0,3 @@ | ||
import postcss, { Root, Parser } from 'postcss'; | ||
import postcss, { Root, Parser, AcceptedPlugin } from 'postcss'; | ||
export interface TransformAPI { | ||
@@ -11,8 +12,10 @@ /** | ||
parser, | ||
plugins = [], | ||
}: { | ||
parser?: Parser; | ||
plugins?: AcceptedPlugin[]; | ||
}): TransformAPI['parse'] => { | ||
const parse: TransformAPI['parse'] = source => { | ||
const result = postcss().process(source, { | ||
// Silence a warning about sourcemaps. Not relevant to this use case. | ||
const result = postcss(plugins).process(source, { | ||
// Silence warning about sourcemaps. Not relevant to this use case. | ||
from: undefined, | ||
@@ -26,3 +29,3 @@ parser, | ||
// Re-surface an PostCSS parsing errors. | ||
// Re-surface any PostCSS parsing errors. | ||
// https://github.com/postcss/postcss/issues/1708 | ||
@@ -45,5 +48,6 @@ if ((result as any).error) { | ||
parser, | ||
}: { parser?: Parser } = {}): TransformAPI => { | ||
plugins, | ||
}: { parser?: Parser; plugins?: AcceptedPlugin[] } = {}): TransformAPI => { | ||
const api: TransformAPI = { | ||
parse: createAPIParse({ parser }), | ||
parse: createAPIParse({ parser, plugins }), | ||
}; | ||
@@ -50,0 +54,0 @@ |
@@ -21,5 +21,5 @@ import { createAPI } from './api'; | ||
export const perform = async (options: ProcessTransformOptions) => { | ||
const { transform, parser } = await loadTransform(options.transform); | ||
const { transform, parser, plugins } = await loadTransform(options.transform); | ||
const files = getAllFilesToTransform(options.files); | ||
const api = createAPI({ parser }); | ||
const api = createAPI({ parser, plugins }); | ||
@@ -26,0 +26,0 @@ files.map(file => { |
import { bundleRequire } from 'bundle-require'; | ||
import { Parser } from 'postcss'; | ||
import { AcceptedPlugin, Parser } from 'postcss'; | ||
import { TransformAPI } from './api'; | ||
@@ -41,2 +41,3 @@ import { TransformFileInfo } from './files'; | ||
parser?: Parser; | ||
plugins?: AcceptedPlugin[]; | ||
} | ||
@@ -52,5 +53,7 @@ | ||
const { mod } = await bundleRequire({ filepath }); | ||
const transform = validateTransform(mod.transform || mod.default); | ||
const parser = mod.parser; | ||
return { transform, parser }; | ||
const plugins = mod.plugins; | ||
return { transform, parser, plugins }; | ||
} catch (err) { | ||
@@ -57,0 +60,0 @@ console.error( |
21811
408
167
13