我的gulp配置文件基础是这样的。
const gulp = require('gulp'),
babel = require('gulp-babel'),
ugify = require('gulp-uglify')
gulp.task('watch', function () {
gulp.watch('app.js', ['a'])
})
gulp.task('a', () =>
gulp.src('app.js')
.pipe(babel({
presets: ['es2015']
}))
.pipe(ugify())
.pipe(gulp.dest('dist'))
)
然后我的app.js中用了es6的module语法
import { bar } from './index.js'
var foo = () => {
console.log(bar)
}
foo()
index.js如下
export const bar = 'aaaaaaaaaaaa'
打包后的js如下,但是这样并不能在浏览器端运行。
"use strict";var _index=require("./index.js"),foo=function(){console.log(_index.bar)};foo();
所以,应该怎么修改呢。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
babel只能转化,不能打包,你还需要使用browserify这个插件
gulp不是模块化开发工具,不能把你需要的资源打包。
import的支持需要增加plugin。 https://babeljs.io/docs/plugins/
你应该想实现的是app.js 和index.js 打入一个包中吧?
gulp.src(['app.js','index.js'])