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 touch from 'touch';
|
8 | import AssetMapPlugin from '../src';
|
9 | import ExtractTextPlugin from 'extract-text-webpack-plugin';
|
10 | import asyncTestWrapper from './async-test-wrapper';
|
11 |
|
12 | config = _.cloneDeep(config);
|
13 |
|
14 | var baseDir = path.join(__dirname, 'app');
|
15 |
|
16 | config.module.loaders = config.module.loaders.map(l => {
|
17 | if (l.loader.indexOf('less') === -1) {
|
18 | return l;
|
19 | }
|
20 |
|
21 | l.loader = ExtractTextPlugin.extract('style', 'css!less');
|
22 | return l;
|
23 | });
|
24 |
|
25 | config.plugins = [
|
26 | new AssetMapPlugin(baseDir + '/assets/map.json'),
|
27 | new ExtractTextPlugin('[name]-[chunkhash].css')
|
28 | ];
|
29 |
|
30 | var mapFilePath = config.plugins[0].outputFile;
|
31 |
|
32 | describe('Extract text plugin use case', () => {
|
33 | it('Generates map.json with map to asset entries', done => {
|
34 | rimraf(config.output.path, () => {
|
35 | webpack(config, (err, stats) => {
|
36 | asyncTestWrapper(() => {
|
37 | if (err) throw err;
|
38 | if (stats.hasErrors()) throw 'webpack has errors';
|
39 | if (stats.hasWarnings()) throw 'webpack has warnings';
|
40 |
|
41 | var mapSrc = fs.readFileSync(mapFilePath, {encoding: 'utf-8'});
|
42 | var map = JSON.parse(mapSrc).assets;
|
43 |
|
44 | map['../smiley.jpeg'].should.match(/\/smiley-[0-9a-f]+\.jpeg$/);
|
45 | map['../test-checklist.jpeg'].should.match(/\/test-checklist-[0-9a-f]+\.jpeg$/);
|
46 | }, done);
|
47 | });
|
48 | })
|
49 | });
|
50 |
|
51 | it('Generates map.json with map to chunk entries', done => {
|
52 | rimraf(config.output.path, () => {
|
53 | webpack(config, (err, stats) => {
|
54 | asyncTestWrapper(() => {
|
55 | if (err) throw err;
|
56 | if (stats.hasErrors()) throw 'webpack has errors';
|
57 | if (stats.hasWarnings()) throw 'webpack has warnings';
|
58 |
|
59 | var mapSrc = fs.readFileSync(mapFilePath, {encoding: 'utf-8'});
|
60 | var map = JSON.parse(mapSrc).chunks;
|
61 |
|
62 | expect(map.entry1.length).to.equal(2);
|
63 | map.entry1[0].should.match(/^\/assets\/entry1-[0-9a-f]+\.js$/);
|
64 | map.entry1[1].should.match(/^\/assets\/entry1-[0-9a-f]+\.css/);
|
65 | expect(map.entry2.length).to.equal(2);
|
66 | map.entry2[0].should.match(/^\/assets\/entry2-[0-9a-f]+\.js$/);
|
67 | map.entry2[1].should.match(/^\/assets\/entry2-[0-9a-f]+\.css/);
|
68 | }, done);
|
69 | });
|
70 | })
|
71 | });
|
72 | });
|