gpt4 book ai didi

node.js - React + Gulp + Babelify

转载 作者:太空宇宙 更新时间:2023-11-03 23:32:55 25 4
gpt4 key购买 nike

我正在尝试将我的工作应用程序从 React 和 Gulp with ES5 转换为 React + Gulp 和 ES6。

我只尝试用 babelify 替换 Reactify,但仍然收到 gulp 错误:非法导入声明

这是我的 gulpfile.js:

"use strict";

var gulp = require('gulp');
var connect = require('gulp-connect'); //Runs a local dev server
var open = require('gulp-open'); //Open a URL in a web browser
var browserify = require('browserify'); // Bundles JS
var source = require('vinyl-source-stream'); // Use conventional text streams with Gulp
var concat = require('gulp-concat'); //Concatenates files
var lint = require('gulp-eslint'); //Lint JS files, including JSX
var babelify = require("babelify");

var config = {
port: 9005,
devBaseUrl: 'http://localhost',
paths: {
html: './src/*.html',
js: './src/**/*.js',
css: [
'node_modules/bootstrap/dist/css/bootstrap.min.css',
'node_modules/bootstrap/dist/css/bootstrap-theme.min.css',
'node_modules/toastr/toastr.css'
],
dist: './dist',
mainJs: './src/main.js'
}
}

//Start a local development server
gulp.task('connect', function() {
connect.server({
root: ['dist'],
port: config.port,
base: config.devBaseUrl,
livereload: true
});
});

gulp.task('open', ['connect'], function() {
gulp.src('dist/index.html')
.pipe(open({ uri: config.devBaseUrl + ':' + config.port + '/'}));
});

gulp.task('html', function() {
gulp.src(config.paths.html)
.pipe(gulp.dest(config.paths.dist))
.pipe(connect.reload());
});

gulp.task('js', function() {
browserify(config.paths.mainJs)
.transform(babelify)
.bundle()
.on('error', console.error.bind(console))
.pipe(source('bundle.js'))
.pipe(gulp.dest(config.paths.dist + '/scripts'))
.pipe(connect.reload());
});

gulp.task('css', function() {
gulp.src(config.paths.css)
.pipe(concat('bundle.css'))
.pipe(gulp.dest(config.paths.dist + '/css'));
});

gulp.task('lint', function() {
return gulp.src(config.paths.js)
.pipe(lint({config: 'eslint.config.json'}))
.pipe(lint.format());
});

gulp.task('watch', function() {
gulp.watch(config.paths.html, ['html']);
gulp.watch(config.paths.js, ['js', 'lint']);
});

gulp.task('default', ['html', 'js', 'css', 'lint', 'open', 'watch']);

这是我的 js 文件(main.js):

"use strict";

var React = require('react');
var Router = require('react-router');
var routes = require('./routes');
import React from 'react';

Router.run(routes, function(Handler) {
React.render(<Handler/>, document.getElementById('app'));
});

非常感谢

最佳答案

您需要安装babel预设npm 我 babel-preset-es2015 --save-dev

并将配置添加到 .babelrc 像这样

{
"presets": [
"es2015"
]
}

或者最好使用gulp-babel

$ npm install --save-dev gulp-babel babel-preset-es2015

const gulp = require('gulp');
const babel = require('gulp-babel');

gulp.task('default', () => {
return gulp.src('src/app.js')
.pipe(babel({
presets: ['es2015']
}))
.pipe(gulp.dest('dist'));
});

https://www.npmjs.com/package/gulp-babel

关于node.js - React + Gulp + Babelify,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36042311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com