New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

parcel-plugin-externals

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parcel-plugin-externals - npm Package Compare versions

Comparing version 0.3.3 to 0.4.0-pre.20200405.1

# parcel-plugin-externals Changelog
## 0.4.0
- Improved the documentation
- Added support for fake (virtual) modules (#9)
## 0.3.3

@@ -4,0 +9,0 @@

{
"name": "parcel-plugin-externals",
"version": "0.3.3",
"version": "0.4.0-pre.20200405.1",
"description": "A plugin for Parcel to omit declared externals from being included in the emitted bundles.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -16,2 +16,4 @@ # parcel-plugin-externals

### Use Global Require
Consider the following snippet (from *package.json*):

@@ -29,2 +31,14 @@

Alternatively, you could have also written the following snippet (from *package.json*):
```json
{
"externals": [
"react"
]
}
```
### Use Global Variable
Potentially, instead you want to hint Parcel that you already have a global available coming from another script. The `externals` definition can help you.

@@ -42,6 +56,8 @@

Here we tell the plugin to alias the `react` module with `React`. In this case we reference a global variable `React`, which obviously must exist.
Here we tell the plugin to alias the `react` module with `React`. In this case we reference a **global variable** `React`, which obviously must exist.
**Note**: Don't confuse this with the abilities coming from [parcel-plugin-html-externals](https://github.com/stoically/parcel-plugin-html-externals). Values that are non-string instances will be ignored. So you can actually use both plugins, `parcel-plugin-externals` and `parcel-plugin-html-externals` if you want to (or just one of the two).
### Use Custom Locations
The object syntax is a shorthand for combining the keys and values for a replacement expression. The snippet above is acutally equalivant to:

@@ -94,3 +110,3 @@

```js
const rx = /node_modules\/react-(.*?)\//;
const rx = /react-(.*?)\//;

@@ -114,2 +130,26 @@ module.exports = function(path) {

### Virtual Modules
By default, the modules must be present in the local installation. Otherwise Parcel will complain. This is not always possible / the case.
In this scenario you'll need a virtual module. You can get support for this via Parcel's standard `alias`.
Consider the following snippet (from *package.json*):
```json
{
"externals": [
"react",
"foo"
],
"alias": {
"foo": "./src/foo-virtual.js"
}
}
```
Here, we will identify that foo is an external alias and still externalize the call (in the given example to use `require('foo')`). You could leave the virtual module empty.
**Important**: If you have multiple virtual modules you should give them all unique paths. Otherwise, this plugin cannot distinguish between them.
## Changelog

@@ -116,0 +156,0 @@

@@ -5,6 +5,10 @@ const { readFileSync, existsSync, realpathSync } = require("fs");

function inspect(name) {
function inspect(name, alias) {
let scope = "";
let path = "";
if (alias[name] !== undefined) {
name = alias[name];
}
if (name.startsWith("@")) {

@@ -28,6 +32,16 @@ scope = name.substr(0, name.indexOf("/"));

function resolveModule(rule, targetDir) {
function resolveModule(rule, targetDir, alias) {
const { name } = splitRule(rule);
const { fullName, path } = inspect(name);
const { fullName, path } = inspect(name, alias);
if (fullName.startsWith('./') || fullName.startsWith('/')) {
return [
{
name,
rule,
path: resolve(targetDir, fullName),
}
];
}
try {

@@ -130,7 +144,7 @@ const packageName = path ? `${fullName}/${path}` : fullName;

function makeResolver(targetDir, externalNames) {
function makeResolver(targetDir, externalNames, alias) {
const externals = [];
for (const name of externalNames) {
const modules = resolveModule(name, targetDir);
const modules = resolveModule(name, targetDir, alias);
externals.push(...modules.map(m => ({

@@ -164,6 +178,6 @@ ...m,

function combineExternals(rootDir, plain, externals) {
function combineExternals(rootDir, plain, externals, alias) {
if (Array.isArray(externals)) {
const values = externals.concat(plain);
return makeResolver(rootDir, values);
return makeResolver(rootDir, values, alias);
} else if (typeof externals === "object") {

@@ -174,3 +188,3 @@ const values = Object.keys(externals)

.concat(plain);
return makeResolver(rootDir, values);
return makeResolver(rootDir, values, alias);
} else if (typeof externals === "string") {

@@ -211,3 +225,4 @@ const externalPath = resolve(rootDir, externals);

const externals = data.externals || [];
return combineExternals(rootDir, plain, externals);
const alias = data.alias || {};
return combineExternals(rootDir, plain, externals, alias);
} catch (ex) {

@@ -214,0 +229,0 @@ console.error(ex);