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

Add a way to pass local variables as options #15

Open
wants to merge 3 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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ import reduxLogger from 'redux-logger'
// #endif
```

You can pass variables as options to the loader:

```js
// webpack.config.js
module: {
rules: [{
test: /\.js$/,
use: [
'babel-loader',
{
loader: 'webpack-conditional-loader',
options: {
isReady: true
}
]
}]
}
```
```js
// myFile.js
// #if isReady
console.log(`I'm ready!`)
// #endif

```

## Credits
- [GCC C conditional documentation](https://gcc.gnu.org/onlinedocs/gcc-3.0.2/cpp_4.html)

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"src/",
"test/"
],
"dependencies": {},
"dependencies": {
"loader-utils": "^1.2.3"
},
"devDependencies": {
"standard": "^10.0.2",
"tap": "^10.7.0",
Expand Down
11 changes: 8 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-eval */
const os = require('os');
const os = require('os')
const { getOptions } = require('loader-utils')

function getPredicate (line) {
return /\/\/ #if (.*)/.exec(line)[1]
Expand Down Expand Up @@ -37,13 +38,17 @@ function searchBlocks (sourceByLine) {
return blocks
}

function getTruthyBlocks (blocks) {
function getTruthyBlocks (blocks, options) {
const truthyBlocks = blocks.slice()
let i = 0
let action = ''

while (i < truthyBlocks.length) {
if (truthyBlocks[i] && truthyBlocks[i].type === 'begin') {
// Let the predicate see the options we're giving.
Object.keys(options).forEach(k => {
global[k] = options[k]
})
if (eval(truthyBlocks[i].predicate)) {
truthyBlocks[i] = undefined
action = 'deleteNextEndBlock'
Expand Down Expand Up @@ -100,7 +105,7 @@ module.exports = function (source) {
try {
const sourceByLine = source.split(os.EOL)
const blocks = searchBlocks(sourceByLine)
const truthyBlocks = getTruthyBlocks(blocks)
const truthyBlocks = getTruthyBlocks(blocks, getOptions(this))
const transformedSource = commentCodeInsideBlocks(sourceByLine, truthyBlocks)

return transformedSource.join('\n')
Expand Down
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const truthy = require('../build/truthy')
const falsey = require('../build/falsey')
const envVarTruthy = require('../build/env-var-truthy')
const envVarFalsey = require('../build/env-var-falsey')
const localVarTruthy = require('../build/local-var-truthy')
const localVarFalsey = require('../build/local-var-falsey')

tap.test('comment blocks with falsey predicate', (test) => {
test.equal(falsey, 1)
Expand All @@ -23,3 +25,13 @@ tap.test('comment env var blocks with falsey predicate', (test) => {
test.equal(envVarFalsey, 1)
test.end()
})

tap.test('comment env var blocks with falsey predicate', (test) => {
test.equal(localVarFalsey, true)
test.end()
})

tap.test('comment env var blocks with falsey predicate', (test) => {
test.equal(localVarTruthy, true)
test.end()
})
7 changes: 7 additions & 0 deletions test/test-files/local-var-falsey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let a = true

// #if localVar.falsey
a = false
// #endif

module.exports = a
7 changes: 7 additions & 0 deletions test/test-files/local-var-truthy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let a = false

// #if localVar.truthy
a = true
// #endif

module.exports = a
12 changes: 11 additions & 1 deletion webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module.exports = {
entry: {
'env-var-truthy': './test/test-files/env-var-truthy.js',
'env-var-falsey': './test/test-files/env-var-falsey.js',
'local-var-truthy': './test/test-files/local-var-truthy.js',
'local-var-falsey': './test/test-files/local-var-falsey.js',
'falsey': './test/test-files/falsey.js',
'truthy': './test/test-files/truthy.js'
},
Expand All @@ -21,7 +23,15 @@ module.exports = {
module: {
rules: [{
test: /\.js$/,
use: ['conditional-loader']
use: [{
loader: 'conditional-loader',
options: {
localVar: {
truthy: true,
falsey: false
}
}
}]
}]
}
}