Skip to content

Commit

Permalink
[#43] ✨ Support esm/cjs dual packages
Browse files Browse the repository at this point in the history
  • Loading branch information
yongholeeme committed Dec 20, 2024
1 parent cfdb279 commit d4d2158
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"description": "code-style for naverpay",
"scripts": {
"prepare": "husky install",
"build": "turbo build",
"lint": "eslint",
"lint:fix": "pnpm run lint --fix",
"prettier": "prettier --check '**/*.{json,yaml,md,ts,tsx,js,jsx}'",
Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-config/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import fs from 'node:fs'

import node from './configs/node.js'
import front from './configs/front.js'
import typescript from './configs/typescript.js'

const pkg = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url), 'utf8'))
import pkg from './package.json'

export default {
meta: {
Expand Down
19 changes: 16 additions & 3 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
"version": "1.0.7",
"description": "eslint config for naverpay",
"type": "module",
"main": "./index.js",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.mjs",
"exports": {
"import": "./index.js",
"default": "./index.js"
".": {
"import": "./dist/esm/index.mjs",
"require": "./dist/cjs/index.js",
"default": "./dist/cjs/index.js"
}
},
"files": [
"dist"
],
"scripts": {
"clean": "rm -rf dist",
"prebuild": "pnpm run clean",
"build": "rollup -c",
"test": "vitest run",
"test:watch": "vitest watch",
"version": "pnpm version",
Expand Down Expand Up @@ -51,6 +61,9 @@
],
"license": "MIT",
"devDependencies": {
"@rollup/plugin-json": "^6.1.0",
"builtin-modules": "^4.0.0",
"rollup": "^4.27.4",
"vitest": "^2.1.5"
}
}
32 changes: 32 additions & 0 deletions packages/eslint-config/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import path from 'path'
import fs from 'fs'
import builtins from 'builtin-modules'
import json from '@rollup/plugin-json'

/**
* @type {import('rollup').RollupOptions}
*/
export default ['esm', 'cjs'].map((module) => {
const pkg = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url), 'utf8'))

return {
input: 'index.js',
output: {
format: module,
dir: 'dist/' + module,
...(module === 'esm'
? {
entryFileNames: `[name]${path.extname('./dist/' + module + '/index.mjs')}`,
preserveModulesRoot: path.dirname('index.js'),
preserveModules: true,
interop: 'esModule',
}
: {
exports: 'auto',
interop: 'auto',
}),
},
external: [...Object.keys(pkg?.dependencies || []), ...Object.keys(pkg?.peerDependencies || []), ...builtins],
plugins: [json()],
}
})
4 changes: 1 addition & 3 deletions packages/eslint-plugin/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import fs from 'node:fs'

import memoReactComponents from './rules/memo-react-components.js'
import optimizeSvgComponents from './rules/optimize-svg-components.js'
import preventDefaultImport from './rules/prevent-default-import.js'
import sortExports from './rules/sort-exports.js'
import svgUniqueId from './rules/svg-unique-id.js'

const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8'))
import pkg from '../package.json'

const plugin = {
meta: {
Expand Down
19 changes: 18 additions & 1 deletion packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,33 @@
"version": "1.2.1",
"description": "eslint plugin for naverpay",
"type": "module",
"main": "./lib/index.js",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.mjs",
"exports": {
".": {
"import": "./dist/esm/index.mjs",
"require": "./dist/cjs/index.js",
"default": "./dist/cjs/index.js"
}
},
"files": [
"dist"
],
"scripts": {
"clean": "rm -rf dist",
"prebuild": "pnpm run clean",
"build": "rollup -c",
"test": "vitest run",
"test:watch": "vitest watch",
"version": "pnpm version",
"deploy": "pnpm publish"
},
"devDependencies": {
"@babel/eslint-parser": "^7.25.9",
"@rollup/plugin-json": "^6.1.0",
"@typescript-eslint/parser": "^8.15.0",
"builtin-modules": "^4.0.0",
"rollup": "^4.27.4",
"vitest": "^2.1.5"
},
"peerDependencies": {
Expand Down
32 changes: 32 additions & 0 deletions packages/eslint-plugin/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import path from 'path'
import fs from 'fs'
import builtins from 'builtin-modules'
import json from '@rollup/plugin-json'

/**
* @type {import('rollup').RollupOptions}
*/
export default ['esm', 'cjs'].map((module) => {
const pkg = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url), 'utf8'))

return {
input: 'lib/index.js',
output: {
format: module,
dir: 'dist/' + module,
...(module === 'esm'
? {
entryFileNames: `[name]${path.extname('./dist/' + module + '/index.mjs')}`,
preserveModulesRoot: path.dirname('lib/index.js'),
preserveModules: true,
interop: 'esModule',
}
: {
exports: 'auto',
interop: 'auto',
}),
},
external: [...Object.keys(pkg?.dependencies || []), ...Object.keys(pkg?.peerDependencies || []), ...builtins],
plugins: [json()],
}
})
69 changes: 68 additions & 1 deletion pnpm-lock.yaml

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

3 changes: 2 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "https://turborepo.org/schema.json",
"pipeline": {
"test": {"cache": false},
"test:watch": {"cache": false}
"test:watch": {"cache": false},
"build": {"cache": false, "dependsOn": ["^build"]}
}
}

0 comments on commit d4d2158

Please sign in to comment.