如何让 Javascript Const 与 Grunt Uglify 一起工作?

问题描述

我正在尝试将 javascript Const 与 Grunt Uglify 一起使用,但收到错误:

意外标记:关键字«const»。

我做了一些谷歌搜索并尝试过

npm install terser-webpack-plugin --save-dev

然而那没有用。

据说我需要将以下内容添加到我的插件数组中...

const TerserPlugin = require('terser-webpack-plugin')

new TerserPlugin({
    parallel: true,terserOptions: {
        ecma: 6,},}),

我不确定到底在哪里添加这个?它会出现在我的 grunt file.js 中吗?

此外,我还看到一个解决方法可能是使用 grunt-contrib-uglify 的和谐分支。但我担心这种方法已经过时了?

最终,我正在尝试编写一个 cookie 同意弹出窗口。有谁知道如何让 Const 与 grunt uglify 一起工作?

或者有没有人有更好的编码 cookie 同意弹出窗口的建议?

非常感谢。

编辑:按要求添加 grunt 文件

// Load Grunt
module.exports = function (grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),// Tasks
    sass: { // Begin Sass Plugin (this takes all your scss files and compiles them into style.css)
      dist: {
        files: [{
          expand: true,cwd: 'assets/scss/my-theme',src: ['**/*.scss'],dest: 'dist/css',ext: '.css'
      }]
      }
    },postcss: { // Begin Post CSS Plugin (this applies browser prefixes to your compiled style.css in 'dist' folder)
      options: {
        map: false,processors: [
          require('autoprefixer')({
                overrideBrowserslist: ['last 2 versions']
              })
        ]
      },dist: {
        src: 'dist/css/style.css'
      }
    },cssmin: { // Begin CSS Minify Plugin (this takes your style.css file from 'dist',minifies it and creates style.min.css)
      target: {
        files: [{
          expand: true,cwd: 'dist/css',src: ['style.css','!*.min.css'],ext: '.min.css'
    }]
      }
    },uglify: { // Begin JS Uglify Plugin (this takes your my-theme js files in 'assets',minifies them and creates script.min.js in 'dist' folder)
      build: {
        src: ['assets/js/my-theme/*.js'],dest: 'dist/js/script.min.js'
      }
    },watch: { // Compile everything into one task with Watch Plugin (this watches for any changes to scss and js files,so make sure it is pointing to your 'my-theme' CSS and JS folders in 'assets')
      css: {
        files: 'assets/scss/my-theme/**/*.scss',tasks: ['sass','postcss','cssmin']
      },js: {
        files: 'assets/js/my-theme/**/*.js',tasks: ['uglify']
      }
    }
  });
  // Load Grunt plugins
  grunt.loadNpmTasks('grunt-contrib-sass');
  grunt.loadNpmTasks('grunt-postcss');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-terser');

  // Register Grunt tasks
  grunt.registerTask('default',['watch']);
};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)