Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Commit

Permalink
Fixed resolving dependencies inside tag attributes (#16)
Browse files Browse the repository at this point in the history
* Added support for block nodes

Block nodes might not have a `filename` field but instead a `nodes` field with an array of sub-nodes.
Additionally, I added a check on the `filename` field to see if it's undefined to avoid parcel from crashing during the bundling step in cases where the node would have no `filename` field.

* Update PugAsset.ts

* Fixed semicolon

* Added type declarations

* Fixed resolving dependencies inside tag attributes

* Fix side-effect bug introduced by double url resolution

* Remove unused url resolution and linked modules

* Removed unused module declaration from modules.d.ts

* Added srcset to the list of supported attributes
  • Loading branch information
dolma authored and Ty3uK committed Mar 20, 2018
1 parent 5aeea99 commit 8f94cb9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
3 changes: 3 additions & 0 deletions src/Asset/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ export class Asset {
parse(code: string): any;
addDependency(path: string, options: Object): any;
addURLDependency(url: string): string;
process(): Promise<any>;

name: string;
isAstDirty: boolean;
contents: string;
ast: any;
options: any;
dependencies: Set<Object>;
generated: any;
package: any;
}
41 changes: 25 additions & 16 deletions src/PugAsset.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import url = require('url');
import path = require('path');

import { Asset } from './Asset';
import isURL = require('parcel-bundler/lib/utils/is-url');
import HTMLAsset = require('parcel-bundler/lib/assets/HTMLAsset');

import load = require('pug-load');
import lexer = require('pug-lexer');
Expand Down Expand Up @@ -41,10 +38,14 @@ const ATTRS: Dictionary<string[]> = {
'iframe',
'embed'
],
srcset: ['img'],
href: ['link', 'a'],
poster: ['video']
};

// A regex to detect if a variable is a 'pure' string (no evaluation needed)
const PURE_STRING_REGEX: RegExp = /(^"([^"]+)"$)|(^'([^']+)'$)/g;

export = class PugAsset extends Asset {
public type = 'html';

Expand Down Expand Up @@ -72,11 +73,11 @@ export = class PugAsset extends Asset {
(cNode as Block).nodes.forEach((n: any) => recursiveCollect(n));
} else {
if (cNode.filename && cNode.filename !== this.name && !this.dependencies.has(cNode.filename)) {
this.addDependency(cNode.filename, {
name: cNode.filename,
includedInParent: true,
});
}
this.addDependency(cNode.filename, {
name: cNode.filename,
includedInParent: true,
});
}
}
};

Expand All @@ -86,21 +87,29 @@ export = class PugAsset extends Asset {
for (const attr of node.attrs) {
const elements = ATTRS[attr.name];
if (node.type === 'Tag' && elements && elements.indexOf(node.name) > -1) {
let assetPath = attr.val.substring(1, attr.val.length - 1);
assetPath = this.addURLDependency(assetPath);
if (!isURL(assetPath)) {
// Use url.resolve to normalize path for windows
// from \path\to\res.js to /path/to/res.js
assetPath = url.resolve(path.join(this.options.publicURL, assetPath), '');
if (PURE_STRING_REGEX.test(attr.val)) {
const assetPath = attr.val.substring(1, attr.val.length - 1);
this.addURLDependency(assetPath);
}
attr.val = `'${assetPath}'`;
}
}
}
return node;
});
}

public async process(): Promise<any> {
await super.process();

const htmlAsset = new HTMLAsset(this.name, this.package, this.options);
htmlAsset.contents = this.generated.html;
await htmlAsset.process();

Object.assign(this, htmlAsset);

return this.generated;
}

public generate() {
const result = generateCode(this.ast, {
compileDebug: false,
Expand Down
13 changes: 9 additions & 4 deletions src/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ declare module 'parcel-bundler/lib/Asset' {
export = Asset;
}

declare module 'parcel-bundler/lib/utils/is-url' {
function isURL(url: string): boolean;
export = isURL;
declare module 'parcel-bundler/lib/assets/HTMLAsset' {
class HTMLAsset {
constructor(name: string, pkg: string, options: any);

process(): Promise<any>;

contents: string;
}
export = HTMLAsset;
}

declare module 'pug-load' {
Expand Down Expand Up @@ -64,4 +70,3 @@ declare module 'pug-filters' {
function runFilter(name: string, str: string, options: any, currentDirectory: string, funcName: string): any;
function handleFilters(ast: any, filters?: any): any;
}

0 comments on commit 8f94cb9

Please sign in to comment.