Skip to content

Commit 7e7f0fa

Browse files
committed
chore: migrate to esm modules
1 parent a0ea819 commit 7e7f0fa

15 files changed

+125
-59
lines changed

.babelrc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
{
22
"presets": [
3-
"@babel/preset-env"
4-
]
3+
[
4+
"@babel/preset-env", {
5+
"modules": "cjs"
6+
}
7+
]
8+
],
9+
10+
"plugins": [
11+
"add-module-exports"
12+
],
13+
14+
"env": {
15+
"es": {
16+
"presets": [
17+
[
18+
"@babel/preset-env", {
19+
"modules": "false"
20+
}
21+
]
22+
]
23+
}
24+
}
525
}

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
"name": "rollup-plugin-esformatter",
33
"version": "0.7.0",
44
"description": "Rollup plugin to beautify bundle with esformatter",
5-
"main": "dist/index.js",
5+
"main": "dist/cjs/index.js",
6+
"module": "dist/es/index.js",
7+
"exports": {
8+
".": {
9+
"require": "./dist/cjs/index.js",
10+
"import": "./dist/es/index.js",
11+
"default": "./dist/index.js"
12+
},
13+
"./dist/": "./dist/"
14+
},
615
"scripts": {
716
"clean": "gulp clean",
817
"lint": "gulp lint",
@@ -42,6 +51,7 @@
4251
"@babel/core": "7.8.7",
4352
"@babel/preset-env": "7.8.7",
4453
"@babel/register": "7.8.6",
54+
"babel-plugin-add-module-exports": "^1.0.2",
4555
"eslint": "6.8.0",
4656
"eslint-config-google": "0.14.0",
4757
"fancy-log": "1.3.3",
@@ -51,8 +61,10 @@
5161
"gulp-conventional-changelog": "2.0.29",
5262
"gulp-eslint": "6.0.0",
5363
"gulp-git": "2.10.0",
64+
"gulp-header-comment": "0.6.1",
5465
"gulp-jasmine": "4.0.0",
5566
"gulp-prettier": "2.3.0",
67+
"gulp-strip-banner": "0.0.2",
5668
"jasmine-core": "3.5.0",
5769
"q": "1.5.1",
5870
"rimraf": "3.0.2",

scripts/build/index.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,45 @@
2525
const path = require('path');
2626
const gulp = require('gulp');
2727
const babel = require('gulp-babel');
28+
const stripBanner = require('gulp-strip-banner');
29+
const headerComment = require('gulp-header-comment');
2830
const prettier = require('gulp-prettier');
2931
const config = require('../config');
3032

31-
module.exports = function build() {
33+
module.exports = gulp.series(
34+
buildCjs,
35+
buildEsm
36+
);
37+
38+
/**
39+
* Build CommonJS bundles.
40+
*
41+
* @return {NodeJS.WritableStream} The gulp stream.
42+
*/
43+
function buildCjs() {
44+
return build('cjs');
45+
}
46+
47+
/**
48+
* Build ESM bundles.
49+
*
50+
* @return {NodeJS.WritableStream} The gulp stream.
51+
*/
52+
function buildEsm() {
53+
return build('es');
54+
}
55+
56+
/**
57+
* Build bundle for given environment.
58+
*
59+
* @param {string} envName Environment id.
60+
* @return {NodeJS.WritableStream} The gulp stream.
61+
*/
62+
function build(envName) {
3263
return gulp.src(path.join(config.src, '**', '*.js'))
33-
.pipe(babel())
64+
.pipe(stripBanner())
65+
.pipe(babel({envName}))
66+
.pipe(headerComment({file: path.join(config.root, 'LICENSE')}))
3467
.pipe(prettier())
35-
.pipe(gulp.dest(config.dist));
36-
};
68+
.pipe(gulp.dest(path.join(config.dist, envName)));
69+
}

src/.eslintrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"parserOptions": {
3+
"sourceType": "module"
4+
}
5+
}
6+

src/index-rollup-legacy.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@
2222
* SOFTWARE.
2323
*/
2424

25-
'use strict';
25+
import hasIn from 'lodash.hasin';
26+
import isNil from 'lodash.isnil';
27+
import RollupPluginEsFormatter from './rollup-plugin-esformatter.js';
2628

27-
const hasIn = require('lodash.hasin');
28-
const isNil = require('lodash.isnil');
29-
const RollupPluginEsFormatter = require('./rollup-plugin-esformatter.js');
30-
31-
module.exports = (options) => {
29+
export default (options) => {
3230
const plugin = new RollupPluginEsFormatter(options);
3331

3432
return {

src/index-rollup-stable.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
* SOFTWARE.
2323
*/
2424

25-
'use strict';
25+
import RollupPluginEsFormatter from './rollup-plugin-esformatter.js';
2626

27-
const RollupPluginEsFormatter = require('./rollup-plugin-esformatter.js');
28-
29-
module.exports = (options) => {
27+
export default (options) => {
3028
const plugin = new RollupPluginEsFormatter(options);
3129

3230
return {

src/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
* SOFTWARE.
2323
*/
2424

25-
'use strict';
25+
import {rollup} from 'rollup';
26+
import legacy from './index-rollup-legacy.js';
27+
import stable from './index-rollup-stable.js';
2628

27-
const rollup = require('rollup');
2829
const VERSION = rollup.VERSION || '0';
2930
const MAJOR_VERSION = Number(VERSION.split('.')[0]) || 0;
3031
const IS_ROLLUP_LEGACY = MAJOR_VERSION === 0;
31-
module.exports = IS_ROLLUP_LEGACY ? require('./index-rollup-legacy.js') : require('./index-rollup-stable.js');
32+
const esformatter = IS_ROLLUP_LEGACY ? legacy : stable;
33+
34+
export default esformatter;

src/rollup-plugin-esformatter.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@
2222
* SOFTWARE.
2323
*/
2424

25-
'use strict';
26-
27-
const omitBy = require('lodash.omitby');
28-
const isEmpty = require('lodash.isempty');
29-
const hasIn = require('lodash.hasin');
30-
const isNil = require('lodash.isnil');
31-
const MagicString = require('magic-string');
32-
const diff = require('diff');
33-
const esformatter = require('esformatter');
25+
import omitBy from 'lodash.omitby';
26+
import isEmpty from 'lodash.isempty';
27+
import hasIn from 'lodash.hasin';
28+
import isNil from 'lodash.isnil';
29+
import MagicString from 'magic-string';
30+
import {diffChars} from 'diff';
31+
import esformatter from 'esformatter';
3432

3533
/**
3634
* The internal plugin options.
@@ -43,7 +41,7 @@ const OPTIONS = new Set([
4341
/**
4442
* The rollup plugin for ESFormatter.
4543
*/
46-
module.exports = class RollupPluginEsFormatter {
44+
export default class RollupPluginEsFormatter {
4745
/**
4846
* Initialize plugin.
4947
*
@@ -113,7 +111,7 @@ module.exports = class RollupPluginEsFormatter {
113111
console.warn(`[${this.name}] This may take a moment (depends on the size of your bundle)`);
114112

115113
const magicString = new MagicString(source);
116-
const changes = diff.diffChars(source, output);
114+
const changes = diffChars(source, output);
117115

118116
let idx = 0;
119117

@@ -137,4 +135,4 @@ module.exports = class RollupPluginEsFormatter {
137135
}),
138136
};
139137
}
140-
};
138+
}

test/.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
2+
"parserOptions": {
3+
"sourceType": "module"
4+
},
25
"env": {
36
"jasmine": true
47
}
58
}
6-

test/index-rollup-legacy.spec.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
* SOFTWARE.
2323
*/
2424

25-
'use strict';
26-
27-
const esformatter = require('esformatter');
28-
const verifyWarnLogsBecauseOfSourcemap = require('./utils/verify-warn-logs-because-of-source-map.js');
29-
const verifyWarnLogsNotTriggered = require('./utils/verify-warn-logs-not-triggered.js');
30-
const plugin = require('../src/index-rollup-legacy.js');
25+
import esformatter from 'esformatter';
26+
import verifyWarnLogsBecauseOfSourcemap from './utils/verify-warn-logs-because-of-source-map.js';
27+
import verifyWarnLogsNotTriggered from './utils/verify-warn-logs-not-triggered.js';
28+
import plugin from '../src/index-rollup-legacy.js';
3129

3230
describe('rollup-plugin-esformatter [legacy]', () => {
3331
beforeEach(() => {

test/index-rollup-stable.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424

2525
'use strict';
2626

27-
const esformatter = require('esformatter');
28-
const verifyWarnLogsBecauseOfSourcemap = require('./utils/verify-warn-logs-because-of-source-map.js');
29-
const verifyWarnLogsNotTriggered = require('./utils/verify-warn-logs-not-triggered.js');
30-
const plugin = require('../src/index-rollup-stable.js');
27+
import esformatter from 'esformatter';
28+
import verifyWarnLogsBecauseOfSourcemap from './utils/verify-warn-logs-because-of-source-map.js';
29+
import verifyWarnLogsNotTriggered from './utils/verify-warn-logs-not-triggered.js';
30+
import plugin from '../src/index-rollup-stable.js';
3131

3232
describe('rollup-plugin-esformatter [stable]', () => {
3333
beforeEach(() => {

test/it/it.spec.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@
2222
* SOFTWARE.
2323
*/
2424

25-
'use strict';
26-
27-
const fs = require('fs');
28-
const path = require('path');
29-
const rollup = require('rollup');
30-
const tmp = require('tmp');
31-
const Q = require('q');
32-
const verifyWarnLogsBecauseOfSourcemap = require('../utils/verify-warn-logs-because-of-source-map.js');
33-
const esformatter = require('../../src/index.js');
25+
import fs from 'fs';
26+
import path from 'path';
27+
import * as rollup from 'rollup';
28+
import tmp from 'tmp';
29+
import Q from 'q';
30+
import verifyWarnLogsBecauseOfSourcemap from '../utils/verify-warn-logs-because-of-source-map.js';
31+
import esformatter from '../../src/index.js';
3432

3533
describe('rollup-plugin-esformatter', () => {
3634
let tmpDir;

test/rollup-plugin-esformatter.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
'use strict';
2626

27-
const RollupPluginEsFormatter = require('../src/rollup-plugin-esformatter.js');
28-
const verifyWarnLogsBecauseOfSourcemap = require('./utils/verify-warn-logs-because-of-source-map.js');
29-
const verifyWarnLogsNotTriggered = require('./utils/verify-warn-logs-not-triggered.js');
27+
import RollupPluginEsFormatter from '../src/rollup-plugin-esformatter.js';
28+
import verifyWarnLogsBecauseOfSourcemap from './utils/verify-warn-logs-because-of-source-map.js';
29+
import verifyWarnLogsNotTriggered from './utils/verify-warn-logs-not-triggered.js';
3030

3131
describe('RollupPluginEsFormatter', () => {
3232
beforeEach(() => {

test/utils/verify-warn-logs-because-of-source-map.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
*
2828
* @return {void}
2929
*/
30-
module.exports = function verifyWarnLogsBecauseOfSourcemap() {
30+
export default function verifyWarnLogsBecauseOfSourcemap() {
3131
expect(console.warn).toHaveBeenCalledWith(
3232
'[rollup-plugin-esformatter] Sourcemap is enabled, computing diff is required'
3333
);
3434

3535
expect(console.warn).toHaveBeenCalledWith(
3636
'[rollup-plugin-esformatter] This may take a moment (depends on the size of your bundle)'
3737
);
38-
};
38+
}

test/utils/verify-warn-logs-not-triggered.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
*
2828
* @return {void}
2929
*/
30-
module.exports = function verifyWarnLogsNotTriggered() {
30+
export default function verifyWarnLogsNotTriggered() {
3131
expect(console.warn).not.toHaveBeenCalledWith(
3232
'[rollup-plugin-esformatter] Sourcemap is enabled, computing diff is required'
3333
);
3434

3535
expect(console.warn).not.toHaveBeenCalledWith(
3636
'[rollup-plugin-esformatter] This may take a moment (depends on the size of your bundle)'
3737
);
38-
};
38+
}

0 commit comments

Comments
 (0)