Skip to content

Commit 6732548

Browse files
committed
fix: concat all
1 parent 9da2483 commit 6732548

File tree

2 files changed

+134
-131
lines changed

2 files changed

+134
-131
lines changed

lib/concatJS.js

+27-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const cheerio = require('cheerio');
44
const Promise = require('bluebird');
5-
const minimatch = require('minimatch');
5+
// const minimatch = require('minimatch');
66

77
function concatJS(data) {
88
const hexo = this;
@@ -14,8 +14,8 @@ function concatJS(data) {
1414
const route = hexo.route;
1515
const routeList = route.list();
1616

17-
let include = options.include;
18-
if (include && !Array.isArray(include)) include = [include];
17+
// let include = options.include;
18+
// if (include && !Array.isArray(include)) include = [include];
1919

2020
// 1. get all local scripts
2121
const htmls = {};
@@ -38,7 +38,7 @@ function concatJS(data) {
3838
if (!src.startsWith('//') && !src.startsWith('http')) {
3939
src = route.format(src);
4040
// ignore and remove duplicated
41-
if(srcs.indexOf(src) === -1){
41+
if (srcs.indexOf(src) === -1) {
4242
srcs.push(src);
4343
$scripts[src] = $script;
4444
} else {
@@ -57,43 +57,46 @@ function concatJS(data) {
5757
// 2. extract common scripts
5858
.then(() => {
5959
const htmlPaths = Object.keys(htmls);
60-
const commons = [];
60+
const scripts = [];
6161
// collect the scripts
6262
for (const path of htmlPaths) {
6363
const html = htmls[path];
64-
for (const src of html.srcs.reverse()) { // reverse for deletion
65-
if (commons.indexOf(src) === -1) {
64+
const srcs = html.srcs;
65+
for(let i = srcs.length - 1; i >= 0; --i) { // reverse for deletion
66+
const src = srcs[i];
67+
if (scripts.indexOf(src) === -1) {
6668
// if a script exists in every html which has scripts,
6769
// or match the pattern in include array
68-
if (htmlPaths.every(path => htmls[path].srcs.indexOf(src) !== -1) ||
69-
include.some(pattern => minimatch(src, pattern, { matchBase: true }))) {
70-
// remove
71-
log.log('update Concate JS: remove %s from %s', src, path);
72-
html.$scripts[src].remove()
73-
delete html.$scripts[src];
74-
html.srcs.splice(html.srcs.indexOf(src), 1);
75-
commons.push(src);
76-
}
77-
} else {
78-
// remove
79-
log.log('update Concate JS: remove %s from %s', src, path);
80-
html.$scripts[src].remove()
81-
delete html.$scripts[src];
82-
html.srcs.splice(html.srcs.indexOf(src), 1);
70+
// if (htmlPaths.every(path => htmls[path].srcs.indexOf(src) !== -1) ||
71+
// include.some(pattern => minimatch(src, pattern, { matchBase: true }))) {
72+
// // remove
73+
// log.log('update Concate JS: remove %s from %s', src, path);
74+
// html.$scripts[src].remove()
75+
// delete html.$scripts[src];
76+
// html.srcs.splice(html.srcs.indexOf(src), 1);
77+
scripts.push(src);
78+
//}
8379
}
80+
// remove
81+
log.log('update Concate JS: remove %s from %s', src, path);
82+
html.$scripts[src].remove()
83+
delete html.$scripts[src];
84+
srcs.splice(srcs.indexOf(src), 1);
8485
}
86+
html.srcs = srcs;
8587
}
8688

87-
if (commons.length > 1) {
89+
if (scripts.length > 1) {
8890
// add the bundle script to all html
8991
for (const path of htmlPaths) {
9092
const html = htmls[path];
93+
//html.$(`<script type="text/javascript" src="${route.format(bundlePath)}"></script>`).insertBefore(html.$('body>script[src]').first());
9194
html.$('body').append(`<script type="text/javascript" src="${route.format(bundlePath)}"></script>`);
9295
route.set(path, html.$.html());
9396
log.log('update Concate JS: add %s to %s', route.format(bundlePath), path);
9497
}
9598
}
96-
return commons;
99+
return scripts.reverse();
97100
})
98101
// 3. concat the script
99102
.then((scripts) => {

test/concatJS.test.js

+107-107
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('ConcatJS', () => {
103103
});
104104
});
105105

106-
it('should not concat scripts which exist in some htmls (which have local scripts)', () => {
106+
it('should concat scripts which exist in some htmls', () => {
107107

108108
const promise = concatJS.call(hexo);
109109
return promise.then(() => {
@@ -119,16 +119,16 @@ describe('ConcatJS', () => {
119119
});
120120
// assertion
121121
if (file === 'concatJS1.html') {
122-
expect(srcs.some(src => src.startsWith('/script2.js'))).to.be.true;
122+
expect(srcs.some(src => src.startsWith('/script2.js'))).to.be.false;
123123
}
124124
} else if (file.includes('script2.js')) {
125-
expect(hexoRoute.buffer[format(file)], 'js file has been removed').to.be.ok;
125+
expect(hexoRoute.buffer[format(file)], 'js file has been removed').to.be.undefined;
126126
}
127127
}
128128
});
129129
});
130130

131-
it('should concat scripts which exist in all htmls (which have local scripts)', () => {
131+
it('should concat scripts which exist in all htmls', () => {
132132

133133
const promise = concatJS.call(hexo);
134134
return promise.then(() => {
@@ -158,115 +158,115 @@ describe('ConcatJS', () => {
158158
});
159159
});
160160

161-
describe('when `include` option exist', () => {
162-
// Configure.
163-
let include = ['script2'];
164-
const hexo = {
165-
config: {
166-
js_concator: {
167-
enable: true,
168-
bundle_path: '//js/bundle.js',
169-
include,
170-
}
171-
},
172-
route: hexoRoute,
173-
};
161+
// describe('when `include` option exist', () => {
162+
// // Configure.
163+
// let include = ['script2'];
164+
// const hexo = {
165+
// config: {
166+
// js_concator: {
167+
// enable: true,
168+
// bundle_path: '//js/bundle.js',
169+
// include,
170+
// }
171+
// },
172+
// route: hexoRoute,
173+
// };
174174

175-
beforeEach(() => {
176-
include = ['script2'];
177-
});
175+
// beforeEach(() => {
176+
// include = ['script2'];
177+
// });
178178

179-
it('should warp the `include` to an array if it is not an array', () => {
180-
include = 'script'
181-
hexo.config.js_concator.include = include;
182-
const promise = concatJS.call(hexo);
183-
return promise.then(() => {
184-
for (const file of fixtures) {
185-
if (file.includes('.html')) {
186-
// extract src
187-
const $ = cheerio.load(hexoRoute.buffer[file]);
188-
const srcs = [];
189-
$('script[src]').each((idx, ele) => {
190-
const $script = $(ele);
191-
const src = $script.attr('src');
192-
srcs.push(src);
193-
});
179+
// it('should warp the `include` to an array if it is not an array', () => {
180+
// include = 'script'
181+
// hexo.config.js_concator.include = include;
182+
// const promise = concatJS.call(hexo);
183+
// return promise.then(() => {
184+
// for (const file of fixtures) {
185+
// if (file.includes('.html')) {
186+
// // extract src
187+
// const $ = cheerio.load(hexoRoute.buffer[file]);
188+
// const srcs = [];
189+
// $('script[src]').each((idx, ele) => {
190+
// const $script = $(ele);
191+
// const src = $script.attr('src');
192+
// srcs.push(src);
193+
// });
194194

195-
// assertion
196-
if (file === 'concatJS1.html' || file === 'concatJS2.html') {
197-
expect(srcs).contains(format(hexo.config.js_concator.bundle_path));
198-
} else {
199-
expect(srcs).does.not.contains(format(hexo.config.js_concator.bundle_path));
200-
}
201-
} else if (file.includes('script1.js') ||
202-
file.includes('script3.js') ||
203-
[include].some(pattern => minimatch(file, pattern, { matchBase: true }))) {
204-
expect(hexoRoute.buffer[format(file)], 'js file has been removed').to.be.undefined;
205-
}
206-
expect(hexoRoute.buffer[format(hexo.config.js_concator.bundle_path)]).to.has.length.greaterThan(0);
207-
}
208-
});
209-
});
195+
// // assertion
196+
// if (file === 'concatJS1.html' || file === 'concatJS2.html') {
197+
// expect(srcs).contains(format(hexo.config.js_concator.bundle_path));
198+
// } else {
199+
// expect(srcs).does.not.contains(format(hexo.config.js_concator.bundle_path));
200+
// }
201+
// } else if (file.includes('script1.js') ||
202+
// file.includes('script3.js') ||
203+
// [include].some(pattern => minimatch(file, pattern, { matchBase: true }))) {
204+
// expect(hexoRoute.buffer[format(file)], 'js file has been removed').to.be.undefined;
205+
// }
206+
// expect(hexoRoute.buffer[format(hexo.config.js_concator.bundle_path)]).to.has.length.greaterThan(0);
207+
// }
208+
// });
209+
// });
210210

211-
it('should not touch the remote scripts', () => {
211+
// it('should not touch the remote scripts', () => {
212212

213-
const promise = concatJS.call(hexo);
214-
return promise.then(() => {
215-
for (const file of fixtures) {
216-
if (file.includes('.html')) {
217-
const $raw = cheerio.load(htmls[file]);
218-
const expectRemoteScripts = [];
219-
$raw('script[src]').each((idx, ele) => {
220-
const $script = $raw(ele);
221-
const src = $script.attr('src');
222-
if (src.startsWith('//') || src.startsWith('http')) {
223-
expectRemoteScripts.push(src);
224-
}
225-
});
213+
// const promise = concatJS.call(hexo);
214+
// return promise.then(() => {
215+
// for (const file of fixtures) {
216+
// if (file.includes('.html')) {
217+
// const $raw = cheerio.load(htmls[file]);
218+
// const expectRemoteScripts = [];
219+
// $raw('script[src]').each((idx, ele) => {
220+
// const $script = $raw(ele);
221+
// const src = $script.attr('src');
222+
// if (src.startsWith('//') || src.startsWith('http')) {
223+
// expectRemoteScripts.push(src);
224+
// }
225+
// });
226226

227-
const $ = cheerio.load(hexoRoute.buffer[file]);
228-
const actualRemoteScripts = [];
229-
$('script[src]').each((idx, ele) => {
230-
const $script = $(ele);
231-
const src = $script.attr('src');
232-
if (src.startsWith('//') || src.startsWith('http')) {
233-
actualRemoteScripts.push(src);
234-
}
235-
});
236-
expect(actualRemoteScripts).to.be.deep.equal(expectRemoteScripts);
237-
}
238-
}
239-
});
240-
});
227+
// const $ = cheerio.load(hexoRoute.buffer[file]);
228+
// const actualRemoteScripts = [];
229+
// $('script[src]').each((idx, ele) => {
230+
// const $script = $(ele);
231+
// const src = $script.attr('src');
232+
// if (src.startsWith('//') || src.startsWith('http')) {
233+
// actualRemoteScripts.push(src);
234+
// }
235+
// });
236+
// expect(actualRemoteScripts).to.be.deep.equal(expectRemoteScripts);
237+
// }
238+
// }
239+
// });
240+
// });
241241

242-
it('should concat scripts which exist in all htmls (which have local scripts) or match the `include pattern`', () => {
243-
const promise = concatJS.call(hexo);
244-
return promise.then(() => {
245-
for (const file of fixtures) {
246-
if (file.includes('.html')) {
247-
// extract src
248-
const $ = cheerio.load(hexoRoute.buffer[file]);
249-
const srcs = [];
250-
$('script[src]').each((idx, ele) => {
251-
const $script = $(ele);
252-
const src = $script.attr('src');
253-
srcs.push(src);
254-
});
242+
// it('should concat scripts which exist in all htmls (which have local scripts) or match the `include pattern`', () => {
243+
// const promise = concatJS.call(hexo);
244+
// return promise.then(() => {
245+
// for (const file of fixtures) {
246+
// if (file.includes('.html')) {
247+
// // extract src
248+
// const $ = cheerio.load(hexoRoute.buffer[file]);
249+
// const srcs = [];
250+
// $('script[src]').each((idx, ele) => {
251+
// const $script = $(ele);
252+
// const src = $script.attr('src');
253+
// srcs.push(src);
254+
// });
255255

256-
// assertion
257-
if (file === 'concatJS1.html' || file === 'concatJS2.html') {
258-
expect(srcs).contains(format(hexo.config.js_concator.bundle_path));
259-
} else {
260-
expect(srcs).does.not.contains(format(hexo.config.js_concator.bundle_path));
261-
}
262-
} else if (file.includes('script1.js') ||
263-
file.includes('script3.js') ||
264-
include.some(pattern => minimatch(file, pattern, { matchBase: true }))) {
265-
expect(hexoRoute.buffer[format(file)], 'js file has been removed').to.be.undefined;
266-
}
267-
expect(hexoRoute.buffer[format(hexo.config.js_concator.bundle_path)]).to.has.length.greaterThan(0);
268-
}
269-
});
270-
});
271-
});
256+
// // assertion
257+
// if (file === 'concatJS1.html' || file === 'concatJS2.html') {
258+
// expect(srcs).contains(format(hexo.config.js_concator.bundle_path));
259+
// } else {
260+
// expect(srcs).does.not.contains(format(hexo.config.js_concator.bundle_path));
261+
// }
262+
// } else if (file.includes('script1.js') ||
263+
// file.includes('script3.js') ||
264+
// include.some(pattern => minimatch(file, pattern, { matchBase: true }))) {
265+
// expect(hexoRoute.buffer[format(file)], 'js file has been removed').to.be.undefined;
266+
// }
267+
// expect(hexoRoute.buffer[format(hexo.config.js_concator.bundle_path)]).to.has.length.greaterThan(0);
268+
// }
269+
// });
270+
// });
271+
// });
272272
});

0 commit comments

Comments
 (0)