Skip to content

Commit

Permalink
fix(bundle): client and server compatibility (#20)
Browse files Browse the repository at this point in the history
* fix(bundle): making syntax highlighter compatible on client and server
- Exposing modes and getmode as core exports

* chore: documentation
  • Loading branch information
gratcliff authored Oct 5, 2020
1 parent e3c0c67 commit bd4362b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 32 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "@readme/syntax-highlighter",
"description": "ReadMe's React-based syntax highlighter",
"version": "9.0.1",
"main": "dist/index.js",
"main": "dist/index.node.js",
"browser": "dist/index.js",
"dependencies": {
"@readme/variable": "^7.2.1",
"codemirror": "^5.48.2",
Expand Down
1 change: 1 addition & 0 deletions src/codeEditor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import { Controlled as CodeMirror } from 'react-codemirror2';
import { getMode } from '../utils/modes';
import defaults from './cm.options';
import '../utils/cm-mode-imports';
import './style.scss';

const CodeEditor = ({ className, code, lang, options, children, ...attr }) => {
Expand Down
1 change: 1 addition & 0 deletions src/codeMirror/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
import Variable, { VARIABLE_REGEXP } from '@readme/variable';
import { getMode } from '../utils/modes';

import '../utils/cm-mode-imports';
import './style.scss';
import 'codemirror/addon/runmode/runmode';
import 'codemirror/mode/meta';
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import codemirror from './codeMirror';
import codeEditor from './codeEditor';
import uppercase from './utils/uppercase';
import canonical from './utils/canonical';
import { modes } from './utils/modes';

const SyntaxHighlighter = (
code,
Expand Down Expand Up @@ -33,4 +34,4 @@ const SyntaxHighlighter = (
};

export default SyntaxHighlighter;
export { uppercase, canonical };
export { uppercase, canonical, modes };
5 changes: 5 additions & 0 deletions src/index.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import uppercase from './utils/uppercase';
import canonical from './utils/canonical';
import { modes, getMode } from './utils/modes';

export { uppercase, canonical, modes, getMode };
24 changes: 24 additions & 0 deletions src/utils/cm-mode-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Codemirror-specfic mode-integrations, used for styling syntax
require('codemirror/mode/clike/clike');
require('codemirror/mode/clojure/clojure');
require('codemirror/mode/d/d');
require('codemirror/mode/dart/dart');
require('codemirror/mode/diff/diff');
require('codemirror/mode/dockerfile/dockerfile');
require('codemirror/mode/erlang/erlang');
require('codemirror/mode/go/go');
require('codemirror/mode/groovy/groovy');
require('codemirror/mode/htmlmixed/htmlmixed');
require('codemirror/mode/http/http');
require('codemirror/mode/javascript/javascript');
require('codemirror/mode/julia/julia');
require('codemirror/mode/perl/perl');
require('codemirror/mode/php/php');
require('codemirror/mode/powershell/powershell');
require('codemirror/mode/python/python');
require('codemirror/mode/ruby/ruby');
require('codemirror/mode/rust/rust');
require('codemirror/mode/shell/shell');
require('codemirror/mode/sql/sql');
require('codemirror/mode/swift/swift');
require('codemirror/mode/yaml/yaml');
25 changes: 0 additions & 25 deletions src/utils/modes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,6 @@
// There are 2 types of lookup, single and array. e.g. `html` needs to be rendered using
// `htmlmixed`, but `java`, needs to be rendered using the `clike` mode.
//
// We also have the mimeType to potentially in future load in new types dynamically.
require('codemirror/mode/clike/clike');
require('codemirror/mode/clojure/clojure');
require('codemirror/mode/d/d');
require('codemirror/mode/dart/dart');
require('codemirror/mode/diff/diff');
require('codemirror/mode/dockerfile/dockerfile');
require('codemirror/mode/erlang/erlang');
require('codemirror/mode/go/go');
require('codemirror/mode/groovy/groovy');
require('codemirror/mode/htmlmixed/htmlmixed');
require('codemirror/mode/http/http');
require('codemirror/mode/javascript/javascript');
require('codemirror/mode/julia/julia');
require('codemirror/mode/perl/perl');
require('codemirror/mode/php/php');
require('codemirror/mode/powershell/powershell');
require('codemirror/mode/python/python');
require('codemirror/mode/ruby/ruby');
require('codemirror/mode/rust/rust');
require('codemirror/mode/shell/shell');
require('codemirror/mode/sql/sql');
require('codemirror/mode/swift/swift');
require('codemirror/mode/yaml/yaml');

const modes = {
asp: 'clike',
aspx: 'clike',
Expand Down
28 changes: 23 additions & 5 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const TerserPlugin = require('terser-webpack-plugin');
const path = require('path');

module.exports = {
entry: ['./src/index.js'],
const base = {
mode: 'production',
module: {
rules: [
Expand Down Expand Up @@ -41,12 +40,31 @@ module.exports = {
umd: 'react-dom',
},
},
resolve: {
extensions: ['.js', '.jsx'],
},
};

const serverConfig = {
...base,
target: 'node',
entry: ['./src/index.node.js'],
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
filename: 'index.node.js',
libraryTarget: 'commonjs2',
},
resolve: {
extensions: ['.js', '.jsx'],
};

const clientConfig = {
...base,
target: 'web',
entry: ['./src/index.js'],
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
libraryTarget: 'commonjs2',
},
};

module.exports = [serverConfig, clientConfig];

0 comments on commit bd4362b

Please sign in to comment.