babel / minify

:scissors: An ES6+ aware minifier based on the Babel toolchain (beta)
https://babeljs.io/repl
MIT License
4.4k stars 225 forks source link

babel-plugin-transform-remove-console is not working in docker container #988

Closed smoothdvd closed 4 years ago

smoothdvd commented 4 years ago

Describe the bug

babel-plugin-transform-remove-console is not working in docker container

To Reproduce

Minimal code to reproduce the bug

console.log('foo');

Actual Output

ERROR Failed to compile with 5 errors1:54:17 AM

error in ./src/components/Article.vue

Module Error (from ./node_modules/eslint-loader/index.js):

/app/src/components/Article.vue 40:7 warning 'v-html' directive can lead to XSS attack vue/no-v-html 246:7 error Unexpected console statement no-console

✖ 2 problems (1 error, 1 warning)

If there is no Error thrown,

Expected Output

console.log() method should be removed when build with production env

Stack Trace

If applicable,

Configuration

How are you using babel-minify?

"@vue/cli-plugin-babel": "~4.4.0", "babel-eslint": "^10.1.0", "babel-plugin-transform-remove-console": "^6.9.4", "eslint": "^6.7.2",

babel.config.js :

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset',
  ],
  plugins: [
    ['import', {
      libraryName: 'vant',
      libraryDirectory: 'es',
      // style: true,
      style: (name) => `${name}/style/less`,
    }, 'vant'],
  ],
  env: {
    production: {
      plugins: ['transform-remove-console'],
    },
  },
};

.eslintrc.js:

module.exports = {
  root: true,
  env: {
    node: true,
  },
  extends: [
    'plugin:vue/recommended',
    '@vue/airbnb',
  ],
  parserOptions: {
    parser: 'babel-eslint',
  },
  rules: {
    'func-names': ["error", "as-needed"],
    'key-spacing': ['error', { 'mode': 'minimum' }],
    'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
  },
  overrides: [
    {
      files: [
        '**/__tests__/*.{j,t}s?(x)',
        '**/tests/unit/**/*.spec.{j,t}s?(x)',
      ],
      env: {
        jest: true,
      },
    },
  ],
};

Dockerfile:

# Dev stage
FROM node:10-alpine as dev

WORKDIR /app
RUN npm config set registry=https://registry.npm.taobao.org/

# Build stage
FROM dev as build

COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile --ignore-scripts
COPY . .
RUN yarn build

# Release stage
FROM nginx:stable-alpine as release

COPY --from=build /app/dist/ /usr/share/nginx/html/
COPY ./nginx/ /etc/nginx/

Possible solution

Additional context

clarle commented 4 years ago

I think babel-eslint is triggering before babel-plugin-transform-remove-console and you have the no-console rule as seen in your output.

That's throwing the error that you see which is why it might not be removed. I'm going to close this issue for now, but feel free to reopen it if have any reproducible Docker container that I can test out with what you mentioned.