-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add plugin-sass with new onChange/markChanged interface
- Loading branch information
1 parent
62908d9
commit dcc5d99
Showing
7 changed files
with
186 additions
and
12 deletions.
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,35 @@ | ||
# @snowpack/plugin-sass | ||
|
||
This plugin adds [Sass](https://sass-lang.com/) support to any Snowpack project. With this plugin, you can import any `*.scss` or `*.sass` Sass file from JavaScript and have it compile to CSS. | ||
|
||
This plugin also adds support for `.module.scss` Sass Modules. [Learn more.](https://www.snowpack.dev/#import-css-modules) | ||
|
||
## A Note on Sass Implementations | ||
|
||
Sass is interesting in that multiple compilers are available: [sass](https://www.npmjs.com/package/sass) (written in Dart) & [node-sass](https://www.npmjs.com/package/node-sass) (written in JavaScript). Both packages run on Node.js and both are popular on npm. However, [node-sass is now considered deprecated](https://github.com/sass/node-sass/issues/2952). | ||
|
||
**This plugin was designed to work with the `sass` package.** `sass` is automatically installed with this plugin as a direct dependency, so no extra effort is required on your part. | ||
|
||
## Usage | ||
|
||
```bash | ||
npm i @snowpack/plugin-sass | ||
``` | ||
|
||
Then add the plugin to your Snowpack config: | ||
|
||
```js | ||
// snowpack.config.js | ||
|
||
module.exports = { | ||
plugins: [ | ||
['@snowpack/plugin-sass', { /* see options below */ } | ||
], | ||
}; | ||
``` | ||
|
||
## Plugin Options | ||
|
||
| Name | Type | Description | | ||
| :------- | :-------: | :----------------- | | ||
| `native` | `boolean` | If true, the plugin will ignore the npm version of sass installed locally for the native Sass CLI [installed separately](https://sass-lang.com/install). This involves extra set up, but the result can be [up to 9x faster.](https://stackoverflow.com/a/56422541) Defaults to false. | |
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,20 @@ | ||
{ | ||
"version": "1.0.0", | ||
"name": "@snowpack/plugin-sass", | ||
"main": "plugin.js", | ||
"license": "MIT", | ||
"homepage": "https://github.com/pikapkg/snowpack/tree/master/plugins/plugin-sass#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/pikapkg/snowpack.git", | ||
"directory": "plugins/plugin-sass" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"execa": "^4.0.3", | ||
"npm-run-path": "^4.0.1", | ||
"sass": "^1.3.0" | ||
} | ||
} |
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,88 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const execa = require('execa'); | ||
const npmRunPath = require('npm-run-path'); | ||
|
||
const IMPORT_REGEX = /\@(use|import)\s*['"](.*?)['"]/g; | ||
|
||
function scanSassImports(fileContents, filePath) { | ||
// TODO: Replace with matchAll once Node v10 is out of TLS. | ||
// const allMatches = [...result.matchAll(new RegExp(HTML_JS_REGEX))]; | ||
const allMatches = []; | ||
let match; | ||
const regex = new RegExp(IMPORT_REGEX); | ||
while ((match = regex.exec(fileContents))) { | ||
allMatches.push(match); | ||
} | ||
// Inherit the default file extension of the importer. This is a cheap | ||
// but effective shortcut to supporting both ".scss" and ".sass" | ||
// since users rarely mix both, and performing multiple lookups | ||
// would be too expensive. | ||
const missingFileExt = path.extname(filePath); | ||
// return all imports, resolved to true files on disk. | ||
return allMatches | ||
.map((match) => match[2]) | ||
.filter((s) => s.trim()) | ||
.map((s) => { | ||
if (!path.extname(s)) { | ||
s += missingFileExt; | ||
} | ||
return path.resolve(path.dirname(filePath), s); | ||
}); | ||
} | ||
|
||
module.exports = function postcssPlugin(_, {native}) { | ||
const importedByMap = new Map(); | ||
|
||
function addImportsToMap(filePath, sassImports) { | ||
for (const imported of sassImports) { | ||
const importedBy = importedByMap.get(imported); | ||
if (importedBy) { | ||
importedBy.add(filePath); | ||
} else { | ||
importedByMap.set(imported, new Set([filePath])); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
name: '@snowpack/plugin-sass', | ||
resolve: { | ||
input: ['.scss', '.sass'], | ||
output: ['.css'], | ||
}, | ||
/** when a file changes, also mark it's importers as changed. */ | ||
onChange({filePath}) { | ||
const importedBy = importedByMap.get(filePath); | ||
if (!importedBy) { | ||
return; | ||
} | ||
importedByMap.delete(filePath); | ||
for (const importerFilePath of importedBy) { | ||
this.markChange(importerFilePath); | ||
} | ||
}, | ||
/** Load the Sass file and compile it to CSS. */ | ||
async load({filePath, isDev}) { | ||
const contents = fs.readFileSync(filePath, 'utf8'); | ||
// During development, we need to track changes to Sass dependencies. | ||
if (isDev) { | ||
const sassImports = scanSassImports(contents, filePath); | ||
addImportsToMap(filePath, sassImports); | ||
} | ||
// Build the file. | ||
const {stdout, stderr} = await execa( | ||
'sass', | ||
['--stdin', '--load-path', path.dirname(filePath)], | ||
{ | ||
input: contents, | ||
env: native ? undefined : npmRunPath.env(), | ||
extendEnv: native ? true : false, | ||
}, | ||
); | ||
// Handle the output. | ||
if (stderr) throw new Error(stderr); | ||
if (stdout) return stdout; | ||
}, | ||
}; | ||
}; |
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
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
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
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