Skip to content

Commit cecb28b

Browse files
CLI is working! Using resolved paths for wp deps
1 parent 932285b commit cecb28b

File tree

6 files changed

+132
-25
lines changed

6 files changed

+132
-25
lines changed

bin/actions.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const webpack = require('webpack');
2+
const WebpackDevServer = require('webpack-dev-server');
3+
const path = require('path');
4+
const config = require('../webpack.config');
5+
6+
const port = 3000;
7+
8+
const options = {
9+
hot: true
10+
};
11+
12+
const launchServer = (configUpdates = {}) => {
13+
const customConfig = { ...config, ...configUpdates };
14+
const server = new WebpackDevServer(webpack(customConfig), options);
15+
16+
server.listen(port, 'localhost', function(err) {
17+
if (err) {
18+
console.log(err);
19+
}
20+
console.log('WebpackDevServer listening at localhost:', port);
21+
});
22+
};
23+
24+
const launchMDXServer = mdxFilePath => {
25+
if (!mdxFilePath) {
26+
// developer error - must supply an entry file path
27+
throw new Error('MDX file path must be provided.');
28+
}
29+
30+
const cliRoot = path.resolve(__dirname, '..');
31+
const absoluteMdxFilePath = path.resolve(mdxFilePath);
32+
const nodeModules = path.resolve(__dirname, '../node_modules');
33+
34+
const configUpdates = {
35+
mode: 'development',
36+
context: cliRoot,
37+
entry: './mdx-slides/index.js',
38+
resolve: {
39+
alias: {
40+
'spectacle-user-mdx': absoluteMdxFilePath
41+
},
42+
modules: [nodeModules]
43+
}
44+
};
45+
46+
launchServer(configUpdates);
47+
};
48+
49+
module.exports = {
50+
launchMDXServer
51+
};

bin/args.js

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
const yargs = require('yargs');
2-
const fs = require('fs');
32

43
// Validate and normalize.
54
const validate = parser => {
65
const { argv } = parser;
76
const { mdx } = argv;
87

9-
return Promise.resolve().then(() =>
10-
new Promise((resolve, reject) => {
11-
fs.readFile(mdx, (err, data) => {
12-
if (err) {
13-
reject(err);
14-
} else {
15-
resolve(data);
16-
}
17-
});
18-
}).then(buf => buf.toString('utf8'))
19-
);
8+
return Promise.resolve({ mdx });
209
};
2110

2211
const args = () =>

bin/cli.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
#!/usr/bin/env node
22

3-
const parse = require('./args').parse;
3+
const args = require('./args');
4+
const actions = require('./actions');
5+
var path = require('path');
46

57
const main = () =>
68
Promise.resolve()
79
// Parse arguments.
8-
.then(parse)
9-
.then(out => {
10-
console.log(out);
10+
.then(args.parse)
11+
.then(parsedInput => {
12+
const mdxFilePath = parsedInput.mdx;
13+
if (mdxFilePath) {
14+
actions.launchMDXServer(mdxFilePath);
15+
} else {
16+
throw new Error('Unsupported action.');
17+
}
1118
})
1219
.catch(err => {
1320
// Try to get full stack, then full string if not.
1421
console.error(err.stack || err.toString());
15-
1622
process.exit(1);
1723
});
1824

examples/MDX/slides.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Test from './test-component';
22
export const testProp = 50;
33

4-
# Hello World
4+
# Hello World 👋
55

66
- one
77
- two

mdx-slides/index.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
import { render } from 'react-dom';
3+
import { MDXProvider } from '@mdx-js/react';
4+
import Deck from '../src/components/Deck';
5+
import Slide from '../src/components/Slide';
6+
7+
// See the webpack config to see how this import alias is made
8+
import slides from 'spectacle-user-mdx';
9+
10+
const components = {};
11+
12+
const MDXSlides = () => (
13+
<MDXProvider components={components}>
14+
<Deck loop>
15+
{slides.map((S, i) => (
16+
<Slide key={`slide-${i}`} slideNum={i}>
17+
<S />
18+
</Slide>
19+
))}
20+
</Deck>
21+
</MDXProvider>
22+
);
23+
24+
render(<MDXSlides />, document.getElementById('root'));

webpack.config.js

+44-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
const path = require('path');
22
const HtmlWebpackPlugin = require('html-webpack-plugin');
33

4+
/*
5+
* In order for the CLI to find loaders and babel configurations when
6+
* launching from another directory, we have to import the dependencies
7+
* explicitly from node_modules. Under the same circumstances, webpack
8+
* can not find the .babelrc file and so we must configure babel directly
9+
* in the webpack configuration.
10+
*/
11+
const babelLoader = path.resolve(__dirname, 'node_modules', 'babel-loader');
12+
const presetEnv = path.resolve(__dirname, 'node_modules', '@babel/preset-env');
13+
const presetReact = path.resolve(
14+
__dirname,
15+
'node_modules',
16+
'@babel/preset-react'
17+
);
18+
const pluginObjectSpread = path.resolve(
19+
__dirname,
20+
'node_modules',
21+
'@babel/plugin-proposal-object-rest-spread'
22+
);
23+
const pluginClassProperties = path.resolve(
24+
__dirname,
25+
'node_modules',
26+
'@babel/plugin-proposal-class-properties'
27+
);
28+
29+
const mdxSlideLoader = path.resolve(__dirname, 'mdx-slide-loader');
30+
31+
const babelConfigOptions = {
32+
babelrc: false,
33+
presets: [require.resolve(presetEnv), require.resolve(presetReact)],
34+
plugins: [
35+
require.resolve(pluginObjectSpread),
36+
require.resolve(pluginClassProperties)
37+
]
38+
};
39+
440
module.exports = {
541
entry: './index.js',
642
output: {
@@ -10,19 +46,20 @@ module.exports = {
1046
module: {
1147
rules: [
1248
{
13-
test: /\.js$/,
49+
test: /\.jsx?$/,
1450
use: {
15-
loader: 'babel-loader',
16-
options: {
17-
presets: ['@babel/preset-env']
18-
}
51+
loader: require.resolve(babelLoader),
52+
options: babelConfigOptions
1953
}
2054
},
2155
{
2256
test: /\.mdx$/,
2357
use: [
24-
{ loader: 'babel-loader' },
25-
{ loader: require.resolve('./mdx-slide-loader') }
58+
{
59+
loader: require.resolve(babelLoader),
60+
options: babelConfigOptions
61+
},
62+
{ loader: require.resolve(mdxSlideLoader) }
2663
]
2764
}
2865
]

0 commit comments

Comments
 (0)