Skip to content

Commit

Permalink
Merge pull request #10 from rickiesmooth/master
Browse files Browse the repository at this point in the history
Migrate to v2
  • Loading branch information
jtberglund authored Oct 12, 2018
2 parents 9f42865 + 9d420d4 commit 7f7b68b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 54 deletions.
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": false,
"singleQuote": true,
"trailingComma": "es5"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-plugin-reason",
"version": "1.3.0",
"version": "2.0.0",
"description": "Gatsby plugin so you can write your site in ReasonML!",
"main": "index.js",
"scripts": {
Expand Down
127 changes: 74 additions & 53 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,88 @@
import { compileFileSync } from 'bsb-js';
import { compileFileSync } from 'bsb-js'

import fs from 'fs';
import { getPathForComponent } from './utils';
import path from 'path';
import fs from 'fs'
import { getPathForComponent } from './utils'
import path from 'path'

const BS_TEST = /\.(bs.js)$/;
const REASON_TEST = /\.(ml|re)$/;
const BS_TEST = /\.(bs.js)$/
const REASON_TEST = /\.(ml|re)$/

const isCompiledFile = fileName => BS_TEST.test(fileName);
const isReasonFile = fileName => REASON_TEST.test(fileName);
const isCompiledFile = fileName => BS_TEST.test(fileName)
const isReasonFile = fileName => REASON_TEST.test(fileName)

export const modifyWebpackConfig = ({ config }) =>
config.loader('reason', {
test: REASON_TEST,
loader: 'bs-loader'
});
export const onCreateWebpackConfig = ({ stage, actions }) => {
const { setWebpackConfig } = actions
setWebpackConfig({
module: {
rules: [
{
test: REASON_TEST,
use: [`bs-loader`],
},
],
},
})
}

const jsFilePath = (buildDir, moduleDir, resourcePath, inSource, bsSuffix) => {
const mlFileName = resourcePath.replace(buildDir, '');
const jsFileName = mlFileName.replace(REASON_TEST, bsSuffix);
const mlFileName = resourcePath.replace(buildDir, '')
const jsFileName = mlFileName.replace(REASON_TEST, bsSuffix)

if (inSource) {
return path.join(buildDir, jsFileName);
}
if (inSource) {
return path.join(buildDir, jsFileName)
}

return path.join(buildDir, 'lib', moduleDir, jsFileName);
};
return path.join(buildDir, 'lib', moduleDir, jsFileName)
}

export const preprocessSource = ({ filename }) => {
if (!isReasonFile(filename)) {
return null;
}
const moduleDir = 'js';
const compiledFilePath = jsFilePath(process.cwd(), moduleDir, filename, false, '.bs.js');
try {
return compileFileSync(moduleDir, compiledFilePath);
} catch (e) {
// Don't need to print error message since bsb will already do that
}
};
if (!isReasonFile(filename)) {
return null
}
const moduleDir = 'js'
const compiledFilePath = jsFilePath(
process.cwd(),
moduleDir,
filename,
false,
'.bs.js'
)
try {
return compileFileSync(moduleDir, compiledFilePath)
} catch (e) {
// Don't need to print error message since bsb will already do that
}
}

export const resolvableExtensions = () => ['.ml', '.re'];
export const resolvableExtensions = () => ['.ml', '.re']

export const onCreatePage = ({ page, boundActionCreators: { createPage, deletePage } }, { derivePathFromComponentName }) => {
return new Promise((resolve, reject) => {
const oldPage = Object.assign({}, page);
const { component, path } = page;
export const onCreatePage = (
{ page, boundActionCreators: { createPage, deletePage } },
{ derivePathFromComponentName }
) => {
return new Promise((resolve, reject) => {
const oldPage = Object.assign({}, page)
const { component, path } = page

if (isCompiledFile(component)) {
// Remove .bs components so we don't have duplicates
deletePage(oldPage);
} else if (derivePathFromComponentName && isReasonFile(component) && !path.endsWith('.html')) {
// Try to grab the name of the component from the ReasonReact
// component instead of using the file name
const source = fs.readFileSync(component, 'utf-8');
const newPath = getPathForComponent(source);
if (newPath !== undefined) {
const newPage = Object.assign({}, page, { path: newPath });
deletePage(oldPage);
createPage(newPage);
}
}
if (isCompiledFile(component)) {
// Remove .bs components so we don't have duplicates
deletePage(oldPage)
} else if (
derivePathFromComponentName &&
isReasonFile(component) &&
!path.endsWith('.html')
) {
// Try to grab the name of the component from the ReasonReact
// component instead of using the file name
const source = fs.readFileSync(component, 'utf-8')
const newPath = getPathForComponent(source)
if (newPath !== undefined) {
const newPage = Object.assign({}, page, { path: newPath })
deletePage(oldPage)
createPage(newPage)
}
}

resolve();
});
};
resolve()
})
}

0 comments on commit 7f7b68b

Please sign in to comment.