forked from FormidableLabs/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone-page.js
204 lines (174 loc) · 6.24 KB
/
one-page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'use strict';
/**
* Generate the JS `index.html` from `examples/js/index.js`
*/
const fs = require('fs').promises;
const path = require('path');
const { transformFileAsync } = require('@babel/core');
const pretty = require('pretty');
// Paths
const EXAMPLES = path.resolve(__dirname, '../..');
const SPECTACLE_PATH = path.resolve(__dirname, '../../../packages/spectacle');
const SRC_FILE = path.join(EXAMPLES, 'js/index.js');
const DEST_FILE = path.join(EXAMPLES, 'one-page/index.html');
// Dependencies.
const ESM_SH_VERSION = 'v121'; // v121, stable, etc.
const {
dependencies,
peerDependencies
} = require(`${SPECTACLE_PATH}/package.json`);
const reactPkgPath = require.resolve('react/package.json', {
paths: [SPECTACLE_PATH]
});
const { version: reactVersion } = require(reactPkgPath);
const DEPS = `deps=react@${reactVersion}`;
// Toggle dev resources. (Use if debugging load / dependency errors).
const IS_DEV = false;
const DEV = IS_DEV ? '&dev' : '';
// Use local built spectacle? Toggle to `true` for dev-only.
// Note: Due to CORS, you'll need to run `pnpm run --filter ./examples/one-page start` and
// open http://localhost:5000/examples/one-page to work.
const USE_LOCAL = false;
// ================================================================================================
// Import Map
// ================================================================================================
const importUrl = (k, v, extra = '') => {
// Pin react and react-dom.
if (k === 'react' || k === 'react-dom') {
v = reactVersion;
}
return `https://esm.sh/${ESM_SH_VERSION}/${k}@${v}${extra}?${DEPS}${DEV}`;
};
const getImportMap = () => {
// Start with extra imports for one-page alone.
const importMap = {
htm: importUrl('htm', '^3'),
spectacle: USE_LOCAL
? '../../packages/spectacle/lib/index.mjs'
: 'https://esm.sh/spectacle@10?bundle'
};
Object.entries(Object.assign({}, dependencies, peerDependencies))
.sort((a, b) => a[0].localeCompare(b[0]))
.forEach(([k, v]) => {
// General
importMap[k] = importUrl(k, v);
// Special case internal deps
if (k === 'react') {
importMap[`${k}/jsx-runtime`] = importUrl(k, v, '/jsx-runtime');
}
if (k === 'react-syntax-highlighter') {
importMap[`${k}/dist/cjs/styles/prism/vs-dark.js`] = importUrl(
k,
v,
'/dist/esm/styles/prism/vs-dark.js'
);
importMap[`${k}/dist/cjs/styles/prism/index.js`] = importUrl(
k,
v,
'/dist/esm/styles/prism/index.js'
);
}
});
return importMap;
};
// ================================================================================================
// Rewriting
// ================================================================================================
const htmImport = `
import htm from 'htm';
const html = htm.bind(React.createElement);
`
.replace(/ /gm, '')
.trim();
const spectacleImportReplacer = (match, imports) => {
// Prettify imports
imports = imports
.split(',')
.map((i) => ` ${i.trim()}`)
.join(`,\n`);
return `import {\n${imports}\n} from 'spectacle';\n\n${htmImport}`;
};
const getSrcContent = async (src) => {
let { code } = await transformFileAsync(src, {
babelrc: false,
configFile: false,
plugins: ['babel-plugin-transform-jsx-to-htm']
});
// Mutate exports and comments.
code = code
// Mutate exports to our global imports.
.replace(/import {[ ]*(.*)} from 'spectacle';/, spectacleImportReplacer)
// Hackily fix / undo babel's poor control comment placment.
.replace(/\/\/ SPECTACLE_CLI/gm, '\n// SPECTACLE_CLI')
.replace(/(\/\/ SPECTACLE_CLI[^\n]*)[\n]{2}/gm, '$1\n')
.replace(/(\/\/ SPECTACLE_CLI[^\n]*_START)/gm, '\n$1');
// Beautify htm snippets.
code = code.replace(
/(html`)(<\${[\s\S]*?}>)(`;)/gm,
(match, open, htm, close) => {
// Initial cleanup for inline strings and functions
htm = htm.replace(/>\${/gm, '>\n${').replace(/}<\/\${/gm, '}\n</${');
// Protect indentation of inline strings by temporarily wrapping in <pre>
htm = htm
.replace(/\n\${`/gm, '\n<pre>SPECTACLE_ONE_PAGE_TEMP_MARKER${`')
.replace(/`}\n/gm, '`}SPECTACLE_ONE_PAGE_TEMP_MARKER</pre>\n');
// Make the HTML pretty:
htm = pretty(htm).replace(
/<pre>SPECTACLE_ONE_PAGE_TEMP_MARKER|SPECTACLE_ONE_PAGE_TEMP_MARKER<\/pre>/g,
''
);
// Final tweaks:
htm = `${open}${htm}${close}`
// Initial newline
.replace('html`<${', 'html`\n<${')
// Indent
.split('\n')
.join('\n ')
// Handle pretty() erroneous newline after string literal
.replace(/\${\"\n[ ]*/gm, '${"')
// Final newline
.replace('}>`;', '}>\n`;');
return htm;
}
);
return code;
};
// ================================================================================================
// Output
// ================================================================================================
const writeDestContent = async (destFile, code) => {
// Format for indentation in index.html.
const indent = ' ';
code = `${indent}${code.split('\n').join(`\n${indent}`)}`;
// Import map
let importMap = JSON.stringify({ imports: getImportMap() }, null, 2);
importMap = `${indent}${importMap.split('\n').join(`\n${indent}`)}`;
// Get destination content.
let destContent = (await fs.readFile(destFile)).toString();
// Mutate in our updated code.
destContent = destContent
.replace(
/(<script type="importmap">\n)[\s\S]*?(\n[ ]*<\/script>)/m,
(match, open, close) => `${open}${importMap}${close}`
)
.replace(
/(<script type="module">\n)[\s\S]*?(\n[ ]*<\/script>\n[ ]*<\/body>\n[ ]*<\/html>)/m,
(match, open, close) => `${open}${code}${close}`
)
// Trim trailing spaces
.split('\n')
.map((line) => line.trimRight())
.join('\n');
// Update one-page
await fs.writeFile(destFile, destContent);
};
const main = async () => {
const code = await getSrcContent(SRC_FILE);
await writeDestContent(DEST_FILE, code);
};
if (require.main === module) {
main().catch((err) => {
console.error(err); // eslint-disable-line no-console
process.exit(1); // eslint-disable-line no-process-exit
});
}