UNPKG

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