从Webpack 1升级到4 ...缩小问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从Webpack 1升级到4 ...缩小问题相关的知识,希望对你有一定的参考价值。

所以我终于有时间将使用webpack 1(1.13.0)的grunt任务升级到最新的webpack 4(在这个答案的时候:4.30.1)。

在装载机上出现大量错误之后,让所有工作完成,除了引发错误的缩小:(

我注意到Webpack 1输出.js文件为

var MyLibrary =
/******/ (function(modules)  // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = ;
/******/
/******/    // The require function
/******/    function __webpack_require__(moduleId) 
/******/
/******/        // Check if module is in cache
/******/        if(installedModules[moduleId])
/******/            return installedModules[moduleId].exports;
...

但是使用Webpack 4输出是“压缩的”

var MyLibrary=function(e)var n=;function t(a)if(n[a])return n[a].exports;var o=n[a]=i:a,l:!1,exports:;return e[a].call(o.exports,o,o.exports,t),o.l=!0,o.exportsreturn t.m=e,t.c=n,t.d=function(e,n,a)t.o(e,n)||Object.defineProperty(e,n,enumerable:!0,get:a),t.r=function(e)...

并使用与Webpack 1完全相同的源代码

> grunt run --client=mylibrary

Running "run" task
↬ Using production configuration

Running "webpack:default" (webpack) task
Version: webpack 1.13.0
           Asset    Size  Chunks             Chunk Names
    mylibrary.js  450 kB       0  [emitted]  main
mylibrary.js.map  545 kB       0  [emitted]  main

Running "replace:default" (replace) task
>> 2 replacements in 1 file.

Running "uglify:default" (uglify) task
File dist/mylibrary/mylibrary.min.js created: 450.09 kB → 177.94 kB
>> 1 file created.

Running "compress:default" (compress) task
>> Compressed 1 file

Done.

使用Webpack 4我在尝试缩小文件时收到错误

> grunt run --client=mylibrary

Running "run" task
↬ Using production configuration

Running "webpack" task
   56 modules

Running "replace:default" (replace) task
>> 2 replacements in 1 file.

Running "uglify:default" (uglify) task
 SyntaxError: Unexpected token: name «a», expected: punc «;»
    at JS_Parse_Error.get (<anonymous>:71:23)
    at formatError (internal/util/inspect.js:817:16)
    at formatRaw (internal/util/inspect.js:616:14)
    at formatValue (internal/util/inspect.js:511:10)
    at inspect (internal/util/inspect.js:191:10)
    at Object.formatWithOptions (util.js:84:12)
    at Console.(anonymous function) (console.js:191:15)
    at Console.log (console.js:202:31)
    at E:\mylibrary\repositories\bitbucket\sc-mylibrary\node_modules\grunt-contrib-uglify\tasks\uglify.js:144:17
    at Array.forEach (<anonymous>:null:null)
    at Object.<anonymous> (E:\mylibrary\repositories\bitbucket\sc-mylibrary\node_modules\grunt-contrib-uglify\tasks\uglify.js:78:16)
    at Object.<anonymous> (E:\mylibrary\repositories\bitbucket\sc-mylibrary\node_modules\grunt\lib\grunt\task.js:252:15)
    at Object.thisTask.fn (E:\mylibrary\repositories\bitbucket\sc-mylibrary\node_modules\grunt\lib\grunt\task.js:70:16)
    at Object.<anonymous> (E:\mylibrary\repositories\bitbucket\sc-mylibrary\node_modules\grunt\lib\util\task.js:294:30)
    at Task.runTaskFn (E:\mylibrary\repositories\bitbucket\sc-mylibrary\node_modules\grunt\lib\util\task.js:244:24)
    at Task.<anonymous> (E:\mylibrary\repositories\bitbucket\sc-mylibrary\node_modules\grunt\lib\util\task.js:293:12)
    at E:\mylibrary\repositories\bitbucket\sc-mylibrary\node_modules\grunt\lib\util\task.js:220:11
    at process._tickCallback (internal/process/next_tick.js:61:11)

  message: 'Unexpected token: name «a», expected: punc «;»',
  filename: 'mylibrary.js',
  line: 9,
  col: 14469,
  pos: 60510 
>> Uglifying source dist/mylibrary/mylibrary.js failed.
Warning: Uglification failed.
Unexpected token: name «a», expected: punc «;». 
Line 9 in dist/mylibrary/mylibrary.js
 Use --force to continue.

Aborted due to warnings.

在两个版本中,grunt-contrib-uglify插件的设置方式相同

grunt.initConfig(
    uglify: 
        options: 
            sourceMap: false
        ,
        default: 
            files: 
                'dist/mylibrary/mylibrary.min.js': ['dist/mylibrary/mylibrary.js']
            
        
    ,
    ...
);

插件中唯一的变化是现在它是“4.0.1”而之前我使用的是“1.0.2”......但即使改回来,它也会抛出完全相同的错误...所以我认为它是原始文件。 ..

有没有办法像以前一样输出.js文件?

答案

所以在Webpack 4中

mode: "production"

实际上输出一个无空白版本,很快就会设置为

mode: "development"

它输出正确的美化文件,并从那里很容易跟进错误。

以上是关于从Webpack 1升级到4 ...缩小问题的主要内容,如果未能解决你的问题,请参考以下文章

webpack 4升级到 webpack 5 (node 14.6 升级到 node16 引发的问题)

webpack升级指南

无痛升级到 Webpack 4 指南

尝试从 Babel 6 升级到 Babel 7 时,webpack 模块构建失败

babel 7.x 和 webpack 4.x 配置vue项目

webpack 3.1 升级webpack 4.0