native-sfc 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 YieldRay
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Native-SFC
2
+
3
+ Load a single HTML file as a Web Component.
4
+
5
+ ## How it works
6
+
7
+ The component loader fetches everything (HTML, JS, CSS) as text,
8
+ then processes them to create a Web Component.
9
+
10
+ 1. **HTML Parsing**: The HTML file is parsed and processed
11
+ 2. **Style Handling**: All `<style>` and `<link rel="stylesheet">` are collected and converted to `adoptedStyleSheets` of the component's shadow root (avoids FOUC)
12
+ 3. **Script Handling**: ESM modules (`<script type="module">`) are evaluated and their exports are merged. The `default` export, if it's a class extending `HTMLElement`, becomes the base class of the component
13
+ 4. **Global Styles**: Styles with `global` attribute are moved to the outer document instead of the shadow root
14
+ 5. **URL Rewriting**: All relative URLs in `src` and `href` attributes are rewritten to absolute URLs based on the component file location
15
+
16
+ ## API
17
+
18
+ ### `loadComponent(name, url, afterConstructor?)`
19
+
20
+ Load a component from a HTML file and register it as a custom element.
21
+
22
+ - `name`: The custom element name (e.g., `"my-component"`)
23
+ - `url`: The URL to the component HTML file (relative to the importer)
24
+ - `afterConstructor`: Optional callback executed after the component constructor
25
+
26
+ ### `defineComponent(fc)`
27
+
28
+ A helper function for dual-mode component definition:
29
+
30
+ - When used inside a `loadComponent`-imported module, it defines a web component class
31
+ - When used in normal document context, it runs the function with `document` as root
32
+
33
+ ## Limitations
34
+
35
+ - Dynamic imports with relative paths are NOT supported.
36
+ - Inside the ESM modules, the `from` syntax in import statements are rewritten to absolute URL, since all modules are actually loaded as blob URLs.
37
+ - Since all styles are moved to `adoptedStyleSheets` in component's shadow root, we CANNOT use `@import` rules in styles.
38
+ - Relative URLs in CSS (e.g., `background-image: url(./bg.png)`) are resolved relative to the main document (the page URL), not the component file URL.
39
+ - Components loaded with the same name and URL are cached and reused.
40
+ - Only `script[src]`'s src' `link[rel="stylesheet"]`'s href will be rewritten. Warn that `a[href]`/`img[src]`/etc with relative URLs in the HTML body will NOT be rewritten.
41
+
42
+ ## Next Steps
43
+
44
+ - Implement component debugger
45
+ - Implement component bundler in both runtime and server-side
46
+ - Consider to support rewriting relative URLs in CSS