-
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 (#1225)
* add plugin-sass with new onChange/markChanged interface * add tests * update docs * update yarn lockfile * update ts * update snapshots * add back missing snapshot tests * update add true .sass support
- Loading branch information
1 parent
d235ddb
commit 68e8d77
Showing
18 changed files
with
323 additions
and
36 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
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
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,89 @@ | ||
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, fileExt) { | ||
// 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 all imports, resolved to true files on disk. | ||
return allMatches | ||
.map((match) => match[2]) | ||
.filter((s) => s.trim()) | ||
.map((s) => { | ||
// 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. | ||
if (!path.extname(s)) { | ||
s += fileExt; | ||
} | ||
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.markChanged(importerFilePath); | ||
} | ||
}, | ||
/** Load the Sass file and compile it to CSS. */ | ||
async load({filePath, isDev}) { | ||
const fileExt = path.extname(filePath); | ||
const contents = fs.readFileSync(filePath, 'utf8'); | ||
// During development, we need to track changes to Sass dependencies. | ||
if (isDev) { | ||
const sassImports = scanSassImports(contents, filePath, fileExt); | ||
addImportsToMap(filePath, sassImports); | ||
} | ||
// If file is `.sass`, use YAML-style. Otherwise, use default. | ||
const args = ['--stdin', '--load-path', path.dirname(filePath)]; | ||
if (fileExt === '.sass') { | ||
args.push('--indented'); | ||
} | ||
// Build the file. | ||
const {stdout, stderr} = await execa('sass', args, { | ||
input: contents, | ||
env: native ? undefined : npmRunPath.env(), | ||
extendEnv: native ? true : false, | ||
}); | ||
// Handle the output. | ||
if (stderr) throw new Error(stderr); | ||
if (stdout) return stdout; | ||
}, | ||
}; | ||
}; |
23 changes: 23 additions & 0 deletions
23
plugins/plugin-sass/test/__snapshots__/plugin.test.js.snap
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 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`plugin-sass returns the compiled Sass result: App.sass 1`] = ` | ||
"body { | ||
font-family: Helvetica, sans-serif; | ||
} | ||
.App { | ||
text-align: center; | ||
background: #333; | ||
}" | ||
`; | ||
|
||
exports[`plugin-sass returns the compiled Sass result: App.scss 1`] = ` | ||
"body { | ||
font-family: Helvetica, sans-serif; | ||
} | ||
.App { | ||
text-align: center; | ||
background: #333; | ||
}" | ||
`; |
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 @@ | ||
awegagwagawingawignTHISISBADCODE |
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,5 @@ | ||
@use 'base' | ||
|
||
.App | ||
text-align: center | ||
background: base.$primary-color |
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,6 @@ | ||
// _base.scss | ||
$font-stack: Helvetica, sans-serif | ||
$primary-color: #333 | ||
|
||
body | ||
font-family: $font-stack |
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,6 @@ | ||
@use 'base'; | ||
|
||
.App { | ||
text-align: center; | ||
background: base.$primary-color; | ||
} |
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,7 @@ | ||
// _base.scss | ||
$font-stack: Helvetica, sans-serif; | ||
$primary-color: #333; | ||
|
||
body { | ||
font-family: $font-stack; | ||
} |
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,49 @@ | ||
const plugin = require('../plugin.js'); | ||
const path = require('path'); | ||
|
||
const pathToSassApp = path.join(__dirname, 'fixtures/sass/App.sass'); | ||
const pathToSassBase = path.join(__dirname, 'fixtures/sass/base.sass'); | ||
const pathToScssApp = path.join(__dirname, 'fixtures/scss/App.scss'); | ||
const pathToBadCode = path.join(__dirname, 'fixtures/bad/bad.scss'); | ||
|
||
describe('plugin-sass', () => { | ||
|
||
test('returns the compiled Sass result', async () => { | ||
const p = plugin(null, {}); | ||
const sassResult = await p.load({filePath: pathToSassApp, isDev: false}); | ||
expect(sassResult).toMatchSnapshot('App.sass'); | ||
const scssResult = await p.load({filePath: pathToScssApp, isDev: true}); | ||
expect(scssResult).toMatchSnapshot('App.scss'); | ||
}); | ||
|
||
test('throws an error when stderr output is returned', async () => { | ||
const p = plugin(null, {}); | ||
expect(p.load({filePath: pathToBadCode, isDev: false})).rejects.toThrow('Command failed with exit code'); | ||
}); | ||
|
||
test('marks a dependant as changed when an imported changes and isDev=true', async () => { | ||
const p = plugin(null, {}); | ||
p.markChanged = jest.fn(); | ||
await p.load({filePath: pathToSassApp, isDev: true}); | ||
expect(p.markChanged.mock.calls).toEqual([]); | ||
p.onChange({filePath: pathToSassApp}); | ||
expect(p.markChanged.mock.calls).toEqual([]); | ||
p.onChange({filePath: pathToSassBase}); | ||
expect(p.markChanged.mock.calls).toEqual([[pathToSassApp]]); | ||
}); | ||
|
||
test('does not track dependant changes when isDev=false', async () => { | ||
const p = plugin(null, {}); | ||
p.markChanged = jest.fn(); | ||
await p.load({filePath: pathToSassApp, isDev: false}); | ||
p.onChange({filePath: pathToSassApp}); | ||
p.onChange({filePath: pathToSassBase}); | ||
expect(p.markChanged.mock.calls).toEqual([]); | ||
}); | ||
|
||
test('uses native sass CLI when native option = true', async () => { | ||
const p = plugin(null, {native: true}); | ||
process.env.PATH = ''; | ||
expect(p.load({filePath: pathToSassApp, isDev: false})).rejects.toThrow('EPIPE'); | ||
}); | ||
}); |
Oops, something went wrong.