【问题标题】:Strip plugin for Rollup does not remove console.log statementsRollup 的 Strip 插件不会删除 console.log 语句
【发布时间】:2021-10-25 03:13:02
【问题描述】:

我正在为 Rollup 运行以下配置,以构建用 Svelte 编写的应用程序。不幸的是,console.log 语句在为生产构建应用程序时被保留。我仍然在浏览器中看到来自 console.log 的输出。我确信生产标志设置正确,因为 Babel 可以正常工作。我的 rollup.config.js 如下:

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
    }
};

【问题讨论】:

    标签: javascript svelte strip rollup


    【解决方案1】:

    抱歉,这有点晚了,但请尝试将 production && terser(), 替换为:

            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 } ,
            })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多