Skip to content
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

adds options.type override #9

Open
wants to merge 2 commits 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
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need this file

// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Debug Jest",
// "preLaunchTask": "Jest watch",
// "postDebugTask": "",
"port": 9229,
"protocol": "inspector",
// "trace": true
}
]
}
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Probably you use [sass-loader](https://github.com/webpack-contrib/sass-loader) o
For sass-loader:
```js
{
rules: [
rules: [{
test: /\.scss$/,
use: [
{
Expand All @@ -37,15 +37,15 @@ For sass-loader:
loader: "js-to-styles-var-loader"
}
]
]
}]
}
```

For less-loader:

```js
{
rules: [
rules: [{
test: /\.less$/,
use: [
{
Expand All @@ -61,9 +61,28 @@ For less-loader:
loader: "js-to-styles-var-loader"
}
]
]
}]
}
```

Specify type:
```js
{
rules: [{
test: /\.less$/,
use: [
"less-loader",
{
loader : "js-to-styles-var-loader",
options: {
type: "less"
}
}
]
}]
}
```

#### Usage

Let's assume we would like to store some variable data in a module like this:
Expand Down
9 changes: 8 additions & 1 deletion __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ describe('js-to-styles-vars-loader', () => {
expect(operator.getPreprocessorType).toHaveBeenCalledWith({ resource: context._module.resource });
});

it('calls operator.mergeVarsToContent with content and loader context, and custom preprocessor type', () => {
const customLessContext = {...context, options: { type: 'less', }}

spyOn(operator, 'mergeVarsToContent');
loader.call(customLessContext, 'asdf');
expect(operator.mergeVarsToContent).toHaveBeenCalledWith('asdf', customLessContext, 'less');
})

it('calls operator.mergeVarsToContent with content and loader context, and preprocessor type', () => {
spyOn(operator, 'mergeVarsToContent');
loader.call(context, 'asdf');
expect(operator.mergeVarsToContent).toHaveBeenCalledWith('asdf', context, 'sass');

});

it('handles .sass extension for sass files', () => {
Expand Down
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path');
const decache = require('decache');
const squba = require('squba')
const getOptions = require('loader-utils').getOptions

const requireReg = /require\s*\((["'])([\w.\/]+)(?:\1)\)((?:\.[\w_-]+)*);?/igm;

Expand Down Expand Up @@ -127,8 +128,16 @@ exports.operator = operator;

const loader = function (content) {
const webpackContext = this;
const resource = operator.getResource(webpackContext);
const preprocessorType = operator.getPreprocessorType({ resource });
// const options = getOptions(this);
let preprocessorType

if(this.options && this.options.type) {
preprocessorType = this.options.type
} else {
const resource = operator.getResource(webpackContext);
preprocessorType = operator.getPreprocessorType({ resource });
}

const merged = operator.mergeVarsToContent(content, webpackContext, preprocessorType);
return merged;
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"scripts": {
"test": "jest --config jest.config",
"test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand --config jest.config",
"test:coverage": "jest --config jest.config --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
"test:watch": "jest --watch --config jest.config"
},
Expand Down