Skip to content

Commit

Permalink
Build CJS files via Rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Jul 3, 2023
1 parent b55c5d3 commit e2d0016
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/__tests__/writeOutputFile.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fileURLToPath } from 'node:url';
import fs from 'node:fs/promises';
import path from 'node:path';

import writeOutputFile from '../writeOutputFile.js';
import writeOutputFile from '../writeOutputFile.mjs';

describe('writeOutputFile', () => {
it('creates a file', async () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { isPlainObject } = require('./utils/validateTypes');
const checkInvalidCLIOptions = require('./utils/checkInvalidCLIOptions');
const printConfig = require('./printConfig');
const standalone = require('./standalone');
const writeOutputFile = require('./writeOutputFile');
const writeOutputFile = require('./writeOutputFile.cjs');
const resolveCustomFormatter = require('./resolveCustomFormatter');
const {
DEFAULT_CACHE_LOCATION,
Expand Down
19 changes: 19 additions & 0 deletions lib/writeOutputFile.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const node_path = require('node:path');
const promises = require('node:fs/promises');
const stripAnsi = require('strip-ansi');
const writeFileAtomic = require('write-file-atomic');

/**
* @param {string} content
* @param {string} filePath
* @returns {Promise<void>}
*/
async function writeOutputFile(content, filePath) {
await promises.mkdir(node_path.dirname(filePath), { recursive: true });

await writeFileAtomic(node_path.normalize(filePath), stripAnsi(content));
}

module.exports = writeOutputFile;
17 changes: 0 additions & 17 deletions lib/writeOutputFile.js

This file was deleted.

15 changes: 15 additions & 0 deletions lib/writeOutputFile.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { dirname, normalize } from 'node:path';
import { mkdir } from 'node:fs/promises';
import stripAnsi from 'strip-ansi';
import writeFileAtomic from 'write-file-atomic';

/**
* @param {string} content
* @param {string} filePath
* @returns {Promise<void>}
*/
export default async function writeOutputFile(content, filePath) {
await mkdir(dirname(filePath), { recursive: true });

await writeFileAtomic(normalize(filePath), stripAnsi(content));
}
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@
},
"files": [
"bin/**/*.js",
"bin/**/*.cjs",
"bin/**/*.mjs",
"lib/**/*.js",
"lib/**/*.cjs",
"lib/**/*.mjs",
"!**/__tests__/**",
"!lib/testUtils/**",
"types/stylelint/index.d.ts"
],
"scripts": {
"benchmark-rule": "node scripts/benchmark-rule.mjs",
"build": "rollup -c",
"postinstall": "npm run build",
"format": "prettier . --write --cache",
"lint": "npm-run-all --parallel --continue-on-error lint:*",
"lint:formatting": "prettier . --check --cache",
Expand All @@ -45,7 +51,7 @@
"prepare": "husky install && patch-package",
"release": "np --no-release-draft",
"pretest": "npm run lint",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test": "npm run build && node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test-coverage": "npm test --ignore-scripts -- --coverage",
"test-only": "npm test --ignore-scripts",
"version": "changeset version",
Expand Down Expand Up @@ -211,6 +217,7 @@
"postcss-sass": "^0.5.0",
"postcss-scss": "^4.0.6",
"remark-cli": "^11.0.0",
"rollup": "^3.26.0",
"sugarss": "^4.0.1",
"typescript": "^5.1.6"
},
Expand Down
14 changes: 14 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fg from 'fast-glob';

const inputFiles = fg.globSync(['lib/**/*.mjs', '!**/__tests__/**', '!lib/testUtils/**']);

export default inputFiles.map((input) => {
return {
input,
output: {
format: 'cjs',
file: input.replace('.mjs', '.cjs'),
generatedCode: 'es2015',
},
};
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"noUnusedParameters": true,
"noUncheckedIndexedAccess": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"skipLibCheck": false,
"paths": {
"*": ["./node_modules/@types/*", "./types/*"]
Expand Down

0 comments on commit e2d0016

Please sign in to comment.