Skip to content

Commit

Permalink
add development mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gxy5202 committed Jan 23, 2022
1 parent dc5b740 commit ce66d48
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
## Feature

This plugin will help you remove the hash from a bundle file name(parcel v2).
This plugin will help you remove the hash from a bundle file name(only support parcel v2).

## Why

If you are using parcel to package files, you may encounter the following situations.
When you package index.html that references other files, parcel treats those files as shared bundles and adds a hash to the file name by default, even if you set --no-content-hash in package.json.
When you package index.html that references other files, parcel treats those files as shared bundles and adds a hash to the file name by default, even if you set `--no-content-hash` in package.json.

So when you need a filename without any hash value, using this plugin can help you. For example, when you are developing a web browser extension, you need all file names to be stable and hashless

## Installation

`npm install --save-dev parcel-namer-hashless -D`
`npm install --save-dev parcel-namer-hashless`

## Useage

Expand All @@ -34,6 +34,11 @@ If you want to packge your index.html, make sure add "source" to your package.js
}
```

## development

If you are running in development, hash can not be removed.
(cause parcel need every file has a unique name);

## Result

If you run the plugin successfully, the terminal will output:
Expand Down
18 changes: 13 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ var CONFIG = Symbol["for"]("parcel-plugin-config");
var _default = new _plugin.Namer({
name: function name(_ref) {
return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
var bundle, bundleGraph, logger, oldName, nameArr, newName;
var bundle, bundleGraph, logger, options, oldName, nameArr, newName;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
bundle = _ref.bundle, bundleGraph = _ref.bundleGraph, logger = _ref.logger;
bundle = _ref.bundle, bundleGraph = _ref.bundleGraph, logger = _ref.logger, options = _ref.options;
_context.next = 3;
return _namerDefault["default"][CONFIG].name({
bundle: bundle,
Expand All @@ -41,8 +41,16 @@ var _default = new _plugin.Namer({
case 3:
oldName = _context.sent;

if (!(options.mode === "development")) {
_context.next = 6;
break;
}

return _context.abrupt("return", oldName);

case 6:
if (bundle.needsStableName) {
_context.next = 10;
_context.next = 12;
break;
}

Expand All @@ -54,10 +62,10 @@ var _default = new _plugin.Namer({
});
return _context.abrupt("return", newName);

case 10:
case 12:
return _context.abrupt("return", oldName);

case 11:
case 13:
case "end":
return _context.stop();
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "parcel-namer-hashless",
"version": "1.0.2",
"version": "1.0.3",
"description": "A parcel plugin that helps you remove the hash from the file name",
"scripts": {
"build": "babel src --out-dir lib --extensions \".ts\" && yalc push"
Expand Down Expand Up @@ -35,7 +35,7 @@
"lib/index.js"
],
"engines": {
"parcel": "^2.2.0",
"parcel": ">=2.2.0",
"node": ">= 10.0.0"
}
}
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import defaultName from "@parcel/namer-default";

const CONFIG = Symbol.for("parcel-plugin-config");
export default new Namer({
async name({ bundle, bundleGraph, logger }) {
async name({ bundle, bundleGraph, logger, options }) {
const oldName: string = await defaultName[CONFIG].name({
bundle,
bundleGraph,
logger,
});

// if mode is development, return file name with hash
if (options.mode === "development") {
return oldName;
}

// if filename has hash,
if (!bundle.needsStableName) {
const nameArr = oldName.split(".");
Expand Down

0 comments on commit ce66d48

Please sign in to comment.