-
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 2227e5d
Showing
7 changed files
with
164 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,23 @@ | ||
# @snowpack/plugin-sass | ||
|
||
This plugin adds [Sass](https://sass-lang.com/) support to any Snowpack project. With this plugin, you can import any `*.scss` Sass from JavaScript and have it compile to CSS. | ||
|
||
This plugin also supports `.module.scss` Sass modules. [Learn more.](https://www.snowpack.dev/#import-css-modules) | ||
|
||
## 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' | ||
], | ||
}; | ||
``` |
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,19 @@ | ||
{ | ||
"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" | ||
} | ||
} |
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,79 @@ | ||
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); | ||
} | ||
|
||
return allMatches | ||
.map((match) => match[2]) | ||
.filter((s) => s.trim()) | ||
.map((s) => { | ||
if (!path.extname(s)) { | ||
s += '.scss'; | ||
} | ||
return path.resolve(path.dirname(filePath), s); | ||
}); | ||
} | ||
|
||
module.exports = function postcssPlugin(_, options) { | ||
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'], | ||
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: npmRunPath.env(), | ||
extendEnv: true, | ||
}); | ||
// 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