This repository has been archived by the owner on Dec 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
[wip] @snowpack/plugin-sass & @snowpack/plugin-postcss #167
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# @snowpack/plugin-postcss | ||
|
||
Use PostCSS to process CSS in Snowpack. Good for autoprefixing, minification, and anything else. | ||
|
||
``` | ||
npm install --save-dev @snowpack/plugin-postcss | ||
``` | ||
|
||
Add `@snowpack/plugin-postcss` to your `snowpack.config.json`: | ||
|
||
```json | ||
{ | ||
"plugins": ["@snowpack/plugin-postcss"] | ||
} | ||
``` | ||
|
||
Also create a `postcss.config.js` file in your project root: | ||
|
||
```bash | ||
npm i autoprefixer cssnano | ||
``` | ||
|
||
```js | ||
const autoprefixer = require("autoprefixer"); | ||
const cssnano = require("cssnano"); | ||
|
||
module.exports = { | ||
plugins: [ | ||
autoprefixer("last 2 versions"), | ||
...(process.env.NODE_ENV === "production" ? [cssnano()] : []), // minify for build but not dev (for speed) | ||
// add more PostCSS plugins here | ||
], | ||
}; | ||
``` | ||
|
||
See [the PostCSS documentation](https://github.com/postcss/postcss) on what all can be added here. | ||
|
||
### Plugin Options | ||
|
||
| Name | Default | Description | | ||
| :------- | :-------------------- | :-------------------------------------------------------------------------------------------------------------------------------- | | ||
| `config` | `./postcss.config.js` | Where to load PostCSS config (set this if your config file isn’t named `postcss.config.js`). | | ||
| `sass` | `false` | Set to `true` to run `.scss` and `.sass` files through PostCSS after compiling (requires [@snowpack/plugin-sass](../plugin-sass)) | | ||
|
||
### Configuring PostCSS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "@snowpack/plugin-postcss", | ||
"version": "1.0.0", | ||
"main": "plugin.js", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"peerDependencies": {}, | ||
"dependencies": { | ||
"node-sass": "^4.14.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const path = require("path"); | ||
const postcss = require("postcss"); | ||
|
||
module.exports = (config, options = {}) => ({ | ||
input: [".css", ...(options.sass ? [".sass", ".scss"] : [])], | ||
output: [".css"], | ||
async build({ contents, filePath }) { | ||
const postCSSConfigPath = path.resolve( | ||
process.cwd(), | ||
options.config || "postcss.config.js" | ||
); | ||
const postCSSConfig = require(postCSSConfigPath); | ||
let output = ""; | ||
|
||
try { | ||
output = await postcss(postCSSConfig.plugins || []).process(contents); | ||
} catch (err) { | ||
throw new Error(`[${filePath}]: ${err}`); | ||
} | ||
|
||
return { ".css": output }; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# @snowpack/plugin-sass | ||
|
||
Use Sass to transform `.sass` and `.scss` files. | ||
|
||
``` | ||
npm install --save-dev @snowpack/plugin-sass | ||
``` | ||
|
||
#### `snowpack.config.json` | ||
|
||
```json | ||
{ | ||
"plugins": ["@snowpack/plugin-sass"] | ||
} | ||
``` | ||
|
||
### Using with PostCSS | ||
|
||
If you need autoprefixing and/or minification, pair this with the [@snowpack/plugin-postcss](../plugin-postcss) plugin with `sass: true` enabled (otherwise, `.sass` and `.scss` files will be ignored by PostCSS): | ||
|
||
```json | ||
{ | ||
"plugins": [ | ||
"@snowpack/plugin-sass", | ||
["@snowpack/plugin-postcss", { "sass": true }] | ||
] | ||
} | ||
``` | ||
|
||
### Plugin Options | ||
|
||
None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "@snowpack/plugin-sass", | ||
"version": "1.0.0", | ||
"main": "plugin.js", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"peerDependencies": {}, | ||
"dependencies": { | ||
"autoprefixer": "^9.8.4", | ||
"cssnano": "^4.1.10", | ||
"node-sass": "^4.14.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const path = require("path"); | ||
const sass = require("node-sass"); | ||
|
||
module.exports = () => ({ | ||
input: [".scss", ".sass"], | ||
output: [".css"], | ||
async build({ contents, filePath }) { | ||
if (contents.indexOf("@use ") !== -1) { | ||
console.warn( | ||
"node-sass doesn’t support @use yet. Using @import is preferred. See https://github.com/sass/node-sass/issues/2886." | ||
); | ||
} | ||
|
||
const includePaths = [path.resolve(process.cwd(), path.dirname(filePath))]; | ||
|
||
const out = sass.renderSync({ | ||
data: contents, | ||
includePaths, | ||
}); | ||
const { css } = out; | ||
return { ".css": css.toString() }; | ||
}, | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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’m actually pretty 👎 on
node-sass
. Particularly with it having some pretty fundamental issues like these:For those not familiar, this is VERY different than Dart Sass (the default Sass CLI). It’s essentially a major version behind, and has some pretty big bugs. 🤔 I’m wondering if there’s a way to use Dart Sass’ CLI somehow in this plugin. That would certainly make this plugin not only more usable and bug-free, but possibly faster, too.