Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix webpack support #159

Merged
merged 5 commits into from
Jan 15, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ browserify-test/script.js
browserify-test/script.map
header-test/script.js
header-test/script.map
webpack-test/compiled.js
10 changes: 8 additions & 2 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 14 additions & 5 deletions source-map-support.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -378,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');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be var to not upset any old browsers? Maybe we can move the try/catch to the top of the file even 🤔

var fs
try {
  fs = require('fs')
} catch (_) {
  // Loading in catch block to convert webpack error to warning.
}
    if (!contents && fs && fs.existsSync(source)) {
      contents = fs.readFileSync(source, 'utf8');

if (!contents && fs.existsSync(source)) {
contents = fs.readFileSync(source, 'utf8');
}
} catch (err) {
/* NOP */
}

// Format the line from the original source code like node does
Expand Down Expand Up @@ -460,7 +464,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) {
Expand Down
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ it('eval', function() {
'Error: test',

// Before Node 4, `Object.eval`, after just `eval`.
/^ at (?:Object\.)?eval \(eval at <anonymous> \((?:.*\/)?line1\.js:1001:101\)/,
/^ at (?:Object\.)?eval \(eval at (<anonymous>|exports.test) \((?:.*\/)?line1\.js:1001:101\)/,

/^ at Object\.exports\.test \((?:.*\/)?line1\.js:1001:101\)$/
]);
Expand All @@ -191,8 +191,8 @@ it('eval inside eval', function() {
'eval("eval(\'throw new Error(\\"test\\")\')");'
], [
'Error: test',
/^ at (?:Object\.)?eval \(eval at <anonymous> \(eval at <anonymous> \((?:.*\/)?line1\.js:1001:101\)/,
/^ at (?:Object\.)?eval \(eval at <anonymous> \((?:.*\/)?line1\.js:1001:101\)/,
/^ at (?:Object\.)?eval \(eval at (<anonymous>|exports.test) \(eval at (<anonymous>|exports.test) \((?:.*\/)?line1\.js:1001:101\)/,
/^ at (?:Object\.)?eval \(eval at (<anonymous>|exports.test) \((?:.*\/)?line1\.js:1001:101\)/,
/^ at Object\.exports\.test \((?:.*\/)?line1\.js:1001:101\)$/
]);
});
Expand Down Expand Up @@ -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 <anonymous> \((?:.*\/)?line1\.js:1001:101\)/,
/^ at (?:Object\.)?eval \(eval at (<anonymous>|exports.test) \((?:.*\/)?line1\.js:1001:101\)/,
/^ at Object\.exports\.test \((?:.*\/)?line1\.js:1001:101\)$/
]);
});
Expand Down
5 changes: 5 additions & 0 deletions webpack-test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>
Make sure to run build.js.
This test should say either "Test failed" or "Test passed":
</p>
<script src="compiled.js"></script>
16 changes: 16 additions & 0 deletions webpack-test/script.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 12 additions & 0 deletions webpack-test/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var webpack = require('webpack');

module.exports = {
entry: './script.js',
devtool: 'inline-source-map',
output: {
filename: 'compiled.js'
},
resolve: {
extensions: ['', '.js']
}
};