diff --git a/config/shared.js b/config/shared.js index 75f6c56..4597d8c 100644 --- a/config/shared.js +++ b/config/shared.js @@ -30,6 +30,7 @@ const nodeTarget = () => () => ({ }); const baseConfig = () => webpackBlocks.group([ + webpackBlocks.sourceMaps(), babelLoader(), nodeTarget(), ]); diff --git a/config/webpack.config.js b/config/webpack.config.js index 4520c19..3d74e4e 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -3,7 +3,7 @@ const webpackBlocks = require('@webpack-blocks/webpack2'); const shared = require('./shared.js'); -const NODE_EXTERNAL_DEPS = ['babylon', 'babel-generator', 'babel-traverse', 'babel-types', 'babel-template']; +const NODE_EXTERNAL_DEPS = ['babel-core']; const excludeNodeDepsFromCompilation = () => () => ({ externals: NODE_EXTERNAL_DEPS.map(dep => ({ diff --git a/dist/index.js b/dist/index.js index 328ce89..159fc2d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1,2 @@ -module.exports=function(e){function n(i){if(t[i])return t[i].exports;var r=t[i]={i:i,l:!1,exports:{}};return e[i].call(r.exports,r,r.exports,n),r.l=!0,r.exports}var t={};return n.m=e,n.c=t,n.i=function(e){return e},n.d=function(e,t,i){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:i})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n(n.s=8)}([function(e,n,t){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}var r=t(1),o=i(r);e.exports=function(e){return this.cacheable&&this.cacheable(),(0,o.default)(this,e).code}},function(e,n,t){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}function r(e){if(e&&e.__esModule)return e;var n={};if(null!=e)for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&(n[t]=e[t]);return n.default=e,n}function o(e){var n=e.node.arguments[0].value;return e.replaceWith(v.logicalExpression("||",v.CallExpression(v.identifier("__getInjection"),[v.stringLiteral(n)]),e.node)),n}function u(e,n){var t=a.parse(n),i=[];(0,p.default)(t,{CallExpression:function(e){v.isIdentifier(e.node.callee,{name:"require"})&&(i.push(o(e)),e.skip())}}),0===i.length&&e.emitWarning("The module you are trying to inject into doesn't have any dependencies. Are you sure you want to do this?");var r=v.arrayExpression(i.map(function(e){return v.stringLiteral(e)})),u=(0,j.default)({SOURCE:t,DEPENDENCIES:r});return(0,s.default)(u,{})}Object.defineProperty(n,"__esModule",{value:!0}),n.default=u;var c=t(7),a=r(c),d=t(3),s=i(d),l=t(5),p=i(l),f=t(6),v=r(f),_=t(2),j=i(_)},function(e,n,t){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(n,"__esModule",{value:!0});var r=t(4),o=i(r);n.default=(0,o.default)("\n module.exports = function __injector(__injections) {\n __injections = __injections || {};\n\n (function __validateInjection() {\n var validDependencies = DEPENDENCIES;\n var injectedDependencies = Object.keys(__injections);\n var invalidInjectedDependencies = injectedDependencies.filter(function (dependency) {\n return validDependencies.indexOf(dependency) === -1;\n });\n\n if (invalidInjectedDependencies.length > 0) {\n var validDependenciesString = '- ' + validDependencies.join('\\n- ');\n var injectedDependenciesString = '- ' + injectedDependencies.join('\\n- ');\n var invalidDependenciesString = '- ' + invalidInjectedDependencies.join('\\n- ');\n\n throw new Error('Some of the injections you passed in are invalid.\\n' +\n 'Valid injection targets for this module are:\\n' + validDependenciesString + '\\n' +\n 'The following injections were passed in:\\n' + injectedDependenciesString + '\\n' +\n 'The following injections are invalid:\\n' + invalidDependenciesString\n );\n }\n })();\n\n var module = { exports: {} };\n var exports = module.exports;\n\n function __getInjection(dependency) {\n return __injections.hasOwnProperty(dependency) ? __injections[dependency] : null;\n }\n\n (function () {\n SOURCE\n })();\n\n return module.exports;\n }\n")},function(e,n){e.exports=require("babel-generator")},function(e,n){e.exports=require("babel-template")},function(e,n){e.exports=require("babel-traverse")},function(e,n){e.exports=require("babel-types")},function(e,n){e.exports=require("babylon")},function(e,n,t){e.exports=t(0)}]); \ No newline at end of file +module.exports=function(e){function n(i){if(t[i])return t[i].exports;var r=t[i]={i:i,l:!1,exports:{}};return e[i].call(r.exports,r,r.exports,n),r.l=!0,r.exports}var t={};return n.m=e,n.c=t,n.i=function(e){return e},n.d=function(e,t,i){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:i})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n(n.s=4)}([function(e,n){e.exports=require("babel-core")},function(e,n,t){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}var r=t(2),o=i(r);e.exports=function(e){this.cacheable&&this.cacheable();var n=(0,o.default)(this,e),t=n.code,i=n.map;this.callback(null,t,i)}},function(e,n,t){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}function r(e){var n=e.node.arguments[0].value;return e.replaceWith(s.types.logicalExpression("||",s.types.CallExpression(s.types.identifier("__getInjection"),[s.types.stringLiteral(n)]),e.node)),n}function o(e,n){var t=(0,s.transform)(n),i=t.ast,o=[];(0,s.traverse)(i,{CallExpression:function(e){s.types.isIdentifier(e.node.callee,{name:"require"})&&(o.push(r(e)),e.skip())}}),0===o.length&&e.emitWarning("The module you are trying to inject into doesn't have any dependencies. Are you sure you want to do this?");var c=s.types.arrayExpression(o.map(function(e){return s.types.stringLiteral(e)})),d=s.types.file(s.types.program([(0,a.default)({SOURCE:i,DEPENDENCIES:c})]));return(0,s.transformFromAst)(d,n,{sourceMaps:e.sourceMap,sourceFileName:e.resourcePath})}Object.defineProperty(n,"__esModule",{value:!0}),n.default=o;var s=t(0),c=t(3),a=i(c)},function(e,n,t){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var i=t(0);n.default=(0,i.template)("\n module.exports = function __injector(__injections) {\n __injections = __injections || {};\n\n (function __validateInjection() {\n var validDependencies = DEPENDENCIES;\n var injectedDependencies = Object.keys(__injections);\n var invalidInjectedDependencies = injectedDependencies.filter(function (dependency) {\n return validDependencies.indexOf(dependency) === -1;\n });\n\n if (invalidInjectedDependencies.length > 0) {\n var validDependenciesString = '- ' + validDependencies.join('\\n- ');\n var injectedDependenciesString = '- ' + injectedDependencies.join('\\n- ');\n var invalidDependenciesString = '- ' + invalidInjectedDependencies.join('\\n- ');\n\n throw new Error('Some of the injections you passed in are invalid.\\n' +\n 'Valid injection targets for this module are:\\n' + validDependenciesString + '\\n' +\n 'The following injections were passed in:\\n' + injectedDependenciesString + '\\n' +\n 'The following injections are invalid:\\n' + invalidDependenciesString + '\\n'\n );\n }\n })();\n\n var module = { exports: {} };\n var exports = module.exports;\n\n function __getInjection(dependency) {\n return __injections.hasOwnProperty(dependency) ? __injections[dependency] : null;\n }\n\n (function () {\n SOURCE\n })();\n\n return module.exports;\n }\n")},function(e,n,t){e.exports=t(1)}]); +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/package.json b/package.json index b29f79f..9e28ae2 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "build-prod": "webpack --config config/webpack.config.js -p", "build-test": "webpack --config config/webpack.test.config.js", "eslint": "eslint {src,config,__tests__}/**", - "test": "mocha tmp/testBundle.js" + "test": "mocha tmp/testBundle.js --require source-map-support/register" }, "files": [ "*.md", @@ -21,21 +21,17 @@ }, "license": "MIT", "dependencies": { - "babel-generator": "~6", - "babel-template": "~6", - "babel-traverse": "~6", - "babel-types": "~6", - "babylon": "~6" + "babel-core": "~6" }, "devDependencies": { "@webpack-blocks/webpack2": "^0.4.0", - "babel-core": "^6.23.1", "babel-loader": "^6.4.0", "babel-preset-es2015": "^6.22.0", "eslint": "^3.17.1", "eslint-config-airbnb-base": "^11.1.1", "eslint-plugin-import": "^2.2.0", "mocha": "^3.2.0", + "source-map-support": "^0.4.14", "webpack": "^2.2.1" }, "peerDependencies": { diff --git a/src/index.js b/src/index.js index 112a436..0c140da 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,6 @@ module.exports = function injectifyLoader(source) { this.cacheable(); } - const { code } = injectify(this, source); - return code; + const { code, map } = injectify(this, source); + this.callback(null, code, map); }; diff --git a/src/injectify.js b/src/injectify.js index 292e320..7085535 100644 --- a/src/injectify.js +++ b/src/injectify.js @@ -1,22 +1,18 @@ -import * as babylon from 'babylon'; -import generate from 'babel-generator'; -import traverse from 'babel-traverse'; -import * as t from 'babel-types'; - +import { transform, traverse, types as t, transformFromAst } from 'babel-core'; import wrapperTemplate from './wrapper_template.js'; function processRequireCall(path) { const dependencyString = path.node.arguments[0].value; path.replaceWith(t.logicalExpression('||', - t.CallExpression(t.identifier('__getInjection'), [t.stringLiteral(dependencyString)]), - path.node), - ); + t.CallExpression(t.identifier('__getInjection'), [t.stringLiteral(dependencyString)]), + path.node), + ); return dependencyString; } export default function injectify(context, source) { - const ast = babylon.parse(source); + const { ast } = transform(source); const dependencies = []; traverse(ast, { @@ -30,12 +26,18 @@ export default function injectify(context, source) { if (dependencies.length === 0) { context.emitWarning('The module you are trying to inject into doesn\'t have any dependencies. ' + - 'Are you sure you want to do this?'); + 'Are you sure you want to do this?'); } const dependenciesArrayAst = t.arrayExpression( - dependencies.map(dependency => t.stringLiteral(dependency)), - ); - const wrappedSourceAst = wrapperTemplate({ SOURCE: ast, DEPENDENCIES: dependenciesArrayAst }); - return generate(wrappedSourceAst, {}); + dependencies.map(dependency => t.stringLiteral(dependency)), + ); + const wrapperModuleAst = t.file(t.program([ + wrapperTemplate({ SOURCE: ast, DEPENDENCIES: dependenciesArrayAst }), + ])); + + return transformFromAst(wrapperModuleAst, source, { + sourceMaps: context.sourceMap, + sourceFileName: context.resourcePath, + }); } diff --git a/src/wrapper_template.js b/src/wrapper_template.js index aaec973..d2d761a 100644 --- a/src/wrapper_template.js +++ b/src/wrapper_template.js @@ -1,4 +1,4 @@ -import template from 'babel-template'; +import { template } from 'babel-core'; export default template(` module.exports = function __injector(__injections) { diff --git a/yarn.lock b/yarn.lock index f9b3df8..51b1fac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -206,7 +206,7 @@ babel-code-frame@^6.11.0, babel-code-frame@^6.16.0, babel-code-frame@^6.22.0: esutils "^2.0.2" js-tokens "^3.0.0" -babel-core@^6.23.1, babel-core@^6.24.0: +babel-core@^6.24.0, babel-core@~6: version "6.24.0" resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.24.0.tgz#8f36a0a77f5c155aed6f920b844d23ba56742a02" dependencies: @@ -230,7 +230,7 @@ babel-core@^6.23.1, babel-core@^6.24.0: slash "^1.0.0" source-map "^0.5.0" -babel-generator@^6.24.0, babel-generator@~6: +babel-generator@^6.24.0: version "6.24.0" resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.24.0.tgz#eba270a8cc4ce6e09a61be43465d7c62c1f87c56" dependencies: @@ -568,7 +568,7 @@ babel-runtime@^6.18.0, babel-runtime@^6.22.0: core-js "^2.4.0" regenerator-runtime "^0.10.0" -babel-template@^6.22.0, babel-template@^6.23.0, babel-template@~6: +babel-template@^6.22.0, babel-template@^6.23.0: version "6.23.0" resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.23.0.tgz#04d4f270adbb3aa704a8143ae26faa529238e638" dependencies: @@ -578,7 +578,7 @@ babel-template@^6.22.0, babel-template@^6.23.0, babel-template@~6: babylon "^6.11.0" lodash "^4.2.0" -babel-traverse@^6.22.0, babel-traverse@^6.23.0, babel-traverse@^6.23.1, babel-traverse@~6: +babel-traverse@^6.22.0, babel-traverse@^6.23.0, babel-traverse@^6.23.1: version "6.23.1" resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.23.1.tgz#d3cb59010ecd06a97d81310065f966b699e14f48" dependencies: @@ -592,7 +592,7 @@ babel-traverse@^6.22.0, babel-traverse@^6.23.0, babel-traverse@^6.23.1, babel-tr invariant "^2.2.0" lodash "^4.2.0" -babel-types@^6.19.0, babel-types@^6.22.0, babel-types@^6.23.0, babel-types@~6: +babel-types@^6.19.0, babel-types@^6.22.0, babel-types@^6.23.0: version "6.23.0" resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.23.0.tgz#bb17179d7538bad38cd0c9e115d340f77e7e9acf" dependencies: @@ -601,7 +601,7 @@ babel-types@^6.19.0, babel-types@^6.22.0, babel-types@^6.23.0, babel-types@~6: lodash "^4.2.0" to-fast-properties "^1.0.1" -babylon@^6.11.0, babylon@^6.15.0, babylon@~6: +babylon@^6.11.0, babylon@^6.15.0: version "6.16.1" resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.16.1.tgz#30c5a22f481978a9e7f8cdfdf496b11d94b404d3" @@ -3228,9 +3228,9 @@ source-list-map@^0.1.4, source-list-map@~0.1.7: version "0.1.8" resolved "https://registry.yarnpkg.com/source-list-map/-/source-list-map-0.1.8.tgz#c550b2ab5427f6b3f21f5afead88c4f5587b2106" -source-map-support@^0.4.2: - version "0.4.13" - resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.13.tgz#9782e6f7deb424d5f173327a1879eb46453bdcd4" +source-map-support@^0.4.14, source-map-support@^0.4.2: + version "0.4.14" + resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.14.tgz#9d4463772598b86271b4f523f6c1f4e02a7d6aef" dependencies: source-map "^0.5.6"