Skip to content

feat: add less ~ import #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,18 @@ That's it, you can now import `.styl` `.scss` `.sass` `.less` files in your libr

#### imports

__For Sass/Scss Only.__
__For Sass/Scss/Less 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";
```

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

## Options

### extensions
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"types/index.d.ts"
],
"scripts": {
"test": "npm run lint && jest",
"test": "jest",
"test:cov": "npm run lint && jest --coverage",
"build": "bili",
"lint": "xo",
Expand Down Expand Up @@ -55,6 +55,7 @@
"concat-with-sourcemaps": "^1.1.0",
"cssnano": "^5.0.1",
"import-cwd": "^3.0.0",
"less-plugin-npm-import": "^2.1.0",
"p-queue": "^6.6.2",
"pify": "^5.0.0",
"postcss-load-config": "^3.0.0",
Expand Down
4 changes: 3 additions & 1 deletion src/less-loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pify from 'pify'
import LessNpmImport from 'less-plugin-npm-import'
import humanlizePath from './utils/humanlize-path'
import { loadModule } from './utils/load-module'

Expand All @@ -15,7 +16,8 @@ export default {
let { css, map, imports } = await pify(less.render.bind(less))(code, {
...this.options,
sourceMap: this.sourceMap && {},
filename: this.id
filename: this.id,
plugins: [new LessNpmImport({ prefix: '~' })]
})

for (const dep of imports) {
Expand Down
35 changes: 35 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,41 @@ console.log(css_248z$5, css_248z$4);
"
`;

exports[`less 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_248z = \\".a {\\\\n color: pink;\\\\n}\\\\n.b {\\\\n color: red;\\\\n}\\\\n.c {\\\\n color: black;\\\\n}\\\\n.foo {\\\\n color: magenta;\\\\n}\\\\n\\";
styleInject(css_248z);
"
`;

exports[`minimize extract: css code 1`] = `".bar,body{color:red}body{background:red}#sidebar{background-color:#faa;width:30%}#header{color:#6c94be}.pcss{color:red}"`;

exports[`minimize extract: js code 1`] = `
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/less-import/_partial.less
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@color: magenta;
5 changes: 5 additions & 0 deletions test/fixtures/less-import/foo.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import "_partial";

.foo {
color: @color;
}
1 change: 1 addition & 0 deletions test/fixtures/less-import/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.less'

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/less-import/node_modules/foo/bar/a.less

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/less-import/node_modules/foo/bar/b.less

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/less-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/less-import/node_modules/foo/package.json

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

5 changes: 5 additions & 0 deletions test/fixtures/less-import/style.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import "~foo/bar/a";
@import "~foo/bar/b";
@import "~foo/bar/c";
@import "~foo/bar/_partial";
@import "foo.less";
7 changes: 7 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ snapshotMany('sass', [
}
])

snapshotMany('less', [
{
title: 'import',
input: 'less-import/index.js'
}
])

test('onExtract', async () => {
const result = await write({
input: 'simple/index.js',
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,11 @@ arrify@^2.0.1:
resolved "https://registry.yarnpkg.com/arrify/-/arrify-2.0.1.tgz#c9655e9331e0abcd588d2a7cad7e9956f66701fa"
integrity sha512-3duEwti880xqi4eAMN8AyR4a0ByT90zoYdLlevfrvU43vb0YZwZVfxOgxWrLXXXpyugL0hNZc9G6BiB5B3nUug==

asap@~2.0.3:
version "2.0.6"
resolved "https://registry.npm.taobao.org/asap/download/asap-2.0.6.tgz#e50347611d7e690943208bbdafebcbc2fb866d46"
integrity sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY=

asn1.js@^5.2.0:
version "5.4.1"
resolved "https://registry.yarnpkg.com/asn1.js/-/asn1.js-5.4.1.tgz#11a980b84ebb91781ce35b0fdc2ee294e3783f07"
Expand Down Expand Up @@ -5766,6 +5771,14 @@ latest-version@^5.1.0:
dependencies:
package-json "^6.3.0"

less-plugin-npm-import@^2.1.0:
version "2.1.0"
resolved "https://registry.nlark.com/less-plugin-npm-import/download/less-plugin-npm-import-2.1.0.tgz#823e6986c93318a98171ca858848b6bead55bf3e"
integrity sha1-gj5phskzGKmBccqFiEi2vq1Vvz4=
dependencies:
promise "~7.0.1"
resolve "~1.1.6"

less@^3.12.2:
version "3.13.1"
resolved "https://registry.yarnpkg.com/less/-/less-3.13.1.tgz#0ebc91d2a0e9c0c6735b83d496b0ab0583077909"
Expand Down Expand Up @@ -7646,6 +7659,13 @@ promise.series@^0.2.0:
resolved "https://registry.yarnpkg.com/promise.series/-/promise.series-0.2.0.tgz#2cc7ebe959fc3a6619c04ab4dbdc9e452d864bbd"
integrity sha1-LMfr6Vn8OmYZwEq029yeRS2GS70=

promise@~7.0.1:
version "7.0.4"
resolved "https://registry.nlark.com/promise/download/promise-7.0.4.tgz#363e84a4c36c8356b890fed62c91ce85d02ed539"
integrity sha1-Nj6EpMNsg1a4kP7WLJHOhdAu1Tk=
dependencies:
asap "~2.0.3"

prompts@^2.0.1:
version "2.4.0"
resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.0.tgz#4aa5de0723a231d1ee9121c40fdf663df73f61d7"
Expand Down Expand Up @@ -8099,6 +8119,11 @@ resolve@^1.10.0, resolve@^1.10.1, resolve@^1.11.0, resolve@^1.13.1, resolve@^1.1
is-core-module "^2.2.0"
path-parse "^1.0.6"

resolve@~1.1.6:
version "1.1.7"
resolved "https://registry.npm.taobao.org/resolve/download/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"
integrity sha1-IDEU2CrSxe2ejgQRs5ModeiJ6Xs=

responselike@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/responselike/-/responselike-1.0.2.tgz#918720ef3b631c5642be068f15ade5a46f4ba1e7"
Expand Down