From 96cd9dcdc0163a47733662e3186a6e519ce070cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnar=20=C3=9E=C3=B3r=20Sveinsson?= Date: Thu, 7 Sep 2017 22:03:06 +0000 Subject: [PATCH 1/6] Read bsconfig instead of webpack options --- index.js | 23 +++++++++++++++++++++-- package.json | 3 ++- test/simple/webpack.test.js | 17 ++++++++--------- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 88022f3..88a47c7 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +const { readBsConfigSync } = require('read-bsconfig') const path = require('path') const { readFile, readFileSync } = require('fs') const { execFile, execFileSync } = require('child_process') @@ -89,11 +90,29 @@ const getCompiledFileSync = (moduleDir, path) => { return transformSrc(moduleDir, res.toString()) } +const getBsConfigModuleOptions = buildDir => { + const bsconfig = readBsConfigSync(buildDir) + if (!bsconfig) { + throw new Error(`bsconfig not found in ${buildDir}`) + } + + if (!bsconfig['package-specs'] || !bsconfig['package-specs'].length) { + throw new Error('package-specs not defined in bsconfig') + } + + const moduleSpec = bsconfig['package-specs'][0] + const module = typeof moduleSpec === 'string' ? moduleSpec : moduleSpec.module + const inSource = + typeof moduleSpec === 'string' ? false : moduleSpec['in-source'] + return { module, 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 bsconfig = getBsConfigModuleOptions(buildDir) + const moduleDir = bsconfig.module || 'js' + const inSourceBuild = bsconfig.inSource || false this.addContextDependency(this.context) const callback = this.async() diff --git a/package.json b/package.json index 99535ae..cf0fe16 100644 --- a/package.json +++ b/package.json @@ -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.0" }, "devDependencies": { "jest": "^21.0.1", diff --git a/test/simple/webpack.test.js b/test/simple/webpack.test.js index 2f6c8b9..f5ea728 100644 --- a/test/simple/webpack.test.js +++ b/test/simple/webpack.test.js @@ -14,20 +14,19 @@ const baseConfig = { use: { loader, options: { - module: 'es6', - cwd: __dirname - } - } - } - ] + cwd: __dirname, + }, + }, + }, + ], }, resolve: { - extensions: ['.re', '.ml', '.js'] + extensions: ['.re', '.ml', '.js'], }, output: { path: output, - libraryTarget: 'commonjs2' - } + libraryTarget: 'commonjs2', + }, } it('runs', done => { From 38ef36d69c59407bfd68464526a9e32b29e4fb80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnar=20=C3=9E=C3=B3r=20Sveinsson?= Date: Thu, 7 Sep 2017 22:10:33 +0000 Subject: [PATCH 2/6] Fix prettier config to remove unecessary diff --- test/simple/webpack.test.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/simple/webpack.test.js b/test/simple/webpack.test.js index f5ea728..2d67848 100644 --- a/test/simple/webpack.test.js +++ b/test/simple/webpack.test.js @@ -14,19 +14,19 @@ const baseConfig = { use: { loader, options: { - cwd: __dirname, - }, - }, - }, - ], + cwd: __dirname + } + } + } + ] }, resolve: { - extensions: ['.re', '.ml', '.js'], + extensions: ['.re', '.ml', '.js'] }, output: { path: output, - libraryTarget: 'commonjs2', - }, + libraryTarget: 'commonjs2' + } } it('runs', done => { From 8cd955966179a1ae87f20dc24c90576f1a6dc908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnar=20=C3=9E=C3=B3r=20Sveinsson?= Date: Fri, 8 Sep 2017 00:12:51 +0000 Subject: [PATCH 3/6] Use async version of readBsConfig --- index.js | 103 ++++++++++++++++++++++++++------------------------- package.json | 2 +- 2 files changed, 54 insertions(+), 51 deletions(-) diff --git a/index.js b/index.js index 88a47c7..298cefa 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -const { readBsConfigSync } = require('read-bsconfig') +const { readBsConfig } = require('read-bsconfig') const path = require('path') const { readFile, readFileSync } = require('fs') const { execFile, execFileSync } = require('child_process') @@ -90,65 +90,68 @@ const getCompiledFileSync = (moduleDir, path) => { return transformSrc(moduleDir, res.toString()) } -const getBsConfigModuleOptions = buildDir => { - const bsconfig = readBsConfigSync(buildDir) - if (!bsconfig) { - throw new Error(`bsconfig not found in ${buildDir}`) - } +const getBsConfigModuleOptions = (buildDir, cb) => { + readBsConfig(buildDir).then(bsconfig => { + if (!bsconfig) { + throw new Error(`bsconfig not found in ${buildDir}`) + } - if (!bsconfig['package-specs'] || !bsconfig['package-specs'].length) { - throw new Error('package-specs not defined in bsconfig') - } + if (!bsconfig['package-specs'] || !bsconfig['package-specs'].length) { + cb({ module: 'js', inSource: false }) + } - const moduleSpec = bsconfig['package-specs'][0] - const module = typeof moduleSpec === 'string' ? moduleSpec : moduleSpec.module - const inSource = - typeof moduleSpec === 'string' ? false : moduleSpec['in-source'] - return { module, inSource } + const moduleSpec = bsconfig['package-specs'][0] + const moduleDir = + typeof moduleSpec === 'string' ? moduleSpec : moduleSpec.module + const inSource = + typeof moduleSpec === 'string' ? false : moduleSpec['in-source'] + cb({ moduleDir, inSource }) + }) } module.exports = function loader() { const options = getOptions(this) || {} const buildDir = options.cwd || CWD - const bsconfig = getBsConfigModuleOptions(buildDir) - const moduleDir = bsconfig.module || 'js' - const inSourceBuild = bsconfig.inSource || false - - 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) - - if (!errorMessages) { - if (!(err instanceof Error)) err = new Error(err) - this.emitError(err) - return callback(err, null) - } + const bsconfig = getBsConfigModuleOptions(buildDir, bsconfig => { + const moduleDir = options.module || bsconfig.moduleDir || 'js' + const inSourceBuild = options.inSource || bsconfig.inSource || false + + this.addContextDependency(this.context) + const callback = this.async() + 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) => { diff --git a/package.json b/package.json index cf0fe16..63ba1c9 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "homepage": "https://github.com/rrdelaney/bs-loader#readme", "dependencies": { "loader-utils": "^1.1.0", - "read-bsconfig": "^1.0.0" + "read-bsconfig": "^1.0.1" }, "devDependencies": { "jest": "^21.0.1", From 3e1e07d86f00e06b7dce2e20fbc7055cfd02b939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnar=20=C3=9E=C3=B3r=20Sveinsson?= Date: Fri, 8 Sep 2017 00:15:47 +0000 Subject: [PATCH 4/6] Change cb -> promise for bsconfig reader --- index.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 298cefa..4ca8f0a 100644 --- a/index.js +++ b/index.js @@ -90,29 +90,31 @@ const getCompiledFileSync = (moduleDir, path) => { return transformSrc(moduleDir, res.toString()) } -const getBsConfigModuleOptions = (buildDir, cb) => { - readBsConfig(buildDir).then(bsconfig => { - if (!bsconfig) { - throw new Error(`bsconfig not found in ${buildDir}`) - } +const getBsConfigModuleOptions = buildDir => { + return new Promise((resolve, reject) => + readBsConfig(buildDir).then(bsconfig => { + if (!bsconfig) { + throw new Error(`bsconfig not found in ${buildDir}`) + } - if (!bsconfig['package-specs'] || !bsconfig['package-specs'].length) { - cb({ module: 'js', inSource: false }) - } + 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'] - cb({ moduleDir, inSource }) - }) + const moduleSpec = bsconfig['package-specs'][0] + const moduleDir = + typeof moduleSpec === 'string' ? moduleSpec : moduleSpec.module + const inSource = + typeof moduleSpec === 'string' ? false : moduleSpec['in-source'] + resolve({ moduleDir, inSource }) + }) + ) } module.exports = function loader() { const options = getOptions(this) || {} const buildDir = options.cwd || CWD - const bsconfig = getBsConfigModuleOptions(buildDir, bsconfig => { + const bsconfig = getBsConfigModuleOptions(buildDir).then(bsconfig => { const moduleDir = options.module || bsconfig.moduleDir || 'js' const inSourceBuild = options.inSource || bsconfig.inSource || false From 528e00003fe52cfd0b3ad74c00a6a700f5d4c2cc Mon Sep 17 00:00:00 2001 From: Ryan Delaney Date: Mon, 18 Sep 2017 15:37:09 -0700 Subject: [PATCH 5/6] Move this.callback outside of promise, remove `new Promise` --- index.js | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index 4ca8f0a..f7c112f 100644 --- a/index.js +++ b/index.js @@ -90,36 +90,36 @@ const getCompiledFileSync = (moduleDir, path) => { return transformSrc(moduleDir, res.toString()) } -const getBsConfigModuleOptions = buildDir => { - return new Promise((resolve, reject) => - readBsConfig(buildDir).then(bsconfig => { - if (!bsconfig) { - throw new Error(`bsconfig not found in ${buildDir}`) - } +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 }) - } + 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'] - resolve({ moduleDir, inSource }) - }) - ) -} + 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 bsconfig = getBsConfigModuleOptions(buildDir).then(bsconfig => { + const callback = this.async() + + this.addContextDependency(this.context) + + getBsConfigModuleOptions(buildDir).then(bsconfig => { const moduleDir = options.module || bsconfig.moduleDir || 'js' const inSourceBuild = options.inSource || bsconfig.inSource || false - this.addContextDependency(this.context) - const callback = this.async() const compiledFilePath = getJsFile( buildDir, moduleDir, From a7c9f9795206457dcb2be322f914ab67f6496a4d Mon Sep 17 00:00:00 2001 From: Ryan Delaney Date: Mon, 18 Sep 2017 15:40:27 -0700 Subject: [PATCH 6/6] Add new test case for no-config --- test/no-config/bsconfig.json | 10 ++++++++ test/no-config/fixtures/add.js | 13 ++++++++++ test/no-config/fixtures/add.re | 1 + test/no-config/fixtures/dec.js | 1 + test/no-config/fixtures/fib.js | 31 +++++++++++++++++++++++ test/no-config/fixtures/fib.ml | 9 +++++++ test/no-config/fixtures/fib.mli | 1 + test/no-config/package.json | 6 +++++ test/no-config/webpack.test.js | 45 +++++++++++++++++++++++++++++++++ 9 files changed, 117 insertions(+) create mode 100644 test/no-config/bsconfig.json create mode 100644 test/no-config/fixtures/add.js create mode 100644 test/no-config/fixtures/add.re create mode 100644 test/no-config/fixtures/dec.js create mode 100644 test/no-config/fixtures/fib.js create mode 100644 test/no-config/fixtures/fib.ml create mode 100644 test/no-config/fixtures/fib.mli create mode 100644 test/no-config/package.json create mode 100644 test/no-config/webpack.test.js diff --git a/test/no-config/bsconfig.json b/test/no-config/bsconfig.json new file mode 100644 index 0000000..9d9a224 --- /dev/null +++ b/test/no-config/bsconfig.json @@ -0,0 +1,10 @@ +{ + "name": "insource-test", + "sources": ["fixtures"], + "package-specs": [ + { + "module": "es6", + "in-source": true + } + ] +} diff --git a/test/no-config/fixtures/add.js b/test/no-config/fixtures/add.js new file mode 100644 index 0000000..4d53867 --- /dev/null +++ b/test/no-config/fixtures/add.js @@ -0,0 +1,13 @@ +// Generated by BUCKLESCRIPT VERSION 1.9.0, PLEASE EDIT WITH CARE +'use strict'; + + +function add(x, y) { + return x + y | 0; +} + +export { + add , + +} +/* No side effect */ diff --git a/test/no-config/fixtures/add.re b/test/no-config/fixtures/add.re new file mode 100644 index 0000000..f7a4ff9 --- /dev/null +++ b/test/no-config/fixtures/add.re @@ -0,0 +1 @@ +let add x y => x + y; diff --git a/test/no-config/fixtures/dec.js b/test/no-config/fixtures/dec.js new file mode 100644 index 0000000..49408c9 --- /dev/null +++ b/test/no-config/fixtures/dec.js @@ -0,0 +1 @@ +export const dec = x => x - 1; diff --git a/test/no-config/fixtures/fib.js b/test/no-config/fixtures/fib.js new file mode 100644 index 0000000..e82d8cb --- /dev/null +++ b/test/no-config/fixtures/fib.js @@ -0,0 +1,31 @@ +// Generated by BUCKLESCRIPT VERSION 1.9.0, PLEASE EDIT WITH CARE +'use strict'; + +import * as Add from "./add.js"; +import * as $dot$slashdec from "./dec"; + +function fib(n) { + var _n = n; + var _a = 1; + var _b = 1; + while(true) { + var b = _b; + var a = _a; + var n$1 = _n; + if (n$1) { + _b = Add.add(a, b); + _a = b; + _n = $dot$slashdec.dec(n$1); + continue ; + + } else { + return a; + } + }; +} + +export { + fib , + +} +/* ./dec Not a pure module */ diff --git a/test/no-config/fixtures/fib.ml b/test/no-config/fixtures/fib.ml new file mode 100644 index 0000000..e475b86 --- /dev/null +++ b/test/no-config/fixtures/fib.ml @@ -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 diff --git a/test/no-config/fixtures/fib.mli b/test/no-config/fixtures/fib.mli new file mode 100644 index 0000000..e324e01 --- /dev/null +++ b/test/no-config/fixtures/fib.mli @@ -0,0 +1 @@ +val fib : int -> int diff --git a/test/no-config/package.json b/test/no-config/package.json new file mode 100644 index 0000000..fd14392 --- /dev/null +++ b/test/no-config/package.json @@ -0,0 +1,6 @@ +{ + "name": "no-config-test", + "private": true, + "version": "100.0.0", + "license": "MIT" +} diff --git a/test/no-config/webpack.test.js b/test/no-config/webpack.test.js new file mode 100644 index 0000000..2d67848 --- /dev/null +++ b/test/no-config/webpack.test.js @@ -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() + }) + }) +})