Skip to content
This repository has been archived by the owner on Dec 3, 2018. It is now read-only.

Commit

Permalink
Merge pull request #27 from reasonml-community/use-bsconfig-for-settings
Browse files Browse the repository at this point in the history
Read bsconfig instead of webpack options
  • Loading branch information
rrdelaney authored Sep 18, 2017
2 parents 0feddd3 + a7c9f97 commit b228152
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 33 deletions.
86 changes: 55 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { readBsConfig } = require('read-bsconfig')
const path = require('path')
const { readFile, readFileSync } = require('fs')
const { execFile, execFileSync } = require('child_process')
Expand Down Expand Up @@ -89,47 +90,70 @@ const getCompiledFileSync = (moduleDir, path) => {
return transformSrc(moduleDir, res.toString())
}

const getBsConfigModuleOptions = buildDir =>
readBsConfig(buildDir).then(bsconfig => {
if (!bsconfig) {
throw new Error(`bsconfig not found in ${buildDir}`)
}

if (!bsconfig['package-specs'] || !bsconfig['package-specs'].length) {
resolve({ module: 'js', inSource: false })
}

const moduleSpec = bsconfig['package-specs'][0]
const moduleDir =
typeof moduleSpec === 'string' ? moduleSpec : moduleSpec.module
const inSource =
typeof moduleSpec === 'string' ? false : moduleSpec['in-source']

return { moduleDir, inSource }
})

module.exports = function loader() {
const options = getOptions(this) || {}
const buildDir = options.cwd || CWD
const moduleDir = options.module || 'js'
const inSourceBuild = options.inSource || false
const callback = this.async()

this.addContextDependency(this.context)
const callback = this.async()
const compiledFilePath = getJsFile(
buildDir,
moduleDir,
this.resourcePath,
inSourceBuild
)

getCompiledFile(
buildDir,
this._compilation,
moduleDir,
compiledFilePath,
(err, res) => {
if (err) {
if (err instanceof Error) err = err.toString()
const errorMessages = getBsbErrorMessages(err)
getBsConfigModuleOptions(buildDir).then(bsconfig => {
const moduleDir = options.module || bsconfig.moduleDir || 'js'
const inSourceBuild = options.inSource || bsconfig.inSource || false

if (!errorMessages) {
if (!(err instanceof Error)) err = new Error(err)
this.emitError(err)
return callback(err, null)
}
const compiledFilePath = getJsFile(
buildDir,
moduleDir,
this.resourcePath,
inSourceBuild
)

for (let i = 0; i < errorMessages.length - 1; ++i) {
this.emitError(new Error(errorMessages[i]))
getCompiledFile(
buildDir,
this._compilation,
moduleDir,
compiledFilePath,
(err, res) => {
if (err) {
if (err instanceof Error) err = err.toString()
const errorMessages = getBsbErrorMessages(err)

if (!errorMessages) {
if (!(err instanceof Error)) err = new Error(err)
this.emitError(err)
return callback(err, null)
}

for (let i = 0; i < errorMessages.length - 1; ++i) {
this.emitError(new Error(errorMessages[i]))
}

callback(new Error(errorMessages[errorMessages.length - 1]), null)
} else {
callback(null, res)
}

callback(new Error(errorMessages[errorMessages.length - 1]), null)
} else {
callback(null, res)
}
}
)
)
})
}

module.exports.process = (src, filename) => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"homepage": "https://github.com/rrdelaney/bs-loader#readme",
"dependencies": {
"loader-utils": "^1.1.0"
"loader-utils": "^1.1.0",
"read-bsconfig": "^1.0.1"
},
"devDependencies": {
"jest": "^21.0.1",
Expand Down
10 changes: 10 additions & 0 deletions test/no-config/bsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "insource-test",
"sources": ["fixtures"],
"package-specs": [
{
"module": "es6",
"in-source": true
}
]
}
13 changes: 13 additions & 0 deletions test/no-config/fixtures/add.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/no-config/fixtures/add.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let add x y => x + y;
1 change: 1 addition & 0 deletions test/no-config/fixtures/dec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const dec = x => x - 1;
31 changes: 31 additions & 0 deletions test/no-config/fixtures/fib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/no-config/fixtures/fib.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
external dec : int -> int = "dec" [@@bs.module "./dec"]
open Add

let fib n =
let rec aux n a b =
if n = 0 then a
else
aux (dec n) b (add a b)
in aux n 1 1
1 change: 1 addition & 0 deletions test/no-config/fixtures/fib.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val fib : int -> int
6 changes: 6 additions & 0 deletions test/no-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "no-config-test",
"private": true,
"version": "100.0.0",
"license": "MIT"
}
45 changes: 45 additions & 0 deletions test/no-config/webpack.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const webpack = require('webpack')
const path = require('path')
const fs = require('fs')

const output = path.join(__dirname, 'output', 'webpack')
const loader = path.join(__dirname, '..', '..')

const baseConfig = {
entry: path.join(__dirname, 'fixtures/fib.ml'),
module: {
rules: [
{
test: /\.(re|ml)$/,
use: {
loader,
options: {
cwd: __dirname
}
}
}
]
},
resolve: {
extensions: ['.re', '.ml', '.js']
},
output: {
path: output,
libraryTarget: 'commonjs2'
}
}

it('runs', done => {
webpack(baseConfig, err => {
expect(err).toBeNull()

fs.readdir(output, (err, files) => {
expect(err).toBeNull()
expect(files.length).toBe(1)
const result = require(path.resolve(output, files[0]))
expect(result.fib(12)).toBe(233)

done()
})
})
})
1 change: 0 additions & 1 deletion test/simple/webpack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const baseConfig = {
use: {
loader,
options: {
module: 'es6',
cwd: __dirname
}
}
Expand Down

0 comments on commit b228152

Please sign in to comment.