-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
从angular1.x到angular2的一点实践(updated:2016-12-12) #1
Comments
(2016-12-12)gulp工具流支持ES5和ES6同时翻译——兼顾新老代码
function webpackWrapper(watch, test, callback) {
var webpackOptions = {
watch: watch,
module: {
// 注释是因为要同时支持ES5和ES6,目前没有研究这个怎么配置格式化文件
// preLoaders: [{ test: /\.js$/, exclude: /node_modules/, loader: 'eslint-loader' }],
loaders: [{ test: /\.js$/, exclude: /node_modules/, loaders: ['babel-loader?presets[]=es2015'] }]
},
output: { filename: 'index.module.js' }
};
if (watch) {
webpackOptions.devtool = 'inline-source-map';
}
var webpackChangeHandler = function(err, stats) {
if (err) {
conf.errorHandler('Webpack')(err);
}
$.util.log(stats.toString({
colors: $.util.colors.supportsColor,
chunks: false,
hash: false,
version: false
}));
browserSync.reload();
if (watch) {
watch = false;
callback();
}
};
var sources = [
path.join(conf.paths.src, '/app/**/*.js'),
path.join('!' + conf.paths.src, '/app/**/lodop_print_template/js_templates/**/*.js'),
path.join('!' + conf.paths.src, '/app/**/*.mock.js'),
path.join('!' + conf.paths.src, '/app/**/*.e2e.js'),
path.join('!' + conf.paths.src, '/app/data/*.js')
];
if (!test) {
sources = sources.concat([
path.join('!' + conf.paths.src, '/app/**/*.spec.js'),
path.join('!' + conf.paths.src, '/app/**/*.spec.es6.js')
]);
}
return gulp.src(sources)
.pipe($.babel({
presets: ['es2015'],
plugins: [
'external-helpers',
]
}))
// 下一步很重要,对angular文件进行排序
.pipe($.angularFilesort()).on('error', conf.errorHandler('AngularFilesort'))
.pipe(webpack(webpackOptions, null, webpackChangeHandler))
.pipe(gulp.dest(path.join(conf.paths.tmp, '/serve/app')));
}
gulp.task('scripts', function() {
return webpackWrapper(false, false);
});
gulp.task('scripts:watch', ['scripts'], function(callback) {
return webpackWrapper(true, false, callback);
});
gulp.task('scripts:test', function() {
return webpackWrapper(false, true);
});
gulp.task('scripts:test-watch', ['scripts'], function(callback) {
return webpackWrapper(true, true, callback);
}); gulp.task('inject', ['scripts', 'styles'], function() {
var injectStyles = gulp.src([
path.join(conf.paths.tmp, '/serve/app/**/*.css'),
path.join(conf.paths.src, '/css/*.css'),
path.join('!' + conf.paths.tmp, '/serve/app/vendor.css')
], { read: false });
var injectScripts = gulp.src([
path.join(conf.paths.tmp, '/serve/app/**/*.module.js')
], { read: false });
var injectOptions = {
ignorePath: [conf.paths.src, path.join(conf.paths.tmp, '/serve')],
addRootSlash: false
};
return gulp.src(path.join(conf.paths.src, '/*.html'))
.pipe($.inject(injectStyles, injectOptions))
.pipe($.inject(injectScripts, injectOptions))
.pipe(wiredep(_.extend({}, conf.wiredep)))
.pipe(gulp.dest(path.join(conf.paths.tmp, '/serve')));
});
|
目前已从原公司离职,本文基本没有续更可能。 |
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
从angular1.x到angular2的一点实践
更新
项目原状
component
项目预期
import
和export
)项目实践(不敢步子迈得太大)
.es6.js
的文件在原目录下翻译成ES5格式的.js
文件,运行或打包时忽略.es6.js
文件;由于babel转义模块化会使用CommonJS,但是对于这一块的打包目前还没有怎么研究,所以暂时使用这种不彻底的方式,下文的代码是我同事所撸,对于gulp我还不是很熟;正在和同事考虑使用browserfy或webpack来把
require
在打包的时候编译掉,这样就可以引入ES6的模块化了,把对于Angular1.x的依赖全部迁移到module文件中去。这个估计需要一段时间,在前面实现的基础上,还要对template的代码进行改造,甚至一些自定义组件也还要花比较多的时间。在目前业务实现还有很多的情况下,只能循序渐进
The text was updated successfully, but these errors were encountered: