Comparing version 1.8.2 to 2.0.0-beta1
193
index.js
@@ -5,38 +5,9 @@ // @flow | ||
const path = require('path') | ||
const os = require('os') | ||
const { readFile, readFileSync } = require('fs') | ||
const { exec, execSync } = require('child_process') | ||
const { getOptions } = require('loader-utils') | ||
const { compileFile, compileFileSync } = require('bsb-js') | ||
/*:: import type { BsModuleFormat } from 'read-bsconfig' */ | ||
/*:: import type { WebpackLoaderThis } from 'webpack' */ | ||
let bsbCommand | ||
try { | ||
bsbCommand = require.resolve('bs-platform/bin/bsb.exe') | ||
} catch (e) { | ||
bsbCommand = `bsb` | ||
} | ||
function isWSL() { | ||
const release = os.release() | ||
return release.substring(release.length - 'Microsoft'.length) === 'Microsoft' | ||
} | ||
const bsb = | ||
os.platform() === 'darwin' | ||
? `script -q /dev/null ${bsbCommand} -make-world -color` | ||
: os.platform() === 'linux' | ||
? isWSL() | ||
? `${bsbCommand} -make-world` // Windows WSL | ||
: `script --return -qfc "${bsbCommand} -make-world -color" /dev/null` // Linux | ||
: `${bsbCommand} -make-world` // Windows Native and others. | ||
const outputDir = 'lib' | ||
const CWD = process.cwd() | ||
const fileNameRegex = /\.(ml|re)$/ | ||
const es6ReplaceRegex = /(from\ "\.\.?\/.*)(\.js)("\;)/g | ||
const commonJsReplaceRegex = /(require\("\.\.?\/.*)(\.js)("\);)/g | ||
const getErrorRegex = /(File [\s\S]*?:\n|Fatal )[eE]rror: [\s\S]*?(?=ninja|\n\n|$)/g | ||
const getSuperErrorRegex = /We've found a bug for you![\s\S]*?(?=ninja: build stopped)/g | ||
const getWarningRegex = /((File [\s\S]*?Warning.+? \d+:)|Warning number \d+)[\s\S]*?(?=\[\d+\/\d+\]|$)/g | ||
@@ -54,71 +25,5 @@ function jsFilePath(buildDir, moduleDir, resourcePath, inSource) { | ||
function transformSrc(moduleDir, src) { | ||
const replacer = moduleDir === 'es6' ? es6ReplaceRegex : commonJsReplaceRegex | ||
/*:: type Options = { moduleDir: BsModuleFormat | 'js', inSource: boolean } */ | ||
return src.replace(replacer, '$1$3') | ||
} | ||
function runBsb(buildDir, compilation) { | ||
if (!compilation.bsbRunner) { | ||
compilation.bsbRunner = new Promise((resolve, reject) => { | ||
exec( | ||
bsb, | ||
{ maxBuffer: Infinity, cwd: buildDir }, | ||
(err, stdout, stderr) => { | ||
const output = `${stdout.toString()}\n${stderr.toString()}` | ||
if (err) { | ||
reject(output) | ||
} else { | ||
resolve(output) | ||
} | ||
} | ||
) | ||
}) | ||
} | ||
return compilation.bsbRunner | ||
} | ||
function runBsbSync() { | ||
execSync(bsb, { stdio: 'pipe' }) | ||
} | ||
function processBsbError(err) { | ||
if (typeof err === 'string') | ||
return err.match( | ||
err.includes('-bs-super-errors') ? getSuperErrorRegex : getErrorRegex | ||
) | ||
if (err.message) return [err.message] | ||
return undefined | ||
} | ||
function getCompiledFile(buildDir, compilation, moduleDir, path) { | ||
return runBsb(buildDir, compilation).then(output => { | ||
return new Promise((resolve, reject) => { | ||
readFile(path, (err, res) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
const src = transformSrc(moduleDir, res.toString()) | ||
resolve({ src, output }) | ||
} | ||
}) | ||
}) | ||
}) | ||
} | ||
function getCompiledFileSync(moduleDir, path) { | ||
try { | ||
runBsbSync() | ||
} catch (e) { | ||
throw e.output.toString() | ||
} | ||
const res = readFileSync(path) | ||
return transformSrc(moduleDir, res.toString()) | ||
} | ||
function getBsConfigModuleOptions(buildDir) { | ||
function getBsConfigModuleOptions(buildDir) /*: Promise<Options> */ { | ||
return readBsConfig(buildDir).then(bsconfig => { | ||
@@ -130,7 +35,8 @@ if (!bsconfig) { | ||
if (!bsconfig['package-specs'] || !bsconfig['package-specs'].length) { | ||
return { module: 'js', inSource: false } | ||
const options /*: Options */ = { moduleDir: 'js', inSource: false } | ||
return options | ||
} | ||
const moduleSpec = bsconfig['package-specs'][0] | ||
const moduleDir = | ||
const moduleDir /*: BsModuleFormat */ = | ||
typeof moduleSpec === 'string' ? moduleSpec : moduleSpec.module | ||
@@ -140,3 +46,4 @@ const inSource = | ||
return { moduleDir, inSource } | ||
const options /*: Options */ = { moduleDir, inSource } | ||
return options | ||
}) | ||
@@ -147,50 +54,41 @@ } | ||
const options = getOptions(this) || {} | ||
const buildDir = options.cwd || CWD | ||
const buildDir = options.cwd || process.cwd() | ||
const callback = this.async() | ||
const showWarnings = | ||
options.showWarnings !== undefined ? options.showWarnings : true | ||
this.addContextDependency(this.context) | ||
getBsConfigModuleOptions(buildDir).then(bsconfig => { | ||
const moduleDir = options.module || bsconfig.moduleDir || 'js' | ||
const inSourceBuild = options.inSource || bsconfig.inSource || false | ||
const showWarnings = | ||
options.showWarnings !== undefined ? options.showWarnings : true | ||
getBsConfigModuleOptions(buildDir) | ||
.then(bsconfig => { | ||
const moduleDir = bsconfig.moduleDir | ||
const compiledFilePath = jsFilePath( | ||
buildDir, | ||
moduleDir, | ||
this.resourcePath, | ||
inSourceBuild | ||
) | ||
const inSourceBuild = options.inSource || bsconfig.inSource || false | ||
getCompiledFile(buildDir, this._compilation, moduleDir, compiledFilePath) | ||
.then(({ src, output }) => { | ||
if (output && showWarnings) { | ||
const warningMessages = output.match(getWarningRegex) | ||
if (warningMessages) { | ||
warningMessages.forEach(message => { | ||
this.emitWarning(new Error(message)) | ||
}) | ||
} | ||
} | ||
const compiledFilePath = jsFilePath( | ||
buildDir, | ||
moduleDir, | ||
this.resourcePath, | ||
inSourceBuild | ||
) | ||
callback(null, src) | ||
}) | ||
.catch(err => { | ||
if (err instanceof Error) err = err.toString() | ||
const errorMessages = processBsbError(err) | ||
return compileFile(buildDir, moduleDir, compiledFilePath) | ||
}) | ||
.then(({ src, warnings, errors }) => { | ||
if (showWarnings) { | ||
warnings.forEach(message => { | ||
this.emitWarning(new Error(message)) | ||
}) | ||
} | ||
if (!errorMessages) { | ||
if (!(err instanceof Error)) err = new Error(err) | ||
this.emitError(err) | ||
return callback(err, null) | ||
if (errors.length > 0) { | ||
for (let i = 0; i < errors.length - 1; ++i) { | ||
this.emitError(errors[i]) | ||
} | ||
for (let i = 0; i < errorMessages.length - 1; ++i) { | ||
this.emitError(new Error(errorMessages[i])) | ||
} | ||
callback(new Error(errorMessages[errorMessages.length - 1]), null) | ||
}) | ||
}) | ||
callback(new Error(errors[errors.length - 1]), null) | ||
} else { | ||
callback(null, src || '') | ||
} | ||
}) | ||
} | ||
@@ -205,16 +103,5 @@ | ||
const moduleDir = 'js' | ||
const compiledFilePath = jsFilePath(CWD, moduleDir, filename, false) | ||
const compiledFilePath = jsFilePath(process.cwd(), moduleDir, filename, false) | ||
try { | ||
return getCompiledFileSync(moduleDir, compiledFilePath) | ||
} catch (err) { | ||
if (err instanceof Error) err = err.toString() | ||
const bsbErrorMessages = processBsbError(err) | ||
if (bsbErrorMessages && bsbErrorMessages.length > 0) { | ||
throw new Error(bsbErrorMessages[0]) | ||
} else { | ||
throw err | ||
} | ||
} | ||
return compileFileSync(moduleDir, compiledFilePath) | ||
} |
{ | ||
"name": "bs-loader", | ||
"version": "1.8.2", | ||
"version": "2.0.0-beta1", | ||
"description": "Bucklescript integration in Webpack", | ||
@@ -30,2 +30,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"bsb-js": "^1.0.0", | ||
"loader-utils": "^1.1.0", | ||
@@ -32,0 +33,0 @@ "read-bsconfig": "^1.0.1" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
0
8417
3
80
1
+ Addedbsb-js@^1.0.0
+ Addedbsb-js@1.1.7(transitive)