UNPKG

2.4 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 ExtractTextPlugin from 'extract-text-webpack-plugin';
10import asyncTestWrapper from './async-test-wrapper';
11
12config = _.cloneDeep(config);
13
14var baseDir = path.join(__dirname, 'app');
15
16config.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
25config.plugins = [
26 new AssetMapPlugin(baseDir + '/assets/map.json'),
27 new ExtractTextPlugin('[name]-[chunkhash].css')
28];
29
30var mapFilePath = config.plugins[0].outputFile;
31
32describe('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});