Skip to content

Commit

Permalink
Merge pull request #112 from Draccoz/twc-111
Browse files Browse the repository at this point in the history
#111: Importing scripts produces wrong src output tag
  • Loading branch information
Daniel Busłowicz committed Sep 24, 2017
2 parents d161c6a + 1348f68 commit 615996d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class Import {
case ".css":
return `<link rel="stylesheet" href="${this.resolveModule()}">`;
default:
return `<link rel="import" href="${this.resolveModule()}">`;
return `<link rel="import" href="${this.resolveModule(".html")}">`;
}
}

Expand All @@ -109,8 +109,8 @@ export class Import {
*
* @returns Resolved module/component path
*/
private resolveModule() {
const [ , repo = "", path = this.module ] = this.module.match(/(?:([a-z]+):)?(.*)/) || [];
private resolveModule(ext = "") {
const [ , repo = "", path = this.module ] = this.module.match(/(?:([a-z]+):)?(.*?(\.[\w\d]+)?)$/) || [];
let modulePath = path;
if (repo in paths) {
modulePath = join(paths[ repo ], path);
Expand All @@ -119,7 +119,7 @@ export class Import {
} else if (path.startsWith("~")) {
modulePath = path.substr(1);
} else if (/^\.\.?\//.test(path)) {
return `${path}.html`;
return `${path}${ext}`;
} else {
return path;
}
Expand Down
51 changes: 30 additions & 21 deletions tests/targets/polymer1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,36 @@ describe("Polymer v1 output", () => {
}

it("should add imports", () => {
const component = transpile(`
import { CustomElement, template } from "twc/polymer";
import "bower:polymer/polymer.html";
import "yarn:polymer/polymer.html";
import "~bower_components/polymer/polymer.html";
import { prop } from "bower:some/component.html#NS";
import "style.css";
import "script.js";
import "./module";
import "../module";`);

expect(component.es5).to.equalIgnoreSpaces(`
<link rel="import" href="../../polymer/polymer.html">
<link rel="import" href="../node_modules/polymer/polymer.html">
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../../some/component.html">
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<link rel="import" href="./module.html">
<link rel="import" href="../module.html">`
);
expect(transpile(`import { CustomElement, template } from "twc/polymer";`).es5)
.to.equal("\n");

expect(transpile(`import "bower:polymer/polymer.html";`).es5)
.to.equal(`<link rel="import" href="../../polymer/polymer.html">\n`);

expect(transpile(`import "yarn:polymer/polymer.html";`).es5)
.to.equal(`<link rel="import" href="../node_modules/polymer/polymer.html">\n`);

expect(transpile(`import "~bower_components/polymer/polymer.html";`).es5)
.to.equal(`<link rel="import" href="../bower_components/polymer/polymer.html">\n`);

expect(transpile(`import { prop } from "bower:some/component.html#NS";`).es5)
.to.equal(`<link rel="import" href="../../some/component.html">\n`);

expect(transpile(`import "style.css";`).es5)
.to.equal(`<link rel="stylesheet" href="style.css">\n`);

expect(transpile(`import "script.js";`).es5)
.to.equal(`<script src="script.js"></script>\n`);

expect(transpile(`import './path/script.js';`).es5)
.to.equal(`<script src="./path/script.js"></script>\n`);

expect(transpile(`import "./module";`).es5)
.to.equal(`<link rel="import" href="./module.html">\n`);

expect(transpile(`import "../module";`).es5)
.to.equal(`<link rel="import" href="../module.html">\n`);

});
it("should allow components without inheritance", () => {
const component = transpile(`
Expand Down

0 comments on commit 615996d

Please sign in to comment.