Skip to content

Commit

Permalink
use regex to get comment
Browse files Browse the repository at this point in the history
  • Loading branch information
friskfly committed Nov 30, 2015
1 parent a1185e5 commit 1a1558e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 57 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
node_modules
bundle.js
bundle.js.map
12 changes: 12 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ var ab = {
"B can't see this"
})()
/* #end */

// #if version-b,version-c
(function(){
"A can't see this"
})()
// #end

// #if version-a,version-c
(function(){
"B can't see this"
})()
// #end
35 changes: 24 additions & 11 deletions lib/collect.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
module.exports = function(comments) {
var re = /(\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/)|(\/\/(.*))/g
module.exports = function(source) {
var collect = []
var data = []
comments.forEach(function(comment) {
var value = comment.value.trim()
if (value.indexOf('#if') === 0) {
comment.append = {
if: true
}
data.push(comment)
re.lastIndex = 0

var match
while (match = re.exec(source)) {
var v = match[0]
if(v.indexOf('/*') === 0){
v = v.slice(0, v.length - 2)
}
if (value.indexOf('#end') === 0 && data[0] && data[0].append.if) {
v = v.slice(2).trim()
if (v.indexOf('#if') === 0) {
data.push({
value: v,
range: [re.lastIndex - match[0].length, re.lastIndex],
if :true
})
}

if (v.indexOf('#end') === 0 && data[0] && data[0].if) {
collect.push({
if: data[0],
end: comment
end: {
range: [re.lastIndex - match[0].length, re.lastIndex],
value: v
}
})
data = []
}
})
}
return collect
}
43 changes: 3 additions & 40 deletions lib/loader.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,13 @@
var espree = require('espree')
var collect = require('./collect')
var generate = require('./generate')

module.exports = function(source) {
this.cacheable()
if(!this.options['if-loader']){
if (!this.options['if-loader']) {
this.emitError('if-loader config undefined')
}
var ast = espree.parse(source, {
range: true,
loc: true,
comments: true,
attachComment: true,
tokens: true,
tolerant: true,
ecmaFeatures: {
arrowFunctions: true,
blockBindings: true,
destructuring: true,
regexYFlag: true,
regexUFlag: true,
templateStrings: true,
binaryLiterals: true,
octalLiterals: true,
unicodeCodePointEscapes: true,
defaultParams: true,
restParams: true,
forOf: true,
objectLiteralComputedProperties: true,
objectLiteralShorthandMethods: true,
objectLiteralShorthandProperties: true,
objectLiteralDuplicateProperties: true,
generators: true,
spread: true,
superInFunctions: true,
classes: true,
newTarget: false,
modules: true,
jsx: true,
globalReturn: true,
experimentalObjectRestSpread: true
}
})
var coll = collect(ast.comments)
var coll = collect(source)
if (coll.length) {
source = generate.call(this,source, coll)
source = generate.call(this, source, coll)
}
return source
}
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "if-loader",
"version": "1.0.0",
"description": "js if-loader",
"description": "This is a preprocesser for the webpack module bundler. It support the ' if ' directive,similar to C ' #ifdef ' ",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand All @@ -12,9 +12,6 @@
"webpack": "^1.12.9"
},
"dependencies": {
"escodegen": "^1.7.1",
"espree": "^2.2.5",
"estraverse": "^4.1.1",
"source-map": "^0.5.3"
}
}
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ module.exports = {
loader: './index.js'
}]
},
devtool: 'source-map'
// devtool: 'source-map'
}

0 comments on commit 1a1558e

Please sign in to comment.