Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-conditional-loader",
"version": "1.0.12",
"version": "1.0.13",
"description": "#ifdef for JavaScript",
"main": "src/index.js",
"directories": {
Expand Down
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable no-eval */
const os = require('os')
const { getOptions } = require('loader-utils')

function getPredicate (line) {
Expand Down Expand Up @@ -103,7 +102,7 @@ function commentLine (line) {

module.exports = function (source) {
try {
const sourceByLine = source.split(os.EOL)
const sourceByLine = source.split(/[\r\n]+/g)
const blocks = searchBlocks(sourceByLine)
const truthyBlocks = getTruthyBlocks(blocks, getOptions(this))
const transformedSource = commentCodeInsideBlocks(sourceByLine, truthyBlocks)
Expand Down
26 changes: 20 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
const tap = require('tap')
const truthy = require('../build/truthy')
const falsey = require('../build/falsey')
const posixTruthy = require('../build/posix-truthy')
const posixFalsey = require('../build/posix-falsey')
const windowsTruthy = require('../build/windows-truthy')
const windowsFalsey = require('../build/windows-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)

tap.test('comment blocks with falsey predicate (POSIX EOL)', (test) => {
test.equal(posixFalsey, 1)
test.end()
})

tap.test('dont comment blocks with truthy predicate (POSIX EOL)', (test) => {
test.equal(posixTruthy, 2)
test.end()
})

tap.test('dont comment blocks with truthy predicate', (test) => {
test.equal(truthy, 2)
tap.test('comment blocks with falsey predicate (Windows EOL)', (test) => {
test.equal(windowsFalsey, 1)
test.end()
})

tap.test('dont comment blocks with truthy predicate (Windows EOL', (test) => {
test.equal(windowsTruthy, 2)
test.end()
})

Expand All @@ -35,3 +48,4 @@ 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/posix-falsey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let a = 1

// #if 1 === 2
a = 2
// #endif

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

// #if 1 === 1
a = 2
// #endif

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

// #if 1 === 2
a = 2
// #endif

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

// #if 1 === 1
a = 2
// #endif

module.exports = a
6 changes: 4 additions & 2 deletions webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ module.exports = {
'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'
'posix-falsey': './test/test-files/posix-falsey.js',
'posix-truthy': './test/test-files/posix-truthy.js',
'windows-falsey': './test/test-files/windows-falsey.js',
'windows-truthy': './test/test-files/windows-truthy.js',
},
output: {
path: path.resolve(__dirname, 'build'),
Expand Down