vite-plugin-externals
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -7,9 +7,2 @@ import MagicString from 'magic-string'; | ||
export function viteExternalsPlugin(externals = {}, userOptions = {}) { | ||
const { useWindow = true } = userOptions; | ||
const transofrmModuleName = (moduleId) => { | ||
if (!useWindow) { | ||
return moduleId; | ||
} | ||
return `window['${moduleId}']`; | ||
}; | ||
return { | ||
@@ -46,22 +39,15 @@ name: 'vite-plugin-externals', | ||
} | ||
const newImportStr = specifiers.reduce((s, specifier) => { | ||
const { local } = specifier; | ||
if (specifier.type === 'ImportDefaultSpecifier') { | ||
s += `const ${local.name} = ${transofrmModuleName(externalValue)}\n`; | ||
} | ||
else if (specifier.type === 'ImportSpecifier') { | ||
const { imported } = specifier; | ||
s += `const ${local.name} = ${transofrmModuleName(externalValue)}.${imported.name}\n`; | ||
} | ||
else if (specifier.type === 'ImportNamespaceSpecifier') { | ||
s += `const ${local.name} = ${transofrmModuleName(externalValue)}\n`; | ||
} | ||
return s; | ||
}, ''); | ||
const newImportStr = replaceImports(specifiers, externalValue, userOptions); | ||
s.overwrite(statementStart, statementEnd, newImportStr); | ||
}); | ||
code = s ? s.toString() : code; | ||
if (!s) { | ||
return; | ||
} | ||
return { | ||
code, | ||
map: s ? s.generateMap({ hires: true }) : null, | ||
code: s.toString(), | ||
map: s.generateMap({ | ||
source: id, | ||
includeContent: true, | ||
hires: true, | ||
}), | ||
}; | ||
@@ -71,2 +57,25 @@ }, | ||
} | ||
function replaceImports(specifiers, externalValue, options) { | ||
const { useWindow = true } = options; | ||
const transformModuleName = (moduleId) => { | ||
if (!useWindow) { | ||
return moduleId; | ||
} | ||
return `window['${moduleId}']`; | ||
}; | ||
return specifiers.reduce((s, specifier) => { | ||
const { local } = specifier; | ||
if (specifier.type === 'ImportDefaultSpecifier') { | ||
s += `const ${local.name} = ${transformModuleName(externalValue)}\n`; | ||
} | ||
else if (specifier.type === 'ImportSpecifier') { | ||
const { imported } = specifier; | ||
s += `const ${local.name} = ${transformModuleName(externalValue)}.${imported.name}\n`; | ||
} | ||
else if (specifier.type === 'ImportNamespaceSpecifier') { | ||
s += `const ${local.name} = ${transformModuleName(externalValue)}\n`; | ||
} | ||
return s; | ||
}, ''); | ||
} | ||
function isNeedExternal(options, code, id, ssr) { | ||
@@ -73,0 +82,0 @@ const { disableSsr = true, filter, } = options; |
@@ -10,9 +10,2 @@ "use strict"; | ||
function viteExternalsPlugin(externals = {}, userOptions = {}) { | ||
const { useWindow = true } = userOptions; | ||
const transofrmModuleName = (moduleId) => { | ||
if (!useWindow) { | ||
return moduleId; | ||
} | ||
return `window['${moduleId}']`; | ||
}; | ||
return { | ||
@@ -49,22 +42,15 @@ name: 'vite-plugin-externals', | ||
} | ||
const newImportStr = specifiers.reduce((s, specifier) => { | ||
const { local } = specifier; | ||
if (specifier.type === 'ImportDefaultSpecifier') { | ||
s += `const ${local.name} = ${transofrmModuleName(externalValue)}\n`; | ||
} | ||
else if (specifier.type === 'ImportSpecifier') { | ||
const { imported } = specifier; | ||
s += `const ${local.name} = ${transofrmModuleName(externalValue)}.${imported.name}\n`; | ||
} | ||
else if (specifier.type === 'ImportNamespaceSpecifier') { | ||
s += `const ${local.name} = ${transofrmModuleName(externalValue)}\n`; | ||
} | ||
return s; | ||
}, ''); | ||
const newImportStr = replaceImports(specifiers, externalValue, userOptions); | ||
s.overwrite(statementStart, statementEnd, newImportStr); | ||
}); | ||
code = s ? s.toString() : code; | ||
if (!s) { | ||
return; | ||
} | ||
return { | ||
code, | ||
map: s ? s.generateMap({ hires: true }) : null, | ||
code: s.toString(), | ||
map: s.generateMap({ | ||
source: id, | ||
includeContent: true, | ||
hires: true, | ||
}), | ||
}; | ||
@@ -75,2 +61,25 @@ }, | ||
exports.viteExternalsPlugin = viteExternalsPlugin; | ||
function replaceImports(specifiers, externalValue, options) { | ||
const { useWindow = true } = options; | ||
const transformModuleName = (moduleId) => { | ||
if (!useWindow) { | ||
return moduleId; | ||
} | ||
return `window['${moduleId}']`; | ||
}; | ||
return specifiers.reduce((s, specifier) => { | ||
const { local } = specifier; | ||
if (specifier.type === 'ImportDefaultSpecifier') { | ||
s += `const ${local.name} = ${transformModuleName(externalValue)}\n`; | ||
} | ||
else if (specifier.type === 'ImportSpecifier') { | ||
const { imported } = specifier; | ||
s += `const ${local.name} = ${transformModuleName(externalValue)}.${imported.name}\n`; | ||
} | ||
else if (specifier.type === 'ImportNamespaceSpecifier') { | ||
s += `const ${local.name} = ${transformModuleName(externalValue)}\n`; | ||
} | ||
return s; | ||
}, ''); | ||
} | ||
function isNeedExternal(options, code, id, ssr) { | ||
@@ -77,0 +86,0 @@ const { disableSsr = true, filter, } = options; |
{ | ||
"name": "vite-plugin-externals", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "externals plugin for vite", | ||
@@ -5,0 +5,0 @@ "author": "crcong", |
# vite-plugin-externals | ||
<p> | ||
<a href="https://www.npmjs.com/package/vite-plugin-externals" target="_blank"> | ||
<img alt="NPM package" src="https://img.shields.io/npm/v/vite-plugin-externals.svg?style=flat"> | ||
</a> | ||
<a href="https://www.npmjs.com/package/vite-plugin-externals" target="_blank"> | ||
<img alt="downloads" src="https://img.shields.io/npm/dt/vite-plugin-externals.svg?style=flat"> | ||
</a> | ||
<a href="https://github.com/vitejs/awesome-vite#helpers" target="_blank"> | ||
<img src="https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg" alt="Awesome"> | ||
</a> | ||
</p> | ||
English | [简体中文](README.zh-CN.md) | ||
@@ -4,0 +16,0 @@ |
# vite-plugin-externals | ||
<p> | ||
<a href="https://www.npmjs.com/package/vite-plugin-externals" target="_blank"> | ||
<img alt="NPM package" src="https://img.shields.io/npm/v/vite-plugin-externals.svg?style=flat"> | ||
</a> | ||
<a href="https://www.npmjs.com/package/vite-plugin-externals" target="_blank"> | ||
<img alt="downloads" src="https://img.shields.io/npm/dt/vite-plugin-externals.svg?style=flat"> | ||
</a> | ||
<a href="https://github.com/vitejs/awesome-vite#helpers" target="_blank"> | ||
<img src="https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg" alt="Awesome"> | ||
</a> | ||
</p> | ||
[English](README.md) | 简体中文 | ||
@@ -4,0 +16,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15649
218
123