Skip to content

Commit

Permalink
ADD: Possilibity to ignore files based on NODE_ENV value (#6)
Browse files Browse the repository at this point in the history
* feat: adding possilibity to have dev or prod ignore files configuration

* fix: format

* fix: format

* fix: use NODE_ENV value as parcelIgnore config key

* RUN: npm i to update package-lock.json

Co-authored-by: Vladimir <vladimir.mikulic2@gmail.com>
  • Loading branch information
bsvobodny and VladimirMikulic authored May 13, 2022
1 parent 7e3e4f1 commit a49c148
Show file tree
Hide file tree
Showing 4 changed files with 1,516 additions and 1,926 deletions.
59 changes: 53 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ You can find example use-cases below.
{
// An array of Regex patterns
"parcelIgnore": [
"jquery.min.js"
"jquery.min.js",
"privacy-policy.html",
"images\/*.*",
"images\/*.*"
]
}

```

**index.html**
Expand All @@ -70,8 +69,8 @@ You can find example use-cases below.
<a href="./privacy-policy.html">File not available at build time.</a>

<!-- These won't be processed by Parcel -->
<img src="./images/my-image.png" alt="my PNG image">
<img src="./images/my-image.jpg" alt="my JPG image">
<img src="./images/my-image.png" alt="my PNG image" />
<img src="./images/my-image.jpg" alt="my JPG image" />
<script src="jquery.min.js"></script>
</body>
</html>
Expand All @@ -88,7 +87,6 @@ You can find example use-cases below.
"{{*.*}}"
]
}

```

**index.html**
Expand All @@ -110,11 +108,60 @@ You can find example use-cases below.
</html>
```

3. Ignoring files based on NODE_ENV value : "development" (`parcel serve`) or "production" (`parcel build`) or custom.

**package.json**

```jsonc
{
// An object containing dev and/or prod files to ignore
"parcelIgnore": {
"development": [
"privacy-policy.html",
"images\/*.*"
],
"production": [
"jquery.min.js",
"images\/*.*"
],
"test": [
"jquery.min.js"
]
}
}

```

**index.html**

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>

<body>
<h1>My Title</h1>
<!-- This won't throw build error -->
<a href="./privacy-policy.html">File not available at build time.</a>

<!-- These won't be processed by Parcel -->
<img src="./images/my-image.png" alt="my PNG image" />
<img src="./images/my-image.jpg" alt="my JPG image" />
<script src="jquery.min.js"></script>
</body>
</html>
```

### 🚀 Build

```sh
parcel build src/index.html # Success!
```

## :wrench: Troubleshooting

If you ran Parcel just before adding this plugin, it's possible that stale Parcel caches are causing build failures. First, try to delete the caches folder `.parcel-cache` in your project's root directory.
Expand Down
10 changes: 7 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const { Resolver } = require('@parcel/plugin');

const pkgJSONPath = path.join(process.cwd(), 'package.json');
const pkgJSON = JSON.parse(fs.readFileSync(pkgJSONPath));
const regexpPatterns = pkgJSON.parcelIgnore || [];

const { parcelIgnore = [] } = pkgJSON;
const regexpPatterns = Array.isArray(parcelIgnore)
? parcelIgnore
: parcelIgnore[process.env.NODE_ENV];

module.exports = new Resolver({
async resolve({ specifier }) {
Expand All @@ -14,8 +18,8 @@ module.exports = new Resolver({

if (shouldExcludeFile) {
console.log(`Skipped ${specifier}`);
return { isExcluded: true }
};
return { isExcluded: true };
}

return null;
}
Expand Down
Loading

0 comments on commit a49c148

Please sign in to comment.