Package Exports
- vue-cli-plugin-externals
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vue-cli-plugin-externals) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
vue-cli-plugin-externals
Manage external modules in the project
Currently only supports cdn module
Translation
Use
Vue-cli 3.x
vue add externalsYarn
yarn add vue-cli-plugin-externals --devNpm
npm install vue-cli-plugin-externals --devFeatures:
- Configure external module page level, all page levels
- Automatically inject webpack externals configuration
- Automatically inject the cdn of the external module into the generated html
Ideas:
The namespace of the plugin configuration is externals, and the plugin configuration item consists of the following two parts:
1.page-configured page-level external module
- Commonly configured all page level external modules
When the app is a single-page app, just configure the common field.
The data structure of the external module configuration is Module, and the data structure is as follows
interface Asset {
path: string;
type: 'css' | 'js';
}
// Module
interface Module {
id: string; // Module unique identifier
assets: string | string[] | Asset[]; // resource path
global?: string | null; // The name of the global variable exposed by the module, if it is a non-exported module; please set an empty, or remove this configuration item
}The overall plugin configuration data structure is as follows:
//vue.config.js
module.exports = {
pluginOptions: {
externals: {
common: Module[],
pages: {
pageName: Module[]
}
}
}
}Priority
General Module > Page Touch Block (mainly limited by webpack externals)
Module to de-thinking ideas:
De-weighting according to Module.id
Implementation process:
- Analyze the module configuration
- Determine if it is a single-page or multi-page application
- Merge deduplication, add module externals information to the webpack externals module
- Add the html-webpack-externals-plugin plugin instance to the webpack plugin according to the configuration.
Example
In a single page application:
// vue.config.js
{
pluginOptions: {
externals: {
common: [
{
id: 'jquery',
assets: 'https://unpkg.com/jquery@3.2.1/dist/jquery.min.js',
global: 'jQuery'
}
];
}
}
}In a case where extension is explicitly not provided:
// vue.config.js
{
pluginOptions: {
externals: {
common: [
{
id: 'vue',
assets: [
{
path: 'https://unpkg.com/vue',
type: 'js'
}
],
global: 'Vue'
}
];
}
}
}In a multi-page application:
{
pages: {
Index: './src/index.js'
}
pluginOptions: {
externals: {
common: [
{
id: 'jquery',
assets: 'https://unpkg.com/jquery@3.2.1/dist/jquery.min.js',
global: 'jQuery',
},
],
pages: {
index: [
{
id: 'cdnModule1',
assets: [
'//pkg.cdn.com/cdnModule1.css',
'//pkg.cdn.com/cdnModule1.js'
],
global: 'cdnModule1'
},
{
id: 'cdnModule2',
assets: [
'//pkg.cdn.com/cdnModule2.js'
],
global: 'cdnModule1'
}
]
}
}
}
}Problem
If the html-webpack-plugin is added after the plugin is executed, the plugin will be invalid. The specific reasons are as follows: