Skip to content

Commit

Permalink
feat(jsx2mp-loader): support remove unused import and require
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftj18 committed Oct 14, 2024
1 parent 6585ead commit a74ae9a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/jsx2mp-loader/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [0.4.37] UnRelease

- Feat: support remove unused import and require of App、Page、Component、Npm package
- Feat: miniappConfig compat the case where the entry of component use multiple platform like index.wechat.js, index.ali.js. And don't change the filed main that can't be resolved
- Feat: hackRegeneratorRuntimeFunction in ali miniapp of copyNpm
- Fix: support platform-loader
- Fix: script-loader exclude 'plugin://xx/comp' in usingComponents

## [0.4.36] - 2022-06-22

Expand Down
4 changes: 2 additions & 2 deletions packages/jsx2mp-loader/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsx2mp-loader",
"version": "0.4.36",
"version": "0.4.37-7",
"description": "",
"files": [
"src"
Expand All @@ -17,7 +17,7 @@
"@babel/plugin-proposal-export-default-from": "^7.5.2",
"@babel/plugin-transform-typescript": "^7.7.4",
"@babel/preset-env": "^7.5.5",
"babel-plugin-danger-remove-unused-import": "^2.0.0",
"babel-plugin-danger-remove-unused-imports": "^2.1.1",
"babel-plugin-minify-dead-code-elimination-while-loop-fixed": "^0.3.0",
"babel-plugin-transform-define": "^1.3.1",
"chalk": "^2.4.2",
Expand Down
2 changes: 2 additions & 0 deletions packages/jsx2mp-loader/src/script-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { isNpmModule, isJSONFile, isTypescriptFile } = require('./utils/judgeModu
const isMiniappComponent = require('./utils/isMiniappComponent');
const parse = require('./utils/parseRequest');
const { output, transformCode } = require('./output');
const eliminateDeadCode = require('./utils/dce');

const ScriptLoader = __filename;

Expand Down Expand Up @@ -250,6 +251,7 @@ module.exports = function scriptLoader(content) {
content
].join('\n');
} else {
content = eliminateDeadCode(content);
// outputFile(rawContent);
outputFile(content);
}
Expand Down
20 changes: 19 additions & 1 deletion packages/jsx2mp-loader/src/utils/dce.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,29 @@ const parserOpts = {
], // support all plugins
};

function removeDeadCode(source) {
// 按需
// if (!source?.includes('import ')) {
// return source;
// }
return transformSync(source, {
parserOpts,
plugins: [
'babel-plugin-minify-dead-code-elimination-while-loop-fixed'
]
}).code;
}

function removeUnusedImport(source) {
// 按需
// if (!source?.includes('import ')) {
// return source;
// }
return transformSync(source, {
parserOpts,
plugins: [
[
require('babel-plugin-danger-remove-unused-import'),
require('babel-plugin-danger-remove-unused-imports'),
{
ignore: 'rax'
}
Expand All @@ -38,6 +55,7 @@ const codeProcessor = (processors = [], sourceCode) => processors

function eliminateDeadCode(source) {
const processors = [
removeDeadCode,
removeUnusedImport,
];

Expand Down

0 comments on commit a74ae9a

Please sign in to comment.