Skip to content

Commit

Permalink
Initial webpack-define-loader project
Browse files Browse the repository at this point in the history
Changes to be committed:
	new file:   .gitignore
	new file:   LICENSE
	new file:   README.md
	new file:   index.js
	new file:   package.json
	new file:   parse.js
	new file:   yarn.lock
  • Loading branch information
shjyh committed Jun 23, 2017
0 parents commit 72d3621
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.DS_Store
.vscode
node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 wangkehan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Webpack-Define-Loader

this loader can provide your code(js/ts/...) the ability of c style conditional compilation

## Installation

this loader requires __node version >= 4__

* npm
```
npm install webpack-define-loader --save-dev
```
* or use yarn
```
yarn add webpack-define-loader --dev
```


## Example:
```js
//webpack2 config
{
//...
module: {
rules: [
test: /\.(js|jsx|vue)$/,
loader: 'webpack-define-loader',
options: {
DEBUG: true,
PLATFORM: 'pc'
},
enforce: 'pre',
exclude: /node_modules/
]
}
//...
}

```

```js
/*
* code file
* conditional compilation begin with `///`
* condition after #if and #elif directive can be any legal js expression
*/

/// #if DEBUG
import parse from './parse.debug'
/// #else
import parse from './parse.release'
/// #endif

/// #if PLATFORM==='pc'
import render from './render.pc'
/// #elif PLATFORM==='mb'
import render from './render.mb'
/// #else
import render from './render.common'
/// #endif
```

## License

MIT

17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";

const loaderUtils = require('loader-utils');
const parse = require("./parse");
module.exports = function (source, map) {
this.cacheable && this.cacheable();
const data = loaderUtils.getOptions(this);

try {
source = parse(source, data);
this.callback(null, source, map);
}
catch (err) {
var errorMessage = "parse error: " + err;
this.callback(new Error(errorMessage));
}
};
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "webpack-define-loader",
"version": "1.0.4",
"engines": {
"node": ">=4"
},
"description": "A c style conditional compilation webpack loader.",
"keywords": [
"loader",
"webpack",
"define",
"ifdef"
],
"main": "index.js",
"files": [
"index.js",
"parse.js"
],
"author": "wang",
"homepage": "https://github.com/kserver/define-loader",
"license": "MIT",
"bugs": {
"url": "https://github.com/kserver/define-loader/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/kserver/define-loader.git"
},
"dependencies": {
"loader-utils": "^1.1.0"
}
}
64 changes: 64 additions & 0 deletions parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"use strict";

function parse(source,defs){
if(!defs) defs = {};

const lines = source.split('\n');
const listVarName = '__$list$__';
let code = `(function(){
const ${listVarName} = [];\n`;
for(let key of Object.keys(defs)){
code += `const ${key} = ${JSON.stringify(defs[key])};\n`;
}
for(let line of lines){
const checkResult = checkLine(line);
if(checkResult){
switch(checkResult.condition){
case 'if':
code += `if(${checkResult.expr}){\n`;
break;
case 'else':
code += '}else{\n';
break;
case 'elif':
code += `}else if(${checkResult.expr}){\n`;
break;
case 'endif':
code += '}\n';
break;
}
}else{
code += `${listVarName}.push(${JSON.stringify(line)});\n`
}
}
code += `return ${listVarName};
})();`;

try{
const result = eval(code);
return result.join('\n');
}catch(e){
throw new Error('error condition');
}
}


function checkLine(line){
const matchResult = line.match(/\/{3,}[\s]*#(if|else|elif|endif)([\s]+[\s\S]*)?$/);

if(matchResult){
const checkResult = {condition:matchResult[1],expr:(matchResult[2]||'').trim()};
if(
(checkResult.condition==='if'||checkResult.condition==='elif')
&&
checkResult.expr===''
){
return false;
}
return checkResult;
}

return false;
}

module.exports = parse;
23 changes: 23 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


big.js@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/big.js/-/big.js-3.1.3.tgz#4cada2193652eb3ca9ec8e55c9015669c9806978"

emojis-list@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/emojis-list/-/emojis-list-2.1.0.tgz#4daa4d9db00f9819880c79fa457ae5b09a1fd389"

json5@^0.5.0:
version "0.5.1"
resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"

loader-utils@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-1.1.0.tgz#c98aef488bcceda2ffb5e2de646d6a754429f5cd"
dependencies:
big.js "^3.1.3"
emojis-list "^2.0.0"
json5 "^0.5.0"

0 comments on commit 72d3621

Please sign in to comment.