@ossy/app 0.1.5 → 0.1.7
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/cli/Api.js +3 -0
- package/cli/build.js +124 -0
- package/cli/client.js +6 -0
- package/cli/server.js +57 -0
- package/cli/temp.js +3 -0
- package/package.json +3 -3
package/cli/Api.js
ADDED
package/cli/build.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import url from 'url';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { rollup } from 'rollup';
|
|
5
|
+
import babel from '@rollup/plugin-babel';
|
|
6
|
+
import { nodeResolve as resolveDependencies } from '@rollup/plugin-node-resolve'
|
|
7
|
+
import resolveCommonJsDependencies from '@rollup/plugin-commonjs'
|
|
8
|
+
import removeOwnPeerDependencies from 'rollup-plugin-peer-deps-external'
|
|
9
|
+
import minifyJS from '@rollup/plugin-terser'
|
|
10
|
+
// import typescript from '@rollup/plugin-typescript'
|
|
11
|
+
import preserveDirectives from "rollup-plugin-preserve-directives"
|
|
12
|
+
import json from "@rollup/plugin-json"
|
|
13
|
+
import copy from 'rollup-plugin-copy';
|
|
14
|
+
import replace from '@rollup/plugin-replace';
|
|
15
|
+
import remove from 'rollup-plugin-delete';
|
|
16
|
+
import arg from 'arg'
|
|
17
|
+
// import inject from '@rollup/plugin-inject'
|
|
18
|
+
|
|
19
|
+
export const build = async (cliArgs) => {
|
|
20
|
+
console.log('[@ossy/cli][app][build] Starting...')
|
|
21
|
+
|
|
22
|
+
const options = arg({
|
|
23
|
+
'--source': String,
|
|
24
|
+
'--s': '--source',
|
|
25
|
+
|
|
26
|
+
'--destination': String,
|
|
27
|
+
'--d': '--destination',
|
|
28
|
+
|
|
29
|
+
'--config': String,
|
|
30
|
+
'-c': '--config',
|
|
31
|
+
}, { argv: cliArgs })
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
const appSourcePath = path.resolve(options['--source'] || 'src/App.jsx');
|
|
35
|
+
let apiSourcePath = path.resolve(options['--api-source'] || 'src/Api.js');
|
|
36
|
+
const configPath = path.resolve(options['--config'] || 'src/config.js');
|
|
37
|
+
const buildPath = path.resolve(options['--destination'] || 'build');
|
|
38
|
+
const publicDir = path.resolve('public')
|
|
39
|
+
|
|
40
|
+
const scriptDir = path.dirname(url.fileURLToPath(import.meta.url))
|
|
41
|
+
const inputClient = path.resolve(scriptDir, 'client.js')
|
|
42
|
+
const inputServer = path.resolve(scriptDir, 'server.js')
|
|
43
|
+
|
|
44
|
+
const inputFiles = [inputClient, inputServer]
|
|
45
|
+
|
|
46
|
+
if (!fs.existsSync(appSourcePath)) {
|
|
47
|
+
throw new Error(`[@ossy/cli][app][build] Source path does not exist: ${appSourcePath}`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!fs.existsSync(apiSourcePath)) {
|
|
51
|
+
apiSourcePath = path.resolve(scriptDir, 'Api.js')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (fs.existsSync(configPath)) {
|
|
55
|
+
inputFiles.push(configPath)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const inputOptions = {
|
|
59
|
+
input: inputFiles,
|
|
60
|
+
plugins: [
|
|
61
|
+
remove({ targets: buildPath }),
|
|
62
|
+
// inject({ 'React': 'react' }),
|
|
63
|
+
replace({
|
|
64
|
+
delimiters: ['%%', '%%'],
|
|
65
|
+
'@ossy/app/source-file': appSourcePath,
|
|
66
|
+
}),
|
|
67
|
+
replace({
|
|
68
|
+
delimiters: ['%%', '%%'],
|
|
69
|
+
'@ossy/api/source-file': apiSourcePath,
|
|
70
|
+
}),
|
|
71
|
+
replace({
|
|
72
|
+
'process.env.NODE_ENV': JSON.stringify('production')
|
|
73
|
+
}),
|
|
74
|
+
json(),
|
|
75
|
+
// removeOwnPeerDependencies(),
|
|
76
|
+
resolveCommonJsDependencies(),
|
|
77
|
+
resolveDependencies({ preferBuiltins: true }),
|
|
78
|
+
babel({
|
|
79
|
+
babelHelpers: 'bundled',
|
|
80
|
+
// exclude: ['**/node_modules/**/*'],
|
|
81
|
+
presets: ['@babel/preset-env', '@babel/preset-react']
|
|
82
|
+
}),
|
|
83
|
+
// preserveDirectives(),
|
|
84
|
+
minifyJS(),
|
|
85
|
+
copy({
|
|
86
|
+
targets: [
|
|
87
|
+
fs.existsSync(publicDir)
|
|
88
|
+
? { src: `${publicDir}/**/*`, dest: 'build/public' }
|
|
89
|
+
: undefined,
|
|
90
|
+
].filter(x => !!x)
|
|
91
|
+
})
|
|
92
|
+
],
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const outputOptions = [
|
|
96
|
+
{
|
|
97
|
+
dir: 'build',
|
|
98
|
+
// preserveModules: true,
|
|
99
|
+
entryFileNames: ({ name }) => {
|
|
100
|
+
if (name === 'server') {
|
|
101
|
+
return '[name].js'
|
|
102
|
+
} else if (name === 'Api') {
|
|
103
|
+
return '[name].js'
|
|
104
|
+
} else if (name === 'client') {
|
|
105
|
+
return 'public/static/main.js'
|
|
106
|
+
} else if (name === 'config') {
|
|
107
|
+
return 'public/static/[name].js'
|
|
108
|
+
} else {
|
|
109
|
+
return 'public/static/[name].js'
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
chunkFileNames: 'public/static/[name]-[hash].js',
|
|
113
|
+
format: 'esm',
|
|
114
|
+
}
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
const bundle = await rollup(inputOptions);
|
|
118
|
+
|
|
119
|
+
for (const options of outputOptions) {
|
|
120
|
+
await bundle.write(options);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
console.log('[@ossy/cli][app][build] Finished');
|
|
124
|
+
};
|
package/cli/client.js
ADDED
package/cli/server.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import url from 'url'
|
|
3
|
+
import React, { createElement } from 'react';
|
|
4
|
+
import express from 'express';
|
|
5
|
+
import { prerenderToNodeStream } from 'react-dom/static'
|
|
6
|
+
import App from '%%@ossy/app/source-file%%'
|
|
7
|
+
import ApiRouter from '%%@ossy/api/source-file%%'
|
|
8
|
+
|
|
9
|
+
const app = express();
|
|
10
|
+
|
|
11
|
+
const currentDir = path.dirname(url.fileURLToPath(import.meta.url))
|
|
12
|
+
const ROOT_PATH = path.resolve(currentDir, 'public')
|
|
13
|
+
|
|
14
|
+
console.log('[@ossy/cli][app][server] ROOT_PATH: ', ROOT_PATH)
|
|
15
|
+
|
|
16
|
+
app.use(express.static(ROOT_PATH));
|
|
17
|
+
|
|
18
|
+
ApiRouter && app.use('/api', ApiRouter)
|
|
19
|
+
|
|
20
|
+
app.get('*', (req, res) => {
|
|
21
|
+
|
|
22
|
+
console.log('[@ossy/cli][app][server] req.url: ', req.url)
|
|
23
|
+
|
|
24
|
+
renderToString(App, { url: req.url })
|
|
25
|
+
.then(html => { res.send(html) })
|
|
26
|
+
.catch(err => { res.send(err) })
|
|
27
|
+
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
app.listen(3000, () => {
|
|
31
|
+
console.log('Server is running on http://localhost:3000');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
async function renderToSteam(App, config) {
|
|
35
|
+
|
|
36
|
+
return prerenderToNodeStream(createElement(App, config), {
|
|
37
|
+
bootstrapModules: ['/static/index.js']
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function renderToString(App, config) {
|
|
43
|
+
|
|
44
|
+
const { prelude } = await prerenderToNodeStream(createElement(App, config), {
|
|
45
|
+
bootstrapModules: ['/static/main.js']
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
let data = '';
|
|
50
|
+
prelude.on('data', chunk => {
|
|
51
|
+
data += chunk;
|
|
52
|
+
});
|
|
53
|
+
prelude.on('end', () => resolve(data));
|
|
54
|
+
prelude.on('error', reject);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
}
|
package/cli/temp.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ossy/app",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "",
|
|
5
5
|
"source": "./src/index.js",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"rollup-plugin-preserve-directives": "^0.4.0"
|
|
45
45
|
},
|
|
46
46
|
"files": [
|
|
47
|
-
"/
|
|
47
|
+
"/cli",
|
|
48
48
|
"README.md"
|
|
49
49
|
],
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "0d479b1b153efa36a487a3c007a43ffa86950aff"
|
|
51
51
|
}
|