From f4c2bf2bbc5787bd524c04295322f56f24698d58 Mon Sep 17 00:00:00 2001 From: kpdecker Date: Sun, 27 Nov 2016 18:04:48 -0600 Subject: [PATCH 1/5] Add test support for webpack --- .gitignore | 1 + build.js | 10 ++++++++-- package.json | 3 ++- webpack-test/index.html | 5 +++++ webpack-test/script.js | 16 ++++++++++++++++ webpack-test/webpack.config.js | 12 ++++++++++++ 6 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 webpack-test/index.html create mode 100644 webpack-test/script.js create mode 100644 webpack-test/webpack.config.js diff --git a/.gitignore b/.gitignore index 19c622f..802b41f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ browserify-test/script.js browserify-test/script.map header-test/script.js header-test/script.map +webpack-test/compiled.js \ No newline at end of file diff --git a/build.js b/build.js index fdbf145..998405c 100755 --- a/build.js +++ b/build.js @@ -5,8 +5,9 @@ var path = require('path'); var querystring = require('querystring'); var child_process = require('child_process'); -var browserify = path.join('node_modules', '.bin', 'browserify'); -var coffee = path.join('node_modules', '.bin', 'coffee'); +var browserify = path.resolve(path.join('node_modules', '.bin', 'browserify')); +var webpack = path.resolve(path.join('node_modules', '.bin', 'webpack')); +var coffee = path.resolve(path.join('node_modules', '.bin', 'coffee')); function run(command, callback) { console.log(command); @@ -71,3 +72,8 @@ run(coffee + ' --map --compile header-test/script.coffee', function(error) { var contents = fs.readFileSync('header-test/script.js', 'utf8'); fs.writeFileSync('header-test/script.js', contents.replace(/\/\/# sourceMappingURL=.*/g, '')) }); + +// Build the webpack test +child_process.exec(webpack, {cwd: 'webpack-test'}, function(error) { + if (error) throw error; +}); diff --git a/package.json b/package.json index 0f8bfdf..9a8e1fe 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "browserify": "3.44.2", "coffee-script": "1.7.1", "http-server": "^0.8.5", - "mocha": "1.18.2" + "mocha": "1.18.2", + "webpack": "^1.13.3" }, "repository": { "type": "git", diff --git a/webpack-test/index.html b/webpack-test/index.html new file mode 100644 index 0000000..33520e0 --- /dev/null +++ b/webpack-test/index.html @@ -0,0 +1,5 @@ +

+Make sure to run build.js. +This test should say either "Test failed" or "Test passed": +

+ diff --git a/webpack-test/script.js b/webpack-test/script.js new file mode 100644 index 0000000..223ce6c --- /dev/null +++ b/webpack-test/script.js @@ -0,0 +1,16 @@ +require('../').install(); + +function foo() { + throw new Error('foo'); +} + +try { + foo(); +} catch (e) { + if (/\bscript\.js\b/.test(e.stack)) { + document.body.appendChild(document.createTextNode('Test passed')); + } else { + document.body.appendChild(document.createTextNode('Test failed')); + console.log(e.stack); + } +} diff --git a/webpack-test/webpack.config.js b/webpack-test/webpack.config.js new file mode 100644 index 0000000..10991c0 --- /dev/null +++ b/webpack-test/webpack.config.js @@ -0,0 +1,12 @@ +var webpack = require('webpack'); + +module.exports = { + entry: './script.js', + devtool: 'inline-source-map', + output: { + filename: 'compiled.js' + }, + resolve: { + extensions: ['', '.js'] + } +}; From 9999e1d9e6483379046cda6fc75bd604f0abb1dd Mon Sep 17 00:00:00 2001 From: kpdecker Date: Sun, 27 Nov 2016 18:12:13 -0600 Subject: [PATCH 2/5] Make node requires conditional This converts the webpack errors to warnings. Individual users may use the ignore plugin to further mute these warnings. Fixes #150 Fixes #153 --- source-map-support.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/source-map-support.js b/source-map-support.js index db36c41..0cffb72 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -1,6 +1,5 @@ var SourceMapConsumer = require('source-map').SourceMapConsumer; var path = require('path'); -var fs = require('fs'); // Only install once if called multiple times var errorFormatterInstalled = false; @@ -72,7 +71,7 @@ retrieveFileHandlers.push(function(path) { // Otherwise, use the filesystem else { - var contents = fs.readFileSync(path, 'utf8'); + var contents = require('fs').readFileSync(path, 'utf8'); } } catch (e) { var contents = null; @@ -460,7 +459,12 @@ exports.install = function(options) { // Support runtime transpilers that include inline source maps if (options.hookRequire && !isInBrowser()) { - var Module = require('module'); + var Module; + try { + Module = require('module'); + } catch (err) { + // NOP: Loading in catch block to convert webpack error to warning. + } var $compile = Module.prototype._compile; if (!$compile.__sourceMapSupport) { From 9f16aa7dcf939021339e931e1ebb0bb7c09c0a5f Mon Sep 17 00:00:00 2001 From: kpdecker Date: Tue, 27 Dec 2016 18:44:59 -0600 Subject: [PATCH 3/5] Fix failing tests --- source-map-support.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/source-map-support.js b/source-map-support.js index 0cffb72..e906b56 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -377,8 +377,13 @@ function getErrorSource(error) { var contents = fileContentsCache[source]; // Support files on disk - if (!contents && fs.existsSync(source)) { - contents = fs.readFileSync(source, 'utf8'); + try { + const fs = require('fs'); + if (!contents && fs.existsSync(source)) { + contents = fs.readFileSync(source, 'utf8'); + } + } catch (err) { + /* NOP */ } // Format the line from the original source code like node does From 731b1fcbabbe3825877187d85c8e7d2b020994c3 Mon Sep 17 00:00:00 2001 From: kpdecker Date: Tue, 27 Dec 2016 18:57:40 -0600 Subject: [PATCH 4/5] Fix eval trace matches under Node 7 --- test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test.js b/test.js index 583dedd..5c28ba0 100644 --- a/test.js +++ b/test.js @@ -180,7 +180,7 @@ it('eval', function() { 'Error: test', // Before Node 4, `Object.eval`, after just `eval`. - /^ at (?:Object\.)?eval \(eval at \((?:.*\/)?line1\.js:1001:101\)/, + /^ at (?:Object\.)?eval \(eval at (|exports.test) \((?:.*\/)?line1\.js:1001:101\)/, /^ at Object\.exports\.test \((?:.*\/)?line1\.js:1001:101\)$/ ]); @@ -191,8 +191,8 @@ it('eval inside eval', function() { 'eval("eval(\'throw new Error(\\"test\\")\')");' ], [ 'Error: test', - /^ at (?:Object\.)?eval \(eval at \(eval at \((?:.*\/)?line1\.js:1001:101\)/, - /^ at (?:Object\.)?eval \(eval at \((?:.*\/)?line1\.js:1001:101\)/, + /^ at (?:Object\.)?eval \(eval at (|exports.test) \(eval at (|exports.test) \((?:.*\/)?line1\.js:1001:101\)/, + /^ at (?:Object\.)?eval \(eval at (|exports.test) \((?:.*\/)?line1\.js:1001:101\)/, /^ at Object\.exports\.test \((?:.*\/)?line1\.js:1001:101\)$/ ]); }); @@ -227,7 +227,7 @@ it('eval with sourceURL inside eval', function() { ], [ 'Error: test', /^ at (?:Object\.)?eval \(sourceURL\.js:1:7\)$/, - /^ at (?:Object\.)?eval \(eval at \((?:.*\/)?line1\.js:1001:101\)/, + /^ at (?:Object\.)?eval \(eval at (|exports.test) \((?:.*\/)?line1\.js:1001:101\)/, /^ at Object\.exports\.test \((?:.*\/)?line1\.js:1001:101\)$/ ]); }); From b19ff7625f631bd8245491edca36b486638423ea Mon Sep 17 00:00:00 2001 From: kpdecker Date: Sun, 15 Jan 2017 08:03:09 -0600 Subject: [PATCH 5/5] Require fs only once, in a try block --- source-map-support.js | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/source-map-support.js b/source-map-support.js index e906b56..5af4780 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -1,6 +1,13 @@ var SourceMapConsumer = require('source-map').SourceMapConsumer; var path = require('path'); +var fs; +try { + fs = require('fs'); +} catch (err) { + /* nop */ +} + // Only install once if called multiple times var errorFormatterInstalled = false; var uncaughtShimInstalled = false; @@ -57,24 +64,19 @@ retrieveFileHandlers.push(function(path) { return fileContentsCache[path]; } - try { + var contents = null; + if (!fs) { // Use SJAX if we are in the browser - if (isInBrowser()) { - var xhr = new XMLHttpRequest(); - xhr.open('GET', path, false); - xhr.send(null); - var contents = null - if (xhr.readyState === 4 && xhr.status === 200) { - contents = xhr.responseText - } + var xhr = new XMLHttpRequest(); + xhr.open('GET', path, false); + xhr.send(null); + var contents = null + if (xhr.readyState === 4 && xhr.status === 200) { + contents = xhr.responseText } - + } else if (fs.existsSync(path)) { // Otherwise, use the filesystem - else { - var contents = require('fs').readFileSync(path, 'utf8'); - } - } catch (e) { - var contents = null; + contents = fs.readFileSync(path, 'utf8'); } return fileContentsCache[path] = contents; @@ -377,13 +379,8 @@ function getErrorSource(error) { var contents = fileContentsCache[source]; // Support files on disk - try { - const fs = require('fs'); - if (!contents && fs.existsSync(source)) { - contents = fs.readFileSync(source, 'utf8'); - } - } catch (err) { - /* NOP */ + if (!contents && fs && fs.existsSync(source)) { + contents = fs.readFileSync(source, 'utf8'); } // Format the line from the original source code like node does