File: test.js

package info (click to toggle)
node-rollup-plugin-replace 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 376 kB
  • sloc: makefile: 6; sh: 2
file content (209 lines) | stat: -rw-r--r-- 5,479 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* eslint-env mocha */
/* eslint-disable no-console */

const assert = require('assert');
const { rollup } = require('rollup');
const replace = require('../dist/rollup-plugin-replace.cjs.js');
const fs = require('fs');
const { SourceMapConsumer } = require('source-map');
const { getLocator } = require('locate-character');

process.chdir(__dirname);

function execute(code, context = {}) {
	let fn;
	const contextKeys = Object.keys(context);
	const argNames = contextKeys.concat('module', 'exports', 'assert', code);

	try {
		fn = new Function(...argNames);
	} catch (err) {
		// syntax error
		console.log(code);
		throw err;
	}
	const module = { exports: {} };
	const argValues = contextKeys.map(key => context[key]).concat(module, module.exports, assert);

	fn(...argValues);

	return module.exports;
}

const getOutputFromGenerated = generated => (generated.output ? generated.output[0] : generated);

async function getCodeFromBundle(bundle, customOptions = {}) {
	const options = Object.assign({ format: 'cjs' }, customOptions);
	return getOutputFromGenerated(await bundle.generate(options)).code;
}

describe('rollup-plugin-replace', () => {
	describe('form', () => {
		const transformContext = {};

		fs.readdirSync('form').forEach(dir => {
			let config;

			try {
				config = require(`./form/${dir}/_config.js`);
			} catch (err) {
				config = {};
			}

			(config.solo ? it.only : it)(`${dir}: ${config.description}`, () => {
				const { transform } = replace(config.options);
				const input = fs.readFileSync(`form/${dir}/input.js`, 'utf-8');
				const expected = fs.readFileSync(`form/${dir}/output.js`, 'utf-8').trim();

				return Promise.resolve(
					transform.call(transformContext, input, `${__dirname}/form/${dir}/input.js`)
				).then(transformed => {
					const actual = (transformed ? transformed.code : input).trim();
					assert.equal(actual, expected);
				});
			});
		});
	});

	describe('function', () => {
		fs.readdirSync('function').forEach(dir => {
			let config;

			try {
				config = require(`./function/${dir}/_config.js`);
			} catch (err) {
				config = {};
			}

			(config.solo ? it.only : it)(`${dir}: ${config.description}`, async () => {
				const options = Object.assign(
					{
						input: `function/${dir}/main.js`
					},
					config.options || {},
					{
						plugins: [
							...((config.options && config.options.plugins) || []),
							replace(config.pluginOptions)
						]
					}
				);

				const bundle = await rollup(options);
				const code = await getCodeFromBundle(bundle);
				if (config.show || config.solo) {
					console.error(code);
				}
				const exports = execute(code, config.context);

				if (config.exports) config.exports(exports);
			});
		});
	});

	describe('misc', () => {
		it('does not mutate the values map properties', async () => {
			const valuesMap = { ANSWER: '42' };
			const bundle = await rollup({
				input: 'main.js',
				plugins: [
					replace({ values: valuesMap }),
					{
						resolveId(id) {
							return id;
						},
						load(importee) {
							if (importee === 'main.js') {
								return 'console.log(ANSWER);';
							}
						}
					}
				]
			});

			const { code } = getOutputFromGenerated(await bundle.generate({ format: 'es' }));
			assert.equal(code.trim(), 'console.log(42);');
			assert.deepEqual(valuesMap, { ANSWER: '42' });
		});

		it('generates sourcemaps', async () => {
			const bundle = await rollup({
				input: 'main.js',
				onwarn(warning) {
					throw new Error(warning.message);
				},
				plugins: [
					replace({ values: { ANSWER: '42' } }),
					{
						resolveId(id) {
							return id;
						},
						load(importee) {
							if (importee === 'main.js') {
								return 'import value from "other.js";\nconsole.log(value);';
							}
							if (importee === 'other.js') {
								return 'export default ANSWER;';
							}
						}
					}
				]
			});

			const { code, map } = getOutputFromGenerated(
				await bundle.generate({ format: 'es', sourcemap: true })
			);

			await SourceMapConsumer.with(map, null, async smc => {
				const locator = getLocator(code, { offsetLine: 1 });

				let generatedLoc = locator('42');
				let loc = smc.originalPositionFor(generatedLoc); // 42
				assert.equal(loc.source, 'other.js');
				assert.equal(loc.line, 1);
				assert.equal(loc.column, 15);

				generatedLoc = locator('log');
				loc = smc.originalPositionFor(generatedLoc); // log
				assert.equal(loc.source, 'main.js');
				assert.equal(loc.line, 2);
				assert.equal(loc.column, 8);
			});
		});

		it('does not generate sourcemaps if disabled', async () => {
			let warned = false;

			const bundle = await rollup({
				input: 'main.js',
				onwarn(warning) {
					assert.equal(
						warning.message,
						"Sourcemap is likely to be incorrect: a plugin ('replace') was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help"
					);
					warned = true;
				},
				plugins: [
					replace({ values: { ANSWER: '42' }, sourcemap: false }),
					{
						resolveId(id) {
							return id;
						},
						load(importee) {
							if (importee === 'main.js') {
								return 'import value from "other.js";\nconsole.log(value);';
							}
							if (importee === 'other.js') {
								return 'export default ANSWER;';
							}
						}
					}
				]
			});

			assert.ok(!warned);
			await bundle.generate({ format: 'es', sourcemap: true });
			assert.ok(warned);
		});
	});
});