forked from microsoft/fluentui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal-codepen.js
45 lines (40 loc) · 1.86 KB
/
local-codepen.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
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const path = require('path');
const fs = require('fs');
const yargs = require('yargs');
const execSync = require('./exec-sync');
const { findGitRoot } = require('./monorepo/index');
const options = yargs.option('webpackConfig', { alias: 'w', type: 'string' }).argv;
const webpackConfigFilePath = options.webpackConfig || 'webpack.codepen.config.js';
const configPath = path.resolve(process.cwd(), webpackConfigFilePath);
const gitRoot = findGitRoot();
if (fs.existsSync(configPath)) {
let ngrok;
try {
console.log("Attempting to npm link globally installed ngrok so it can be require'd");
// This will probably install ngrok globally if it's not already present
execSync('npm link ngrok@3', undefined, gitRoot);
ngrok = require('ngrok');
} catch (err) {
// ngrok has a postbuild step which was slowing down yarn install, so it's been removed
// from the repo dependency list (since this script is the only place it's used)
console.error('This script requires a global install of ngrok: "npm i -g ngrok@3"');
process.exit(1);
}
const webpackConfig = require(configPath);
const compiler = webpack(webpackConfig);
const server = new WebpackDevServer(compiler, webpackConfig.devServer);
const port = webpackConfig.devServer.port || 8080;
server.listen(port, '127.0.0.1', async () => {
const url = await ngrok.connect({ port, host_header: 'localhost:' + port });
console.log(`Starting server on http://${url}`);
console.log(
`Add this to CodePen:
<script type="text/javascript" src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script type="text/javascript" src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script type="text/javascript" src="${url}/fluentui-react.js"></script>
`,
);
});
}