錯誤說明:
pdf.js的新版本默認使用了很多TC39還在stage階段的語法滓鸠,并且package.json主入口鎖定為非編譯成es5的版本殊鞭,而在vue-cli的默認配置下缺少相關編譯的loader妄迁,導致編譯報錯蛾茉。
這里建議無論是工程引用還是庫引用垦江,要么引用編譯后的文件嗓化,要么鎖定依賴版本
解決思路有三:
1棠涮、修改工具鏈,增加babel相關語法的plugin刺覆。
2严肪、工程在引用pdfjs-dist的時候直接指向編譯后的產出物,而不是包名谦屑。(import路徑請注意參考:https://github.com/mozilla/pdf.js/issues/13190#issuecomment-815600801)
3驳糯、降低pdfjs-dist版本到無草案版本。
以上1工具鏈修改如下氢橙,2酝枢、3因為是修改代碼就不介紹了。
錯誤現(xiàn)象:
ERROR1:
Module parse failed: Unexpected token (2902:55)
File was processed with these loaders:
* ./node_modules/cache-loader/dist/cjs.js
* ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
| intent: renderingIntent,
| renderInteractiveForms: renderInteractiveForms === true,
> annotationStorage: annotationStorage?.getAll() || null
| });
| }
ERROR2:
Module parse failed: Unexpected token (4323:147)
File was processed with these loaders:
* ./node_modules/cache-loader/dist/cjs.js
* ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
| numPages: this._numPages,
| annotationStorage: (annotationStorage === null || annotationStorage === void 0 ? void 0 : annotationStorage.getAll()) || null,
> filename: ((_this$_fullReader = this._fullReader) === null || _this$_fullReader === void 0 ? void 0 : _this$_fullReader.filename) ?? null
| }).finally(function () {
| if (annotationStorage) {
ERROR3:
class private methods are not enabled.
原因:
vue-cli默認的配置不能編譯草案語法 Optional chaining 悍手、Nullish coalescing operator 帘睦、Private class features
optional chaining (a?.b)
nullish coalescing operator (a ?? b)
解決操作:
-
升級預置
NOTE: This plugin is included in
@babel/preset-env
, in ES2022 -
增加babel編譯插件(先去MDN或rfc找特性關鍵字,然后去babel找對應插件)
插件地址:https://www.babeljs.cn/docs/babel-plugin-proposal-optional-chaining
npm install --save-dev @babel/plugin-proposal-optional-chaining
npm install --save-dev @babel/plugin-proposal-nullish-coalescing-operator
npm install --save-dev @babel/plugin-proposal-private-methods
安裝后在babel配置文件babel.config.js
或.babelrc
增加babel插件引用:
plugins: ['@babel/plugin-proposal-optional-chaining','@babel/plugin-proposal-nullish-coalescing-operator','@babel/plugin-proposal-private-methods']
然后正常執(zhí)行構建就可以了坦康。