From ecf283c49e300316c281b9a30a38e6ed027b8ad9 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Wed, 5 Feb 2020 19:42:25 +0100 Subject: [PATCH] fix(*): support Windows paths (#6) See: https://github.com/bazelbuild/rules_nodejs/issues/1604 --- src/main.js | 4 +++- test/spec.js | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main.js b/src/main.js index 9ae4f07..8dc8280 100644 --- a/src/main.js +++ b/src/main.js @@ -24,6 +24,8 @@ function findElementByName(d, name) { function normalizePath(p) { p = path.normalize(p); + // Convert paths to posix + p = p.replace(/\\/g, '/'); if (p[0] !== '/' && p[0] !== '.') { p = `./${p}`; } @@ -166,7 +168,7 @@ function main(params, read = fs.readFileSync, write = mkdirpWrite, timestamp = D const rootedOutputDir = removeRootPath(outputDir).replace(/^\//, "./"); function relativeToHtml(p) { // Ignore absolute - if (p.startsWith("/")) { + if (path.isAbsolute(p)) { return p; } diff --git a/test/spec.js b/test/spec.js index 6c320d3..b10ea3e 100644 --- a/test/spec.js +++ b/test/spec.js @@ -1,9 +1,10 @@ +const path = require('path'); const {main, parseArgs} = require('../src/main'); -const inFile = './data/some/index.html'; +const inFile = path.normalize('./data/some/index.html'); function read(file) { - if (file === inFile) return ``; + if (path.normalize(file) === inFile) return ``; throw new Error(`no content for ${file}`); } @@ -108,7 +109,17 @@ describe('HTML inserter', () => { "--roots", 'npm/node_modules/zone.js/dist', '--assets', 'external/npm/node_modules/zone.js/dist/zone.min.js'], read, write, stamper)).toBe(0); expect(output).toBe(scriptHtml('/zone.min.js')); - + }); + + it('should strip the external workspaces prefix in Windows', () => { + if (path.win32.normalize !== path.normalize) { + spyOn(path, 'normalize').and.callFake(path.win32.normalize); + } + + expect(main(["--out", "index.html", "--html", inFile, + "--roots", 'npm/node_modules/zone.js/dist', + '--assets', 'external/npm/node_modules/zone.js/dist/zone.min.js'], read, write, stamper)).toBe(0); + expect(output).toBe(scriptHtml('/zone.min.js')); }); it('should inject .css files as stylesheet link tags', () => {