UNPKG

3.67 kBJavaScriptView Raw
1import _ from 'lodash';
2import path from 'path';
3import webpack from 'webpack';
4import rimraf from 'rimraf';
5import fs from 'fs';
6import config from './webpack.config';
7import touch from 'touch';
8import AssetMapPlugin from '../src';
9import asyncTestWrapper from './async-test-wrapper';
10
11config = _.cloneDeep(config);
12
13var baseDir = path.join(__dirname, 'app');
14
15config.plugins = [
16 new AssetMapPlugin(baseDir + '/assets/map.json')
17];
18
19var mapFilePath = config.plugins[0].outputFile;
20
21describe('Basic use case', () => {
22 it('Generates map.json with map to asset entries', done => {
23 rimraf(config.output.path, () => {
24 webpack(config, (err, stats) => {
25 asyncTestWrapper(() => {
26 if (err) throw err;
27 if (stats.hasErrors()) throw 'webpack has errors';
28 if (stats.hasWarnings()) throw 'webpack has warnings';
29
30 var mapSrc = fs.readFileSync(mapFilePath, {encoding: 'utf-8'});
31 var map = JSON.parse(mapSrc).assets;
32
33 map['../smiley.jpeg'].should.match(/\/smiley-[0-9a-f]+\.jpeg$/);
34 map['../test-checklist.jpeg'].should.match(/\/test-checklist-[0-9a-f]+\.jpeg$/);
35 }, done);
36 });
37 })
38 });
39
40 it('Generates map.json with map to chunk entries', done => {
41 rimraf(config.output.path, () => {
42 webpack(config, (err, stats) => {
43 asyncTestWrapper(() => {
44 if (err) throw err;
45 if (stats.hasErrors()) throw 'webpack has errors';
46 if (stats.hasWarnings()) throw 'webpack has warnings';
47
48 var mapSrc = fs.readFileSync(mapFilePath, {encoding: 'utf-8'});
49 var map = JSON.parse(mapSrc).chunks;
50
51 expect(map.entry1.length).to.equal(1);
52 map.entry1[0].should.match(/^\/assets\/entry1-[0-9a-f]+\.js$/);
53 expect(map.entry2.length).to.equal(1);
54 map.entry2[0].should.match(/^\/assets\/entry2-[0-9a-f]+\.js$/);
55 }, done);
56 });
57 })
58 });
59
60 // Since both Webpack and Mocha watch muck with module loading if you run
61 // mocha in watch mode it will keep re-running this module even without a
62 // file save.
63 it('Only emits if an asset has changed', function(done) {
64 this.timeout(5000);
65
66 rimraf(config.output.path, () => {
67 var compiler = webpack(config);
68 var watcher;
69 var lastMapStats;
70 var assetMap = __dirname + '/app/assets/map.json';
71 var entry1Js = __dirname + '/app/entry1.js'
72 var smiley = __dirname + '/app/smiley.jpeg';
73 var watchCompletions = [
74 function FirstWatchComplete() {
75 lastMapStats = fs.statSync(assetMap);
76 touch.sync(entry1Js);
77 },
78 function SecondWatchComplete() {
79 var newStats = fs.statSync(assetMap);
80 newStats.mtime.should.eql(lastMapStats.mtime);
81 touch.sync(smiley);
82 },
83 function ThirdWatchComplete() {
84 var newStats = fs.statSync(assetMap);
85 newStats.mtime.should.not.eql(lastMapStats.mtime);
86 touch.sync(entry1Js);
87 },
88 function LastWatchComplete() {
89 watcher.close(done);
90 }
91 ];
92 var next = watchCompletions.reverse()
93 .reduce((acc, func) => {
94 return () => {
95 next = acc;
96 func();
97 };
98 }, () => { /* NO OP */ });
99
100 watcher = compiler.watch(1000, (err, stats) => {
101 try {
102 if (err) throw err;
103 if (stats.hasErrors()) throw 'webpack has errors';
104 if (stats.hasWarnings()) throw 'webpack has warnings';
105
106 next();
107 } catch (e) {
108 if (watcher) {
109 watcher.close();
110 }
111
112 done(e);
113 }
114 });
115 })
116 });
117});