diff --git a/src/assets/HTMLAsset.js b/src/assets/HTMLAsset.js index 6e73518eea2..3e4c613cea8 100644 --- a/src/assets/HTMLAsset.js +++ b/src/assets/HTMLAsset.js @@ -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'); @@ -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; diff --git a/src/assets/RawAsset.js b/src/assets/RawAsset.js index a6713af8bb4..c67d240b479 100644 --- a/src/assets/RawAsset.js +++ b/src/assets/RawAsset.js @@ -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)};` }; } } diff --git a/src/packagers/HTMLPackager.js b/src/packagers/HTMLPackager.js index ac943bc3abf..6bef663e485 100644 --- a/src/packagers/HTMLPackager.js +++ b/src/packagers/HTMLPackager.js @@ -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) { @@ -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)) } }); } diff --git a/src/utils/urlJoin.js b/src/utils/urlJoin.js new file mode 100644 index 00000000000..c4e169df46b --- /dev/null +++ b/src/utils/urlJoin.js @@ -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), ''); +};