完全披露:我在消除 npm 和 node 时打开了一个与此非常相似的问题。您可以在以下位置找到该问题:npm i 和 npm update Breaking gulp, browserify builds。尽管与此问题有些相似,但该问题的根源在构建堆栈中较低,并诊断出一个相关但不相同的问题。请不要将此标记为重复!
你期待会发生什么?
gulp 将 vue.js 组件转换为函数式 JS
究竟发生了什么?
运行gulp contacts
会导致捆绑包不完整,这会导致 Chrome 出现错误:
SyntaxError: Unexpected end of input
在火狐中:
SyntaxError: missing } after property list[Learn More] contacts-bundle.js:1443:5
note: { opened at line 1, column 460
这仅发生在单个组件中(这是项目的新组件),并且仅在const Axios = require('axios');
存在时发生。
注释或删除该行允许构建成功完成。没有其他具有此行的组件受到影响。
使用时也会出现这种情况import Axios from 'Axios';
。
文件构建不正确的第一个主要指标是包的行数。一个正确构建的包通常超过 11k LOC,而不管预编译 LOC 的数量是多少。这个特定的捆绑包大约是 1,100 LOC。
以下是在 devtools 中查看源代码时触发错误的示例:
module.exports = function parseHeaders(headers) {
var parsed = {};
var key;
var val;
var i;
if (!headers) { return parsed; }
utils.forEach(headers.split('\n'), function parser(line) {
i = line.indexOf(':');
key = utils.trim(line.substr(0, i)).toLowerCase();
val = utils.trim(line.substr(i + 1));
if (key) {
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
}
});
return parsed;
};
},{"./../utils":26}] <--- this is where the error is triggered
环境:
Operating System: MacOS High Sierra v.10.13.5
$ node -v
v10.6.0
$ npm -v
6.1.0
$ node -p process.version
v10.6.0
$ node -p process.versions
{ http_parser: '2.8.0',
node: '10.6.0',
v8: '6.7.288.46-node.13',
uv: '1.21.0',
zlib: '1.2.11',
ares: '1.14.0',
modules: '64',
nghttp2: '1.32.0',
napi: '3',
openssl: '1.1.0h',
icu: '62.1',
unicode: '11.0',
cldr: '33.1',
tz: '2018e' }
$ node -p process.platform
darwin
$ node -p process.arch
x64
$ node -p "require('node-sass').info"
node-sass 4.9.1 (Wrapper) [JavaScript]
libsass 3.5.4 (Sass Compiler) [C/C++]
$ npm ls --depth=0
app@ /Users/user/projects/app
├── ajv@5.5.2
├── avoriaz@3.6.0
├── axios@0.16.2
├── babel-core@6.26.3
├── babel-plugin-transform-async-to-generator@6.24.1
├── babel-plugin-transform-async-to-module-method@6.24.1
├── babel-plugin-transform-runtime@6.23.0
├── babel-preset-es2015@6.24.1
├── babelify@7.3.0
├── browserify@14.5.0
├── browserify-hmr@0.3.6
├── c3@0.4.23
├── chai@4.1.2
├── cross-env@1.0.8
├── d3@4.13.0
├── envify@3.4.1
├── eonasdan-bootstrap-datetimepicker@4.17.47
├── eslint@4.19.1
├── eslint-config-airbnb@16.1.0
├── eslint-config-airbnb-base@12.1.0
├── eslint-plugin-html@1.7.0
├── eslint-plugin-import@2.13.0
├── eslint-plugin-jsx-a11y@6.1.0
├── eslint-plugin-react@7.10.0
├── glob@7.1.2
├── gulp@3.9.1
├── gulp-google-fonts-base64-css@1.0.4
├── gulp-rename@1.3.0
├── gulp-rev@8.1.1
├── gulp-sass@3.2.1
├── gulp-sourcemaps@2.6.4
├── http-server@0.9.0
├── karma@1.7.1
├── karma-browserify@5.3.0
├── karma-chai@0.1.0
├── karma-chrome-launcher@2.2.0
├── karma-mocha@1.3.0
├── mocha@3.5.3
├── moment@2.22.2
├── npm-run-all@2.3.0
├── qs@6.5.2
├── sinon@2.4.1
├── uglify-js@2.8.29
├── underscore@1.9.1
├── vinyl-source-stream@1.1.2
├── vinyl-transform@1.0.0
├── vue@2.5.16
├── vue-affix@0.2.4
├── vue-bootstrap-datetimepicker@4.1.4
├── vue-pagination-2@0.3.2
├── vue-spinner@1.0.3
├── vue-strap@2.0.2 (github:wffranco/vue-strap#7becab2b3f6e6b7e281afd777c455e3d83fde927)
├── vue-template-compiler@2.5.16
├── vueify@9.4.1
├── vuejs-datepicker@0.9.29
├── watchify@3.11.0
└── webdriver-manager@12.1.0
gulp --v
[15:17:45] CLI version 2.0.1
[15:17:45] Local version 3.9.1
代码和配置:
包.json
{
"name": "project",
"description": "Some project",
"author": "Someone <noone@email.com>",
"private": true,
"scripts": {
"watchify": "watchify -vd -p browserify-hmr -e js/vue-projects/src/main.js -o js/vue-projects/dist/build.js",
"serve": "http-server -o -s -c 1 -a localhost",
"dev": "npm-run-all --parallel watchify serve",
"buildtest": "cross-env NODE_ENV=production browserify -g envify js/vue-mini-apps/test/main.js | uglifyjs -c warnings=false -m > js/vue-mini-apps/dist/test.js",
"builddev": "cross-env browserify -g envify js/vue-mini-apps/materials/app.js | uglifyjs -c warnings=false -m > js/vue-mini-apps/dist/materials.js",
"build": "cross-env NODE_ENV=production browserify -g envify js/vue-mini-apps/materials/app.js | uglifyjs -c warnings=false -m > js/vue-mini-apps/dist/materials.js"
},
"dependencies": {
"ajv": "^5.5.1",
"axios": "^0.16.2",
"eonasdan-bootstrap-datetimepicker": "^4.17.47",
"glob": "^7.1.2",
"gulp-rename": "^1.2.2",
"moment": "^2.19.4",
"underscore": "^1.8.3",
"v-calendar": "^0.5.5",
"vue": "^2.5.9",
"vue-bootstrap-datetimepicker": "^4.1.3",
"vue-pagination-2": "^0.3.2",
"vue-spinner": "^1.0.3",
"vue-strap": "github:wffranco/vue-strap",
"vue-template-compiler": "^2.5.9",
"vueify": "^9.4.1"
},
"devDependencies": {
"avoriaz": "^3.6.0",
"babel-core": "^6.26.0",
"babel-plugin-transform-async-to-generator": "^6.24.1",
"babel-plugin-transform-async-to-module-method": "^6.24.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babelify": "^7.2.0",
"browserify": "^14.5.0",
"browserify-hmr": "^0.3.6",
"chai": "^4.1.2",
"cross-env": "^1.0.6",
"envify": "^3.4.1",
"eslint": "^4.13.0",
"eslint-config-airbnb": "^16.1.0",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-html": "^1.5.3",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^6.0.2",
"eslint-plugin-react": "^7.5.1",
"gulp": "^3.9.1",
"gulp-google-fonts-base64-css": "^1.0.4",
"gulp-rev": "^8.1.1",
"gulp-sass": "^3.2.0",
"gulp-sourcemaps": "^2.6.4",
"http-server": "^0.9.0",
"karma": "^1.7.1",
"karma-browserify": "^5.2.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.0.0",
"karma-mocha": "^1.2.0",
"mocha": "^3.5.3",
"npm-run-all": "^2.1.2",
"sinon": "^2.3.8",
"uglify-js": "^2.8.29",
"vinyl-source-stream": "^1.1.2",
"vinyl-transform": "^1.0.0",
"watchify": "^3.11.0",
"webdriver-manager": "^12.0.6"
},
"browserify": {
"transform": [
"vueify",
"babelify"
]
},
"browser": {
"vue": "vue/dist/vue.common.js"
}
}
gulpfile.js
var gulp = require('gulp');
var browserify = require('browserify');
var vueify = require('vueify');
var transform = require('vinyl-transform');
var source = require('vinyl-source-stream');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var fs = require("fs")
var babelify = require('babelify');
var glob = require('glob');
var rename = require('gulp-rename');
var rev = require('gulp-rev');
gulp.task('contacts', function() {
//determine all files to be compiled
glob('./js/vue-mini-apps/contacts/**/*.app.js', function(err, files) {
if (err) {
gutil.log(gutil.colors.red('Glob error:'),err);
}
//browerify each file
var tasks = files.map(function(entry) {
return browserify(entry, {debug:true})
.transform(babelify, {presets: ['es2015'], plugins: ["transform-runtime", "transform-async-to-generator"]})
.bundle()
.pipe(source(entry))
//determine file name from file path
.pipe(rename('contacts-bundle.js'))
.pipe(gulp.dest('./js/vue-mini-apps/dist'));
});
});
});
尝试的补救措施:
我试过了:
跑步:
rm -rf node_modules
npm cache clean
npm install
npm update
将 node 和 npm 升级到最新版本。
debug: false
在 browserify 调用中设置
从生产系统和从事同一项目的两个独立同事复制已知的工作 package.json 和 package-lock.json。使用已知的工作包*.json 文件,我运行rm -rf node_modules
, npm cache clear --force
, npm install
, 并收到相同的结果。
根据 gulp 的文档查看了常见问题及其修复页面
创建了一个完全独立的、简单的目录来模仿受影响项目的结构,并且错误仍然存在。
与 npm 团队合作以消除节点或 npm 作为问题的潜在来源——我们将其缩小为 gulp 或 gulp 插件问题。
我还咨询了 Vue.js 支持团队,他们也同意这是 gulp 或 gulp 插件的问题。
这个问题最奇怪的部分是它影响了单个新组件,该组件与 30 多个同样导入或需要 Axios 的其他组件在同一构建任务中。任何帮助将不胜感激!
资料来源:
Gulp Sass with errLogToConsole: true 仍然停止我的其他监视任务
获取:SyntaxError: missing ) 在参数列表之后但无法找出 hulpfile.js 有什么问题
https://github.com/gulpjs/gulp/issues/2065
https://github.com/dlmanning/gulp-sass/wiki/Common-Issues-and-Their-Fixes