-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from rickiesmooth/master
Migrate to v2
- Loading branch information
Showing
3 changed files
with
80 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"semi": false, | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
} |