情景问题
项目有很多新的 js 语法 es6 es7 等等
如你所愿市面上很多浏览器已经大部分支持这些与语法了
但是 ie 就差强人意了 直接歇菜
如何解决
利用 node 插件完成语法降级
实施情景
npm install --save-dev @babel/core
npm install --save-dev @babel/preset-env
{
"presets": [
[
"@babel/preset-env",
{
"corejs": "2",
"useBuiltIns": "usage" // 按需加载:usage 表示明确使用到的 Polyfill 引用。在一些 ES2015+ 语法不支持的环境下,每个需要用到 Polyfill 的引用时,会自动加上
},
]
]
}
npm install --save-dev @babel/cli
npm install -g browserify
npm install --save-dev babelify @babel/core
npm install core-js@2 --save-dev
npm install uglify-js --save-dev
mkdir -p /usr/local/node
cd /usr/local/node
wget https://nodejs.org/download/release/v14.6.0/node-v14.6.0-linux-x64.tar.gz
tar -zxvf node-v14.6.0-linux-x64.tar.gz
mv node-v14.6.0-linux-x64 node14
PATH=$PATH:/usr/local/node/node14/bin
\cp /root/my-project/src/main/resources/static/js -r ./
find js/business/ -name '*.js' -type f -exec npx browserify {} -t babelify -o lib/{} \;
find lib/js/business -name '*.js' -type f -exec npx uglifyjs {} -m -c -o {}.mini.js \;
\cp -r lib/js/business /root/my-project/src/main/resources/static/js
packson.json
{
"name": "es6-es5",
"version": "1.0.0",
"description": "es6 transform es5",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "npx browserify parent.js -t babelify -o parent.bundle.js"
},
"author": "aming",
"license": "ISC",
"devDependencies": {
"@babel/cli": "^7.17.6",
"@babel/core": "^7.17.8",
"@babel/preset-env": "^7.16.11",
"babelify": "^10.0.0",
"browserify": "^17.0.0",
"core-js": "^2.6.12",
"uglify-js": "^3.15.3"
},
"dependencies": {
"npx": "^10.2.2"
}
}
项目脚本
JS_PATH=src/main/resources/static/js/business/
echo '[starting...] transform path is: '$JS_PATH
echo '[starting install node]'
npm i
npm audit fix
echo '[starting babelify]'
find $JS_PATH -name '*.js' -not -name 'productadd.js' -type f -exec npx browserify {} -t babelify -o {} \;
echo '[starting uglifyjs]'
find $JS_PATH -name '*.js' -not -name 'productadd.js' -type f -exec npx uglifyjs {} -m -c -o {}.mini.js \;
|