Skip to content

Commit

Permalink
Add common urlJoin utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Dec 23, 2017
1 parent 38f8aaf commit 5210bc6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
10 changes: 2 additions & 8 deletions src/assets/HTMLAsset.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const Asset = require('../Asset');
const parse = require('posthtml-parser');
const api = require('posthtml/lib/api');
const path = require('path');
const url = require('url');
const urlJoin = require('../utils/urlJoin');
const render = require('posthtml-render');
const posthtmlTransform = require('../transforms/posthtml');
const isURL = require('../utils/is-url');
Expand Down Expand Up @@ -46,12 +45,7 @@ class HTMLAsset extends Asset {
if (elements && elements.includes(node.tag)) {
let assetPath = this.addURLDependency(node.attrs[attr]);
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),
''
);
assetPath = urlJoin(this.options.publicURL, assetPath);
}
node.attrs[attr] = assetPath;
this.isAstDirty = true;
Expand Down
12 changes: 5 additions & 7 deletions src/assets/RawAsset.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
const Asset = require('../Asset');
const url = require('url');
const urlJoin = require('../utils/urlJoin');

class RawAsset extends Asset {
// Don't load raw assets. They will be copied by the RawPackager directly.
load() {}

generate() {
let publicURL = this.options.publicURL;
publicURL = /\/$/.test(publicURL) ? publicURL : `${publicURL}/`;

const pathToAsset = JSON.stringify(
url.resolve(publicURL, this.generateBundleName())
const pathToAsset = urlJoin(
this.options.publicURL,
this.generateBundleName()
);

return {
js: `module.exports=${pathToAsset};`
js: `module.exports=${JSON.stringify(pathToAsset)};`
};
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/packagers/HTMLPackager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Packager = require('./Packager');
const posthtml = require('posthtml');
const path = require('path');
const url = require('url');
const urlJoin = require('../utils/urlJoin');

class HTMLPackager extends Packager {
async addAsset(asset) {
Expand Down Expand Up @@ -40,10 +40,7 @@ class HTMLPackager extends Packager {
tag: 'link',
attrs: {
rel: 'stylesheet',
href: url.resolve(
path.join(this.options.publicURL, path.basename(bundle.name)),
''
)
href: urlJoin(this.options.publicURL, path.basename(bundle.name))
}
});
}
Expand Down
8 changes: 8 additions & 0 deletions src/utils/urlJoin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const url = require('url');
const path = require('path');

module.exports = function(publicURL, assetPath) {
// Use url.resolve to normalize path for windows
// from \path\to\res.js to /path/to/res.js
return url.resolve(path.join(publicURL, assetPath), '');
};

0 comments on commit 5210bc6

Please sign in to comment.