UNPKG

3.98 kBJavaScriptView Raw
1var _ = require('lodash');
2var assert = require('assert');
3var async = require('async');
4var fs = require('fs');
5var path = require('path');
6
7var miaow = require('../index');
8
9var cwdPath = path.resolve(__dirname, './fixtures/tasks');
10var outputPath = path.resolve(__dirname, './output');
11var defaultOptions = {
12 cwd: cwdPath,
13 output: outputPath,
14 pack: false
15};
16
17function exec(options, results, cb) {
18 async.series([
19 miaow.compile.bind(miaow, options),
20 function (cb) {
21 async.each(results, function (result, cb) {
22 fs.readFile(
23 path.join(outputPath, result.path),
24 {encoding: 'utf-8'},
25 function (err, data) {
26 if (err) {
27 return cb(err);
28 }
29
30 assert.equal(data, result.content);
31 cb();
32 });
33 }, cb);
34 }
35 ], cb);
36}
37
38describe('任务', function () {
39 this.timeout(10e3);
40
41 it('普通任务', function (done) {
42 var options = _.extend({}, defaultOptions, {
43 module: {
44 tasks: [
45 {
46 test: /foo\.js$/,
47 plugins: function (option, cb) {
48 assert.equal(
49 this.file.contents.toString(),
50 'module.exports = \'你好,世界!\';\n'
51 );
52
53 this.file.contents = new Buffer('module.exports = \'Hello, world!\';\n');
54 cb();
55 }
56 }
57 ]
58 }
59 });
60
61 exec(options, [
62 {
63 path: 'foo.js',
64 content: 'module.exports = \'Hello, world!\';\n'
65 }
66 ], done);
67 });
68
69 it('任务序列', function (done) {
70 var options = _.extend({}, defaultOptions, {
71 module: {
72 tasks: [
73 {
74 test: /foo\.js$/,
75 plugins: [function (option, cb) {
76 assert.equal(this.file.contents.toString(), 'module.exports = \'你好,世界!\';\n');
77 this.file.contents = new Buffer('module.exports = \'Hello, world!\';\n');
78 cb();
79 }, function (option, cb) {
80 assert.equal(this.file.contents.toString(), 'module.exports = \'Hello, world!\';\n');
81 this.file.contents = new Buffer('module.exports = \'Hello, 世界!\';\n');
82 cb();
83 }]
84 }
85 ]
86 }
87 });
88
89 exec(options, [
90 {
91 path: 'foo.js',
92 content: 'module.exports = \'Hello, 世界!\';\n'
93 }
94 ], done);
95 });
96
97 it('使用缓存', function (done) {
98 function addHash(option, cb) {
99 var relativePathList = [];
100 var moduleUrlMap = {};
101 var module = this;
102 var contents = module.file.contents.toString();
103
104 // 获取所有依赖的模块路径
105 contents.replace(/require\(['"](\S+)['"]\)/g, function (statement, relativePath) {
106 relativePathList.push(relativePath);
107
108 return statement;
109 });
110
111 // 获取所有模块的hash值
112 async.eachSeries(_.uniq(relativePathList), function (relativePath, cb) {
113 module.getModule(relativePath, function (err, module) {
114 if (err) {
115 return cb(err);
116 }
117
118 moduleUrlMap[relativePath] = module.url;
119 cb();
120 });
121 }, function (err) {
122 if (err) {
123 return cb(err);
124 }
125
126 // 做内容替换
127 contents = contents.replace(/require\(['"](\S+)['"]\)/g, function (statement, relativePath) {
128 return statement.replace(relativePath, moduleUrlMap[relativePath]);
129 });
130
131 module.file.contents = new Buffer(contents);
132 cb();
133 });
134 }
135
136 var options = _.extend({}, defaultOptions, {
137 domain: '//www.example.com',
138 module: {
139 tasks: [
140 {
141 test: /bar\.js$/,
142 plugins: addHash
143 }
144 ]
145 }
146 });
147
148 exec(options, [
149 {
150 path: 'bar.js',
151 content: 'var foo = require(\'//www.example.com/foo_1ca528407f.js\');\n\nconsole.log(foo);\n'
152 }
153 ], done);
154 });
155});