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

时间:2019-04-25 14:35:52

标签: webpack minify webpack-4

所以我终于有时间将使用 webpack 1 (1.13.0)的艰巨任务升级到最新的 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.exports}return 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文件?

1 个答案:

答案 0 :(得分:0)

因此在 Webpack 4

mode: "production"

实际上会输出无空格的版本,不久您将设置为

mode: "development"

它会输出正确的经过整理的文件,从那里可以轻松地跟踪错误。