File: modules-sync.js

package info (click to toggle)
node-browser-resolve 2.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 716 kB
  • sloc: javascript: 927; makefile: 15; sh: 3
file content (160 lines) | stat: -rw-r--r-- 6,032 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var assert = require('assert');
var resolve = require('../');

var fixtures_dir = __dirname + '/fixtures/node_modules';

// no package.json, load index.js
it('index.js of module dir', function() {
    var path = resolve.sync('module-a', { paths: [ fixtures_dir ], package: { main: 'fixtures' } });
    assert.equal(path, require.resolve('./fixtures/node_modules/module-a/index'));
});

// package.json main field specifies other location
it('alternate main', function() {
    var path = resolve.sync('module-b', { paths: [ fixtures_dir ], package: { main: 'fixtures' } });
    assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main'));
});

// package.json has 'browser' field which is a string
it('string browser field as main', function() {
    var path = resolve.sync('module-c', { paths: [ fixtures_dir ], package: { main: 'fixtures' } });
    assert.equal(path, require.resolve('./fixtures/node_modules/module-c/browser'));
});

// package.json has 'browser' field which is a string
it('string browser field as main - require subfile', function() {
    var parent = {
        filename: fixtures_dir + '/module-c/browser.js',
        paths: [ fixtures_dir + '/module-c/node_modules' ],
        package: { main: './browser.js' }
    };

    var path = resolve.sync('./bar', parent);
    assert.equal(path, require.resolve('./fixtures/node_modules/module-c/bar'));
});

// package.json has browser field as object
// one of the keys replaces the main file
// this would be done if the user needed to replace main and some other module
it('object browser field as main', function() {
    var path = resolve.sync('module-d', { paths: [ fixtures_dir ], package: { main: 'fixtures' } });
    assert.equal(path, require.resolve('./fixtures/node_modules/module-d/browser'));
});

// package.json has browser field as object
// one of the keys replaces the main file
// however the main has no prefix and browser uses ./ prefix for the same file
it('object browser field as main', function() {
    var path = resolve.sync('module-k', { paths: [ fixtures_dir ], package: { main: 'fixtures' } });
    assert.equal(path, require.resolve('./fixtures/node_modules/module-k/browser'));
});

// browser field in package.json maps ./foo.js -> ./browser.js
// when we resolve ./foo while in module-e, this mapping should take effect
// the result is that ./foo resolves to ./browser
it('object browser field replace file', function() {
    var parent = {
        filename: fixtures_dir + '/module-e/main.js',
        package: { main: './main.js' }
    };

    var path = resolve.sync('./foo', parent);
    assert.equal(path, require.resolve('./fixtures/node_modules/module-e/browser'));
});

// browser field in package.json maps "module" -> "alternate module"
it('test foobar -> module-b replacement', function() {
    var parent = {
        filename: fixtures_dir + '/module-h/index.js',
        package: { main: './index.js' }
    };

    var path = resolve.sync('foobar', parent);
    assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main'));
});

// browser field in package.json maps "relative file" -> "relative file" with no extension
it('test ./x -> ./y replacement', function() {
    var parent = {
        filename: fixtures_dir + '/module-t/index.js',
        package: { main: './index.js' }
    };

    var path = resolve.sync('./x', parent);
    assert.equal(path, require.resolve('./fixtures/node_modules/module-t/y.js'));
});

// same as above but replacing core
it('test core -> module-c replacement', function() {
    var parent = {
        filename: fixtures_dir + '/module-h/index.js',
        package: { main: './index.js' }
    };

    var path = resolve.sync('querystring', parent);
    assert.equal(path, require.resolve('./fixtures/node_modules/module-c/browser'));
});

// browser field in package.json maps "module" -> "alternate module"
it('test foobar -> module-b replacement with transform', function() {
    var parent = {
        filename: fixtures_dir + '/module-i/index.js',
        package: { main: './index.js' }
    };

    var path = resolve.sync('foobar', parent);
    assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main'));
});

it('test foobar -> module-i replacement with transform in replacement', function() {
    var parent = {
        filename: fixtures_dir + '/module-j/index.js',
        package: { main: './index.js' }
    };

    var path = resolve.sync('foobar', parent);
    assert.equal(path, require.resolve('./fixtures/node_modules/module-i/index'));
});

// same as above, but without a paths field in parent
// should still checks paths on the filename of parent
it('object browser field replace file - no paths', function() {
    var parent = {
        filename: fixtures_dir + '/module-f/lib/main.js',
        package: { main: './lib/main.js' }
    };

    var path = resolve.sync('./foo', parent);
    assert.equal(path, require.resolve('./fixtures/node_modules/module-f/lib/browser'));
});

it('replace module in browser field object', function() {
    var parent = {
        filename: fixtures_dir + '/module-g/index.js',
        package: { main: './index.js' }
    };

    var path = resolve.sync('foobar', parent);
    assert.equal(path, require.resolve('./fixtures/node_modules/module-g/foobar-browser'));
});

it('override engine shim', function() {
    var parent = {
        filename: fixtures_dir + '/override-engine-shim/index.js',
        package: { main: './index.js' },
        modules: { url: "wonderland" }
    };
    var path = resolve.sync('url', parent);
    assert.equal(path, require.resolve('./fixtures/node_modules/override-engine-shim/url-browser'));
});

it('alt-browser field', function() {
    var parent = {
        filename: fixtures_dir + '/alt-browser-field/index.js',
        package: { main: './index.js' },
        browser: 'chromeapp'
    };

    var path = resolve.sync('url', parent);
    assert.equal(path, require.resolve('./fixtures/node_modules/alt-browser-field/url-chromeapp'));
});