1 | import _ from 'lodash';
|
2 | import path from 'path';
|
3 | import webpack from 'webpack';
|
4 | import rimraf from 'rimraf';
|
5 | import fs from 'fs';
|
6 | import config from './webpack.config';
|
7 | import AssetMapPlugin from '../src';
|
8 | import asyncTestWrapper from './async-test-wrapper';
|
9 |
|
10 | config = _.cloneDeep(config);
|
11 |
|
12 | var baseDir = path.join(__dirname, 'app');
|
13 |
|
14 | config.plugins = [
|
15 | new AssetMapPlugin(baseDir + '/assets/map.json', baseDir)
|
16 | ];
|
17 |
|
18 | var mapFilePath = config.plugins[0].outputFile;
|
19 |
|
20 | describe('Relative to use case', () => {
|
21 | it('Generates map.json with map to asset entries, relative to alternate path', done => {
|
22 | rimraf(config.output.path, () => {
|
23 | webpack(config, (err, stats) => {
|
24 | asyncTestWrapper(() => {
|
25 | if (err) throw err;
|
26 | if (stats.hasErrors()) throw 'webpack has errors';
|
27 | if (stats.hasWarnings()) throw 'webpack has warnings';
|
28 |
|
29 | var mapSrc = fs.readFileSync(mapFilePath, {encoding: 'utf-8'});
|
30 | var map = JSON.parse(mapSrc).assets;
|
31 |
|
32 | map['./smiley.jpeg'].should.match(/\/smiley-[0-9a-f]+\.jpeg$/);
|
33 | map['./test-checklist.jpeg'].should.match(/\/test-checklist-[0-9a-f]+\.jpeg$/);
|
34 | }, done);
|
35 | });
|
36 | })
|
37 | });
|
38 |
|
39 | it('Generates map.json with map to chunk entries', done => {
|
40 | rimraf(config.output.path, () => {
|
41 | webpack(config, (err, stats) => {
|
42 | asyncTestWrapper(() => {
|
43 | if (err) throw err;
|
44 | if (stats.hasErrors()) throw 'webpack has errors';
|
45 | if (stats.hasWarnings()) throw 'webpack has warnings';
|
46 |
|
47 | var mapSrc = fs.readFileSync(mapFilePath, {encoding: 'utf-8'});
|
48 | var map = JSON.parse(mapSrc).chunks;
|
49 |
|
50 | expect(map.entry1.length).to.equal(1);
|
51 | map.entry1[0].should.match(/^\/assets\/entry1-[0-9a-f]+\.js$/);
|
52 | expect(map.entry2.length).to.equal(1);
|
53 | map.entry2[0].should.match(/^\/assets\/entry2-[0-9a-f]+\.js$/);
|
54 | }, done);
|
55 | });
|
56 | })
|
57 | });
|
58 | });
|