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

Webpack / Vue CLI: defineOptions is not defined (production mode) #23

Closed
ShaneLee-9 opened this issue May 19, 2022 · 32 comments
Closed

Comments

@ShaneLee-9
Copy link

image

image

dependencies:
"vue": "^3.2.13",
"unplugin-vue-define-options": "^0.6.1",
”webpack": "5.70.0"

vue.config.js config:
image

@sxzz
Copy link
Member

sxzz commented May 20, 2022

Please provide a reproduction repo.

@ShaneLee-9
Copy link
Author

This is the project repo: https://github.com/ShaneLee-9/vue-define-options-demo
This is the production: https://shanelee-9.github.io/vue-define-options-demo/

@sxzz sxzz added bug Something isn't working upstream labels May 21, 2022
@wangshuai0
Copy link

同样遇到,解决了吗

@sxzz
Copy link
Member

sxzz commented May 30, 2022

用于webpack下暂时有点问题

@giserman001
Copy link

用于webpack下暂时有点问题

如何在typescript下定义d.ts声明

@sxzz
Copy link
Member

sxzz commented Jun 2, 2022

@ShaneLee-9 @wangshuai0 似乎是与 babel 一起用的时候会出问题,尝试去掉 babel 试试

@sxzz
Copy link
Member

sxzz commented Jun 2, 2022

@giserman001 #13 (comment)

@HOLYYUN
Copy link

HOLYYUN commented Jun 23, 2022

有合适的解决方案么?我在dev环境没有问题,build完在生产环境出现了这个报错.

@sxzz
Copy link
Member

sxzz commented Jun 23, 2022

暂无解决方法,等上游修复

@HOLYYUN
Copy link

HOLYYUN commented Jun 24, 2022

暂无解决方法,等上游修复
Copy that!

@loosheng
Copy link

You can set parallel: false in vue.config.js to disable thread-loader.

Related:
vuejs/vue-cli#2785 (comment)
webpack-contrib/thread-loader#27

@sxzz sxzz added has workaround and removed bug Something isn't working labels Jun 29, 2022
@sxzz sxzz changed the title error in production:defineOptions is not defined Webpack / Vue CLI: defineOptions is not defined (production mode) Jul 4, 2022
@sxzz sxzz pinned this issue Jul 4, 2022
@funyaliga
Copy link

我用vite也是生产环境not defined,但dev环境没问题

@zjluse
Copy link

zjluse commented Jul 26, 2022

发现把node_modules删掉,重新install,马上再build就不会有问题, 但是偶尔关机或者重启编辑器可能就会再次报错!

@sxzz
Copy link
Member

sxzz commented Jul 26, 2022

尝试上面提到的方法。
#23 (comment)

@ckvv
Copy link

ckvv commented Aug 12, 2022

"0.8.0" 在 vite中存在相同问题, 回退到"0.7.3"版本正常,

@sxzz
Copy link
Member

sxzz commented Aug 12, 2022

@ckvv 请提供最小复现。
https://antfu.me/posts/why-reproductions-are-required-zh

@ckvv
Copy link

ckvv commented Aug 12, 2022

@sxzz 似乎类型文件地址错了https://unpkg.com/browse/unplugin-vue-define-options@0.8.2/package.json
"types": "index.d.ts", 应该是 "types": "dist/index.d.ts",

https://github.com/ckvv/vite-project-unplugin-vue-macros

  • run pnpm build
[vite:dts] Start generate declaration files...
src/components/button.vue.ts:7:1 - error TS2304: Cannot find name 'defineOptions'.

7 defineOptions({
  ~~~~~~~~~~~~~

[vite:dts] Declaration files built in 1721ms.

@sxzz
Copy link
Member

sxzz commented Aug 12, 2022

@ckvv 没问题的,因为使用了别名

  "typesVersions": {
    "*": {
      "*": [
        "./dist/*",
        "./*"
      ]
    }
  },

@iEuJen
Copy link
Contributor

iEuJen commented Sep 9, 2022

"0.8.0" 在 vite中存在相同问题, 回退到"0.7.3"版本正常,

是的,我用最新版[0.11.2]也出现报错的情况,在回退到0.7.3之后便正常了。

vscode报错信息

any
找不到名称“defineOptions”。ts(2304)
没有可用的快速修复

然后我发现:
0.7.3版本的 unplugin-vue-define-options/index.d.ts 是这样的:

import * as unplugin from 'unplugin';
export { transform } from './core/transform.js';
import { defineComponent } from 'vue';
import { FilterPattern } from '@rollup/pluginutils';
import '@vue/compiler-sfc';

declare global {
    const defineOptions: typeof defineComponent;
}
interface Options {
    include?: FilterPattern;
    exclude?: FilterPattern | undefined;
}
declare type OptionsResolved = Required<Options>;
declare const _default: unplugin.UnpluginInstance<Options>;

export { Options, OptionsResolved, _default as default };

而0.11.2版本的 unplugin-vue-define-options/index.d.ts 是这样的:

import * as unplugin from 'unplugin';
export { transform } from './core/transform.js';
import { FilterPattern } from '@rollup/pluginutils';

interface Options {
    include?: FilterPattern;
    exclude?: FilterPattern;
}
declare type OptionsResolved = Omit<Required<Options>, 'exclude'> & {
    exclude?: FilterPattern;
};
declare const _default: unplugin.UnpluginInstance<Options | undefined>;

export { Options, OptionsResolved, _default as default };

似乎是类型文件缺少了定义const defineOptions: typeof defineComponent;


下面是我的项目依赖:

{
 "dependencies": {
    "@dcloudio/uni-app": "3.0.0-alpha-3050320220727002",
    "@dcloudio/uni-app-plus": "3.0.0-alpha-3050320220727002",
    "@dcloudio/uni-components": "3.0.0-alpha-3050320220727002",
    "@dcloudio/uni-h5": "3.0.0-alpha-3050320220727002",
    "@dcloudio/uni-mp-alipay": "3.0.0-alpha-3050320220727002",
    "@dcloudio/uni-mp-baidu": "3.0.0-alpha-3050320220727002",
    "@dcloudio/uni-mp-kuaishou": "3.0.0-alpha-3050320220727002",
    "@dcloudio/uni-mp-lark": "3.0.0-alpha-3050320220727002",
    "@dcloudio/uni-mp-qq": "3.0.0-alpha-3050320220727002",
    "@dcloudio/uni-mp-toutiao": "3.0.0-alpha-3050320220727002",
    "@dcloudio/uni-mp-weixin": "3.0.0-alpha-3050320220727002",
    "@dcloudio/uni-quickapp-webview": "3.0.0-alpha-3050320220727002",
    "pinia": "^2.0.20",
    "vue": "^3.2.37",
    "vue-i18n": "^9.1.9",
    "vuex": "^4.0.2"
  },
  "devDependencies": {
    "@dcloudio/types": "^3.0.7",
    "@dcloudio/uni-automator": "3.0.0-alpha-3050320220727002",
    "@dcloudio/uni-cli-shared": "3.0.0-alpha-3050320220727002",
    "@dcloudio/uni-stacktracey": "3.0.0-alpha-3050320220727002",
    "@dcloudio/vite-plugin-uni": "3.0.0-alpha-3050320220727002",
    "@typescript-eslint/eslint-plugin": "^4.18.0",
    "@typescript-eslint/parser": "^4.18.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/eslint-config-standard": "^5.1.2",
    "@vue/eslint-config-typescript": "^7.0.0",
    "eslint": "^6.7.2",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.0",
    "eslint-plugin-vue": "^7.0.0",
    "postcss-html": "^1.3.0",
    "postcss-scss": "^4.0.3",
    "sass": "^1.54.5",
    "stylelint": "^14.5.3",
    "stylelint-config-html": "^1.0.0",
    "stylelint-config-prettier": "^9.0.3",
    "stylelint-config-standard": "^25.0.0",
    "stylelint-order": "^5.0.0",
    "stylelint-scss": "^4.1.0",
    "typescript": "^4.7.4",
    "unplugin-vue-define-options": "^0.11.2",
    "vite": "^2.9.14"
  }
}

@sxzz
Copy link
Member

sxzz commented Sep 9, 2022

最新版需要手动配置

// tsconfig.json
{
  "compilerOptions": {
    // ...
    "types": ["unplugin-vue-define-options/macros-global" /* ... */]
  }
}

@qinyang912
Copy link

qinyang912 commented Oct 20, 2022

I have a minimal webpack config:

const VueLoaderPlugin = require('vue-loader/lib/plugin');
const defineOptions = require('unplugin-vue-define-options/webpack');
const nodeExternals = require('webpack-node-externals');

module.exports = {
  mode: 'development',
  entry: './test.vue',
  externals: [nodeExternals()],
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: [
          {
            loader: 'vue-loader',
          },
        ],
      },
    ],
  },
  plugins: [new VueLoaderPlugin(), defineOptions()],
  devtool: false,
};

the configuration above can not work, unless I add a loader

{
  loader: 'unplugin/dist/webpack/loaders/transform?unpluginName=unplugin-vue-define-options',
}

the full configuration is:

const VueLoaderPlugin = require('vue-loader/lib/plugin');
const defineOptions = require('unplugin-vue-define-options/webpack');
const nodeExternals = require('webpack-node-externals');

module.exports = {
  mode: 'development',
  entry: './test.vue',
  externals: [nodeExternals()],
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: [
          {
            loader: 'vue-loader',
          },
          {
            loader:
              'unplugin/dist/webpack/loaders/transform?unpluginName=unplugin-vue-define-options',
          },
        ],
      },
    ],
  },
  plugins: [new VueLoaderPlugin(), defineOptions()],
  devtool: false,
};

I have create a StackBlitz demo, the link: https://stackblitz.com/edit/node-fu77mv?file=package.json,index.js,test.vue,webpack.config.js,dist%2Fmain.js

you can run npm run build in the terminal to see the different in the output file dist/main.js, the screenshot:

with this loader:
image

without this loader:
image

you can see, without this loader, the defineOptions cannot be converted

@sxzz
Copy link
Member

sxzz commented Oct 20, 2022

@qinyang912 Thanks for your feedback! For Vue 2 (Vue loader 15) + Webpack, an additional option is required.

  plugins: [
    defineOptions({
      include: [/\.vue$/, /\.vue\?vue/],
    }),
    new VueLoaderPlugin(),
  ],

I will add it to the documentation later.

@qq619433305
Copy link

请问目前有解决办法了么。同为webpack5报错,显示 defineOptions is not defined

@Nstd
Copy link

Nstd commented Nov 11, 2022

请问目前有解决办法了么。同为webpack5报错,显示 defineOptions is not defined

@qq619433305

vue 3
webpack 5
非 TS
使用这个可以:#23 (comment)

   defineOptions({
      include: [/\.vue$/, /\.vue\?vue/],
    })

@ShaneLee-9
Copy link
Author

请问目前有解决办法了么。同为webpack5报错,显示 defineOptions is not defined

@qq619433305

vue 3 webpack 5 非 TS 使用这个可以:#23 (comment)

   defineOptions({
      include: [/\.vue$/, /\.vue\?vue/],
    })

@Nstd
使用这个配置尝试,打包后还是会报一样的错

@Nstd
Copy link

Nstd commented Nov 17, 2022

@ShaneLee-9 你用的啥环境(版本)?配置文件看看

@ShaneLee-9
Copy link
Author

@Nstd 这是仓库地址:https://github.com/ShaneLee-9/vue-options-plugin-test
用的 vue-cli v5 搭建的 vue3 模板,只修改了 vue.config.jsHellowWorld.vue 文件。开发环境没问题,打包后报错,下面是运行的截图:

开发环境

开发环境

打包后

生产环境

@ShaneLee-9
Copy link
Author

@Nstd 对了,node 版本是 14.19.2npm9.1.1,系统是 Win10 22H2

@Nstd
Copy link

Nstd commented Nov 21, 2022

@Nstd 对了,node 版本是 14.19.2npm9.1.1,系统是 Win10 22H2

@ShaneLee-9 需要再加个一个配置:
#23 (comment)

@ShaneLee-9
Copy link
Author

@Nstd 有用!

sxzz added a commit that referenced this issue Dec 15, 2022
sxzz added a commit that referenced this issue Dec 15, 2022
@zhoutengshen
Copy link

我也遇到了,咋整

@CyanSalt
Copy link

You can set parallel: false in vue.config.js to disable thread-loader.

Related: vuejs/vue-cli#2785 (comment) webpack-contrib/thread-loader#27

You can use parallelize-webpack-unplugin if you have tried this method and it works. It partially simplifies the parameters that unplugin depends on to allow the plugin to support running with thread-loader, so that you can enjoy the benefits of concurrent compilation at the same time.

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

No branches or pull requests