FEvivi 2020-06-16
|-js |-src |-module1.js |-module2.js |-module3.js |-main.js |-index.html |-package.json
第一步,全局安装:npm install babel-cli browserify -g
第二步,局部安装:npm install babel-preset-es2015
备注:若全局已经安装过browserify,可以在第一步中去除browserify
{ "presets": ["es2015"] }
js/src/module1.js
//分别暴露 export function foo() { console.log(‘module1 foo()‘); } export function bar() { console.log(‘module1 bar()‘); } export const DATA_ARR = [1, 3, 5, 1]
js/src/module2.js
//统一暴露 let data = ‘module2 data‘ function fun1() { console.log(‘module2 fun1() ‘ + data); } function fun2() { console.log(‘module2 fun2() ‘ + data); } export {fun1, fun2}
js/src/module3.js
//默认暴露 export default { name: ‘Tom‘, setName: function (name) { this.name = name } }
下载jQuery模块: npm install jquery --save
js/src/main.js
import {foo, bar} from ‘./module1‘ import {DATA_ARR} from ‘./module1‘ import {fun1, fun2} from ‘./module2‘ import person from ‘./module3‘ import $ from ‘jquery‘ $(‘body‘).css(‘background‘, ‘red‘) foo() bar() console.log(DATA_ARR); fun1() fun2() person.setName(‘JACK‘) console.log(person.name);
babel js/src -d js/build
browserify js/build/main.js -o js/build/build.js
<script type="text/javascript" src="js/build/build.js"></script>