我正在尝试将 Jotted javascript 库与 gulp-Angular 项目结合使用。该库本身工作正常,但我试图包含该库的 Codemirror 插件,但遇到了问题。当我尝试添加库并运行 gulpserve
时,出现以下错误:
解析错误:“js/codemirror.js”,第 4 行:意外的保留字
导致问题的行如下所示:
从'../util.js'导入*作为util
我见过一些人引用使用 babel 来解决此类问题,但解决方案似乎有点超出了我的技能水平。
这是该插件的链接:
https://github.com/ghinda/jotted
我当前的server.js
'use strict';
var path = require('path');
var gulp = require('gulp');
var conf = require('./conf');
var babel = require('gulp-babel');
var browserSync = require('browser-sync');
var browserSyncSpa = require('browser-sync-spa');
var util = require('util');
var proxyMiddleware = require('http-proxy-middleware');
function browserSyncInit(baseDir, browser) {
browser = browser === undefined ? 'default' : browser;
var routes = null;
if (baseDir === conf.paths.src || (util.isArray(baseDir) && baseDir.indexOf(conf.paths.src) !== -1)) {
routes = {
'/bower_components': 'bower_components'
};
}
var server = {
baseDir: baseDir,
routes : routes
};
/*
* You can add a proxy to your backend by uncommenting the line below.
* You just have to configure a context which will we redirected and the target url.
* Example: $http.get('/users') requests will be automatically proxified.
*
* For more details and option, https://github.com/chimurai/http-proxy-middleware/blob/v0.9.0/README.md
*/
// server.middleware = proxyMiddleware('/users', {target: 'http://jsonplaceholder.typicode.com', changeOrigin: true});
browserSync.instance = browserSync.init({
startPath: '/',
server : server,
browser: browser,
host: '192.168.0.20',
https: false,
port : parseInt(process.env.GULP_PORT) || 8684
});
}
browserSync.use(browserSyncSpa({
selector: '[ng-app]'// Only needed for angular apps
}));
gulp.task('serve', ['watch'], function () {
browserSyncInit([path.join(conf.paths.tmp, '/serve'), conf.paths.src]);
});
gulp.task('serve:dist', ['build'], function () {
browserSyncInit(conf.paths.dist);
});
gulp.task('serve:e2e', ['inject'], function () {
browserSyncInit([conf.paths.tmp + '/serve', conf.paths.src], []);
});
gulp.task('serve:e2e-dist', ['build'], function () {
browserSyncInit(conf.paths.dist, []);
});
var gulp = require('gulp');
var webserver = require('gulp-webserver');
gulp.task('webserver', function() {
gulp.src('src')
.pipe(webserver({
host: '0.0.0.0',
livereload: true,
directoryListing: true,
open: true
}));
});
我当前的 gulpfile.babel.js
/**
* Welcome to your gulpfile!
* The gulp tasks are splitted in several files in the gulp directory
* because putting all here was really too long
*/
'use strict';
var gulp = require('gulp');
var wrench = require('wrench');
var babel = require('gulp-babel');
/**
* This will load all js or coffee files in the gulp directory
* in order to load all gulp tasks
*/
wrench.readdirSyncRecursive('./gulp').filter(function (file) {
return (/\.(js|coffee)$/i).test(file);
}).map(function (file) {
require('./gulp/' + file);
});
/**
* Default task clean temporaries directories and launch the
* main optimization build task
*/
gulp.task('default', ['clean'], function () {
gulp.start('build');
});
堆栈跟踪
...standard build procedure...
[15:12:01] all files 224.05 kB
[15:12:01] Finished 'scripts' after 2.64 s
[15:12:01] Starting 'inject'...
[15:12:01] gulp-inject 10 files into index.html.
[15:12:02] [AngularFilesort] Error in plugin 'gulp-angular-filesort'
Message:
Error in parsing: "js/codemirror.js", Line 4: Unexpected reserved word
[15:12:02] gulp-inject Nothing to inject into index.html.
[15:12:02] Finished 'inject' after 337 ms
[15:12:02] Starting 'watch'...
[15:12:02] Finished 'watch' after 93 ms
[15:12:02] Starting 'serve'...
[15:12:02] Finished 'serve' after 24 ms
...standard serve procedure...
最佳答案
我的猜测是,您在不支持模块的 ES5 环境中使用 import
语句运行代码。
如果您像我怀疑的那样在 Node 中运行它,则可以使用 required
代替。
关于javascript - 包括 Jotted 的 Codemirror 插件和 Angular gulp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41335693/