Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified ESM build #3704

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
'eqeqeq': ['error', 'always', { 'null': 'ignore' }],
Copy link
Author

@trusktr trusktr Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is the only remnant of CommonJS in the repo (apart from the dependencies which I haven't updated yet).

In the upcoming ESLint v9, the new "flat config" format will also be in Node ESM format, and we'll have to move ESLint config to ESM at that point.


// imports
'import/extensions': ['warn', 'never'],
'import/extensions': ['warn', 'always'], // "always" is most compatible with default vanilla ES Modules
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having .js extensions, even if not to taste, is needed for the simplest ESM setup, and TypeScript officially recommends writing .js directly in TypeScript import statements for this purpose. So, we force it here so we don't mess up.

If we remove the .js extensions, the vanilla ESM example over at https://github.com/trusktr/prism-v2-test will break.

'import/order': [
'warn',
{
Expand Down
7 changes: 7 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Xrequire": "@babel/register",
"Xexperimental-specifier-resolution=node": "",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • cleanup

"node-option": [
"loader=ts-node/esm"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes ts-mocha run our files as Node ESM files.

]
}
1 change: 0 additions & 1 deletion .mocharc.yml

This file was deleted.

8 changes: 4 additions & 4 deletions benchmark/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import fs from 'fs';
import path from 'path';
import { gitP } from 'simple-git';
import { argv } from 'yargs';
import { parseLanguageNames } from '../tests/helper/test-case';
import { config as baseConfig } from './config';
import type { Prism } from '../src/core';
import type { Config, ConfigOptions } from './config';
import { parseLanguageNames } from '../tests/helper/test-case.js';
import { config as baseConfig } from './config.js';
import type { Prism } from '../src/core.js';
import type { Config, ConfigOptions } from './config.js';
import type { Options, Stats } from 'benchmark';


Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
"name": "prismjs",
"version": "1.29.0",
"description": "Lightweight, robust, elegant syntax highlighting. A spin-off project from Dabblet.",
"main": "prism.js",
"type": "module",
"main": "dist/core/prism.js",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opt into the modern Node ESM standard, and point to the main file to get Prism from (there was no more ./prism.js).

"style": "themes/prism.css",
"engines": {
"node": ">=14"
},
"scripts": {
"benchmark": "ts-node benchmark/benchmark.ts",
"build": "ts-node scripts/build.ts",
"build": "ts-node-esm scripts/build.ts && npm run tsc",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running with ts-node-esm runs files as Node ESM, rather than CommonJS.

"prepare": "npm run build",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is from #3699

"lint": "eslint . --cache",
"lint:fix": "npm run lint -- --fix",
"lint:ci": "eslint . --max-warnings 0",
Expand All @@ -23,7 +25,8 @@
"test:plugins": "ts-mocha tests/plugins/**/*.ts",
"test:runner": "ts-mocha tests/testrunner-tests.ts",
"test": "npm-run-all test:*",
"tsc": "tsc && tsc -p tests/tsconfig.json"
"typecheck": "tsc --noEmit && tsc -p tests/tsconfig.json --noEmit",
"tsc": "tsc"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use a plain and simple tsc step to build dist/ output. The new typecheck script replaces the old tsc script for type checking without emitting output.

We should probably cover the other TS files as well, currently this covers only src/ and tests/.

},
"repository": {
"type": "git",
Expand Down
Loading