Skip to content

Commit 7814a95

Browse files
committed
Initial commit.
0 parents  commit 7814a95

32 files changed

+14989
-0
lines changed

.babelrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env"
5+
]
6+
]
7+
}

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/lib

.eslintrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
rules:
2+
indent:
3+
- 2
4+
- 2
5+
quotes:
6+
- 2
7+
- single
8+
- avoidEscape: true
9+
linebreak-style:
10+
- 2
11+
- unix
12+
semi:
13+
- 2
14+
- always
15+
env:
16+
es6: true
17+
node: true
18+
extends: 'eslint:recommended'
19+
parser: 'babel-eslint'

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# IDE files.
2+
/.idea
3+
4+
# Project files.
5+
/coverage
6+
/npm-debug.log
7+
/lib
8+
/node_modules

.releaserc.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
plugins:
3+
- '@semantic-release/commit-analyzer'
4+
- '@semantic-release/release-notes-generator'
5+
- [ '@semantic-release/changelog', { changelogFile: 'CHANGELOG.md' } ]
6+
- [ '@semantic-release/git', { assets: [ 'CHANGELOG.md'] } ]
7+
- '@semantic-release/npm'
8+
- '@semantic-release/github'

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- 13
4+
notifications:
5+
email: false
6+
after_success:
7+
- "[[ $TRAVIS_PULL_REQUEST != 'false' ]] && npx semantic-release-github-pr"
8+
- npx semantic-release --debug
9+
cache: npm

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
## [1.0.0] - 2020-10-20
11+
12+
### Added
13+
14+
- Initial release.
15+
- Ability to resolve imports from `includePaths`.
16+
- Ability to use a custom `resolver` function.
17+
- 100% code coverage!!! 🥳
18+
19+
[unreleased]: https://github.com/codebymikey/node-sass-conditional-importer/compare/v1.0.0...HEAD
20+
[1.0.0]: https://github.com/codebymikey/node-sass-conditional-importer/releases/tag/v1.0.0

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 codebymikey
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
# node-sass-conditional-importer
2+
3+
A conditional/dynamic importer for [node-sass]. It provides the ability to `@import` Sass files dynamically based on
4+
their (environment) extension prefix, similar to React Native's
5+
[platform-specific extensions][react-platform-specific-extensions] behaviour.
6+
7+
[![npm](https://img.shields.io/npm/v/node-sass-conditional-importer.svg)](https://www.npmjs.com/package/node-sass-conditional-importer)
8+
[![build status](https://travis-ci.org/codebymikey/node-sass-conditional-importer.svg?branch=master)](https://travis-ci.org/codebymikey/node-sass-conditional-importer)
9+
10+
It reads in a list of `environments` extension prefixes, which it'll attempt to use over the default file.
11+
12+
The example use case for this importer is as follows, say you have the following folder structure:
13+
14+
```
15+
scss
16+
├── custom
17+
│ ├── style.custom.scss
18+
│ ├── style.development.scss
19+
│ ├── style.production.scss
20+
│ └── style.scss
21+
└── main.scss
22+
```
23+
24+
And you want to import a different version of `style.scss` based on a given build environment/variable.
25+
This is not currently possible easily because Sass does not allow dynamic `@import`s
26+
[using interpolation][sass-no-dynamic-imports] or in [`if` statements][sass-no-if-imports].
27+
28+
This importer allows you to simply pass in your current environment into the importer, and it checks
29+
for whether the environment-specific override file exists before importing it.
30+
31+
The `environments` will be a list of environments ordered by the priority with which they should be used.
32+
33+
If none of the environment file overrides are available, then it falls back to the original file.
34+
35+
## Usage
36+
### Configuration options
37+
* `environments`: An array of environment extensions to look up. e.g.
38+
```javascript
39+
// process.env.NODE_ENV = 'production';
40+
// Look for [`${file}.production.scss`, `${file}.fallback.scss`]
41+
[process.env.NODE_ENV, 'fallback']
42+
```
43+
44+
### [node-sass]
45+
This module hooks into [node-sass's importer api][node-sass-importer-api].
46+
47+
```javascript
48+
var sass = require('node-sass');
49+
var conditionalImporter = require('node-sass-conditional-importer');
50+
51+
sass.render({
52+
file: scssFilename,
53+
importer: [
54+
conditionalImporter({
55+
environments: [
56+
// Search for `*.custom.scss` files first,
57+
// Followed `*.(development|production).scss` files.
58+
'custom',
59+
process.env.NODE_ENV,
60+
],
61+
}),
62+
// .. other importers
63+
],
64+
}, function(err, result) { /*...*/ });
65+
```
66+
67+
### Webpack / [sass-loader](https://github.com/jtangelder/sass-loader)
68+
69+
#### Webpack v1
70+
71+
```javascript
72+
import conditionalImporter from 'node-sass-conditional-importer';
73+
74+
// Webpack config
75+
export default {
76+
module: {
77+
loaders: [{
78+
test: /\.scss$/,
79+
loaders: ['style', 'css', 'sass']
80+
}],
81+
},
82+
sassLoader: {
83+
importer: conditionalImporter({
84+
environments: [
85+
// Import based on the NODE_ENV environment variable.
86+
process.env.NODE_ENV,
87+
],
88+
})
89+
}
90+
};
91+
```
92+
93+
#### Webpack v2
94+
95+
```javascript
96+
import conditionalImporter from 'node-sass-conditional-importer';
97+
98+
// Webpack config
99+
export default {
100+
module: {
101+
rules: [
102+
{
103+
test: /\.scss$/,
104+
use: [
105+
'style-loader',
106+
{
107+
loader: 'css-loader',
108+
options: {
109+
importLoaders: 1
110+
},
111+
},
112+
{
113+
loader: 'sass-loader',
114+
options: {
115+
importer: conditionalImporter({
116+
environments: [
117+
// Import based on the NODE_ENV environment variable.
118+
process.env.NODE_ENV,
119+
],
120+
}),
121+
},
122+
},
123+
],
124+
},
125+
],
126+
},
127+
};
128+
```
129+
130+
## Custom resolver
131+
132+
Should you care to resolve paths using some kind of custom logic, for example,
133+
resolving `~/` relative to the project root or some other arbitrary directory,
134+
you can do it using the following:
135+
136+
`main.scss`:
137+
138+
```scss
139+
@import '~/dynamic.scss';
140+
body {
141+
background: $background;
142+
}
143+
```
144+
145+
`custom/dynamic.myenvironment.scss`:
146+
147+
```scss
148+
$background: red;
149+
```
150+
151+
```js
152+
var path = require('path');
153+
var sass = require('node-sass');
154+
var conditionalImporter = require('node-sass-conditional-importer');
155+
156+
sass.render({
157+
file: './main.scss',
158+
importer: [
159+
conditionalImporter({
160+
environments: ['myenvironment'],
161+
resolver: function(dir, url) {
162+
return url.startsWith('~/')
163+
? path.resolve(dir, 'custom', url.substr(2))
164+
: path.resolve(dir, url);
165+
},
166+
})
167+
],
168+
}, function(err, result) { console.log(err || result.css.toString()) });
169+
```
170+
171+
## Known issues
172+
173+
- With a folder structure like:
174+
```
175+
scss
176+
├── custom
177+
│ ├── style.custom.scss
178+
│ ├── style.development.scss
179+
│ ├── style.production.scss
180+
│ └── style.scss
181+
└── main.scss
182+
```
183+
184+
A file like `style.production.scss` may not be used to import `style.scss` as it'll result in an import loop.
185+
186+
The recommended solution is to create a shared include file like `_style--shared.scss` and import that instead.
187+
188+
## Thanks to
189+
This importer is inspired by [node-sass-json-importer].
190+
191+
## 📄 License
192+
193+
node-sass-conditional-importer is MIT licensed, as found in the [LICENSE][license] file.
194+
195+
[node-sass]: https://github.com/sass/node-sass
196+
[react-platform-specific-extensions]: https://reactnative.dev/docs/platform-specific-code#platform-specific-extensions
197+
[node-sass-importer-api]: https://github.com/sass/node-sass#importer--v200---experimental
198+
[node-sass-json-importer]: https://github.com/pmowrer/node-sass-json-importer
199+
[sass-no-dynamic-imports]: https://sass-lang.com/documentation/at-rules/import#interpolation
200+
[sass-no-if-imports]: https://stackoverflow.com/q/13879042
201+
[license]: https://github.com/codebymikey/node-sass-conditional-importer/blob/master/LICENSE

0 commit comments

Comments
 (0)