improve build setup

This commit is contained in:
Rene Haas
2022-10-31 14:27:42 +01:00
parent 3d095f747d
commit 5e0e21c89e
12 changed files with 144 additions and 75 deletions
+3 -1
View File
@@ -26,7 +26,9 @@
},
"exports": {
".": "./src/createRollupConfig.js",
"./playwright": "./src/playwright/createPlaywrightRollupConfig.js"
"./playwright": "./src/playwright/createPlaywrightRollupConfig.js",
"./plugin/copy": "./src/plugins/copy.js",
"./plugin/packageJson": "./src/plugins/packageJson.js"
},
"version": "0.0.0"
}
-44
View File
@@ -1,44 +0,0 @@
const path = require('path');
const { rollupVirtual } = require('./plugins');
module.exports = (_, options) => {
const { extractPackageJson, outDir } = options;
const {
input = './package.json',
output = './package.json',
json,
} = typeof extractPackageJson === 'object' ? extractPackageJson : {};
const resolvedInput = path.resolve(input);
const inputPackageJson = require(resolvedInput);
return {
input: resolvedInput,
onwarn: (warning, warn) => {
if (warning.code !== 'EMPTY_BUNDLE') {
warn(warning);
}
},
output: {
file: path.resolve(outDir, output),
},
plugins: [
rollupVirtual({
[resolvedInput]: '',
}),
{
generateBundle(__, bundle) {
const bundleKeys = Object.keys(bundle);
if (bundleKeys.length > 1) {
throw new Error('Unexpected entries length.');
}
bundle[bundleKeys[0]].code = JSON.stringify(
typeof json === 'function' ? json(inputPackageJson) : inputPackageJson,
null,
2
);
},
},
],
};
};
+47
View File
@@ -0,0 +1,47 @@
const { rollupVirtual } = require('./plugins');
const rollupPluginClean = require('../plugins/clean');
const rollupPluginCopy = require('../plugins/copy');
const rollupPluginPackageJson = require('../plugins/packageJson');
module.exports = (_, options) => {
const { project, verbose, clean, copy, outDir, projectDir, extractPackageJson } = options;
return {
input: 'preBuild',
onwarn: (warning, warn) => {
if (warning.code !== 'EMPTY_BUNDLE') {
warn(warning);
}
},
output: {
dir: outDir,
},
plugins: [
verbose && {
name: 'PROJECT',
buildStart() {
// eslint-disable-next-line no-console
console.log('');
// eslint-disable-next-line no-console
console.log('PROJECT : ', project);
// eslint-disable-next-line no-console
console.log('OPTIONS : ', options);
},
},
clean && outDir !== projectDir && rollupPluginClean({ paths: [outDir], verbose }),
extractPackageJson && rollupPluginPackageJson(extractPackageJson),
copy && rollupPluginCopy({ paths: copy, verbose }),
rollupVirtual({
preBuild: '',
}),
{
generateBundle(__, bundle) {
const virtualEntry = Object.keys(bundle).find((key) => key.includes('virtual'));
if (virtualEntry) {
delete bundle[virtualEntry];
}
},
},
].filter(Boolean),
};
};
+4 -19
View File
@@ -7,7 +7,6 @@ const glob = require('glob');
const resolve = require('@~local/config/resolve');
const defaultOptions = require('./defaultOptions');
const pipelineDefault = require('./pipeline.default');
const rollupPluginClean = require('./plugins/clean');
const workspaceRoot = path.dirname(execSync('npm root').toString());
const pkg = require(`${workspaceRoot}/package.json`);
@@ -32,6 +31,7 @@ const resolvePath = (basePath, pathToResolve, appendExt) => {
const mergeAndResolveOptions = (userOptions) => {
const {
clean: defaultClean,
copy: defaultCopy,
outDir: defaultOutDir,
paths: defaultPaths,
versions: defaultVersions,
@@ -47,6 +47,7 @@ const mergeAndResolveOptions = (userOptions) => {
const {
project,
clean: rawClean,
copy: rawCopy,
outDir: rawOutDir,
paths: rawPaths = {},
alias: rawAlias = {},
@@ -74,6 +75,7 @@ const mergeAndResolveOptions = (userOptions) => {
versions: rawVersions ?? defaultVersions,
useEsbuild: rawUseEsbuild ?? defaultUseEsbuild,
clean: rawClean ?? defaultClean,
copy: rawCopy ?? defaultCopy,
outDir: rawOutDir ?? defaultOutDir,
paths: {
...defaultPaths,
@@ -120,26 +122,9 @@ const mergeAndResolveOptions = (userOptions) => {
const createConfig = (userOptions = {}) => {
const options = mergeAndResolveOptions(userOptions);
const { project, useEsbuild, verbose, clean, outDir, projectDir } = options;
const { useEsbuild } = options;
const result = pipelineDefault(resolve, options, useEsbuild);
const resultArr = Array.isArray(result) ? result : [result];
const prePlugins = !Array.isArray(resultArr[0].plugins) ? [] : resultArr[0].plugins;
resultArr[0].plugins = prePlugins;
if (verbose) {
prePlugins.push({
name: 'PROJECT',
buildStart() {
console.log('');
console.log('PROJECT : ', project);
console.log('OPTIONS : ', options);
},
});
}
if (clean && outDir !== projectDir) {
prePlugins.push(rollupPluginClean({ paths: [outDir], verbose }));
}
return resultArr;
};
+1
View File
@@ -5,6 +5,7 @@ module.exports = {
useEsbuild: false,
outDir: './dist',
clean: false,
copy: false,
paths: {
js: '.',
types: './types',
+4 -4
View File
@@ -2,16 +2,16 @@ const bundleScriptDefault = require('./bundle/script.default');
const bundleScriptEsbuild = require('./bundle/script.esbuild');
const bundleStyles = require('./bundle/styles');
const bundleTypes = require('./bundle/types');
const bundlePackageJson = require('./bundle/packageJson');
const preBuild = require('./bundle/pre');
module.exports = (resolve, options, esbuild) => {
const { extractTypes, extractStyles, extractPackageJson } = options;
const { extractTypes, extractStyles } = options;
const bundleScript = esbuild ? bundleScriptEsbuild : bundleScriptDefault;
const pkgJson = extractPackageJson && bundlePackageJson(resolve, options);
const pre = preBuild(resolve, options);
const styles = extractStyles && bundleStyles(resolve, options);
const types = extractTypes && bundleTypes(resolve, options);
const js = bundleScript(resolve, options);
return [pkgJson, styles, types, js].flat().filter((build) => !!build);
return [pre, styles, types, js].flat().filter((build) => !!build);
};
+1 -1
View File
@@ -5,7 +5,7 @@ module.exports = ({ paths = [], verbose = false } = {}) => {
let cleaned = false;
return {
name: 'clean',
async buildStart() {
buildStart() {
if (!cleaned) {
paths.forEach((currPath) => {
const resolvedPath = path.resolve(currPath);
+25
View File
@@ -0,0 +1,25 @@
const path = require('path');
const fs = require('fs');
module.exports = ({ paths = [], verbose = false } = {}) => {
return {
name: 'copy',
buildStart() {
paths.forEach((currPath) => {
const resolvedPath = path.resolve(currPath);
if (fs.existsSync(resolvedPath)) {
if (verbose) {
// eslint-disable-next-line no-console
console.log(`Copy: ${resolvedPath}`);
}
this.emitFile({
type: 'asset',
source: fs.readFileSync(resolvedPath),
fileName: path.basename(resolvedPath),
});
}
});
},
};
};
+20
View File
@@ -0,0 +1,20 @@
const path = require('path');
module.exports = ({ input = 'package.json', output = 'package.json', json } = {}) => {
const resolvedInput = path.resolve(input);
const inputPackageJson = require(resolvedInput);
return {
name: 'packageJson',
buildStart() {
this.emitFile({
type: 'asset',
source: JSON.stringify(
typeof json === 'function' ? json(inputPackageJson) : inputPackageJson,
null,
2
),
fileName: output,
});
},
};
};
@@ -2,10 +2,6 @@
"name": "overlayscrollbars-react",
"private": true,
"version": "0.4.0",
"files": [
"dist",
"README.md"
],
"main": "./dist/overlayscrollbars-react.umd.js",
"module": "./dist/overlayscrollbars-react.es.js",
"types": "./dist/overlayscrollbars-react.d.ts",
@@ -1,6 +1,8 @@
import { resolve } from 'node:path';
import { defineConfig } from 'vite';
import { esbuildResolve } from 'rollup-plugin-esbuild-resolve';
import rollupPluginPackageJson from '@~local/rollup/plugin/packageJson';
import rollupPluginCopy from '@~local/rollup/plugin/copy';
import react from '@vitejs/plugin-react';
export default defineConfig({
@@ -20,6 +22,40 @@ export default defineConfig({
overlayscrollbars: 'OverlayScrollbarsGlobal',
},
},
plugins: [
rollupPluginCopy({ paths: ['README.md'] }),
rollupPluginPackageJson({
json: ({
name,
version,
description,
author,
license,
homepage,
bugs,
repository,
keywords,
peerDependencies,
}) => {
return {
name,
version,
description,
author,
license,
homepage,
bugs,
repository,
keywords,
main: 'overlayscrollbars-react.umd.js',
module: 'overlayscrollbars-react.es.js',
types: 'types/overlayscrollbars-react.d.ts',
peerDependencies,
sideEffects: false,
};
},
}),
],
},
},
plugins: [esbuildResolve(), react({ jsxRuntime: 'classic' })],
+3 -2
View File
@@ -10,10 +10,11 @@ module.exports = createRollupConfig({
extractStyles: true,
extractTypes: true,
clean: true,
copy: ['README.md'],
extractPackageJson: {
json: ({
name,
version: originalVersion,
version: v,
description,
author,
license,
@@ -24,7 +25,7 @@ module.exports = createRollupConfig({
}) => {
return {
name,
version: originalVersion,
version: v,
description,
author,
license,