@smplrspace/smplr-loader 0.0.1-beta.0

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/.eslintignore ADDED
@@ -0,0 +1,3 @@
1
+ node_modules
2
+ dist
3
+ src/generated/smplr.d.ts
package/.eslintrc.js ADDED
@@ -0,0 +1,42 @@
1
+ module.exports = {
2
+ env: {
3
+ browser: true,
4
+ es2021: true,
5
+ jest: true,
6
+ node: true,
7
+ },
8
+ parser: '@typescript-eslint/parser',
9
+ parserOptions: {
10
+ ecmaVersion: 'latest',
11
+ sourceType: 'module',
12
+ },
13
+ settings: {
14
+ 'import/parsers': {
15
+ '@typescript-eslint/parser': ['.ts', '.tsx'],
16
+ },
17
+ 'import/resolver': {
18
+ typescript: {},
19
+ },
20
+ },
21
+ extends: [
22
+ 'eslint:recommended',
23
+ 'plugin:@typescript-eslint/recommended',
24
+ 'plugin:prettier/recommended',
25
+ 'plugin:import/recommended',
26
+ 'plugin:import/typescript',
27
+ ],
28
+ plugins: ['@typescript-eslint', 'simple-import-sort'],
29
+ rules: {
30
+ 'prettier/prettier': [
31
+ 'error',
32
+ {
33
+ singleQuote: true,
34
+ semi: false,
35
+ printWidth: 120,
36
+ },
37
+ ],
38
+ 'import/newline-after-import': 'error',
39
+ 'simple-import-sort/imports': 'error',
40
+ 'simple-import-sort/exports': 'error',
41
+ },
42
+ }
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # smplr-loader
2
+ This tiny NPM package can be installed to easily load a typed version of Smplr.js from our CDN in your code. See [the documentation](https://docs.smplrspace.com/) to learn more.
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@smplrspace/smplr-loader",
3
+ "version": "0.0.1-beta.0",
4
+ "description": "NPM package to load a typed Smplr.js from CDN easily",
5
+ "main": "src/index.ts",
6
+ "scripts": {
7
+ "dev": "echo 'No dev'",
8
+ "serve": "echo 'No serve'",
9
+ "libtest": "echo 'No libtest'",
10
+ "tsc": "tsc --noEmit",
11
+ "lint": "eslint . && prettier --check **/*.json && yarn tsc",
12
+ "lint:fix": "eslint --fix . && prettier -w **/*.json"
13
+ },
14
+ "lint-staged": {
15
+ "*.ts": [
16
+ "yarn run eslint"
17
+ ],
18
+ "*.json": [
19
+ "yarn run prettier --check"
20
+ ]
21
+ },
22
+ "devDependencies": {
23
+ "@typescript-eslint/eslint-plugin": "^5.42.0",
24
+ "@typescript-eslint/parser": "^5.42.0",
25
+ "eslint": "^8.26.0",
26
+ "eslint-config-prettier": "^8.5.0",
27
+ "eslint-import-resolver-typescript": "^3.5.2",
28
+ "eslint-plugin-import": "^2.26.0",
29
+ "eslint-plugin-prettier": "^4.2.1",
30
+ "prettier": "^2.7.1",
31
+ "typescript": "4.7.4"
32
+ },
33
+ "homepage": "https://www.smplrspace.com",
34
+ "keywords": [
35
+ "smplrspace",
36
+ "floor plan",
37
+ "digital twin"
38
+ ],
39
+ "packageManager": "yarn@3.2.4"
40
+ }
File without changes
package/src/index.ts ADDED
@@ -0,0 +1,55 @@
1
+ import { loadEsmModule, loadUmdScript } from './loadScript'
2
+ import { loadStylesheet } from './loadStylesheet'
3
+ import { Smplr } from './types'
4
+
5
+ export type { Smplr } from './types'
6
+
7
+ const SMPLR = {
8
+ umd: {
9
+ prod: 'https://app.smplrspace.com/lib/smplr.js',
10
+ dev: 'https://dev.smplrspace.com/lib/smplr.js',
11
+ local: 'http://localhost:3000/lib/smplr.umd.js',
12
+ },
13
+ esm: {
14
+ prod: 'https://app.smplrspace.com/lib/smplr.mjs',
15
+ dev: 'https://dev.smplrspace.com/lib/smplr.mjs',
16
+ local: 'http://localhost:3000/lib/smplr.mjs',
17
+ },
18
+ css: {
19
+ prod: 'https://app.smplrspace.com/lib/smplr.css',
20
+ dev: 'https://dev.smplrspace.com/lib/smplr.css',
21
+ local: 'http://localhost:3000/lib/style.css',
22
+ },
23
+ }
24
+
25
+ type BundleType = 'esm' | 'umd'
26
+ type Env = 'prod' | 'dev' | 'local'
27
+
28
+ export async function loadSmplrJs(bundle: BundleType = 'esm', env: Env = 'prod'): Promise<Smplr> {
29
+ try {
30
+ // we don't wait for the stylesheet, just start to load it
31
+ loadStylesheet(SMPLR.css[env])
32
+ } catch (e) {
33
+ console.warn('oops')
34
+ // ignore errors, they will be printed anyway
35
+ }
36
+ // load script
37
+ try {
38
+ if (bundle === 'esm') {
39
+ const smplr = (await loadEsmModule(SMPLR.esm[env])) as Smplr
40
+ console.log('loaded esm', smplr)
41
+ return smplr
42
+ } else {
43
+ await loadUmdScript(SMPLR.umd[env])
44
+ const smplr = window.smplr as Smplr
45
+ if (!smplr) {
46
+ throw new Error('Failed to load smplr.js')
47
+ }
48
+ console.log('loaded umd', smplr)
49
+ return smplr
50
+ }
51
+ } catch (error) {
52
+ console.error(error)
53
+ throw new Error('Failed to load smplr.js')
54
+ }
55
+ }
@@ -0,0 +1,21 @@
1
+ export const loadUmdScript = (url: string) => {
2
+ return new Promise((resolve, reject) => {
3
+ try {
4
+ const scriptElement = document.createElement('script')
5
+ scriptElement.type = 'text/javascript'
6
+ scriptElement.async = true
7
+ scriptElement.src = url
8
+ scriptElement.addEventListener('load', () => {
9
+ resolve('ok')
10
+ })
11
+ scriptElement.addEventListener('error', () => {
12
+ reject(`Failed to load the script from ${url}`)
13
+ })
14
+ document.body.appendChild(scriptElement)
15
+ } catch (error) {
16
+ reject(error)
17
+ }
18
+ })
19
+ }
20
+
21
+ export const loadEsmModule = (url: string) => import(url /* @vite-ignore */)
@@ -0,0 +1,22 @@
1
+ export const loadStylesheet = (url: string) => {
2
+ return new Promise((resolve, reject) => {
3
+ try {
4
+ const linkElement = document.createElement('link')
5
+ linkElement.type = 'text/css'
6
+ linkElement.href = url
7
+ linkElement.rel = 'stylesheet'
8
+ linkElement.addEventListener('load', () => {
9
+ resolve('ok')
10
+ })
11
+ linkElement.addEventListener('error', () => {
12
+ const error = new Error(`Failed to load the stylesheet from ${url}`)
13
+ console.error(error)
14
+ reject(error)
15
+ })
16
+ document.head.appendChild(linkElement)
17
+ } catch (error) {
18
+ console.error(error)
19
+ reject(error)
20
+ }
21
+ })
22
+ }
package/src/types.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ // eslint-disable-next-line import/no-unresolved
2
+ import * as smplr from './generated/smplr'
3
+
4
+ export {}
5
+
6
+ export interface Smplr {
7
+ version: typeof smplr.version
8
+ Space: typeof smplr.Space
9
+ }
10
+
11
+ declare global {
12
+ interface Window {
13
+ smplr?: Smplr
14
+ }
15
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "lib": ["DOM", "DOM.Iterable", "ESNext"],
5
+ "allowJs": true,
6
+ "skipLibCheck": true,
7
+ "esModuleInterop": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "strict": true,
10
+ "module": "ESNext",
11
+ "moduleResolution": "Node",
12
+ "resolveJsonModule": true,
13
+ "isolatedModules": true,
14
+ "noEmit": true
15
+ },
16
+ "include": ["src"]
17
+ }