From 7cdae9fddb133dc88eeeb605e8871bbf976da360 Mon Sep 17 00:00:00 2001 From: Caridy Patino Date: Mon, 31 Mar 2014 11:18:55 -0400 Subject: [PATCH 1/2] improving detection for cjs. fixes issue #9 --- index.js | 4 ++++ test/fixtures/cjs-export-number.js | 1 + test/fixtures/cjs-export-regex.js | 1 + test/fixtures/cjs-export-string.js | 1 + 4 files changed, 7 insertions(+) create mode 100644 test/fixtures/cjs-export-number.js create mode 100644 test/fixtures/cjs-export-regex.js create mode 100644 test/fixtures/cjs-export-string.js diff --git a/index.js b/index.js index db3f6ed..b382b2d 100644 --- a/index.js +++ b/index.js @@ -99,7 +99,11 @@ function extract(src) { } finally { // very dummy detection process for CommonJS modules if (typeof context.module.exports === 'function' || + typeof context.module.exports === 'string' || + typeof context.module.exports === 'number' || typeof context.exports === 'function' || + typeof context.exports === 'string' || + typeof context.exports === 'number' || Object.keys(context.module.exports).length > 0 || Object.keys(context.exports).length > 0 || Object.getPrototypeOf(context.module.exports) || diff --git a/test/fixtures/cjs-export-number.js b/test/fixtures/cjs-export-number.js new file mode 100644 index 0000000..4bbffde --- /dev/null +++ b/test/fixtures/cjs-export-number.js @@ -0,0 +1 @@ +module.exports = 2; diff --git a/test/fixtures/cjs-export-regex.js b/test/fixtures/cjs-export-regex.js new file mode 100644 index 0000000..56c9712 --- /dev/null +++ b/test/fixtures/cjs-export-regex.js @@ -0,0 +1 @@ +module.exports = new RegExp('abc'); diff --git a/test/fixtures/cjs-export-string.js b/test/fixtures/cjs-export-string.js new file mode 100644 index 0000000..1eaebea --- /dev/null +++ b/test/fixtures/cjs-export-string.js @@ -0,0 +1 @@ +exports = 'something'; From a23b8f74f303d4d77de97f227469c47e30f89808 Mon Sep 17 00:00:00 2001 From: Caridy Patino Date: Mon, 31 Mar 2014 11:36:34 -0400 Subject: [PATCH 2/2] reducing complexity by making module an alias of context when executing the cjs code --- index.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index b382b2d..810d76f 100644 --- a/index.js +++ b/index.js @@ -80,10 +80,8 @@ function extract(src) { }); throw new Error('Common JS script detected'); }; - context.module = { - exports: Object.create(null) - }; - context.exports = context.module.exports; + context.exports = Object.create(null); + context.module = context; // executing the JavaScript source into a new context to avoid leaking @@ -98,15 +96,10 @@ function extract(src) { } } finally { // very dummy detection process for CommonJS modules - if (typeof context.module.exports === 'function' || - typeof context.module.exports === 'string' || - typeof context.module.exports === 'number' || - typeof context.exports === 'function' || + if (typeof context.exports === 'function' || typeof context.exports === 'string' || typeof context.exports === 'number' || - Object.keys(context.module.exports).length > 0 || Object.keys(context.exports).length > 0 || - Object.getPrototypeOf(context.module.exports) || Object.getPrototypeOf(context.exports)) { mods.push({type: 'cjs'}); }