-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.spec.js
97 lines (77 loc) · 2.66 KB
/
index.spec.js
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
import { transformFileSync } from '@babel/core';
import { expect } from 'chai';
import path from 'path';
import fs from 'fs';
import gulpUtil from 'gulp-util';
import gulpBabel from 'gulp-babel';
describe('transforms assets', () => {
const transform = (filename, config = {}) =>
transformFileSync(path.resolve(__dirname, filename), {
babelrc: false,
presets: ['@babel/es2015'],
plugins: [
['./src/index.js', config],
],
});
it('replaces require statements with filename', () => {
expect(transform('fixtures/require-txt.js', {
extensions: ['txt'],
}).code).to.be.equal(`"use strict";
var file = "file.txt?9LDjftP";`);
});
it('replaces import statements with filename', () => {
expect(transform('fixtures/import-txt.js', {
extensions: ['txt'],
}).code).to.be.equal(`"use strict";
var _file = _interopRequireDefault("file.txt?9LDjftP");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }`);
});
it('replaces import statements with filename and then exports', () => {
expect(transform('fixtures/import-export-txt.js', {
extensions: ['txt'],
}).code).to.be.equal(`"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "file", {
enumerable: true,
get: function get() {
return _file.default;
}
});
var _file = _interopRequireDefault("file.txt?9LDjftP");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }`);
});
it('replaces import statement with filename via gulp', (cb) => {
const stream = gulpBabel({
babelrc: false,
presets: ['@babel/es2015'],
plugins: [
['./src/index.js', { extensions: ['txt'] }],
],
});
stream.on('data', (file) => {
expect(file.contents.toString()).to.be.equal(`"use strict";
var _file = _interopRequireDefault("file.txt?9LDjftP");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }`);
});
stream.on('end', cb);
stream.write(new gulpUtil.File({
cwd: __dirname,
base: path.join(__dirname, 'fixtures'),
path: path.join(__dirname, 'fixtures/import-txt.js'),
contents: fs.readFileSync(path.join(__dirname, 'fixtures/import-txt.js')),
}));
stream.end();
});
it('removes global requires', () => {
expect(transform('fixtures/empty-require.js', {
extensions: ['txt'],
}).code).to.be.equal('"use strict";');
});
it('fixtures/empty-import.js', () => {
expect(transform('fixtures/empty-import.js', {
extensions: ['txt'],
}).code).to.be.equal('"use strict";');
});
});