Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让Javascript常量与Grunt Uglify一起工作?_Javascript_Constants_Gruntjs - Fatal编程技术网

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

如何让Javascript常量与Grunt Uglify一起工作?,javascript,constants,gruntjs,Javascript,Constants,Gruntjs,我试图将javascript常量与Grunt Uglify一起使用,但收到错误: 意外标记:关键字«const» 我在谷歌上搜索了一下,并尝试了一下 npm install terser-webpack-plugin --save-dev 然而,这并没有起作用 据说我需要在我的插件阵列中添加以下内容 const TerserPlugin=require('terser-webpack-plugin') 新的TerserPlugin({ 对,, 三种选择:{ ecma:6, }, }), 但我

我试图将javascript常量与Grunt Uglify一起使用,但收到错误:

意外标记:关键字«const»

我在谷歌上搜索了一下,并尝试了一下

npm install terser-webpack-plugin --save-dev
然而,这并没有起作用

据说我需要在我的插件阵列中添加以下内容

const TerserPlugin=require('terser-webpack-plugin')
新的TerserPlugin({
对,,
三种选择:{
ecma:6,
},
}),
但我不确定该在哪里添加这个?它是否在我的grunt file.js中

此外,我还看到了一个修复方法,可能是使用grunt contrib uglify的harmony分支。但我担心这种方法已经过时了

最终,我尝试编写一个cookie同意弹出窗口。有人知道如何让康斯特和格伦特·乌格利菲一起工作吗

还是有人对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'],
          dest: 'dist/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']);
};

你在使用grunt还是webpack?这似乎不适合我的眼睛。或者你正在使用grunt并尝试使用webpack插件,或者反之亦然。请指定你的工具链并发布你的GrunFile(分别是网页包文件)。谢谢你的回复,你让我意识到我在混淆命令。我肯定在用咕噜。由于您的评论,我运行了“npm安装grunt-terser--save dev”,然后将以下内容添加到我的grunt文件“grunt.loadNpmTasks('grunt-terser');”我还编辑了我的原始评论,以包括我的grunt文件。我仍然得到相同的错误,尽管在这个评论中进行了更新。你是使用grunt还是webpack?这似乎不适合我的眼睛。或者你正在使用grunt并尝试使用webpack插件,或者反之亦然。请指定你的工具链并发布你的GrunFile(分别是网页包文件)。谢谢你的回复,你让我意识到我在混淆命令。我肯定在用咕噜。由于您的评论,我运行了“npm安装grunt-terser--save dev”,然后将以下内容添加到我的grunt文件“grunt.loadNpmTasks('grunt-terser');”我还编辑了我的原始评论,以包括我的grunt文件。尽管在这篇评论中进行了更新,我仍然收到了相同的错误。