|
1 | 1 | const path = require('path');
|
| 2 | +const nodeModulesPath = path.resolve(__dirname, '..', '..', 'node_modules') |
2 | 3 |
|
3 | 4 | module.exports = (context, request, callback) => {
|
| 5 | + if(context.startsWith(nodeModulesPath)) { |
| 6 | + const module = context.replace(nodeModulesPath, '').split(path.sep).find(p => !!p) |
| 7 | + try { |
| 8 | + const pkg = require(path.resolve(nodeModulesPath, module, 'package.json')) |
4 | 9 |
|
5 |
| - if (context.indexOf('tns-core-modules') !== -1 |
6 |
| - || context.indexOf('nativescript-') !== -1 |
7 |
| - || /^tns-core-modules/i.test(request) |
8 |
| - || /^(ui|application)/i.test(request)) { |
9 |
| - |
10 |
| - if (request.indexOf('../') === 0 || request.indexOf('./') === 0) { |
11 |
| - let index = context.indexOf('node_modules') + 'node_modules'.length + 1; |
12 |
| - |
13 |
| - request = path.normalize(path.join(context.substring(index).replace(/\\/g, '/'), request)).replace(/\\/g, '/'); |
| 10 | + if(pkg.nativescript) { |
| 11 | + if(request.match(/^\.{0,2}(?:\/|\\\\|\\)/)) { |
| 12 | + request = path.relative(nodeModulesPath, path.resolve(context, request)) |
| 13 | + } |
| 14 | + return callback(null, 'commonjs ' + request.replace(/\\|\\\\/g, '/')) |
| 15 | + } |
| 16 | + } catch(e) { |
| 17 | + // ignore |
14 | 18 | }
|
15 |
| - else if (/^(ui|application)/i.test(request)) { |
16 |
| - request = `tns-core-modules/${request}`.replace(/\\/g, '/'); |
17 |
| - } |
18 |
| - |
19 |
| - return callback(null, 'commonjs ' + request); |
20 | 19 | }
|
21 | 20 |
|
22 |
| - callback(); |
23 |
| -}; |
| 21 | + callback() |
| 22 | +} |
0 commit comments