forked from jmurzy/babel-plugin-transform-assets
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.spec.js
123 lines (105 loc) · 3.87 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
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
import { transformFileSync } from '@babel/core';
import { expect } from 'chai';
import path from 'path';
import fs from 'fs';
import Vinyl from 'vinyl';
import gulpBabel from 'gulp-babel';
import { vol, fs as memfs } from 'memfs';
import { ufs } from 'unionfs';
import { patchFs } from 'fs-monkey';
import withThese from 'mocha-each';
describe('transforms assets', () => {
before(() => {
const ofs = { ...fs };
vol.fromJSON({
'./node_modules/fake-module/file.txt': fs.readFileSync(path.join(__dirname, './file.txt'), 'utf-8'),
});
vol.writeFileSync('./node_modules/fake-module/logo.png', fs.readFileSync(path.join(__dirname, 'fixtures/logo.png')));
ufs.use(memfs).use(ofs);
patchFs(ufs);
});
const transform = (filename, config = {}) => transformFileSync(
path.resolve(__dirname, filename),
{
babelrc: false,
presets: ['@babel/env'],
plugins: [
['./src/index.js', config],
],
},
);
withThese([
['a local file', ''],
['located within a Node.js module', '-from-node-module'],
]).describe('when asset is %s', (description, suffix) => {
it('replaces require statements with filename', () => {
expect(transform(`fixtures/require-txt${suffix}.js`, {
extensions: ['txt'],
}).code).to.be.equal(`"use strict";
var file = "file.txt?vmiIOMq";`);
});
it('replaces import statements with filename', () => {
expect(transform(`fixtures/import-txt${suffix}.js`, {
extensions: ['txt'],
}).code).to.be.equal(`"use strict";
var _file = _interopRequireDefault("file.txt?vmiIOMq");
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${suffix}.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?vmiIOMq");
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/env'],
plugins: [
['./src/index.js', { extensions: ['txt'] }],
],
});
stream.on('data', (file) => {
expect(file.contents.toString()).to.be.equal(`"use strict";
var _file = _interopRequireDefault("file.txt?vmiIOMq");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }`);
});
stream.on('end', cb);
stream.write(new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixtures'),
path: path.join(__dirname, `fixtures/import-txt${suffix}.js`),
contents: fs.readFileSync(path.join(__dirname, `fixtures/import-txt${suffix}.js`)),
}));
stream.end();
});
it('removes global requires', () => {
expect(transform(`fixtures/empty-require${suffix}.js`, {
extensions: ['txt'],
}).code).to.be.equal('"use strict";');
});
it('fixtures/empty-import.js', () => {
expect(transform(`fixtures/empty-import${suffix}.js`, {
extensions: ['txt'],
}).code).to.be.equal('"use strict";');
});
it('fixtures/png-import.js with [contenthash] in name', () => {
expect(transform(`fixtures/png-import${suffix}.js`, {
extensions: ['png'],
name: '/images/[contenthash].[ext]',
}).code).to.be.equal(`"use strict";
var _logo = _interopRequireDefault("/images/b096687fd2b2c782.png");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }`);
});
});
});