From b5e9c323c5823db7a4e663c496b9002c1c3e52b4 Mon Sep 17 00:00:00 2001 From: Josh Rosenbaum Date: Sun, 13 Sep 2020 13:46:03 -0600 Subject: [PATCH] Add support for both Windows and POSIX systems. --- package.json | 2 +- src/index.js | 3 +-- test/index.js | 26 ++++++++++++++++++++------ test/test-files/posix-falsey.js | 7 +++++++ test/test-files/posix-truthy.js | 7 +++++++ test/test-files/windows-falsey.js | 7 +++++++ test/test-files/windows-truthy.js | 7 +++++++ webpack.js | 6 ++++-- 8 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 test/test-files/posix-falsey.js create mode 100644 test/test-files/posix-truthy.js create mode 100644 test/test-files/windows-falsey.js create mode 100644 test/test-files/windows-truthy.js diff --git a/package.json b/package.json index 6e39d81..d288b43 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.js b/src/index.js index 8d185f2..e5c1a89 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,4 @@ /* eslint-disable no-eval */ -const os = require('os') const { getOptions } = require('loader-utils') function getPredicate (line) { @@ -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) diff --git a/test/index.js b/test/index.js index 0d42149..c9bc29e 100644 --- a/test/index.js +++ b/test/index.js @@ -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() }) @@ -35,3 +48,4 @@ tap.test('comment env var blocks with falsey predicate', (test) => { test.equal(localVarTruthy, true) test.end() }) + diff --git a/test/test-files/posix-falsey.js b/test/test-files/posix-falsey.js new file mode 100644 index 0000000..0eeeef2 --- /dev/null +++ b/test/test-files/posix-falsey.js @@ -0,0 +1,7 @@ +let a = 1 + +// #if 1 === 2 +a = 2 +// #endif + +module.exports = a diff --git a/test/test-files/posix-truthy.js b/test/test-files/posix-truthy.js new file mode 100644 index 0000000..355516b --- /dev/null +++ b/test/test-files/posix-truthy.js @@ -0,0 +1,7 @@ +let a = 1 + +// #if 1 === 1 +a = 2 +// #endif + +module.exports = a diff --git a/test/test-files/windows-falsey.js b/test/test-files/windows-falsey.js new file mode 100644 index 0000000..268b0a4 --- /dev/null +++ b/test/test-files/windows-falsey.js @@ -0,0 +1,7 @@ +let a = 1 + +// #if 1 === 2 +a = 2 +// #endif + +module.exports = a diff --git a/test/test-files/windows-truthy.js b/test/test-files/windows-truthy.js new file mode 100644 index 0000000..33adc49 --- /dev/null +++ b/test/test-files/windows-truthy.js @@ -0,0 +1,7 @@ +let a = 1 + +// #if 1 === 1 +a = 2 +// #endif + +module.exports = a diff --git a/webpack.js b/webpack.js index acaa361..e1a46af 100644 --- a/webpack.js +++ b/webpack.js @@ -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'),