Skip to content

Commit

Permalink
Sourcemaps support
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-tikhonov committed Mar 18, 2017
1 parent 0831925 commit e7dc6f9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 32 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions package.json
Expand Up @@ -21,15 +21,10 @@
},
"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",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -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);
};
29 changes: 15 additions & 14 deletions 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, {
Expand All @@ -30,12 +26,17 @@ 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,
});
}
2 changes: 1 addition & 1 deletion 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) {
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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"

Expand Down Expand Up @@ -3229,8 +3229,8 @@ source-list-map@^0.1.4, source-list-map@~0.1.7:
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"
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"

Expand Down

0 comments on commit e7dc6f9

Please sign in to comment.