简体   繁体   中英

Strip plugin for Rollup does not remove console.log statements

I am running the following configuration for Rollup to build an application written in Svelte. Unfortunately, the console.log statements are preserved when app is built for production. I still see the output from console.log in the browser. I am certain that production flag is set correctly, as Babel works as it should. My rollup.config.js is as follows:

import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import strip from '@rollup/plugin-strip';
import replace from '@rollup/plugin-replace';
import alias from '@rollup/plugin-alias';
import { babel } from '@rollup/plugin-babel';
import livereload from 'rollup-plugin-livereload';
import {terser} from 'rollup-plugin-terser';
import del from 'rollup-plugin-delete';
import sveltePreprocess from 'svelte-preprocess';

import postcss from 'rollup-plugin-postcss';
import filesize from 'rollup-plugin-filesize';

import pkg from './package.json';

const outputdir = pkg.outputdir;
const fulloutputdir = pkg.rootdir + outputdir;
const mergedcssfilepath = "css/svelte_components.css";

const production = !process.env.ROLLUP_WATCH;

function serve() {
    let server;

    function toExit() {
        if (server) server.kill(0);
    }

    return {
        writeBundle() {
            if (server) return;
            server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
                stdio: ['ignore', 'inherit', 'inherit'],
                shell: true
            });

            process.on('SIGTERM', toExit);
            process.on('exit', toExit);
        }
    };
}

export default {
    input: 'src/main.js',
    output: {
        sourcemap: !production,
        format: 'es',
        dir: fulloutputdir,
    },
    plugins: [

        production && del({
            targets: [fulloutputdir+'*']
        }),

        json(),

        alias({
            resolve: ['.svelte', '.js', '.mjs', '.json', '.ts', '.css', '.scss'],
            entries: [
                { find: 'pages', replacement: 'src/svelte/pages/culturama' },
                { find: 'views', replacement: 'src/svelte/pages/culturama/views' },
                { find: 'generic', replacement: 'src/svelte/components/generic' },
                { find: 'localcomponents', replacement: 'src/svelte/components/local' },
                { find: 'scripts', replacement: 'src/js'},
                { find: 'jsons', replacement: 'src/json'},
                { find: 'css', replacement: 'src/css'},
            ]
        }),

        replace({
            values: {
                __uibuildtime__: new Date().toLocaleString('en-GB', { timeZone: 'Europe/Stockholm' }),
                __envtype__: pkg.environment,
                __deptype__: process.env.DEPLOYMENT_TYPE
            }
        }),

        svelte({
            // enable run-time checks when not in production
            compilerOptions: {
                dev: !production,
                hydratable: true
            },
            
            preprocess: [ sveltePreprocess({
                postcss:  true,
            }) ],

        }),

        postcss({ 
            extensions: ['.scss', '.sass', '.css'],
            extract: mergedcssfilepath,
            minimize: production,
            sourceMap: !production
        }),
            
        resolve({
            browser: true,
            dedupe: ['svelte']
        }),

        commonjs(),

        production &&  babel({ babelHelpers: 'bundled' }),

        // In dev mode, call `npm run start` once
        // the bundle has been generated
        !production && serve(),

        // Watch the `public` directory and refresh the
        // browser on changes when not in production
        !production && livereload('public'),

        // strip debugging commands like console.log etc.
        production && strip({
            include: '**/*.(mjs|js|svelte)',
            // defaults to `[ 'console.*', 'assert.*' ]`
            functions: ['console.log', 'assert.*', 'debug', 'alert'],
        }),

        // If we're building for production (npm run build
        // instead of npm run dev), minify
        production && terser(),

        production && filesize()

    ],
    onwarn: (warning) => {  
        if (warning.code !== 'CIRCULAR_DEPENDENCY' && warning.code !== 'PLUGIN_WARNING') {
            console.error(`(!) ${warning.message}`);
        }
    },  
    watch: {
        clearScreen: false
    }
};

Sorry, a little late to this but try replacing production && terser(), with:

        production && terser({ 
            ecma: 2015, // ES6
            mangle: { toplevel: true },
            compress: {
                module: true,
                toplevel: true,
                unsafe_arrows: true,
                drop_console: production,
                drop_debugger: production,
            },
            output: { comments: false } ,
        })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM