0

I just run the command:

$ npx create-stencil ionic-pwa

which created a vanilla Ionic 4 PWA with the following directory structure inside directory: src:

.
├── assets
│   └── icon
│       ├── favicon.ico
│       └── icon.png
├── components
│   ├── app-home
│   │   ├── app-home.css
│   │   ├── app-home.spec.ts
│   │   └── app-home.tsx
│   ├── app-profile
│   │   ├── app-profile.css
│   │   ├── app-profile.spec.ts
│   │   └── app-profile.tsx
│   └── my-app
│       ├── my-app.css
│       ├── my-app.spec.ts
│       └── my-app.tsx
├── components.d.ts
├── global
│   ├── app.css
│   └── app.ts
├── helpers
│   └── utils.ts
├── index.html
├── manifest.json
└── sw.js

Then I want to create a custom component with html tag: comp-workspace and file name: comp-workspace.js (or something similar) that I can insert on any vanilla HTML/Javascript page without using any Javascript framework.

There are some things to take into consideration here:

  1. component: comp-workspace can include the following components: { comp-gallery, comp-artboard, comp-text-editor, comp-layer-stack }.

  2. all above components can include standard Ionic components.

Please, take a look to the following diagram:

enter image description here

Here is the directory structure after adding my custom component: comp-workspace and its dependencies.

.
├── assets
│   └── icon
│       ├── favicon.ico
│       └── icon.png
├── components
│   ├── app-home
│   │   ├── app-home.css
│   │   ├── app-home.spec.ts
│   │   └── app-home.tsx
│   ├── app-profile
│   │   ├── app-profile.css
│   │   ├── app-profile.spec.ts
│   │   └── app-profile.tsx
│   ├── comp-workspace
│   │   ├── comp-workspace.css
│   │   ├── comp-workspace.spec.ts
│   │   └── comp-workspace.tsx
│   ├── comp-gallery
│   │   ├── comp-gallery.css
│   │   ├── comp-gallery.spec.ts
│   │   └── comp-gallery.tsx
│   ├── comp-artboard
│   │   ├── comp-artboard.css
│   │   ├── comp-artboard.spec.ts
│   │   └── comp-artboard.tsx
│   ├── comp-text-editor
│   │   ├── comp-text-editor.css
│   │   ├── comp-text-editor.spec.ts
│   │   └── comp-text-editor.tsx
│   ├── comp-layer-stack
│   │   ├── comp-layer-stack.css
│   │   ├── comp-layer-stack.spec.ts
│   │   └── comp-layer-stack.tsx
│   └── my-app
│       ├── my-app.css
│       ├── my-app.spec.ts
│       └── my-app.tsx
├── components.d.ts
├── global
│   ├── app.css
│   └── app.ts
├── helpers
│   └── utils.ts
├── index.html
├── manifest.json
└── sw.js

Then, I want to use my custom component in a way similar to:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Title of the document</title>
<script src="..."></script>
<script src="https://my.repository.com/components/comp-workspace.js"></script>
</head>

<body>
    <comp-workspace attr1="..." attr2="..." attr3="..." ...></comp-workspace>
</body>

</html>

If I run the build command on the console I get the following output:

$ npm run build

> @ionic/pwa-toolkit@0.0.1 build D:\stencil-lab
> stencil build

[25:03.5]  @stencil/core v0.12.1
[25:03.9]  build, app, prod mode, started ...
[25:06.3]  transpile started ...
[25:12.3]  transpile finished in 6.02 s
[25:15.5]  module map started ...
[25:15.5]  generate styles started ...
[25:26.0]  compile global style start ...
[25:28.0]  compile global style finish in 2.03 s
[25:28.0]  generate styles finished in 12.50 s
[25:28.0]  module map finished in 12.50 s
[25:28.0]  generate bundles started ...
[25:28.2]  generate bundles finished in 237 ms
[25:28.2]  generate app files started ...
[25:29.6]  generate app files finished in 1.36 s
[26:03.4]  inject manifest into service worker started ...
[26:08.5]  inject manifest into service worker finished in 5.04 s

[ WARN  ]  build warn
           Generated an empty bundle

[26:08.5]  build finished in 64.58 s

I also get a new directory: www with the following content:

.
├── assets
│   └── icon
│       ├── favicon.ico
│       └── icon.png
├── build
│   ├── app
│   │   ├── 3hkybzk1.es5.js
│   │   ├── 3hkybzk1.js
│   │   ├── 3hkybzk1.sc.es5.js
│   │   ├── 3hkybzk1.sc.js
│   │   ├── 4jakisfl.es5.js
│   │   ├── 4jakisfl.js
│   │   ├── 4jakisfl.sc.es5.js
│   │   ├── 4jakisfl.sc.js
│   │   ├── aa33sq6q.es5.js
│   │   ├── aa33sq6q.js
│   │   ├── [... AND MANY MORE FILES]
│   │   └── svg
│   │       ├── index.js
│   │       ├── ios-add-circle-outline.svg
│   │       ├── ios-add-circle.svg
│   │       ├── ios-add.svg
│   │       ├── ios-airplane.svg
│   │       ├── ios-alarm.svg
│   │       ├── ios-albums.svg
│   │       ├── ios-alert.svg
│   │       ├── ios-american-football.svg
│   │       ├── ios-analytics.svg
│   │       └── [... AND MANY MORE FILES]
│   ├── app.css
│   └── app.js
├── index.html
├── manifest.json
├── sw.js
└── workbox-v3.4.1
    ├── workbox-background-sync.dev.js
    ├── workbox-background-sync.dev.js.map
    ├── workbox-background-sync.prod.js
    ├── workbox-background-sync.prod.js.map
    ├── workbox-broadcast-cache-update.dev.js
    ├── workbox-broadcast-cache-update.dev.js.map
    ├── workbox-broadcast-cache-update.prod.js
    ├── workbox-broadcast-cache-update.prod.js.map
    ├── workbox-cache-expiration.dev.js
    ├── workbox-cache-expiration.dev.js.map
    └── [... AND MANY MORE FILES]

6 directories, 832 files

but as you can see, I don't get an actual javascript bundle file I can use to insert my custom component on any vanilla HTML/Javascript page.

My question is: how do I build just my custom component from inside this Ionic 4 PWA app?

Thanks!

[EDIT 1]

This is a response to the comment from Alexander Staroselsky below, where he says:

You could consider using something like stencil-component-starter to build individual web components that can be bundled and used effectively anywhere.

That's a good point Alexander, and actually that was my initial idea but I found some problems which I posted on this forum:

https://forum.ionicframework.com/t/ion-label-component-inside-the-stencil-component-starter-not-getting-rendered/139763/3#post_4

Aaron Saunders said there (as of 2018-08-28):

It appears that this project (stencil-component-starter) doesn't have Ionic 4 in it by default.

Then he suggested me to use the Ionic PWA Starter Kit project and that's what I was trying to do here. Doing this way everything seems to work well (all Ionic components work), but now I just need to compile just my custom component to a separate file.

Here is a the same discussion in parallel in StackOverflow:

ion-label component inside the stencil-component-starter not getting rendered

Any ideas?

davidesp
  • 3,743
  • 10
  • 39
  • 77
  • thanks Alexander. I responded you on **[EDIT 1]** on my original post, please, check. – davidesp Aug 29 '18 at 21:56
  • Do you either `import '@ionic/core';` in your component or use include the [CDN](https://github.com/ionic-team/ionic/tree/master/core#vanilla-html) @ionic/core assets in your project? Creating a new stencil-component-starter and importing/linking either is allowing the elements to be used and exported. There are definitely styling inconsistencies, but that is an entirely different issue related to the beta. – Alexander Staroselsky Aug 29 '18 at 22:32
  • Alexander, could you maybe post that suggestion on the following post (StackOverflow) for the community convenience? I also can answer you there. Thanks! https://stackoverflow.com/questions/52024158/ion-label-component-inside-the-stencil-component-starter-not-getting-rendered – davidesp Aug 29 '18 at 22:38
  • Alexander, when I take the way of importing: `@ionic/core` in my component everything works just fine, but the compiled component has lot of JavaScript files which I think lot of them is `@ionic/core` or `@stencil/core` source code. Imagine the case where I have 10 custom components, for each of them I'm gonna have maybe 10 times common code. I'm wondering if is there a way to prevent that?. Is maybe the second way you suggested above: "add CDN scripts/styles to the index.html" achieving this?, I mean, the previous core code is not incorporated on my code but I can use it via CDN?. Thanks! – davidesp Aug 30 '18 at 14:59

0 Answers0