0.0.3 • Published 2 years ago

@winner-fed/unplugin-vue-setup-extend v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

unplugin-vue-setup-extend

NPM version

Support <script name> for Vue script setup.

Install

node version: >=12.0.0

vite version: >=2.0.0

npm i unplugin-vue-setup-extend -D
# or
yarn add unplugin-vue-setup-extend -D
// vite.config.ts
import vueSetupExtend from '@winner-fed/unplugin-vue-setup-extend/vite'

export default defineConfig({
  plugins: [
    vueSetupExtend({ /* options */ }),
  ],
})

Example: playground/

// rollup.config.js
import vueSetupExtend from '@winner-fed/unplugin-vue-setup-extend/rollup'

export default {
  plugins: [
    vueSetupExtend({ /* options */ }),
  ],
}

// webpack.config.js
module.exports = {
  /* ... */
  plugins: [
    require('@winner-fed/unplugin-vue-setup-extend/webpack').default({ /* options */ })
  ]
}

// nuxt.config.js
export default {
  buildModules: [
    ['@winner-fed/unplugin-vue-setup-extend/nuxt', { /* options */ }],
  ],
}

This module works for both Nuxt 2 and Nuxt Vite

// vue.config.js
module.exports = {
  configureWebpack: {
    plugins: [
      require('@winner-fed/unplugin-vue-setup-extend/webpack').default({ /* options */ }),
    ],
  },
}

Note

script 标签里不存在任何代码时,@vue/compiler-sfc 转换后的 scriptSetupnull,此种情况不支持转换。

 const { parse, compileScript } = require("@vue/compiler-sfc");

const { descriptor } = parse(
`
<template><h1>12312312</h1></template>
<script setup name="App"></script>
`
);

console.log(descriptor);

descriptor 转换后的结果如下:

{
  filename: 'anonymous.vue',
  source: '\n' +
    '  <template><h1>12312312</h1></template><script setup name="App"></script>\n',
  template: {
    type: 'template',
    content: '<h1>12312312</h1>',
    loc: { source: '<h1>12312312</h1>', start: [Object], end: [Object] },
    attrs: {},
    ast: {
      type: 1,
      ns: 0,
      tag: 'template',
      tagType: 0,
      props: [],
      isSelfClosing: false,
      children: [Array],
      loc: [Object],
      codegenNode: undefined
    },
    map: {
      version: 3,
      sources: [Array],
      names: [],
      mappings: 'AACA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC',
      file: 'anonymous.vue',
      sourceRoot: '',
      sourcesContent: [Array]
    }
  },
  script: null,
  scriptSetup: null,
  styles: [],
  customBlocks: [],
  cssVars: [],
  slotted: false,
  shouldForceReload: [Function: shouldForceReload]
}