汇总,Vue和Buble,scss文件中的意外令牌 [英] Rollup, Vue and Buble, unexpected token in scss file

查看:109
本文介绍了汇总,Vue和Buble,scss文件中的意外令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照Vue官方页面提供的示例,尝试使用Vue和Buble汇总编译SFC.但我不断收到此错误:

I am trying to compile SFC with rollup, using Vue and Buble, following the example suplied in the Vue official page. But I keep getting this error:

src/wrapper.js → dist/chat.min.js...
[!] (plugin buble) SyntaxError: Unexpected token (2:0)
src\components\Chat.vue?vue&type=style&index=0&lang.scss (2:0)
1 :
2 : .chat, .chat>*, .chat * {

这是我的rollup.config.js:

This is my rollup.config.js:

import commonjs from '@rollup/plugin-commonjs'; // Convert CommonJS modules to ES6
import vue from 'rollup-plugin-vue'; // Handle .vue SFC files
import buble from '@rollup/plugin-buble'; // Transpile/polyfill with reasonable browser support
export default {
    input: 'src/wrapper.js', // Path relative to package.json
    output: {
        name: 'Chat',
        exports: 'named',
    },
    plugins: [
        commonjs(),
        vue({
            css: true, // Dynamically inject css as a <style> tag
            compileTemplate: true, // Explicitly convert template to render function
        }),
        buble(), // Transpile to ES5
    ],
};

这是我的wrapper.js:

And this is my wrapper.js:

  // Import vue component
  import component from "./components/Chat.vue";

  // Declare install function executed by Vue.use()
  export function install(Vue) {
    if (install.installed) return;
    install.installed = true;
    Vue.component("chat", component);
  }

  // Create module definition for Vue.use()
  const plugin = {
    install
  };

  // Auto-install when vue is found (eg. in browser via <script> tag)
  let GlobalVue = null;
  if (typeof window !== "undefined") {
    GlobalVue = window.Vue;
  } else if (typeof global !== "undefined") {
    GlobalVue = global.Vue;
  }
  if (GlobalVue) {
    GlobalVue.use(plugin);
  }

  // To allow use as module (npm/webpack/etc.) export component
  export default component;

我尝试从插件中删除气泡,但最终出现错误消息意外令牌(请注意,您需要插件才能导入不是JavaScript的文件)"

I've tried removing buble from the plugins, but I end up with an error saying "Unexpected token (Note that you need plugins to import files that are not JavaScript)"

推荐答案

// rollup.config.js
  import fs from 'fs';
  import path from 'path';
  import VuePlugin from 'rollup-plugin-vue';
  import alias from '@rollup/plugin-alias';
  import commonjs from '@rollup/plugin-commonjs';
  import replace from '@rollup/plugin-replace';
  import babel from 'rollup-plugin-babel';
  import minimist from 'minimist';
  import css from 'rollup-plugin-css-only';

  // Get browserslist config and remove ie from es build targets
  const esbrowserslist = fs.readFileSync('./.browserslistrc')
    .toString()
    .split('\n')
    .filter((entry) => entry && entry.substring(0, 2) !== 'ie');

  const argv = minimist(process.argv.slice(2));

  const projectRoot = path.resolve(__dirname, '..');

  const baseConfig = {
    input: 'src/entry.js',
    plugins: {
      preVue: [
        alias({
          resolve: ['.js', '.vue', '.css'],
          'vue$': require.resolve('vue/dist/vue.esm-bundler.js'),
          entries: {
            '@': path.resolve(projectRoot, 'src')
          }
        }),
      ],
      replace: {
        'process.env.NODE_ENV': JSON.stringify('production'),
        'process.env.ES_BUILD': JSON.stringify('false'),
      },
      vue: {
        css: false,
        template: {
          isProduction: true,
        },
      },
      babel: {
        exclude: 'node_modules/**',
        extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue'],
      }
    },
  };

  const external = [
    'vue',
  ];


  // Customize configs for individual targets
  const buildFormats = [];
  if (!argv.format || argv.format === 'es') {
    const esConfig = {
      ...baseConfig,
      external,
      output: {
        file: 'dist/vue3lib.esm.js',
        format: 'esm',
        exports: 'named',
      },
      plugins: [
        replace({
          ...baseConfig.plugins.replace,
          'process.env.ES_BUILD': JSON.stringify('true'),
        }),
        ...baseConfig.plugins.preVue,
        VuePlugin(baseConfig.plugins.vue),
        babel({
          ...baseConfig.plugins.babel,
          presets: [
            [
              '@babel/preset-env',
              {
                targets: esbrowserslist,
              },
            ],
          ],
        }),
        commonjs(),
        css()
      ],
    };
    buildFormats.push(esConfig);
  }
  // Export config
  export default buildFormats;

希望这会为条目生成esm包

Hope this will generate esm bundle for the entry

这篇关于汇总,Vue和Buble,scss文件中的意外令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆