-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: import external style files tags api
- Loading branch information
1 parent
c2f47f7
commit 34e85cc
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@marko/runtime-tags": patch | ||
--- | ||
|
||
Ensure external style files are imported for tags api component. |
1 change: 1 addition & 0 deletions
1
.../src/__tests__/fixtures/custom-tag-separate-assets/__snapshots__/dom.expected/template.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export const _template_ = "<div></div>"; | ||
export const _walks_ = /* over(1) */"b"; | ||
export const _setup_ = () => {}; | ||
import "./template.style.css"; | ||
import * as _$ from "@marko/runtime-tags/debug/dom"; | ||
export default /* @__PURE__ */_$.createTemplate("__tests__/template.marko", _template_, _walks_, _setup_); |
56 changes: 56 additions & 0 deletions
56
packages/runtime-tags/src/translator/util/get-style-file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { types as t } from "@marko/compiler"; | ||
import path from "path"; | ||
|
||
export default function getStyleFile(file: t.BabelFile) { | ||
const { filename } = file.opts; | ||
const fs = file.markoOpts.fileSystem; | ||
const base = getBase(filename); | ||
const styleMatch = new RegExp( | ||
`^(${escapeRegExp(base)}\\.${"index" === base ? "|" : ""})style\\.\\w+$`, | ||
); | ||
|
||
for (const file of fs.readdirSync(path.dirname(filename)).sort()) { | ||
if (styleMatch.test(file)) { | ||
return `./${file}`; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Given a filename, gets the base name, strips off the file extension | ||
* and removes any arc flags (https://github.com/eBay/arc). | ||
* | ||
* @example | ||
* getBase("/dir/foo.marko") // => "foo" | ||
* getBase("/dir/foo.bar.marko") // => "foo.bar" | ||
* getBase("/dir/foo[bar].marko") // => "foo" | ||
* getBase("/dir/foo[bar].baz.marko") // => "foo.baz" | ||
*/ | ||
function getBase(filename: string) { | ||
const start = filename.lastIndexOf(path.sep) + 1; | ||
const leftDot = filename.indexOf(".", start); | ||
|
||
if (leftDot === -1) { | ||
return filename.slice(start); | ||
} | ||
|
||
const rightDot = filename.lastIndexOf("."); | ||
const closeBracket = leftDot - 1; | ||
if (filename[closeBracket] === "]") { | ||
const openBracket = filename.lastIndexOf("[", closeBracket); | ||
if (openBracket > start) { | ||
// If we match a "]" before the extension and find a "[" before that, | ||
// then we have an arc flag. Strip it off. | ||
return ( | ||
filename.slice(start, openBracket) + filename.slice(leftDot, rightDot) | ||
); | ||
} | ||
} | ||
|
||
return filename.slice(start, rightDot); | ||
} | ||
|
||
const regexpCharsReg = /[\\^$.*+?()[\]{}|]/g; | ||
function escapeRegExp(str: string) { | ||
return str.replace(regexpCharsReg, "\\$&"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters