Skip to content

Commit 50198e8

Browse files
frederickjeanguerinhustxiaoc
authored andcommitted
feat: add support for .mjs files
1 parent 9fe88e8 commit 50198e8

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
build
3+
package-lock.json

index.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,26 @@ const PluginError = require('plugin-error');
88
const colors = require('ansi-colors');
99
const reSourceMapComment = /\n\/\/# sourceMappingURL=.+?$/;
1010
const pathSeparatorRe = /[\/\\]/g;
11+
const jsExtensions = /\.m?js$/;
1112

12-
function parseExt(ext) {
13+
function parseExt(ext, byDefault = ".js") {
1314

1415
let _ext = {};
1516

1617
if (!ext) {
1718
_ext = {
18-
min: "-min.js",
19-
src: ".js"
19+
min: "-min" + byDefault,
20+
src: byDefault
2021
}
2122
} else if (typeof ext === "string") {
2223
_ext = {
2324
min: ext,
24-
src: ".js"
25+
src: byDefault
2526
}
2627
} else {
2728
_ext = {
28-
min: ext.min || "-min.js",
29-
src: ext.src || ".js"
29+
min: ext.min || "-min" + byDefault,
30+
src: ext.src || byDefault
3031
}
3132
}
3233

@@ -52,9 +53,6 @@ module.exports = function(opt) {
5253
//Set the options to the one provided or an empty object.
5354
let options = opt || {};
5455

55-
//Parse the extensions form the options.
56-
let ext = parseExt(options.ext);
57-
5856
//Set options output to itself, or, if null an empty object.
5957
options.output = options.output || {};
6058

@@ -86,7 +84,7 @@ module.exports = function(opt) {
8684
});
8785
}
8886

89-
if (ignore || path.extname(file.path) != '.js') {
87+
if (ignore || !path.extname(file.path).match(jsExtensions)) {
9088
this.push(file);
9189
return callback();
9290
}
@@ -111,9 +109,12 @@ module.exports = function(opt) {
111109
}
112110
options.fromString = options.hasOwnProperty("fromString") ? options.fromString : true;
113111

112+
//Parse the extensions form the options.
113+
let ext = parseExt(options.ext, path.extname(file.path));
114+
114115
let min_file = new Vinyl({
115116
base: file.base,
116-
path: Array.isArray(ext.min) ? file.path.replace(ext.min[0], ext.min[1]) : file.path.replace(/\.js$/, ext.min),
117+
path: Array.isArray(ext.min) ? file.path.replace(ext.min[0], ext.min[1]) : file.path.replace(jsExtensions, ext.min),
117118
});
118119

119120
const uglifyOptions = {
@@ -140,7 +141,7 @@ module.exports = function(opt) {
140141
this.push(min_file);
141142

142143
if (options.noSource !== true) {
143-
file.path = file.path.replace(/\.js$/, ext.src);
144+
file.path = file.path.replace(jsExtensions, ext.src);
144145
this.push(file);
145146
}
146147

test/fixtures/demo6/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict';
2+
module.exports = 'test';

test/fixtures/demo6/index.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// ES6 module with a .mjs extension name
2+
const test = "test";
3+
export default test;

test/minifier.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ const fixtures = path.join(__dirname, 'fixtures');
99
const build = path.join(__dirname, 'build');
1010

1111
function run(project, options) {
12-
return gulp.src(`${fixtures}/${project}/**/*.js`).pipe(minify(options)).pipe(gulp.dest(`${build}/${project}`));
12+
return gulp.src(`${fixtures}/${project}/**/*js`).pipe(minify(options)).pipe(gulp.dest(`${build}/${project}`));
1313
}
1414
describe('test/minifier.test.js', () => {
1515
before(() => {
1616
rimraf.sync(build);
1717
});
1818

1919
after(() => {
20-
rimraf.sync(build);
20+
// rimraf.sync(build);
2121
});
2222

2323
it('should minify ok', (done) => {
@@ -87,4 +87,16 @@ describe('test/minifier.test.js', () => {
8787
});
8888
});
8989

90+
it('should minify .mjs files alongside .js files', (done) => {
91+
const project = 'demo6';
92+
run(project).on('end', () => {
93+
const buildPath = `${build}/${project}`;
94+
assert(fs.existsSync(path.join(buildPath, 'index.js')));
95+
assert(fs.existsSync(path.join(buildPath, 'index-min.js')));
96+
assert(fs.existsSync(path.join(buildPath, 'index.mjs')));
97+
assert(fs.existsSync(path.join(buildPath, 'index-min.mjs')));
98+
done();
99+
});
100+
});
101+
90102
});

0 commit comments

Comments
 (0)