Skip to content

Commit

Permalink
Make module fully ESM compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
niksy committed Jan 14, 2021
1 parent 923d69b commit 4bd8f52
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
8 changes: 7 additions & 1 deletion lib/json-to-sass.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import sass from 'sass';
import { isArray, isPlainObject, isString, isNumber, isBoolean } from 'lodash';
import {
isArray,
isPlainObject,
isString,
isNumber,
isBoolean
} from 'lodash-es';
import parseColor from 'parse-color';
import parseUnit from 'parse-css-dimension';

Expand Down
2 changes: 1 addition & 1 deletion lib/sass-to-json.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sass from 'sass';
import { round } from 'lodash';
import { round } from 'lodash-es';
import rgbHex from 'rgb-hex';
import shortHexColor from 'shorten-css-hex';

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"version": "version-changelog CHANGELOG.md && changelog-verify CHANGELOG.md && git add CHANGELOG.md"
},
"dependencies": {
"lodash": "^4.17.20",
"lodash-es": "^4.17.20",
"parse-color": "^1.0.0",
"parse-css-dimension": "^1.1.0",
"rgb-hex": "^3.0.0",
Expand All @@ -46,6 +46,8 @@
"sass": ">=1"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-node-resolve": "^11.0.1",
"changelog-verify": "^1.1.2",
"eslint": "^7.11.0",
"eslint-config-niksy": "^9.0.0",
Expand Down
74 changes: 38 additions & 36 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,50 @@

const path = require('path');
const { promises: fs } = require('fs');
const { default: resolve } = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');

module.exports = {
input: 'index.js',
output: [
{
const packageType = (() => {
return {
name: 'package-type',
async writeBundle(output) {
let prefix, type;
if (output.file.includes('cjs/')) {
prefix = 'cjs';
type = 'commonjs';
} else if (output.file.includes('esm/')) {
prefix = 'esm';
type = 'module';
}
if (typeof prefix !== 'undefined') {
const package_ = path.join(prefix, 'package.json');
try {
await fs.unlink(package_);
} catch (error) {}
await fs.writeFile(package_, JSON.stringify({ type }), 'utf8');
}
}
};
})();

module.exports = [
{
input: 'index.js',
output: {
file: 'cjs/index.js',
format: 'cjs',
exports: 'auto',
sourcemap: true
},
{
plugins: [packageType, resolve({ resolveOnly: ['lodash-es'] })]
},
{
input: 'index.js',
output: {
file: 'esm/index.js',
format: 'esm',
sourcemap: true
}
],
plugins: [
(() => {
return {
name: 'package-type',
async writeBundle(output) {
let prefix, type;
if (output.file.includes('cjs/')) {
prefix = 'cjs';
type = 'commonjs';
} else if (output.file.includes('esm/')) {
prefix = 'esm';
type = 'module';
}
if (typeof prefix !== 'undefined') {
const package_ = path.join(prefix, 'package.json');
try {
await fs.unlink(package_);
} catch (error) {}
await fs.writeFile(
package_,
JSON.stringify({ type }),
'utf8'
);
}
}
};
})()
]
};
},
plugins: [packageType]
}
];

0 comments on commit 4bd8f52

Please sign in to comment.