Skip to content

Commit

Permalink
Build CJS files via Rollup (#7037)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Jul 26, 2023
1 parent 6fb328b commit 5d5c159
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 51 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@ jobs:
with:
files: ./.coverage/lcov.info

build:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 'lts/*'
cache: npm

- name: Install latest npm
run: npm install --global npm

- name: Install dependencies
run: npm ci

- name: Build
run: npm run build

- name: Check built files
run: npm run build-check

spellcheck:
runs-on: ubuntu-latest
timeout-minutes: 30
Expand Down
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
scripts/**/*.css
system-tests/**/*.css
system-tests/**/*.scss

# This unit test required a parent directory structure and therefore is not inside /fixtures/globs
**/standalone-glob-parent-test/**/*.css

# Ignore built CJS files
lib/**/*.cjs
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
9 changes: 5 additions & 4 deletions lib/cli.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { EOL } from 'node:os';
import path from 'node:path';

import picocolors from 'picocolors';
const { dim, red } = picocolors;

import { EOL } from 'os';
import meow from 'meow';
import path from 'path';
import resolveFrom from 'resolve-from';

import checkInvalidCLIOptions from './utils/checkInvalidCLIOptions.js';
import { isPlainObject } from './utils/validateTypes.js';
import printConfig from './printConfig.js';
import resolveCustomFormatter from './resolveCustomFormatter.js';
import standalone from './standalone.js';
import writeOutputFile from './writeOutputFile.js';
import writeOutputFile from './writeOutputFile.mjs';

import {
DEFAULT_CACHE_LOCATION,
DEFAULT_FORMATTER,
DEFAULT_IGNORE_FILENAME,
EXIT_CODE_ERROR,
} from './constants.js';
} from './constants.mjs';

import { createRequire } from 'module';
// @ts-expect-error
Expand Down
22 changes: 22 additions & 0 deletions lib/constants.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });

const DEFAULT_CACHE_LOCATION = './.stylelintcache';
const CACHE_STRATEGY_METADATA = 'metadata';
const CACHE_STRATEGY_CONTENT = 'content';
const DEFAULT_CACHE_STRATEGY = CACHE_STRATEGY_METADATA;

const DEFAULT_IGNORE_FILENAME = '.stylelintignore';

const DEFAULT_FORMATTER = 'string';

const EXIT_CODE_ERROR = 2;

exports.CACHE_STRATEGY_CONTENT = CACHE_STRATEGY_CONTENT;
exports.CACHE_STRATEGY_METADATA = CACHE_STRATEGY_METADATA;
exports.DEFAULT_CACHE_LOCATION = DEFAULT_CACHE_LOCATION;
exports.DEFAULT_CACHE_STRATEGY = DEFAULT_CACHE_STRATEGY;
exports.DEFAULT_FORMATTER = DEFAULT_FORMATTER;
exports.DEFAULT_IGNORE_FILENAME = DEFAULT_IGNORE_FILENAME;
exports.EXIT_CODE_ERROR = EXIT_CODE_ERROR;
25 changes: 0 additions & 25 deletions lib/constants.js

This file was deleted.

10 changes: 10 additions & 0 deletions lib/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const DEFAULT_CACHE_LOCATION = './.stylelintcache';
export const CACHE_STRATEGY_METADATA = 'metadata';
export const CACHE_STRATEGY_CONTENT = 'content';
export const DEFAULT_CACHE_STRATEGY = CACHE_STRATEGY_METADATA;

export const DEFAULT_IGNORE_FILENAME = '.stylelintignore';

export const DEFAULT_FORMATTER = 'string';

export const EXIT_CODE_ERROR = 2;
2 changes: 1 addition & 1 deletion lib/utils/FileCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {
CACHE_STRATEGY_METADATA,
CACHE_STRATEGY_CONTENT,
DEFAULT_CACHE_STRATEGY,
} = require('../constants');
} = require('../constants.cjs');

/** @typedef {import('file-entry-cache').FileDescriptor["meta"] & { hashOfConfig?: string }} CacheMetadata */

Expand Down
2 changes: 1 addition & 1 deletion lib/utils/getFileIgnorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require('fs');
const path = require('path');
const { default: ignore } = require('ignore');

const { DEFAULT_IGNORE_FILENAME } = require('../constants');
const { DEFAULT_IGNORE_FILENAME } = require('../constants.cjs');
const isPathNotFoundError = require('./isPathNotFoundError');

/**
Expand Down
24 changes: 24 additions & 0 deletions lib/writeOutputFile.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'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');

const _interopDefault = e => e && e.__esModule ? e : { default: e };

const stripAnsi__default = /*#__PURE__*/_interopDefault(stripAnsi);
const writeFileAtomic__default = /*#__PURE__*/_interopDefault(writeFileAtomic);

/**
* @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__default.default(node_path.normalize(filePath), stripAnsi__default.default(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));
}
17 changes: 17 additions & 0 deletions package-lock.json

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

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,26 @@
},
"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",
"build-check": "node scripts/build-check.mjs",
"format": "prettier . --write --cache",
"lint": "npm-run-all --parallel --continue-on-error lint:*",
"lint:formatting": "prettier . --check --cache",
"lint:js": "eslint . --cache --max-warnings=0 --ext .js,.mjs",
"lint:md": "remark . --quiet --frail",
"lint:types": "tsc",
"prepare": "husky install && patch-package",
"prepare": "husky install && patch-package && npm run build",
"release": "np --no-release-draft",
"pretest": "npm run lint",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
Expand Down Expand Up @@ -212,6 +216,7 @@
"postcss-sass": "^0.5.0",
"postcss-scss": "^4.0.6",
"remark-cli": "^11.0.0",
"rollup": "^3.26.2",
"sugarss": "^4.0.1",
"typescript": "^5.1.6"
},
Expand Down
29 changes: 29 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { rmSync } from 'node:fs';

import fg from 'fast-glob';

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

// NOTE: We cannot support CJS for `cli.mjs` since the `meow` dependency is pure ESM.
'!lib/cli.mjs',
]);

// clean up
for (const input of inputFiles) {
rmSync(input.replace('.mjs', '.cjs'), { force: true });
}

export default inputFiles.map((input) => {
return {
input,
output: {
format: 'cjs',
file: input.replace('.mjs', '.cjs'),
generatedCode: 'es2015',
interop: 'auto',
},
};
});
26 changes: 26 additions & 0 deletions scripts/build-check.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { exit, stderr, stdout } from 'node:process';
import { EOL } from 'node:os';
import { execSync } from 'node:child_process';

function main() {
try {
execSync('git diff --exit-code --name-only -- lib', { encoding: 'utf8' });
} catch (error) {
if (error.stdout) {
stdout.write(`You must commit the following files:${EOL}`);
stdout.write(error.stdout);
}

if (error.stderr) {
stderr.write(error.stderr);
}

if (typeof error.status === 'number') {
exit(error.status);
} else {
throw error;
}
}
}

main();
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
}
},
"include": ["lib", "types", "package.json"],
"exclude": ["**/*.test.{js,mjs}", "**/__tests__/"]
"exclude": ["**/*.test.{js,mjs}", "**/__tests__/", "lib/**/*.cjs"]
}

0 comments on commit 5d5c159

Please sign in to comment.