'How to exclude element-plus package (replaced by import CDN) when building with webpack (@vue/cli@next)?
I'm using @vue/cli 5.0.0-beta.3, vue 3.2.8 with element-plus 1.1.0-beta.8.
For the purpose of smaller size of build package, I try to exclude all third party dependencies (vue, vue-router, vuex, element-plus, etc) when building package with import CDN in index.html file:
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<!-- cdn import -->
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.global.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue-router.global.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuex.global.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue-class-component.global.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/index.js"></script>
<!-- cdn import -->
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title>Financial Tools</title>
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
While I failed to exclude element-plus with vue.config.js file:
module.exports = {
config.externals = {
vue: "Vue",
"vue-router": "VueRouter",
vuex: "Vuex",
"element-plus": "ElementPlus",
}
}
vue/cli service start, the page console report error:
Uncaught ReferenceError: ElementPlus is not defined
Here's a sample project to reproduce this issue: https://github.com/linrongbin16/financial_tools_app3
And there's another error:
Uncaught ReferenceError: exports is not defined
Also comes from the element-plus CDN:
"use strict";Object.defineProperty(exports,"__esModule" ...
As a second thought, maybe there's something wrong with my tsconfig.json file:
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"useDefineForClassFields": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"webpack-env"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
But since I'm new to vue3+typescript+element-plus, I have no idea why this happen and how to configure this.
Solution 1:[1]
I got this error (ReferenceError: exports is not defined) in production mode after scaffolding "vuejs-admin-dashboard-template".
Disabling this setting in vite.config.js solved it for me.
build: {
commonjsOptions: {
transformMixedEsModules: false,
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rikard Askelöf |
