diff --git a/README.md b/README.md index 0e000b4..e29f493 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/package.json b/package.json index c3befa4..6e39d81 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ "src/", "test/" ], - "dependencies": {}, + "dependencies": { + "loader-utils": "^1.2.3" + }, "devDependencies": { "standard": "^10.0.2", "tap": "^10.7.0", diff --git a/src/index.js b/src/index.js index dcdad5b..8d185f2 100644 --- a/src/index.js +++ b/src/index.js @@ -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] @@ -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' @@ -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') diff --git a/test/index.js b/test/index.js index 405b85b..0d42149 100644 --- a/test/index.js +++ b/test/index.js @@ -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) @@ -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() +}) diff --git a/test/test-files/local-var-falsey.js b/test/test-files/local-var-falsey.js new file mode 100644 index 0000000..b51377c --- /dev/null +++ b/test/test-files/local-var-falsey.js @@ -0,0 +1,7 @@ +let a = true + +// #if localVar.falsey +a = false +// #endif + +module.exports = a diff --git a/test/test-files/local-var-truthy.js b/test/test-files/local-var-truthy.js new file mode 100644 index 0000000..3d7d76b --- /dev/null +++ b/test/test-files/local-var-truthy.js @@ -0,0 +1,7 @@ +let a = false + +// #if localVar.truthy +a = true +// #endif + +module.exports = a diff --git a/webpack.js b/webpack.js index 4408685..acaa361 100644 --- a/webpack.js +++ b/webpack.js @@ -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' }, @@ -21,7 +23,15 @@ module.exports = { module: { rules: [{ test: /\.js$/, - use: ['conditional-loader'] + use: [{ + loader: 'conditional-loader', + options: { + localVar: { + truthy: true, + falsey: false + } + } + }] }] } }