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

refactor: make @ter/scripts pure-esm #82

Merged
merged 2 commits into from
Aug 20, 2023
Merged
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 packages/app-common/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"root": true,
"extends": "@ter/eslint-config/.eslintrc.basic.js",
"ignorePatterns": ["/lib", "*.cjs", "*.js", "*.mjs"],
"ignorePatterns": ["/coverage", "/lib", "*.cjs", "*.js", "*.mjs"],
"parserOptions": {
"project": "./tsconfig.json"
}
Expand Down
8 changes: 4 additions & 4 deletions packages/app-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@
"directory": "packages/app-common"
},
"scripts": {
"build": "concurrently -n esm,types \"pnpm run build:esm\" \"pnpm run build:types\"",
"build:esm": "tsc --project tsconfigs/tsconfig.esm.json",
"build": "concurrently -n lib,types \"pnpm run build:lib\" \"pnpm run build:types\"",
"build:lib": "tsc --project tsconfigs/tsconfig.lib.json",
"build:types": "tsc --project tsconfigs/tsconfig.types.json",
"clean": "rimraf --glob coverage lib tsconfigs/*.tsbuildinfo",
"dev": "pnpm run build:esm -- --watch",
"dev": "pnpm run build:lib -- --watch",
"format": "prettier --write \"**/*.{cjs,css,html,js,jsx,mjs,json,less,scss,ts,tsx,yaml,yml}\"",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"pre-commit": "lint-staged",
"test": "unit-test",
"typecheck": "tsc --project tsconfig.json --noEmit"
"typecheck": "tsc --project tsconfig.json --noEmit --incremental false"
},
"devDependencies": {
"@ter/eslint-config": "workspace:*",
Expand Down
3 changes: 1 addition & 2 deletions packages/app-common/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": "@ter/tsconfigs/tsconfig.eslint.bundler.json",
"extends": "@ter/tsconfigs/tsconfig.bundler.eslint.json",
"compilerOptions": {
"incremental": false,
"rootDir": "src"
},
"include": ["src/**/*"],
Expand Down
2 changes: 1 addition & 1 deletion packages/app-main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"pre-commit": "lint-staged",
"test": "unit-test",
"test-full": "unit-test",
"typecheck": "tsc --project tsconfig.json --noEmit"
"typecheck": "tsc --project tsconfig.json --noEmit --incremental false"
},
"dependencies": {
"@ter/app-common": "workspace:*",
Expand Down
4 changes: 1 addition & 3 deletions packages/app-main/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"extends": "@ter/tsconfigs/tsconfig.eslint.bundler.json",
"extends": "@ter/tsconfigs/tsconfig.bundler.eslint.json",
"compilerOptions": {
"baseUrl": ".",
"incremental": false,
"noEmit": false,
"rootDir": "src"
},
"include": ["src/**/*"],
Expand Down
2 changes: 1 addition & 1 deletion packages/app-renderer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#000000" />
<meta http-equiv="Content-Security-Policy" content="script-src 'self'">
<meta http-equiv="Content-Security-Policy" content="script-src 'self'" />
<title>template-electron-react</title>
</head>

Expand Down
2 changes: 1 addition & 1 deletion packages/app-renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"pre-commit": "lint-staged",
"test": "unit-test",
"test-full": "unit-test",
"typecheck": "tsc --project tsconfig.json --noEmit"
"typecheck": "tsc --project tsconfig.json --noEmit --incremental false"
},
"dependencies": {
"@reduxjs/toolkit": "^1.9.5",
Expand Down
4 changes: 1 addition & 3 deletions packages/app-renderer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"extends": "@ter/tsconfigs/tsconfig.bundler.react.json",
"extends": "@ter/tsconfigs/tsconfig.react.eslint.json",
"compilerOptions": {
"allowImportingTsExtensions": true,
"baseUrl": ".",
"incremental": false,
"noEmit": true,
"rootDir": "src",

"plugins": [
Expand Down
2 changes: 1 addition & 1 deletion packages/app-renderer/tsconfig.vite.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"moduleResolution": "bundler",
"skipLibCheck": true
},
"include": ["vite.config.ts"]
"include": ["vite.config.mts"]
}
3 changes: 3 additions & 0 deletions packages/eslint-config/.eslintrc.basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ module.exports = {
rules: {
// -------------------- ESLint Built-In Rules --------------------

// https://eslint.org/docs/latest/rules/camelcase
camelcase: 'off',

// https://eslint.org/docs/latest/rules/class-methods-use-this
'class-methods-use-this': 'off',

Expand Down
4 changes: 0 additions & 4 deletions packages/scripts/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# Compilation

/dist

# Test

/coverage
4 changes: 3 additions & 1 deletion packages/scripts/bin/app-main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

require('../dist/bin').appMain();
import { appMain } from '../dist/bin.js';

appMain();
4 changes: 3 additions & 1 deletion packages/scripts/bin/mkdir-working.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

require('../dist/bin').mkdirWorking();
import { mkdirWorking } from '../dist/bin.js';

mkdirWorking();
4 changes: 3 additions & 1 deletion packages/scripts/bin/run-build.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

require('../dist/bin').runBuild();
import { runBuild } from '../dist/bin.js';

runBuild();
4 changes: 3 additions & 1 deletion packages/scripts/bin/run-unpacked.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

require('../dist/bin').runUnpacked();
import { runUnpacked } from '../dist/bin.js';

runUnpacked();
4 changes: 2 additions & 2 deletions packages/scripts/bin/unit-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

'use strict';
import { unitTest } from '../dist/bin.js';

const lifecycleEvent = process.env.npm_lifecycle_event;

require('../dist/bin').unitTest(lifecycleEvent === 'test');
unitTest(lifecycleEvent === 'test');
15 changes: 10 additions & 5 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
"name": "@ter/scripts",
"version": "0.12.0",
"description": "Scripts for ter.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"homepage": "https://github.com/YSoftwareRepo/template-electron-react#readme",
"license": "MIT",
"author": "yifanwww <[email protected]> (https://github.com/yifanwww)",
Expand All @@ -25,16 +30,16 @@
"unit-test": "./bin/unit-test.js"
},
"scripts": {
"build-scripts": "concurrently -n cjs,types \"pnpm run build-scripts:cjs\" \"pnpm run build-scripts:types\"",
"build-scripts:cjs": "tsc --project tsconfigs/tsconfig.cjs.json",
"build-scripts": "concurrently -n dist,types \"pnpm run build-scripts:dist\" \"pnpm run build-scripts:types\"",
"build-scripts:dist": "tsc --project tsconfigs/tsconfig.dist.json",
"build-scripts:types": "tsc --project tsconfigs/tsconfig.types.json",
"clean": "rimraf --glob dist tsconfigs/*.tsbuildinfo",
"dev": "pnpm run build-scripts --watch",
"format": "prettier --write \"**/*.{cjs,css,html,js,jsx,mjs,json,less,scss,ts,tsx,yaml,yml}\"",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"pre-commit": "lint-staged",
"typecheck": "tsc --project tsconfig.json --noEmit"
"typecheck": "tsc --project tsconfig.json --noEmit --incremental false"
},
"dependencies": {
"@babel/core": "^7.22.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assert from 'node:assert';
import child from 'node:child_process';
import fs from 'node:fs';

import { paths } from './utils/paths';
import { paths } from './utils/index.js';

const genCommand = (...params: (string | false | undefined | null)[]) => params.filter(Boolean).join(' ');

Expand Down
3 changes: 3 additions & 0 deletions packages/scripts/src/electron.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import fs from 'node:fs';
import { createRequire } from 'node:module';
import semver from 'semver';

const require = createRequire(import.meta.url);

function getElectronMajorVer(): number {
const pkg = require.resolve('electron/package.json');

Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './electron';
export * from './electron.js';
14 changes: 10 additions & 4 deletions packages/scripts/src/jest/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { Config } from '@jest/types';
import type { Config } from 'jest';
import fs from 'node:fs';
import { createRequire } from 'node:module';
import path from 'node:path';

import { paths } from '../utils';
import { paths } from '../utils/index.js';

function getConfig(): Config.InitialOptions {
const require = createRequire(import.meta.url);

function getConfig(): Config {
const packageJson = process.env.npm_package_json;
const packageDir = packageJson ? path.dirname(packageJson) : process.cwd();

Expand Down Expand Up @@ -44,6 +47,7 @@ function getConfig(): Config.InitialOptions {
'^react-native$': 'react-native-web',
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
'^src/(.*)$': '<rootDir>/src/$1',
'^(.*).js$': ['$1.js', '$1.ts'],
},
moduleFileExtensions: ['js', 'json', 'jsx', 'node', 'ts', 'tsx', 'web.js', 'web.jsx', 'web.ts', 'web.tsx'],

Expand All @@ -56,4 +60,6 @@ function getConfig(): Config.InitialOptions {
};
}

export = getConfig();
const config: Config = getConfig();

export default config;
5 changes: 4 additions & 1 deletion packages/scripts/src/jest/transform.babel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { TransformOptions } from '@babel/core';
import type { SyncTransformer } from '@jest/transform';
import babelJest from 'babel-jest';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);

const transformer: SyncTransformer<TransformOptions> | Promise<SyncTransformer<TransformOptions>> =
babelJest.createTransformer({
Expand All @@ -19,4 +22,4 @@ const transformer: SyncTransformer<TransformOptions> | Promise<SyncTransformer<T
configFile: false,
});

export = transformer;
export default transformer;
2 changes: 1 addition & 1 deletion packages/scripts/src/jest/transform.css.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This is a custom Jest transformer turning style imports into empty objects.
// http://facebook.github.io/jest/docs/en/webpack.html

export = {
export default {
process() {
return {
code: 'module.exports = {};',
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/src/jest/transform.file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'node:path';
// This is a custom Jest transformer turning file imports into filenames.
// http://facebook.github.io/jest/docs/en/webpack.html

export = {
export default {
process(src: string, filename: string) {
const assetFilename = JSON.stringify(path.basename(filename));

Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './paths';
export * from './paths.js';
5 changes: 4 additions & 1 deletion packages/scripts/src/utils/paths.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import electron from 'electron';
import path from 'node:path';
import url from 'node:url';

const _dirname = url.fileURLToPath(new URL('.', import.meta.url));

// `..` points to `<repo>/packages/scripts/src`
// `../..` points to `<repo>/packages/scripts`
// `../../..` points to `<repo>/packages`
// `../../../..` points to `<repo>`
const repo = path.resolve(__dirname, '../../../..');
const repo = path.resolve(_dirname, '../../../..');

const build = path.resolve(repo, 'build');
const rootNodeModules = path.resolve(repo, 'node_modules');
Expand Down
4 changes: 4 additions & 0 deletions packages/scripts/src/webpack/utils/getCacheIdentifier.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);

export function getCacheIdentifier(environment: string, packages: string[]) {
let cacheIdentifier = environment == null ? '' : environment.toString();
for (const packageName of packages) {
Expand Down
19 changes: 12 additions & 7 deletions packages/scripts/src/webpack/webpack.main.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import { createRequire } from 'node:module';
import path from 'node:path';
import url from 'node:url';
import TerserPlugin from 'terser-webpack-plugin';
import type { Configuration, WebpackPluginInstance } from 'webpack';

import { paths } from '../utils';
import { paths } from '../utils/index.js';

import { createEnvironmentHash } from './utils/createEnvironmentHash';
import { getCacheIdentifier } from './utils/getCacheIdentifier';
import { ReloadElectronWebpackPlugin } from './utils/reloadElectronWebpackPlugin';
import { WebpackStatsPrettifyPlugin } from './utils/webpackStatsPrettifyPlugin';
import { createEnvironmentHash } from './utils/createEnvironmentHash.js';
import { getCacheIdentifier } from './utils/getCacheIdentifier.js';
import { ReloadElectronWebpackPlugin } from './utils/reloadElectronWebpackPlugin.js';
import { WebpackStatsPrettifyPlugin } from './utils/webpackStatsPrettifyPlugin.js';

const _filename = url.fileURLToPath(import.meta.url);
const require = createRequire(import.meta.url);

const resolveAppMain = (relative: string) => path.resolve(paths.electronMain, relative);

Expand Down Expand Up @@ -72,7 +77,7 @@ const factory: ConfigurationFactory = (env, argv) => {
store: 'pack',
buildDependencies: {
defaultWebpack: ['webpack/lib/'],
config: [__filename],
config: [_filename],
tsconfig: [appMainPaths.appTsConfig],
},
},
Expand Down Expand Up @@ -258,4 +263,4 @@ const factory: ConfigurationFactory = (env, argv) => {
return webpack;
};

export = factory;
export default factory;
3 changes: 1 addition & 2 deletions packages/scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": "@ter/tsconfigs/tsconfig.eslint.node.json",
"extends": "@ter/tsconfigs/tsconfig.node16.eslint.json",
"compilerOptions": {
"incremental": false,
"rootDir": "src"
},
"include": ["src/**/*"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@ter/tsconfigs/tsconfig.node.cjs.json",
"extends": "@ter/tsconfigs/tsconfig.node16.json",
"compilerOptions": {
"outDir": "../dist",
"rootDir": "../src"
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/tsconfigs/tsconfig.types.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@ter/tsconfigs/tsconfig.node.types.json",
"extends": "@ter/tsconfigs/tsconfig.node16.types.json",
"compilerOptions": {
"outDir": "../dist",
"rootDir": "../src"
Expand Down
10 changes: 1 addition & 9 deletions packages/tsconfigs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@ Typescript configurations for ter.

## Usage

Your typescript configurations can be extended from
- `@ter/tsconfigs/tsconfig.base.json`
- `@ter/tsconfigs/tsconfig.bundler.json`
- `@ter/tsconfigs/tsconfig.bundler.react.json`
- `@ter/tsconfigs/tsconfig.bundler.types.json`
- `@ter/tsconfigs/tsconfig.eslint.bundler.json`
- `@ter/tsconfigs/tsconfig.eslint.node.json`
- `@ter/tsconfigs/tsconfig.node.cjs.json`
- `@ter/tsconfigs/tsconfig.node.types.json`
Your typescript configurations can be extended from this package.

Then you need to specify the following options if need
- `compilerOptions`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./tsconfig.bundler.json",
"compilerOptions": {
"incremental": false,
"noEmit": true
}
}
Loading