Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Generated code is not transpiled by rollup-plugin-babel to ES5 with vue plugin #260

Closed
cloudever opened this issue Oct 3, 2018 · 6 comments

Comments

@cloudever
Copy link

cloudever commented Oct 3, 2018

Expected behavior

Keyword const should be transpiled to var because I've applied .browserslistrc with ie 9 for Babel 7.
It seems to be that code from vue-component-compiler is not transpiled to ES5 partially.

https://github.com/vuejs/vue-component-compiler/blob/afa1cd440123e2e0c195908c1e15935273ac64a9/src/assembler.ts#L304-L307

"rollup-plugin-babel": "^4.0.3",
"rollup-plugin-vue": "^4.3.2",

Actual behavior

I get following code that contains const not var!

	var __vue_staticRenderFns__ = [];
	__vue_render__._withStripped = true;

	  /* style */
	  const __vue_inject_styles__ = undefined;
	  /* scoped */
	  const __vue_scope_id__ = undefined;
	  /* module identifier */
	  const __vue_module_identifier__ = undefined;
	  /* functional template */
	  const __vue_is_functional_template__ = false;
	  /* component normalizer */
	  function __vue_normalize__(
	    template, style, script$$1,
	    scope, functional, moduleIdentifier,
	    createInjector, createInjectorSSR
	  ) {
	    const component = (typeof script$$1 === 'function' ? script$$1.options : script$$1) || {};

Steps to reproduce the behavior

Create babel.config.js

module.exports = (api) => {
  api.cache(api.env('development'));

  const presets = [
    ['@babel/preset-env', {
      modules: false,
      useBuiltIns: false,
      forceAllTransforms: true,
    }],
  ];

  const plugins = [
    '@babel/plugin-proposal-export-default-from',
    '@babel/plugin-proposal-export-namespace-from',
    '@babel/plugin-proposal-class-properties',
    '@babel/plugin-proposal-object-rest-spread',
    ['@babel/plugin-transform-runtime', {
      corejs: 2,
      helpers: true,
      regenerator: false,
    }],
  ];

  return {
    presets,
    plugins,
  };
};

Apply plugins

export default () => [
  vue({
    compileTemplate: true,
    css: false,
  }),
  babel({
    exclude: [/\/core-js\//],
    runtimeHelpers: true,
  }),
  css(),
  replace({
    'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  }),
  resolve(),
  commonjs(),
].filter(Boolean);

And .browserslistrc

> 1%
last 2 versions
ie 9
@cloudever cloudever changed the title Generated code not transpile by rollup-plugin-babel to ES5 Generated code is not transpiled by rollup-plugin-babel to ES5 with vue plugin Oct 3, 2018
@Andarist
Copy link
Member

Andarist commented Oct 3, 2018

From what I remember babel ignores node_modules by default now, so you need to explicitly ask it to transpile them.

@loganfsmyth
Copy link
Collaborator

loganfsmyth commented Oct 3, 2018

Are the files .vue? If so, you need to tell the plugin to process those.

Not related to your question, but FYI api.cache(api.env('development')); makes no sense. What that is saying is, re-evaluate your module.exports config function and every preset/plugin it references, for every single file, when doing a non-development build. That's essentially just making your production builds slower for no reason.

@cloudever
Copy link
Author

Are the files .vue? If so, you need to tell the plugin to process those.

Not related to your question, but FYI api.cache(api.env('development')); makes no sense. What that is saying is, re-evaluate your module.exports config function and every preset/plugin it references, for every single file, when doing a non-development build. That's essentially just making your production builds slower for no reason.

Babel 7.1.2 force me to add api.cache expression

@loganfsmyth
Copy link
Collaborator

Fair. All you need is api.cache(true) though. No need to call api.env('development'). I'll see what I can do to make that clearer.

@ghettovoice
Copy link

ghettovoice commented Oct 11, 2018

@cloudever try to add extensions option to rollup-plugin-babel options:

// plugins 
...
babel({
  runtimeHelpers: true,
  sourceMap: true,
  extensions: ['.js', '.jsx', '.es6', '.es', '.mjs', '.vue'],
}),
...

I had the same issue after upgrade to the latest versions of rollup-plugin-vue, rollup-plugin-babel & etc.
and this options resolved it for me.

@cloudever
Copy link
Author

@ghettovoice Did the trick thank's

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants