Skip to content

Commit

Permalink
feat: update rollup to v4, use esbuild to minify instead of terser
Browse files Browse the repository at this point in the history
  • Loading branch information
dziraf committed Feb 20, 2024
1 parent b845e61 commit c013a17
Show file tree
Hide file tree
Showing 25 changed files with 1,670 additions and 1,621 deletions.
2 changes: 1 addition & 1 deletion .babelrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
["@babel/preset-typescript"]
],
"plugins": [
"@babel/plugin-syntax-import-assertions"
"@babel/plugin-syntax-import-attributes"
],
"only": ["src/", "spec/"],
"ignore": [
Expand Down
3 changes: 3 additions & 0 deletions bin/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import bundler from '../lib/backend/bundler/app.bundler.js'

await bundler.build()
3 changes: 3 additions & 0 deletions bin/globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import bundler from '../lib/backend/bundler/globals.bundler.js'

await bundler.build()
4 changes: 2 additions & 2 deletions bin/watch-dev.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as url from 'url'
import runtime from '@babel/plugin-transform-runtime'
import importAssertions from '@babel/plugin-syntax-import-assertions'
import importAttributes from '@babel/plugin-syntax-import-attributes'

import bundler from '../src/backend/bundler/bundler.js'
import env from '../src/backend/bundler/bundler-env.js'
Expand All @@ -16,7 +16,7 @@ async function build() {
minify: env === 'production',
watch: !once,
babelConfig: {
plugins: [runtime, importAssertions],
plugins: [runtime, importAttributes],
babelHelpers: 'runtime',
include: [
'lib/frontend/**',
Expand Down
60 changes: 28 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
"import": "./index.js",
"require": "./index.js"
},
"./bundler": {
"import": "./lib/backend/bundler/config.js"
}
"./bundler": "./lib/backend/bundler/index.js"
},
"scripts": {
"test": "mocha --loader=ts-node/esm ./spec/index.js",
Expand All @@ -21,11 +19,11 @@
"lint": "eslint './spec/**/*' './src/**/*' './cy/**/*' './*'",
"cover": "NODE_ENV=test nyc --reporter=lcov --reporter=text-lcov npm test",
"codecov": "NODE_ENV=test nyc --reporter=text-lcov npm test | codecov --pipe",
"bundle": "node bin/watch-dev.js",
"bundle:globals": "node bin/bundle-globals.js",
"bundle": "node bin/app.js",
"bundle:globals": "node bin/globals.js",
"cspell": "cspell src/**/*.ts src/**/*.js src/**/*.tsx src/**/*.jsx",
"check:all": "yarn types && yarn cspell && yarn lint && yarn test && ONCE=true yarn bundle && ONCE=true NODE_ENV=production yarn bundle",
"dev": "yarn build && yarn types && yarn bundle:globals && ONCE=true yarn bundle",
"check:all": "yarn types && yarn cspell && yarn lint && yarn test && yarn bundle && NODE_ENV=production yarn bundle",
"dev": "yarn build && yarn types && yarn bundle:globals && yarn bundle && NODE_ENV=production yarn bundle:globals && NODE_ENV=production yarn bundle",
"release": "semantic-release"
},
"bin": {
Expand Down Expand Up @@ -87,23 +85,22 @@
},
"homepage": "https://github.com/SoftwareBrothers/adminjs#readme",
"dependencies": {
"@adminjs/design-system": "^4.0.0",
"@babel/core": "^7.21.0",
"@babel/parser": "^7.21.0",
"@babel/plugin-syntax-import-assertions": "^7.20.0",
"@babel/plugin-transform-runtime": "^7.21.0",
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.21.0",
"@babel/register": "^7.21.0",
"@adminjs/design-system": "^4.0.3",
"@babel/core": "^7.23.9",
"@babel/parser": "^7.23.9",
"@babel/plugin-syntax-import-attributes": "^7.23.3",
"@babel/plugin-transform-runtime": "^7.23.9",
"@babel/preset-env": "^7.23.9",
"@babel/preset-react": "^7.23.3",
"@babel/preset-typescript": "^7.23.3",
"@babel/register": "^7.23.7",
"@hello-pangea/dnd": "^16.2.0",
"@redux-devtools/extension": "^3.2.5",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-replace": "^5.0.2",
"@rollup/plugin-terser": "^0.4.0",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^5.0.5",
"axios": "^1.3.4",
"commander": "^10.0.0",
"flat": "^5.0.2",
Expand All @@ -124,26 +121,28 @@
"react-router": "^6.9.0",
"react-router-dom": "^6.9.0",
"redux": "^4.2.1",
"rollup": "^3.15.0",
"rollup-plugin-polyfill-node": "^0.12.0",
"regenerator-runtime": "^0.14.1",
"rollup": "^4.11.0",
"rollup-plugin-esbuild-minify": "^1.1.1",
"rollup-plugin-polyfill-node": "^0.13.0",
"slash": "^5.0.0",
"uuid": "^9.0.0",
"xss": "^1.0.14"
},
"devDependencies": {
"@babel/cli": "^7.21.0",
"@babel/cli": "^7.23.9",
"@commitlint/cli": "^17.5.0",
"@commitlint/config-conventional": "^17.4.4",
"@semantic-release/git": "^10.0.1",
"@testing-library/react": "^14.0.0",
"@types/babel-core": "^6.25.7",
"@types/babel-core": "^6.25.10",
"@types/chai": "^4.3.4",
"@types/chai-as-promised": "^7.1.5",
"@types/factory-girl": "^5.0.8",
"@types/flat": "^5.0.2",
"@types/lodash": "^4.14.194",
"@types/mocha": "^10.0.1",
"@types/node": "^18.15.11",
"@types/node": "^20.6.0",
"@types/qs": "^6.9.7",
"@types/react": "^18.0.35",
"@types/react-dom": "^18.0.11",
Expand All @@ -155,7 +154,7 @@
"chai": "^4.3.7",
"chai-as-promised": "^7.1.1",
"chai-change": "^2.1.2",
"core-js": "^3.29.1",
"core-js": "^3.36.0",
"cspell": "^6.30.2",
"eslint": "^8.36.0",
"eslint-config-airbnb": "^19.0.4",
Expand All @@ -166,21 +165,18 @@
"eslint-plugin-react-hooks": "^4.6.0",
"factory-girl": "^5.0.4",
"husky": "^8.0.3",
"istanbul": "^0.4.5",
"jsdom": "^21.1.1",
"jsdom-global": "^3.0.2",
"mocha": "^10.2.0",
"node-esm-import-all": "^1.0.0",
"npm-run-all": "^4.1.5",
"nyc": "^15.1.0",
"semantic-release": "^20.1.3",
"semantic-release-slack-bot": "^4.0.0",
"sinon": "^15.0.2",
"sinon-chai": "^3.7.0",
"typescript": "^5.0.2"
"typescript": "^5.3.3"
},
"resolutions": {
"@babel/core": "7.21.0",
"react-redux": "8.0.5",
"redux": "4.2.1"
}
Expand Down
74 changes: 46 additions & 28 deletions src/adminjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import BaseResource from './backend/adapters/resource/base-resource.js'
import BaseDatabase from './backend/adapters/database/base-database.js'
import ConfigurationError from './backend/utils/errors/configuration-error.js'
import ResourcesFactory from './backend/utils/resources-factory/resources-factory.js'
import userComponentsBundler from './backend/bundler/user-components-bundler.js'
import { RecordActionResponse, Action, BulkActionResponse } from './backend/actions/action.interface.js'
import componentsBundler from './backend/bundler/components.bundler.js'
import {
RecordActionResponse,
Action,
BulkActionResponse,
} from './backend/actions/action.interface.js'
import { DEFAULT_PATHS } from './constants.js'
import { ACTIONS } from './backend/actions/index.js'

Expand All @@ -21,6 +25,8 @@ import { relativeFilePathResolver } from './utils/file-resolver.js'
import { Router } from './backend/utils/index.js'
import { ComponentLoader } from './backend/utils/component-loader.js'
import { bundlePath, stylePath } from './utils/theme-bundler.js'
import generateEntry from './backend/bundler/generate-user-component-entry.js'
import { ADMIN_JS_TMP_DIR } from './backend/bundler/utils/constants.js'

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '../package.json'), 'utf-8'))
Expand All @@ -39,12 +45,12 @@ export const defaultOptions: AdminJSOptionsWithDefault = {
}

type ActionsMap = {
show: Action<RecordActionResponse>;
edit: Action<RecordActionResponse>;
delete: Action<RecordActionResponse>;
bulkDelete: Action<BulkActionResponse>;
new: Action<RecordActionResponse>;
list: Action<ListActionResponse>;
show: Action<RecordActionResponse>
edit: Action<RecordActionResponse>
delete: Action<RecordActionResponse>
bulkDelete: Action<BulkActionResponse>
new: Action<RecordActionResponse>
list: Action<ListActionResponse>
}

export type Adapter = { Database: typeof BaseDatabase; Resource: typeof BaseResource }
Expand Down Expand Up @@ -127,9 +133,12 @@ class AdminJS {
* @param {typeof BaseDatabase} options.Database subclass of {@link BaseDatabase}
* @param {typeof BaseResource} options.Resource subclass of {@link BaseResource}
*/
static registerAdapter({ Database, Resource }: {
Database: typeof BaseDatabase;
Resource: typeof BaseResource;
static registerAdapter({
Database,
Resource,
}: {
Database: typeof BaseDatabase
Resource: typeof BaseResource
}): void {
if (!Database || !Resource) {
throw new Error('Adapter has to have both Database and Resource')
Expand All @@ -142,7 +151,9 @@ class AdminJS {
global.RegisteredAdapters = global.RegisteredAdapters || []
global.RegisteredAdapters.push({ Database, Resource })
} else {
throw new Error('Adapter elements have to be a subclass of AdminJS.BaseResource and AdminJS.BaseDatabase')
throw new Error(
'Adapter elements have to be a subclass of AdminJS.BaseResource and AdminJS.BaseDatabase',
)
}
}

Expand All @@ -151,11 +162,13 @@ class AdminJS {
* all external plugins.
*/
async initialize(): Promise<void> {
if (process.env.NODE_ENV === 'production'
&& !(process.env.ADMIN_JS_SKIP_BUNDLE === 'true')) {
if (process.env.NODE_ENV === 'production' && !(process.env.ADMIN_JS_SKIP_BUNDLE === 'true')) {
// eslint-disable-next-line no-console
console.log('AdminJS: bundling user components...')
await userComponentsBundler(this, { write: true })
await componentsBundler.createEntry({
content: generateEntry(this, ADMIN_JS_TMP_DIR),
})
await componentsBundler.build()
}
}

Expand All @@ -167,7 +180,10 @@ class AdminJS {
*/
async watch(): Promise<string | undefined> {
if (process.env.NODE_ENV !== 'production') {
return userComponentsBundler(this, { write: true, watch: true })
await componentsBundler.createEntry({
content: generateEntry(this, ADMIN_JS_TMP_DIR),
})
await componentsBundler.watch()
}
return undefined
}
Expand Down Expand Up @@ -204,11 +220,13 @@ class AdminJS {
findResource(resourceId): BaseResource {
const resource = this.resources.find((m) => m._decorated?.id() === resourceId)
if (!resource) {
throw new Error([
`There are no resources with given id: "${resourceId}"`,
'This is the list of all registered resources you can use:',
this.resources.map((r) => r._decorated?.id() || r.id()).join(', '),
].join('\n'))
throw new Error(
[
`There are no resources with given id: "${resourceId}"`,
'This is the list of all registered resources you can use:',
this.resources.map((r) => r._decorated?.id() || r.id()).join(', '),
].join('\n'),
)
}
return resource
}
Expand All @@ -230,18 +248,18 @@ class AdminJS {
}

if (!fs.existsSync(filePath)) {
throw new ConfigurationError(`Given babel config "${filePath}", doesn't exist.`, 'AdminJS.html')
throw new ConfigurationError(
`Given babel config "${filePath}", doesn't exist.`,
'AdminJS.html',
)
}
if (path.extname(filePath) === '.js') {
// eslint-disable-next-line
const configModule = require(filePath)
config = configModule && configModule.__esModule
? configModule.default || undefined
: configModule
// eslint-disable-next-line max-len
config = configModule && configModule.__esModule ? configModule.default || undefined : configModule
if (!config || typeof config !== 'object' || Array.isArray(config)) {
throw new Error(
`${filePath}: Configuration should be an exported JavaScript object.`,
)
throw new Error(`${filePath}: Configuration should be an exported JavaScript object.`)
}
} else {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/babel.test.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"@babel/preset-typescript"
],
"plugins": ["@babel/plugin-syntax-import-assertions"],
"plugins": ["@babel/plugin-syntax-import-attributes"],
"only": ["src/", "spec/"],
"ignore": [
"src/frontend/assets/scripts/app-bundle.development.js",
Expand Down
47 changes: 47 additions & 0 deletions src/backend/bundler/app.bundler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import path from 'path'
import * as url from 'url'

import { InputOptions, OutputOptions } from 'rollup'
import { nodeResolve as resolve } from '@rollup/plugin-node-resolve'
import * as commonjs from '@rollup/plugin-commonjs'
import * as replace from '@rollup/plugin-replace'
import * as json from '@rollup/plugin-json'
import { minify } from 'rollup-plugin-esbuild-minify'

import { AssetBundler } from './utils/asset-bundler.js'
import { NODE_ENV } from './utils/constants.js'

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

const input: InputOptions = {
input: path.join(__dirname, '../../frontend/bundle-entry.js'),
external: AssetBundler.DEFAULT_EXTERNALS,
plugins: [
resolve({
extensions: AssetBundler.DEFAULT_EXTENSIONS,
mainFields: ['browser', 'main', 'module', 'jsnext:main'],
preferBuiltins: false,
}),
(json as any).default(),
(replace as any).default({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
'process.env.IS_BROWSER': 'true',
'process.env.': 'AdminJS.env.',
preventAssignment: true,
'process.browser': true,
}),
(commonjs as any).default(),
...(NODE_ENV === 'production' ? [minify()] : []),
],
}

const output: OutputOptions = {
name: 'AdminJS',
file: path.join(__dirname, `../../frontend/assets/scripts/app-bundle.${NODE_ENV}.js`),
inlineDynamicImports: true,
globals: AssetBundler.DEFAULT_GLOBALS,
}

const bundler = new AssetBundler(input, output)

export default bundler
3 changes: 0 additions & 3 deletions src/backend/bundler/bundler-env.js

This file was deleted.

Loading

0 comments on commit c013a17

Please sign in to comment.