bs-loader
Advanced tools
Comparing version 1.8.0-0 to 1.8.0-1
140
index.js
@@ -0,14 +1,25 @@ | ||
// @flow | ||
const { readBsConfig } = require('read-bsconfig') | ||
const path = require('path') | ||
const os = require('os') | ||
const { readFile, readFileSync } = require('fs') | ||
const { execFile, execFileSync } = require('child_process') | ||
const { exec, execSync } = require('child_process') | ||
const { getOptions } = require('loader-utils') | ||
/*:: import type { WebpackLoaderThis } from 'webpack' */ | ||
let bsb | ||
let bsbCommand | ||
try { | ||
bsb = require.resolve('bs-platform/bin/bsb.exe') | ||
bsbCommand = require.resolve('bs-platform/bin/bsb.exe') | ||
} catch (e) { | ||
bsb = 'bsb' | ||
bsbCommand = `bsb` | ||
} | ||
const bsb = | ||
os.platform() === 'darwin' | ||
? `script -q /dev/null ${bsbCommand} -make-world -color` | ||
: os.platform() === 'linux' | ||
? `script --return -qfc "${bsbCommand} -make-world -color" /dev/null` | ||
: bsbCommand | ||
const outputDir = 'lib' | ||
@@ -23,3 +34,3 @@ const CWD = process.cwd() | ||
const getJsFile = (buildDir, moduleDir, resourcePath, inSource) => { | ||
function jsFilePath(buildDir, moduleDir, resourcePath, inSource) { | ||
const mlFileName = resourcePath.replace(buildDir, '') | ||
@@ -35,24 +46,28 @@ const jsFileName = mlFileName.replace(fileNameRegex, '.js') | ||
const transformSrc = (moduleDir, src) => | ||
moduleDir === 'es6' | ||
? src.replace(es6ReplaceRegex, '$1$3') | ||
: src.replace(commonJsReplaceRegex, '$1$3') | ||
function transformSrc(moduleDir, src) { | ||
const replacer = moduleDir === 'es6' ? es6ReplaceRegex : commonJsReplaceRegex | ||
const runBsb = (buildDir, compilation, callback) => { | ||
if (compilation.__HAS_RUN_BSB__) return callback() | ||
return src.replace(replacer, '$1$3') | ||
} | ||
function runBsb(buildDir, compilation) { | ||
if (compilation.__HAS_RUN_BSB__) return Promise.resolve() | ||
compilation.__HAS_RUN_BSB__ = true | ||
execFile( | ||
bsb, | ||
['-make-world'], | ||
{ maxBuffer: Infinity, cwd: buildDir }, | ||
callback | ||
) | ||
return new Promise((resolve, reject) => { | ||
exec(bsb, { maxBuffer: Infinity, cwd: buildDir }, (err, stdout, stderr) => { | ||
if (err) { | ||
reject(`${stdout.toString()}\n${stderr.toString()}`) | ||
} else { | ||
resolve() | ||
} | ||
}) | ||
}) | ||
} | ||
const runBsbSync = () => { | ||
execFileSync(bsb, ['-make-world'], { stdio: 'pipe' }) | ||
function runBsbSync() { | ||
execSync(bsb, { stdio: 'pipe' }) | ||
} | ||
const getBsbErrorMessages = err => { | ||
function processBsbError(err) { | ||
if (typeof err === 'string') | ||
@@ -68,14 +83,13 @@ return err.match( | ||
const getCompiledFile = (buildDir, compilation, moduleDir, path, callback) => { | ||
runBsb(buildDir, compilation, (err, stdout, stderr) => { | ||
const errorOutput = `${stdout}\n${stderr}` | ||
if (err) return callback(errorOutput, null) | ||
readFile(path, (err, res) => { | ||
if (err) { | ||
callback(err, err) | ||
} else { | ||
const src = transformSrc(moduleDir, res.toString()) | ||
callback(null, src) | ||
} | ||
function getCompiledFile(buildDir, compilation, moduleDir, path) { | ||
return runBsb(buildDir, compilation).then(() => { | ||
return new Promise((resolve, reject) => { | ||
readFile(path, (err, res) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
const src = transformSrc(moduleDir, res.toString()) | ||
resolve(src) | ||
} | ||
}) | ||
}) | ||
@@ -85,3 +99,3 @@ }) | ||
const getCompiledFileSync = (moduleDir, path) => { | ||
function getCompiledFileSync(moduleDir, path) { | ||
try { | ||
@@ -97,4 +111,4 @@ runBsbSync() | ||
const getBsConfigModuleOptions = buildDir => | ||
readBsConfig(buildDir).then(bsconfig => { | ||
function getBsConfigModuleOptions(buildDir) { | ||
return readBsConfig(buildDir).then(bsconfig => { | ||
if (!bsconfig) { | ||
@@ -105,3 +119,3 @@ throw new Error(`bsconfig not found in ${buildDir}`) | ||
if (!bsconfig['package-specs'] || !bsconfig['package-specs'].length) { | ||
resolve({ module: 'js', inSource: false }) | ||
return { module: 'js', inSource: false } | ||
} | ||
@@ -117,2 +131,3 @@ | ||
}) | ||
} | ||
@@ -130,3 +145,3 @@ module.exports = function loader() { | ||
const compiledFilePath = getJsFile( | ||
const compiledFilePath = jsFilePath( | ||
buildDir, | ||
@@ -138,34 +153,33 @@ moduleDir, | ||
getCompiledFile( | ||
buildDir, | ||
this._compilation, | ||
moduleDir, | ||
compiledFilePath, | ||
(err, res) => { | ||
if (err) { | ||
if (err instanceof Error) err = err.toString() | ||
const errorMessages = getBsbErrorMessages(err) | ||
getCompiledFile(buildDir, this._compilation, moduleDir, compiledFilePath) | ||
.then(res => { | ||
callback(null, res) | ||
}) | ||
.catch(err => { | ||
if (err instanceof Error) err = err.toString() | ||
const errorMessages = processBsbError(err) | ||
if (!errorMessages) { | ||
if (!(err instanceof Error)) err = new Error(err) | ||
this.emitError(err) | ||
return callback(err, null) | ||
} | ||
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])) | ||
} | ||
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) | ||
}) | ||
}) | ||
} | ||
module.exports.process = (src, filename) => { | ||
/*:: declare var c: WebpackLoaderThis; module.exports.call(c) */ | ||
module.exports.process = function process( | ||
src /*: string */, | ||
filename /*: string */ | ||
) { | ||
const moduleDir = 'js' | ||
const compiledFilePath = getJsFile(CWD, moduleDir, filename, false) | ||
const compiledFilePath = jsFilePath(CWD, moduleDir, filename, false) | ||
@@ -176,3 +190,3 @@ try { | ||
if (err instanceof Error) err = err.toString() | ||
const bsbErrorMessages = getBsbErrorMessages(err) | ||
const bsbErrorMessages = processBsbError(err) | ||
@@ -179,0 +193,0 @@ if (bsbErrorMessages && bsbErrorMessages.length > 0) { |
{ | ||
"name": "bs-loader", | ||
"version": "1.8.0-0", | ||
"version": "1.8.0-1", | ||
"description": "Bucklescript integration in Webpack", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest" | ||
"test": "flow && jest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/rrdelaney/bs-loader.git" | ||
"url": "git+https://github.com/reasonml-community.git" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"bucklescript", | ||
"reason", | ||
"webpack", | ||
"ocaml", | ||
"reasonml" | ||
], | ||
"files": ["index.js"], | ||
"keywords": ["bucklescript", "reason", "webpack", "ocaml", "reasonml"], | ||
"author": "Ryan Delaney <rrdelaney@outlook.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/rrdelaney/bs-loader/issues" | ||
"url": "https://github.com/reasonml-community/issues" | ||
}, | ||
"homepage": "https://github.com/rrdelaney/bs-loader#readme", | ||
"homepage": "https://github.com/reasonml-community#readme", | ||
"dependencies": { | ||
@@ -34,2 +26,3 @@ "loader-utils": "^1.1.0", | ||
"devDependencies": { | ||
"flow-bin": "^0.54.1", | ||
"jest": "^21.0.1", | ||
@@ -39,11 +32,4 @@ "webpack": "^3.5.4" | ||
"jest": { | ||
"testPathIgnorePatterns": [ | ||
"/node_modules/", | ||
"/test/", | ||
"/example/" | ||
], | ||
"projects": [ | ||
"test/*" | ||
] | ||
"testPathIgnorePatterns": ["/node_modules/", "/example/"] | ||
} | ||
} |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
10316
154
3
2