Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wendux/keep-loader
一个用于在不同的打包环境下生成不同的代码的Webpack loader,就像C/C++中的宏特性一样。提供了一种在源码中控制打包阶段生成不同代码的能力。
https://github.com/wendux/keep-loader
Last synced: about 1 month ago
JSON representation
一个用于在不同的打包环境下生成不同的代码的Webpack loader,就像C/C++中的宏特性一样。提供了一种在源码中控制打包阶段生成不同代码的能力。
- Host: GitHub
- URL: https://github.com/wendux/keep-loader
- Owner: wendux
- Created: 2018-02-28T07:08:56.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-01T16:46:50.000Z (over 1 year ago)
- Last Synced: 2023-04-10T03:15:54.375Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 397 KB
- Stars: 17
- Watchers: 2
- Forks: 1
- Open Issues: 9
-
Metadata Files:
- Readme: readme-en.md
Awesome Lists containing this project
README
# keep-loader for webpack
## description
Generate different code based on the development and production environment, just like the macro features of some compiled programming languages, like C.
English document:https://github.com/wendux/keep-loader/blob/master/readme-en.md
中文文档:https://github.com/wendux/keep-loader/blob/master/readme.md
## usage
1. Install keep-loader
```javascript
npm install keep-loader --save-dev
```2. Modify your webpack config
```javascript
module: {
...
rules: [
...
{
test: /\.js$/,
use: [
{
loader: 'keep-loader',
options:{
keep:process.env.NODE_ENV === 'production'?"prod":"dev"
}
},
{loader: 'babel-loader'}
],
include: [resolve('src'), resolve('test')]
},
...
]
...
}
```Now, you can use function `KEEP` in your code , for example:
```javascript
var env=""
KEEP("dev",e=>{
console.log("I will be keeped in development environment,but removed in other environment")
env="development"
})
KEEP("prod",e=>{
console.log("I will be keeped in production enviroment,but removed in other environment")
env="production"
})
console.log(env)
//output "development" in development enviroment, "production" in prduction environment.
```### KEEP(env,callback)
This function defined in keep-loader, you don't have to declare manually .
- env : It must match the value of the keep property defined in keep-loader options of webpack. and it must be a string literal, not a variable! because the keep-loader is processing source code at building time, rather than at the time of execution.
- callback: it will be called immediately in place.