|
| 1 | +var acorn = require('acorn'), |
| 2 | + path = require('path'), |
| 3 | + isExpressRouting = require('./isExpressRouting'), |
| 4 | + SourceNode = require('source-map').SourceNode, |
| 5 | + SourceMapConsumer = require('source-map').SourceMapConsumer, |
| 6 | + makeIdentitySourceMap = require('./makeIdentitySourceMap'); |
| 7 | + |
| 8 | +module.exports = function(source, map) { |
| 9 | + if (this.cacheable) { |
| 10 | + this.cacheable(); |
| 11 | + } |
| 12 | + |
| 13 | + var resourcePath = this.resourcePath, |
| 14 | + filename = path.basename(resourcePath); |
| 15 | + |
| 16 | + if (/[\\/]webpack[\\/]express-hot-loader[\\/]/.test(resourcePath)) { |
| 17 | + return this.callback(null, source, map); |
| 18 | + } |
| 19 | + |
| 20 | + var fine = true; |
| 21 | + |
| 22 | + // parse source |
| 23 | + // try { |
| 24 | + // var ast = acorn.parse(source, {ecmaVersion: 6, sourceType: 'module'}); |
| 25 | + // } |
| 26 | + // catch (err) { |
| 27 | + // fine = false; |
| 28 | + // return this.callback(err); |
| 29 | + // } |
| 30 | + |
| 31 | + /* USE PARSING ? |
| 32 | + var names = ast.body.filter(function(node) { |
| 33 | + return node.type === 'FunctionDeclaration'; |
| 34 | + }).map(function(node) { |
| 35 | + return node.id.name; |
| 36 | + }); |
| 37 | + */ |
| 38 | + |
| 39 | + console.log('--------resource --------'); |
| 40 | + console.log(resourcePath); |
| 41 | + if (/node_modules/.test(resourcePath)) { |
| 42 | + return this.callback(null, source, map); |
| 43 | + } |
| 44 | + |
| 45 | + // cleaner |
| 46 | + var src = source.replace(/\r?\n|\r/g, ' '); |
| 47 | + |
| 48 | + // code generation |
| 49 | + var prependTxt = [], |
| 50 | + appendTxt, |
| 51 | + separator = '\n\n'; |
| 52 | + |
| 53 | + var check = isExpressRouting.check(src); |
| 54 | + |
| 55 | + var processor = require('./processor'); |
| 56 | + |
| 57 | + var exportApp = ''; |
| 58 | + |
| 59 | + if (check.containsExpressInstance) { |
| 60 | + exportApp = 'module.exports = app;\n'; |
| 61 | + processor.setExpressResourcePath(resourcePath); |
| 62 | + } |
| 63 | + |
| 64 | + prependTxt = [ |
| 65 | + 'var processor = require(' + JSON.stringify(require.resolve('./processor')) + ');\n', |
| 66 | + ]; |
| 67 | + |
| 68 | + if (processor.mainExpressResourcePath != null && |
| 69 | + processor.mainExpressResourcePath !== resourcePath) { |
| 70 | + // INJECT EXPRESS APP |
| 71 | + prependTxt.push('var expressFile = ' +JSON.stringify(processor.mainExpressResourcePath) + ';\n\t'); |
| 72 | + prependTxt.push('var app = require(' + JSON.stringify(require.resolve(processor.mainExpressResourcePath)) + ');\n\t'); |
| 73 | + } |
| 74 | + |
| 75 | + prependTxt = prependTxt.join(' '); |
| 76 | + |
| 77 | + appendTxt = [ |
| 78 | + exportApp, |
| 79 | + 'if (module.hot && ' +JSON.stringify(fine) +') {\n\t', |
| 80 | + 'module.hot.dispose(function(data){\n\t\t', |
| 81 | + 'if (module.hot.data.routes.length > 0 && app != null) {\n\t\t', |
| 82 | + '\tprocessor.doReload(app, module.hot.data);\n', |
| 83 | + '\t\t}\n', |
| 84 | + '\t});\n\n', |
| 85 | + |
| 86 | + 'var warning = '+JSON.stringify(check.containsExpressInstance)+' && '+(check.routes != null && check.routes.length > 0) + ';\n\n', |
| 87 | + |
| 88 | + 'module.hot.data = {\n\t', |
| 89 | + 'routerNames: '+JSON.stringify(check.routerNames)+',\n\t', |
| 90 | + 'routes: '+JSON.stringify(check.routes)+',\n\t', |
| 91 | + 'warning: warning\n', |
| 92 | + '};\n\n', |
| 93 | + |
| 94 | + 'if (module.hot.data.warning) {\n', |
| 95 | + '\tprocessor.warn();\n', |
| 96 | + '}\n', |
| 97 | + |
| 98 | + '}' |
| 99 | + ].join(' '); |
| 100 | + |
| 101 | + if (this.sourceMap === false) { |
| 102 | + return this.callback(null, [ |
| 103 | + prependTxt, |
| 104 | + source, |
| 105 | + appendTxt |
| 106 | + ].join(separator)); |
| 107 | + } |
| 108 | + |
| 109 | + var newCode = [ |
| 110 | + prependTxt, |
| 111 | + source, |
| 112 | + appendTxt |
| 113 | + ].join(separator); |
| 114 | + |
| 115 | + if (!map) { |
| 116 | + |
| 117 | + // var transform = require("babel-core").transform(source, { |
| 118 | + // sourceMaps: true, |
| 119 | + // filename: this.resourcePath, |
| 120 | + // sourceFileName: this.resourcePath |
| 121 | + // }); |
| 122 | + |
| 123 | + map = makeIdentitySourceMap(source, this.resourcePath); |
| 124 | + } |
| 125 | + |
| 126 | + var node = new SourceNode(null, null, null, [ |
| 127 | + new SourceNode(null, null, this.resourcePath, prependTxt), |
| 128 | + SourceNode.fromStringWithSourceMap(source, new SourceMapConsumer(map)), |
| 129 | + new SourceNode(null, null, this.resourcePath, appendTxt) |
| 130 | + ]).join(separator); |
| 131 | + |
| 132 | + var result = node.toStringWithSourceMap(); |
| 133 | + |
| 134 | + this.callback(null, result.code, result.map.toString()); |
| 135 | +} |
0 commit comments