Skip to content

Commit 6f4ea37

Browse files
authoredAug 20, 2019
feat: webpackImporter option (#732)
1 parent 0330253 commit 6f4ea37

File tree

4 files changed

+163
-3
lines changed

4 files changed

+163
-3
lines changed
 

‎README.md

+33
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,39 @@ module.exports = {
346346

347347
> ℹ In some rare case `node-sass` can output invalid source maps (it is `node-sass` bug), to avoid try to update node-sass to latest version or you can try to set the `outputStyle` option to `compressed` value.
348348
349+
### `webpackImporter`
350+
351+
Type: `Boolean`
352+
Default: `true`
353+
354+
Allows to disable default `webpack` importer.
355+
356+
This can improve performance in some cases. Use it with caution because aliases and `@import` at-rules starts with `~` will not work, but you can pass own `importer` to solve this (see [`importer docs`](https://github.com/sass/node-sass#importer--v200---experimental)).
357+
358+
**webpack.config.js**
359+
360+
```js
361+
module.exports = {
362+
module: {
363+
rules: [
364+
{
365+
test: /\.s[ac]ss$/i,
366+
use: [
367+
'style-loader',
368+
'css-loader',
369+
{
370+
loader: 'sass-loader',
371+
options: {
372+
webpackImporter: false,
373+
},
374+
},
375+
],
376+
},
377+
],
378+
},
379+
};
380+
```
381+
349382
## Examples
350383

351384
### Extracts CSS into separate files

‎src/index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,16 @@ function loader(content) {
6262

6363
const sassOptions = getSassOptions(this, options, content);
6464

65-
sassOptions.importer.push(
66-
webpackImporter(this.resourcePath, resolve, addNormalizedDependency)
67-
);
65+
const shouldUseWebpackImporter =
66+
typeof options.webpackImporter === 'boolean'
67+
? options.webpackImporter
68+
: true;
69+
70+
if (shouldUseWebpackImporter) {
71+
sassOptions.importer.push(
72+
webpackImporter(this.resourcePath, resolve, addNormalizedDependency)
73+
);
74+
}
6875

6976
// Skip empty files, otherwise it will stop webpack, see issue #21
7077
if (sassOptions.data.trim() === '') {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`webpackImporter option false (dart-sass) (sass): errors 1`] = `Array []`;
4+
5+
exports[`webpackImporter option false (dart-sass) (sass): warnings 1`] = `Array []`;
6+
7+
exports[`webpackImporter option false (dart-sass) (scss): errors 1`] = `Array []`;
8+
9+
exports[`webpackImporter option false (dart-sass) (scss): warnings 1`] = `Array []`;
10+
11+
exports[`webpackImporter option false (node-sass) (sass): errors 1`] = `Array []`;
12+
13+
exports[`webpackImporter option false (node-sass) (sass): warnings 1`] = `Array []`;
14+
15+
exports[`webpackImporter option false (node-sass) (scss): errors 1`] = `Array []`;
16+
17+
exports[`webpackImporter option false (node-sass) (scss): warnings 1`] = `Array []`;
18+
19+
exports[`webpackImporter option not specify (dart-sass) (sass): errors 1`] = `Array []`;
20+
21+
exports[`webpackImporter option not specify (dart-sass) (sass): warnings 1`] = `Array []`;
22+
23+
exports[`webpackImporter option not specify (dart-sass) (scss): errors 1`] = `Array []`;
24+
25+
exports[`webpackImporter option not specify (dart-sass) (scss): warnings 1`] = `Array []`;
26+
27+
exports[`webpackImporter option not specify (node-sass) (sass): errors 1`] = `Array []`;
28+
29+
exports[`webpackImporter option not specify (node-sass) (sass): warnings 1`] = `Array []`;
30+
31+
exports[`webpackImporter option not specify (node-sass) (scss): errors 1`] = `Array []`;
32+
33+
exports[`webpackImporter option not specify (node-sass) (scss): warnings 1`] = `Array []`;
34+
35+
exports[`webpackImporter option true (dart-sass) (sass): errors 1`] = `Array []`;
36+
37+
exports[`webpackImporter option true (dart-sass) (sass): warnings 1`] = `Array []`;
38+
39+
exports[`webpackImporter option true (dart-sass) (scss): errors 1`] = `Array []`;
40+
41+
exports[`webpackImporter option true (dart-sass) (scss): warnings 1`] = `Array []`;
42+
43+
exports[`webpackImporter option true (node-sass) (sass): errors 1`] = `Array []`;
44+
45+
exports[`webpackImporter option true (node-sass) (sass): warnings 1`] = `Array []`;
46+
47+
exports[`webpackImporter option true (node-sass) (scss): errors 1`] = `Array []`;
48+
49+
exports[`webpackImporter option true (node-sass) (scss): warnings 1`] = `Array []`;

‎test/webpackImporter-options.test.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
import nodeSass from 'node-sass';
5+
import dartSass from 'sass';
6+
7+
import {
8+
compile,
9+
getTestId,
10+
getCodeFromBundle,
11+
getCodeFromSass,
12+
getImplementationByName,
13+
} from './helpers';
14+
15+
const implementations = [nodeSass, dartSass];
16+
const syntaxStyles = ['scss', 'sass'];
17+
18+
describe('webpackImporter option', () => {
19+
implementations.forEach((implementation) => {
20+
syntaxStyles.forEach((syntax) => {
21+
const [implementationName] = implementation.info.split('\t');
22+
23+
it(`not specify (${implementationName}) (${syntax})`, async () => {
24+
const testId = getTestId('language', syntax);
25+
const options = {
26+
implementation: getImplementationByName(implementationName),
27+
};
28+
const stats = await compile(testId, { loader: { options } });
29+
30+
expect(getCodeFromBundle(stats).css).toBe(
31+
getCodeFromSass(testId, options).css
32+
);
33+
34+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
35+
expect(stats.compilation.errors).toMatchSnapshot('errors');
36+
});
37+
38+
it(`false (${implementationName}) (${syntax})`, async () => {
39+
const testId = getTestId('language', syntax);
40+
const options = {
41+
webpackImporter: false,
42+
implementation: getImplementationByName(implementationName),
43+
};
44+
const stats = await compile(testId, { loader: { options } });
45+
46+
expect(getCodeFromBundle(stats).css).toBe(
47+
getCodeFromSass(testId, options).css
48+
);
49+
50+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
51+
expect(stats.compilation.errors).toMatchSnapshot('errors');
52+
});
53+
54+
it(`true (${implementationName}) (${syntax})`, async () => {
55+
const testId = getTestId('language', syntax);
56+
const options = {
57+
webpackImporter: true,
58+
implementation: getImplementationByName(implementationName),
59+
};
60+
const stats = await compile(testId, { loader: { options } });
61+
62+
expect(getCodeFromBundle(stats).css).toBe(
63+
getCodeFromSass(testId, options).css
64+
);
65+
66+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
67+
expect(stats.compilation.errors).toMatchSnapshot('errors');
68+
});
69+
});
70+
});
71+
});

0 commit comments

Comments
 (0)
Please sign in to comment.