UNPKG

5.74 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
8
9exports.default = function (_ref2) {
10 var t = _ref2.types;
11
12 // some strange error occurs if I move this fn outside context
13 // and then run babel-node with this plugin
14 var localInteropRequire = function localInteropRequire(path) {
15 var res = require((0, _path.resolve)(process.cwd(), path));
16 if ('default' in res) {
17 return res.default;
18 }
19 return res;
20 };
21
22 return {
23 visitor: {
24 CallExpression: function CallExpression(path, _ref3) {
25 var filenameRelative = _ref3.file.opts.filenameRelative;
26 var _ref3$opts = _ref3.opts;
27 _ref3$opts = _ref3$opts === undefined ? {} : _ref3$opts;
28 var _ref3$opts$config = _ref3$opts.config;
29 var configPath = _ref3$opts$config === undefined ? './webpack.config.js' : _ref3$opts$config;
30 var _ref3$opts$verbose = _ref3$opts.verbose;
31 var verbose = _ref3$opts$verbose === undefined ? true : _ref3$opts$verbose;
32 var _path$node = path.node;
33 var calleeName = _path$node.callee.name;
34 var args = _path$node.arguments;
35
36
37 if (calleeName !== 'require' || !args.length || !t.isStringLiteral(args[0])) {
38 return;
39 }
40
41 var config = localInteropRequire((0, _path.resolve)(process.cwd(), configPath));
42 if (Object.keys(config).length === 0) {
43 // it's possible require calls inside webpack config or bad config
44 return;
45 }
46
47 // TODO add other webpack checks
48 if (config.module.loaders.some(function (l) {
49 return l.test.test(args[0].value);
50 })) {
51 var _args = _slicedToArray(args, 1);
52
53 var filePath = _args[0].value;
54
55
56 var fileAbsPath = _resolve2.default.sync(filePath, {
57 basedir: (0, _path.dirname)(filenameRelative)
58 });
59
60 var webPackResult = (0, _runWebPackSync2.default)({ path: fileAbsPath, configPath: configPath, config: config, verbose: verbose });
61
62 var expr = processWebPackResult(webPackResult, config);
63
64 if (expr !== null) {
65 if (expr.type === 'FunctionExpression') {
66 path.remove();
67 } else {
68 path.replaceWith(expr);
69 }
70 } else {
71 path.remove();
72 }
73 }
74 }
75 }
76 };
77};
78
79var _path = require('path');
80
81var _resolve = require('resolve');
82
83var _resolve2 = _interopRequireDefault(_resolve);
84
85var _babylon = require('babylon');
86
87var _babelTraverse = require('babel-traverse');
88
89var _babelTraverse2 = _interopRequireDefault(_babelTraverse);
90
91var _runWebPackSync = require('./runWebPackSync');
92
93var _runWebPackSync2 = _interopRequireDefault(_runWebPackSync);
94
95var _babelTypes = require('babel-types');
96
97function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
98
99var processWebPackResult = function processWebPackResult(webPackResult) {
100 var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
101
102 var _ref$output = _ref.output;
103 _ref$output = _ref$output === undefined ? {} : _ref$output;
104 var _ref$output$publicPat = _ref$output.publicPath;
105 var publicPath = _ref$output$publicPat === undefined ? '' : _ref$output$publicPat;
106
107 var webpackResultAst = (0, _babylon.parse)(webPackResult);
108 var expr = null;
109
110 // without ExtractTextPlugin css-loader result looks like `blabla.locals = {...blbala}`
111 (0, _babelTraverse2.default)(webpackResultAst, {
112 FunctionExpression: function FunctionExpression(pathFn) {
113 if (pathFn.node.params.length >= 2 && pathFn.node.params[1].name === 'exports') {
114 pathFn.traverse({
115 AssignmentExpression: function AssignmentExpression(path) {
116 if (path.node.left.property && path.node.left.property.name === 'locals') {
117 expr = path.node.right;
118 }
119 }
120 });
121 }
122 }
123 });
124
125 // with ExtractTextPlugin css-loader result looks like `module.exports = {...blbala}`
126 if (expr === null) {
127 (0, _babelTraverse2.default)(webpackResultAst, {
128 FunctionExpression: function FunctionExpression(pathFn) {
129 if (pathFn.node.params.length >= 2 && pathFn.node.params[1].name === 'exports') {
130 pathFn.traverse({
131 AssignmentExpression: function AssignmentExpression(path) {
132 if (path.node.left.property && path.node.left.property.name === 'exports') {
133 expr = path.node.right;
134 }
135 },
136 BinaryExpression: function BinaryExpression(pathBin) {
137 pathBin.traverse({
138 MemberExpression: function MemberExpression(pathM) {
139 if (pathM.node.object.name === '__webpack_require__' && pathM.node.property.name === 'p') {
140 pathM.replaceWith((0, _babelTypes.StringLiteral)(publicPath)); // eslint-disable-line
141 }
142 }
143 });
144 }
145 });
146 }
147 }
148 });
149 }
150
151 return _babelTraverse2.default.removeProperties(expr);
152};
\No newline at end of file