Frontend

Babel Ast Demo

Babel AST Demo #

Review

  1. 2023/08/25
const fs = require('fs');
const path = require('path');
const babel = require("@babel/core");
const { parse } = require('@babel/parser');
const traverse = require('@babel/traverse').default;

const srcFilePath = './src/main.js';

// Store source codes
const sourceCodes = {};

// Function to recursively process dependencies
function processDependency(filePath) {
  if (!sourceCodes[filePath]) {
    const sourceCode = fs.readFileSync(filePath, 'utf-8');
    sourceCodes[filePath] = sourceCode;

    // Parse the source code to extract import statements
    const ast = parse(sourceCode, {
      sourceType: 'module',
      plugins: ['jsx', 'typescript'], // Add plugins as needed
    });

    // Traverse the AST to find dependencies
    traverse(ast, {
      ImportDeclaration: ({ node }) => {
        const dependencyPath = node.source.value;
        const absoluteDependencyPath = path.resolve(path.dirname(filePath), dependencyPath);
        processDependency(absoluteDependencyPath);

        // Remove import declarations
        node.specifiers.forEach(specifier => {
          if (t.isImportSpecifier(specifier)) {
            const name = specifier.imported.name;
            const source = t.stringLiteral(dependencyPath);
            const importDeclaration = t.importDeclaration([specifier], source);
            const importIndex = ast.program.body.indexOf(node);
            ast.program.body.splice(importIndex, 1, importDeclaration);
          }
        });
      },
    });
  }
}

// Start processing the main file and its dependencies
processDependency(path.resolve(srcFilePath));

// Output the source codes
Object.entries(sourceCodes).forEach(([filePath, sourceCode]) => {
  console.log(`Source code for file: ${filePath}`);
  console.log(sourceCode);

  const { code } = babel.transform(sourceCode, {})
  console.log('\n--------------------------------------------\n');
  console.log(code);

});


// const data = require('./dist/bundle');
// babel.transformSync(data, { s });

Update npm Version Demo

2019/12/23

#!/bin/bash

dir=$1

cd $dir;
git checkout release/aaa
git pull
branch=`git rev-parse --abbrev-ref HEAD`
echo "\ncurrent branch is: $branch\n"

if [[ $branch != 'release/aaa' ]]; then
    echo "Your branch is not exist."
    exit 1;
fi

# update version
echo '---------------update version-----------------'
cVersion=`awk -F'"' '/"version":/{print $4}' package.json`
cPatch=`echo "$cVersion" | grep -E -o '[[:digit:]]+$'`
nPatch=$(($cPatch+1))
nVersion=`echo "$cVersion" | sed -E "s/[[:digit:]]+$/$nPatch/"`

echo "change version $cPatch to $nPatch"
echo "current version is: : $cVersion"
echo "next version is: $nVersion"
echo ''


# replacde package.json package-lock.json
echo '----------replace package version-------------'
sed -i '' "/\"version\"/s/$cVersion/$nVersion/" package.json
sed -i '' "1,/\"version\"/s/$cVersion/$nVersion/" package-lock.json

# diff
echo '************git diff code************'
git diff | cat
echo '\n'

# add, commit, push
echo "git add -> commit -> push code."
git add package.json package-lock.json
git commit -m 'chore: update version'
git push

echo "done."