babel-plugin-minify-dead-code-elimination only useful for entry scripts
- Dominant language
- JavaScript
- Stars
- 4.4k
- Forks
- 217
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the bug**
`babel-plugin-minify-dead-code-elimination` only useful for entry scripts, the dependencies is not working.
**To Reproduce**
`index.js`
```js
import { setTitle } from './utils';
const isWeb = true;
setTitle('test');
if (isWeb) {
console.log(isWeb);
} else {
console.log(!isWeb);
}
```
`utils.js`
```js
const isWeb = true;
export function setTitle(title) {
if (isWeb) {
document.title = title;
} else {
console.log(title);
}
}
```
**Actual Output**
`index.js`, correct
```js
import { setTitle } from './utils';
const isWeb = true;
setTitle('test');
console.log(isWeb);
```
`utils.js`, **uncorrect**
```js
const isWeb = true;
export function setTitle(title) {
if (isWeb) {
document.title = title;
} else {
console.log(title);
}
}
```
**Expected Output**
`utils.js`
```js
const isWeb = true;
export function setTitle(title) {
document.title = title;
}
```
**Configuration**
How are you using babel-minify?
webpack.config.js
```js
const path = require('path');
module.exports = {
mode: 'production',
entry: './src/index/index.js',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'bundle.js'
},
optimization: {
minimize: false
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
options: {
presets: ['env'],
plugins: ['minify-dead-code-elimination']
}
}
]
}
};
```
package.json
```json
{
"name": "test-dce",
"version": "1.0.0",
"description": "test dead code elimination",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack-dev-server --mode development",
"build": "webpack --mode production"
},
"repository": {},
"keywords": [],
"author": "huya.nzb@alibaba-inc.com",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-plugin-minify-dead-code-elimination": "^0.4.3",
"babel-preset-env": "^1.7.0",
"webpack": "^4.14.0",
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^3.1.4"
},
"dependencies": {}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.