Skip to content

Commit

Permalink
Use ~ to resolve node_modules in Sass (#77)
Browse files Browse the repository at this point in the history
* update bili

* sass imports

* strip .css extension
  • Loading branch information
egoist authored Feb 26, 2018
1 parent 510e9b6 commit 913c2db
Show file tree
Hide file tree
Showing 20 changed files with 725 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
/node_modules
dist
coverage
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ Extract CSS to the same location where JS file is generated but with `.css` exte

You can also set it to an absolute path.

### imports

__For Sass/Scss Only.__

Similar to how webpack's [sass-loader](https://github.com/webpack-contrib/sass-loader#imports) works, you can prepend the path with `~` to tell this plugin to resolve in `node_modules`:

```sass
@import "~bootstrap/dist/css/bootstrap";
```

### modules

Type: `boolean` `object`<br>
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"test": "npm run lint && jest",
"test:cov": "npm run lint && jest --coverage",
"build": "bili --buble.target.node 6",
"build": "bili --target node --no-babel.babelrc",
"lint": "xo",
"prepublishOnly": "npm run build"
},
Expand All @@ -31,7 +31,7 @@
"autoprefixer": "^7.2.4",
"babel-jest": "^22.0.4",
"babel-preset-env": "^1.5.1",
"bili": "^2.2.6",
"bili": "^3.0.1",
"eslint-config-rem": "^4.0.0",
"jest": "^22.0.4",
"less": "^2.7.3",
Expand All @@ -52,6 +52,7 @@
"postcss-modules": "^1.1.0",
"promise.series": "^0.2.0",
"reserved-words": "^0.1.2",
"resolve": "^1.5.0",
"rollup-pluginutils": "^2.0.1",
"style-inject": "^0.3.0"
},
Expand All @@ -76,4 +77,4 @@
]
]
}
}
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs-extra'
import { createFilter } from 'rollup-pluginutils'
import Concat from 'concat-with-sourcemaps'
import Loaders from './loaders'
import { normalizePath } from './utils'
import normalizePath from './utils/normalize-path'

/**
* The options that could be `boolean` or `object`
Expand Down
3 changes: 2 additions & 1 deletion src/less-loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pify from 'pify'
import { localRequire, normalizePath } from './utils'
import normalizePath from './utils/normalize-path'
import localRequire from './utils/local-require'

export default {
name: 'less',
Expand Down
3 changes: 2 additions & 1 deletion src/postcss-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import path from 'path'
import postcss from 'postcss'
import findPostcssConfig from 'postcss-load-config'
import reserved from 'reserved-words'
import { localRequire, normalizePath } from './utils'
import normalizePath from './utils/normalize-path'
import localRequire from './utils/local-require'

const styleInjectPath = require.resolve('style-inject/dist/style-inject.es')

Expand Down
24 changes: 22 additions & 2 deletions src/sass-loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import path from 'path'
import pify from 'pify'
import { localRequire } from './utils'
import resolve from 'resolve'
import localRequire from './utils/local-require'

const moduleRe = /^~([a-z0-9]|@).+/i

export default {
name: 'sass',
Expand All @@ -11,7 +15,23 @@ export default {
file: this.id,
data: code,
indentedSyntax: /\.sass$/.test(this.id),
sourceMap: this.sourceMap
sourceMap: this.sourceMap,
importer: [(url, prev, done) => {
if (!moduleRe.test(url)) return done({ file: prev })

resolve(url.slice(1), {
basedir: path.dirname(this.id),
extensions: ['.scss', '.sass', '.css']
}, (err, id) => {
if (err) {
return Promise.reject(err)
}
done({
// Do not add `.css` extension in order to inline the file
file: id.endsWith('.css') ? id.replace(/\.css$/, '') : id
})
})
}].concat(this.options.importer || [])
})

return {
Expand Down
2 changes: 1 addition & 1 deletion src/stylus-loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pify from 'pify'
import { localRequire } from './utils'
import localRequire from './utils/local-require'

export default {
name: 'stylus',
Expand Down
5 changes: 0 additions & 5 deletions src/utils.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/utils/local-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import path from 'path'

const localRequire = name => require(path.resolve('node_modules', name))

export default localRequire
6 changes: 6 additions & 0 deletions src/utils/normalize-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import path from 'path'

const normalizePath = filepath => path.relative(process.cwd(),
filepath)

export default normalizePath
35 changes: 35 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,41 @@ styleInject(css);
"
`;

exports[`sass:import: js code 1`] = `
"'use strict';
function styleInject(css, ref) {
if ( ref === void 0 ) ref = {};
var insertAt = ref.insertAt;
if (!css || typeof document === 'undefined') { return; }
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
if (insertAt === 'top') {
if (head.firstChild) {
head.insertBefore(style, head.firstChild);
} else {
head.appendChild(style);
}
} else {
head.appendChild(style);
}
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
}
var css = \\".a {\\\\n color: pink; }\\\\n\\\\n.b {\\\\n color: red; }\\\\n\\\\n.c {\\\\n color: black; }\\\\n\\";
styleInject(css);
"
`;

exports[`sass:modules: js code 1`] = `
"'use strict';
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/sass-import/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.scss'
3 changes: 3 additions & 0 deletions test/fixtures/sass-import/node_modules/foo/bar/a.scss

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/fixtures/sass-import/node_modules/foo/bar/b.sass

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/sass-import/node_modules/foo/bar/c.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/sass-import/node_modules/foo/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/sass-import/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@import "~foo/bar/a";
@import "~foo/bar/b";
@import "~foo/bar/c";
5 changes: 5 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ snapshot({
}
})

snapshot({
title: 'sass:import',
input: 'sass-import/index.js'
})

test('onExtract', async () => {
const res = await write({
input: 'simple/index.js',
Expand Down
Loading

0 comments on commit 913c2db

Please sign in to comment.