-
Notifications
You must be signed in to change notification settings - Fork 74
Load PostCSS config #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
a5a7fbe
12a1cb4
9073ef5
e4c2462
7dfaa8c
101a4e4
8b09ffd
ad0dd38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
declare module 'postcss-load-config' { | ||
interface PostCSSConfig { | ||
plugins: any[]; | ||
options?: any; | ||
} | ||
|
||
interface Load { | ||
(context: object, path?: string, options?: object): Promise<PostCSSConfig>; | ||
sync(context: object, path?: string, options?: object): PostCSSConfig; | ||
} | ||
|
||
const load: Load; | ||
|
||
export = load; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,49 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as loadPostCssConfig from 'postcss-load-config'; | ||
import * as ts_module from 'typescript/lib/tsserverlibrary'; | ||
|
||
import { createMatchers } from './helpers/createMatchers'; | ||
import { isCSSFn } from './helpers/cssExtensions'; | ||
import { getDtsSnapshot } from './helpers/cssSnapshots'; | ||
import { Options } from './options'; | ||
|
||
import * as postcss from 'postcss'; | ||
import * as postcssIcssSelectors from 'postcss-icss-selectors'; | ||
|
||
const removePlugin = postcss.plugin('remove-mixins', () => (css) => { | ||
css.walkRules((rule) => { | ||
rule.walkAtRules((atRule) => { | ||
if (atRule.name === 'mixin') { | ||
atRule.remove(); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
function getPostCssConfig(dir: string) { | ||
try { | ||
return loadPostCssConfig.sync({}, dir); | ||
} catch (error) { | ||
return { plugins: [] }; | ||
} | ||
} | ||
|
||
function init({ typescript: ts }: { typescript: typeof ts_module }) { | ||
let _isCSS: isCSSFn; | ||
|
||
function create(info: ts.server.PluginCreateInfo) { | ||
const postcssConfig = getPostCssConfig(info.project.getCurrentDirectory()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tool looks great, but I see it's not official - so I should probably add that to the docs. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that's true. You can see that they use this package in |
||
|
||
const processor = postcss([ | ||
removePlugin(), | ||
...postcssConfig.plugins.filter( | ||
// Postcss mixins plugin might be async and will break the postcss sync output. | ||
(plugin) => !['postcss-mixins'].includes(plugin.postcssPlugin), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's better to just add a note to the docs here? As the plugin (from what I read) is only async when files are passed in. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup! If we don't remove it for them we would have to pass something like |
||
), | ||
postcssIcssSelectors({ mode: 'local' }), | ||
]); | ||
|
||
// User options for plugin. | ||
const options: Options = info.config.options || {}; | ||
|
||
|
@@ -24,7 +59,13 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) { | |
...rest | ||
): ts.SourceFile => { | ||
if (isCSS(fileName)) { | ||
scriptSnapshot = getDtsSnapshot(ts, fileName, scriptSnapshot, options); | ||
scriptSnapshot = getDtsSnapshot( | ||
ts, | ||
processor, | ||
fileName, | ||
scriptSnapshot, | ||
options, | ||
); | ||
} | ||
const sourceFile = _createLanguageServiceSourceFile( | ||
fileName, | ||
|
@@ -47,6 +88,7 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) { | |
if (isCSS(sourceFile.fileName)) { | ||
scriptSnapshot = getDtsSnapshot( | ||
ts, | ||
processor, | ||
sourceFile.fileName, | ||
scriptSnapshot, | ||
options, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -943,7 +943,7 @@ [email protected], core-util-is@~1.0.0: | |
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" | ||
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac= | ||
|
||
cosmiconfig@^5.2.0: | ||
cosmiconfig@^5.0.0, cosmiconfig@^5.2.0: | ||
version "5.2.1" | ||
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-5.2.1.tgz#040f726809c591e77a17c0a3626ca45b4f168b1a" | ||
integrity sha512-H65gsXo1SKjf8zmrJ67eJk8aIRKV5ff2D4uKZIBZShbhGSpEmsQOPW/SKMKYhSTrqR7ufy6RP69rPogdaPh/kA== | ||
|
@@ -1698,6 +1698,13 @@ image-size@~0.5.0: | |
resolved "https://registry.yarnpkg.com/image-size/-/image-size-0.5.5.tgz#09dfd4ab9d20e29eb1c3e80b8990378df9e3cb9c" | ||
integrity sha1-Cd/Uq50g4p6xw+gLiZA3jfnjy5w= | ||
|
||
import-cwd@^2.0.0: | ||
version "2.1.0" | ||
resolved "https://registry.yarnpkg.com/import-cwd/-/import-cwd-2.1.0.tgz#aa6cf36e722761285cb371ec6519f53e2435b0a9" | ||
integrity sha1-qmzzbnInYShcs3HsZRn1PiQ1sKk= | ||
dependencies: | ||
import-from "^2.1.0" | ||
|
||
import-fresh@^2.0.0: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-2.0.0.tgz#d81355c15612d386c61f9ddd3922d4304822a546" | ||
|
@@ -1706,6 +1713,13 @@ import-fresh@^2.0.0: | |
caller-path "^2.0.0" | ||
resolve-from "^3.0.0" | ||
|
||
import-from@^2.1.0: | ||
version "2.1.0" | ||
resolved "https://registry.yarnpkg.com/import-from/-/import-from-2.1.0.tgz#335db7f2a7affd53aaa471d4b8021dee36b7f3b1" | ||
integrity sha1-M1238qev/VOqpHHUuAId7ja387E= | ||
dependencies: | ||
resolve-from "^3.0.0" | ||
|
||
import-local@^2.0.0: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/import-local/-/import-local-2.0.0.tgz#55070be38a5993cf18ef6db7e961f5bee5c5a09d" | ||
|
@@ -3240,6 +3254,14 @@ postcss-icss-selectors@^2.0.3: | |
lodash "^4.17.4" | ||
postcss "^6.0.2" | ||
|
||
postcss-load-config@^2.1.0: | ||
version "2.1.0" | ||
resolved "https://registry.yarnpkg.com/postcss-load-config/-/postcss-load-config-2.1.0.tgz#c84d692b7bb7b41ddced94ee62e8ab31b417b003" | ||
integrity sha512-4pV3JJVPLd5+RueiVVB+gFOAa7GWc25XQcMp86Zexzke69mKf6Nx9LRcQywdz7yZI9n1udOxmLuAwTBypypF8Q== | ||
dependencies: | ||
cosmiconfig "^5.0.0" | ||
import-cwd "^2.0.0" | ||
|
||
postcss@^6.0.2: | ||
version "6.0.23" | ||
resolved "https://registry.yarnpkg.com/postcss/-/postcss-6.0.23.tgz#61c82cc328ac60e677645f979054eb98bc0e3324" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We load this in
cssSnapshots.ts
. Was there a reason you decided to load a processor here as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to move the instantiation of the postcss processor here because it would:
info
to callinfo.project.getCurrentDirectory()
and didn't want to pass new params intocssSnapshots.ts