Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用unocss打包后报错 #45

Closed
viarotel opened this issue Feb 24, 2023 · 8 comments
Closed

使用unocss打包后报错 #45

viarotel opened this issue Feb 24, 2023 · 8 comments
Labels
help wanted Extra attention is needed

Comments

@viarotel
Copy link

image

@viarotel
Copy link
Author

应该是 "*,:before,:after{--un-rotate:0;................................................"

@lisonge
Copy link
Owner

lisonge commented Feb 24, 2023

image

不好意思,我没有复现,可否把你的最小复现demo上传至github

@lisonge lisonge added the help wanted Extra attention is needed label Feb 24, 2023
@viarotel
Copy link
Author

min-project.7z.gz

@viarotel
Copy link
Author

min-project.7z.gz

解压前去掉".gz"

@lisonge
Copy link
Owner

lisonge commented Feb 24, 2023

useUnocss(), 放到 useMonkey 前面就行

import path from 'node:path'
import { defineConfig } from 'vite'
import useVue from '@vitejs/plugin-vue'
import useMonkey, { cdn, util } from 'vite-plugin-monkey'
import useEslint from 'vite-plugin-eslint'
import useUnocss from 'unocss/vite'
// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src/'),
    },
  },
  plugins: [
    useVue(),
    useUnocss(),
    useMonkey({
      entry: 'src/main.js',
      userscript: {
        name: 'min-project',
        author: 'viarotel',
        namespace: 'viarotel/min-project',
        description: 'min-project',
        version: '0.0.1',
        icon: 'https://vitejs.dev/logo.svg',
        require: [],
      },
      build: {
        externalGlobals: {
          vue: cdn.jsdelivr('Vue', 'dist/vue.global.prod.js').concat(
            await util.fn2dataUrl(() => {
              // @ts-ignore
              // eslint-disable-next-line no-undef
              window.Vue = Vue
            }),
          ),
        },
      },
    }),
    useEslint({ fix: true }),
  ],
})

@viarotel
Copy link
Author

useUnocss(), 放到 useMonkey 前面就行

import path from 'node:path'
import { defineConfig } from 'vite'
import useVue from '@vitejs/plugin-vue'
import useMonkey, { cdn, util } from 'vite-plugin-monkey'
import useEslint from 'vite-plugin-eslint'
import useUnocss from 'unocss/vite'
// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src/'),
    },
  },
  plugins: [
    useVue(),
    useUnocss(),
    useMonkey({
      entry: 'src/main.js',
      userscript: {
        name: 'min-project',
        author: 'viarotel',
        namespace: 'viarotel/min-project',
        description: 'min-project',
        version: '0.0.1',
        icon: 'https://vitejs.dev/logo.svg',
        require: [],
      },
      build: {
        externalGlobals: {
          vue: cdn.jsdelivr('Vue', 'dist/vue.global.prod.js').concat(
            await util.fn2dataUrl(() => {
              // @ts-ignore
              // eslint-disable-next-line no-undef
              window.Vue = Vue
            }),
          ),
        },
      },
    }),
    useEslint({ fix: true }),
  ],
})

好的 按照这个方法确实有效 感谢大佬的指导

@lisonge lisonge closed this as completed Feb 24, 2023
@lisonge
Copy link
Owner

lisonge commented Feb 24, 2023

一些补充

出现错误是因为
https://github.com/unocss/unocss/blob/b1377a98e5b443dbec2c4bda166c9c0afbc04dfb/packages/vite/src/modes/global/build.ts#L267
image

执行了替换操作,替换使用的正则是

https://github.com/unocss/unocss/blob/b1377a98e5b443dbec2c4bda166c9c0afbc04dfb/packages/shared-integration/src/layers.ts#L29
image

就把 js 代码里的 " 给替换掉了

替换之前是

("#--unocss--{layer:__ALL__}#--unocss-layer-start--__ALL__--{start:__ALL__} *,::before

替换之后是

( *,::before

@lisonge
Copy link
Owner

lisonge commented Feb 24, 2023

v2.12.1 在 css 首尾加了两个空格,现在如果还让 unocss 在本插件后面工作时替换的字符串将不会缺失引号

也在文档里加了 和其他插件一起使用时, 顺序尽量放在最后一个的说明

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants