@ordergroove/smi-serve 1.0.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/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # `smi-serve`
2
+
3
+ > TODO: description
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ const smiServe = require('smi-serve');
9
+
10
+ // TODO: DEMONSTRATE API
11
+ ```
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@ordergroove/smi-serve",
3
+ "version": "1.0.0",
4
+ "description": "Now I’m the model of a modern major general / The venerated Virginian veteran whose men are all / Lining up, to put me up on a pedestal / Writin’ letters to relatives / Embellishin’ my elegance and eloquence / But the elephant is in the room / The truth is in ya face when ya hear the British cannons go / BOOM",
5
+ "keywords": [],
6
+ "author": "Eugenio Lattanzio <eugenio63@gmail.com>",
7
+ "license": "ISC",
8
+ "main": "src/smi-serve.js",
9
+ "directories": {
10
+ "src": "src",
11
+ "test": "__tests__"
12
+ },
13
+ "files": [
14
+ "src"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/ordergroove/plush-toys.git"
19
+ },
20
+ "scripts": {
21
+ "test": "echo \"Error: run tests from root\" && exit 1"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/ordergroove/plush-toys/issues"
25
+ },
26
+ "bin": {
27
+ "smi-serve": "src/smi-serve.js"
28
+ },
29
+ "homepage": "https://github.com/ordergroove/plush-toys#readme",
30
+ "dependencies": {
31
+ "@ordergroove/smi-precompile": "^1.7.1",
32
+ "esbuild": "^0.14.22",
33
+ "esbuild-plugin-less": "^1.1.6",
34
+ "glob": "^7.2.0"
35
+ }
36
+ }
package/src/client.js ADDED
@@ -0,0 +1,33 @@
1
+ import { bootstrap, html } from '@ordergroove/smi-core';
2
+
3
+ export * from '@ordergroove/smi-core';
4
+
5
+ // import mainView from './views/main.liquid';
6
+
7
+ const params = new URLSearchParams(document.location.hash.substring(1) || window.location.search);
8
+
9
+ const auth_merchant_id = params.get('m') || params.get('merchant_id') || params.get('auth')?.split('|')[3];
10
+ const auth_env = params.get('env') || (params.has('prod') && 'prod') || (params.has('staging') && 'staging') || 'prod';
11
+ const template_merchant_id = params.get('t')?.split(',')[0];
12
+ const template_env = params.get('t')?.split(',')[1] || params.get('env') || auth_env;
13
+
14
+ const is_staging = auth_env.startsWith('s');
15
+
16
+ const og = window.og;
17
+
18
+ window.SMI_SERVE_INIT = {
19
+ ...(params.has('auth')
20
+ ? {
21
+ merchant_id: template_merchant_id || auth_merchant_id || '0e5de2bedc5e11e3a2e4bc764e106cf4',
22
+ env: is_staging ? 'staging' : 'prod',
23
+ auth: params.has('auth')
24
+ }
25
+ : {
26
+ merchant_id: '0e5de2bedc5e11e3a2e4bc764e106cf4',
27
+ auth_url: 'https://static-origin-server.ordergroove.com/0e5de2bedc5e11e3a2e4bc764e106cf4/demo/auth.json',
28
+ env: 'staging'
29
+ }),
30
+ ...Object.fromEntries(params)
31
+ };
32
+
33
+ bootstrap({ ...window.SMI_SERVE_INIT }, window.SMI_MAIN_TEMPLATE);
@@ -0,0 +1,157 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require('fs');
4
+ const util = require('util');
5
+ const glob = util.promisify(require('glob'));
6
+ const path = require('path');
7
+ const esbuild = require('esbuild');
8
+ const { lessLoader } = require('esbuild-plugin-less');
9
+ const precompile = require('@ordergroove/smi-precompile');
10
+
11
+ const watchMode = process.argv.includes('--watch');
12
+ const serveMode = process.argv.includes('--serve');
13
+ const prodMode = process.argv.includes('--prod');
14
+
15
+ /**
16
+ * makes lodash, dayjs and reselect resolves to local
17
+ * smi-core improving the bundle size
18
+ * it also replaces lodash with lodash-es that it's smaller with esbuild
19
+ */
20
+
21
+ const resolve_esm = name => {
22
+ const info = require(`${name}/package.json`);
23
+ const mod = `${name}/${info.module || info.main}`;
24
+
25
+ return require.resolve(mod);
26
+ };
27
+ const resolve_smi_core_modules_local = {
28
+ name: 'resolve_smi_core_modules_local',
29
+ setup(build) {
30
+ build.onResolve({ filter: /^lodash\/?/ }, args => ({
31
+ path: require.resolve(args.path.replace(/^lodash/, 'lodash-es'))
32
+ }));
33
+ build.onResolve({ filter: /^dayjs\/?/ }, args => ({
34
+ path: require.resolve(args.path.replace(/^dayjs/, 'dayjs-es'))
35
+ }));
36
+ build.onResolve({ filter: /^(redux|reselect|@?redux-saga)\/?/ }, args => ({ path: resolve_esm(args.path) }));
37
+ }
38
+ };
39
+ const resolve_smi_templates = {
40
+ name: 'resolve_smi_templates',
41
+ setup(build) {
42
+ build.onLoad({ filter: /main\.liquid/ }, async args => {
43
+ const files = await glob(`${process.cwd()}/src/**/*.*`);
44
+ const fileList = await Promise.all(
45
+ files.map(async file => ({
46
+ name: file.substr(process.cwd() + '/src/'.length),
47
+ content: await fs.promises.readFile(file, 'utf8')
48
+ }))
49
+ );
50
+
51
+ const contents = await precompile(fileList);
52
+ return { contents: `export default ${contents};`, loader: 'js' };
53
+ });
54
+ }
55
+ };
56
+
57
+ const buildConf = {
58
+ entryPoints: {
59
+ 'smi-bundle': path.join(__dirname, 'client.js')
60
+ },
61
+ nodePaths: [path.join(process.cwd(), 'node_modules')],
62
+ banner: {
63
+ js: '(()=>{'
64
+ },
65
+ footer: {
66
+ js: `
67
+ window.og = window.og || {}; window.og.smi = window.og.smi || smi;
68
+ if(typeof exports === 'object' && typeof module === 'object') module.exports = smi;
69
+ })();`
70
+ },
71
+ format: 'iife',
72
+ globalName: 'smi',
73
+ watch: watchMode,
74
+ loader: {
75
+ '.css': 'text'
76
+ },
77
+ bundle: true,
78
+ legalComments: 'none',
79
+ sourcemap: true,
80
+ ...(prodMode && {
81
+ minify: true,
82
+ keepNames: true
83
+ }),
84
+ outdir: 'dist/',
85
+ plugins: [resolve_smi_templates, lessLoader()]
86
+ };
87
+
88
+ (async () => {
89
+ let smiPath;
90
+
91
+ try {
92
+ smiPath = require.resolve('@ordergroove/smi-core', { paths: [process.cwd()] });
93
+ } catch (err) {
94
+ console.log('smi path not found');
95
+ }
96
+ const [mainLess] = await glob('**/main.@(less|css)');
97
+ const [mainLiquid] = await glob('**/main.liquid');
98
+
99
+ if (!smiPath) {
100
+ throw new Error("@ordergroove/smi-core can't be found. Please install it via npm");
101
+ }
102
+
103
+ if (mainLess) {
104
+ buildConf.entryPoints.styles = mainLess;
105
+ } else {
106
+ console.warn('styles not found you can create a main.less file under src/styles/main.less');
107
+ }
108
+
109
+ if (!mainLiquid) {
110
+ throw new Error('main.liquid not found please create a main.liquid file under src/views/main.liquid');
111
+ }
112
+
113
+ buildConf.entryPoints.template = mainLiquid;
114
+
115
+ if (serveMode) {
116
+ await esbuild.serve(
117
+ {
118
+ port: 8080,
119
+ servedir: './dist/'
120
+ },
121
+ buildConf
122
+ );
123
+ fs.writeFileSync(
124
+ `${process.cwd()}/dist/index.html`,
125
+ `<html>
126
+ <head>
127
+ <title>SMI Demo Page</title>
128
+ <meta charset="UTF-8" />
129
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
130
+ <link rel="stylesheet" href="styles.css">
131
+ </head>
132
+ <body style="margin: 0;">
133
+ <div id="og-msi"></div>
134
+ <script type="text/javascript" src="template.js"></script>
135
+ <script>SMI_MAIN_TEMPLATE=og.smi.default</script>
136
+ <script type="text/javascript" src="smi-bundle.js"></script>
137
+ </body>
138
+ </html>`
139
+ );
140
+ console.log('Dev server listening on http://localhost:8080');
141
+ return;
142
+ }
143
+
144
+ const result = await esbuild.build({
145
+ metafile: prodMode,
146
+ ...buildConf
147
+ });
148
+
149
+ if (prodMode) {
150
+ const text = await esbuild.analyzeMetafile(result.metafile, {
151
+ verbose: true
152
+ });
153
+ fs.writeFileSync('./dist/bundle-report.html', `<pre id="esbuild-metadata">${text}</pre>`);
154
+ }
155
+ })().catch(err => {
156
+ console.error(err);
157
+ });