This repository has been archived by the owner on Jul 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.test.js
165 lines (141 loc) · 4.34 KB
/
index.test.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
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
/* eslint-disable no-undefined */
const realFs = require('fs');
const path = require('path');
const webpack = require('webpack');
const MemoryFileSystem = require('memory-fs');
const chai = require('chai');
const mocha = require('mocha');
const expect = chai.expect;
const describe = mocha.describe;
const it = mocha.it;
const Plugin = require('./');
const baseConfig = require('./test/webpack.config');
const manifestOutput = path.join(__dirname, 'test', 'build');
const filePath = path.join(manifestOutput, 'manifest.json');
const runWebpack = function(callback, plugin, webpackConfig) {
const config = Object.assign(baseConfig, webpackConfig || {});
config.plugins.push(plugin);
const compiler = webpack(config);
this.fs = compiler.outputFileSystem = new MemoryFileSystem();
compiler.run(callback.bind(this));
};
describe('webpack-asset-pipeline plugin', () => {
it('can be required', () => {
expect(Plugin).not.to.eq(undefined);
expect(new Plugin().apply).not.to.eq(undefined);
});
it('it emits a file called manifest.json', (done) => {
runWebpack(function(err) {
const fs = this.fs;
const manifestFile = fs.readFileSync(filePath, 'utf-8');
expect(err).to.eq(null);
expect(manifestFile).not.to.eq(undefined);
done();
}, new Plugin());
});
it('it can specify the file name of emitted file', (done) => {
runWebpack(function(err) {
const fs = this.fs;
const alternateFilePath = path.join(manifestOutput, 'allFiles.json');
const manifestFile = fs.readFileSync(alternateFilePath, 'utf-8');
expect(err).to.eq(null);
expect(manifestFile).not.to.eq(undefined);
done();
}, new Plugin({
fileName: 'allFiles.json'
}));
});
it('it should write both to memory and filesystem', (done) => {
runWebpack(function(err) {
const fs = this.fs;
const manifestFile = fs.readFileSync(filePath, 'utf-8');
const manifestFileReal = realFs.readFileSync(filePath, 'utf-8');
expect(manifestFile).to.eq(manifestFileReal);
expect(err).to.eq(null);
expect(manifestFile).not.to.eq(undefined);
done();
}, new Plugin({
writeToFileEmit: true
}));
});
it('it should be able to specify extraneous files', (done) => {
runWebpack(function(err) {
const fs = this.fs;
const manifestFile = fs.readFileSync(filePath, 'utf-8');
const parsed = JSON.parse(manifestFile);
expect(parsed['output.txt']).to.eq('file.txt');
expect(err).to.eq(null);
done();
}, new Plugin({
extraneous: {
'output.txt': 'file.txt'
}
}));
});
it('it should key files by their require path', (done) => {
runWebpack(function(err) {
const fs = this.fs;
const key = './my-files/file.txt';
const manifestFile = fs.readFileSync(filePath, 'utf-8');
const parsed = JSON.parse(manifestFile);
expect(parsed[key]).not.to.eq(undefined);
expect(err).to.eq(null);
done();
}, new Plugin(), {
module: {
loaders: [{
test: /\.(txt|png)$/,
loader: 'file-loader'
}]
}
});
});
it('it should key files by their require path', (done) => {
const newPath = 'test/';
runWebpack(function(err) {
const fs = this.fs;
const manifestFile = fs.readFileSync(filePath, 'utf-8');
const parsed = JSON.parse(manifestFile);
Object.keys(parsed).forEach((parsedKey) => {
expect(parsedKey.includes(newPath)).to.equal(true);
});
expect(err).to.eq(null);
done();
}, new Plugin({
mapAssetPath(assetPath, name) {
return `${newPath}/${name}`;
}
}), {
module: {
loaders: [{
test: /\.(txt|png)$/,
loader: 'file-loader'
}]
}
});
});
it('it should specify if file is chunk', (done) => {
const newPath = 'test/';
const chunkFileName = 'main.js';
runWebpack(function(err) {
expect(err).to.eq(null);
done();
}, new Plugin({
mapAssetPath(assetPath, name, isChunk) {
if (name === chunkFileName) {
expect(isChunk).to.equal(true);
} else {
expect(isChunk).to.equal(false);
}
return `${newPath}/${name}`;
}
}), {
module: {
loaders: [{
test: /\.(txt|png)$/,
loader: 'file'
}]
}
});
});
});