improve build process

This commit is contained in:
Rene
2020-09-04 21:02:46 +02:00
parent dcaebba030
commit 2fcd10ff06
3 changed files with 81 additions and 62 deletions
@@ -1,9 +1,7 @@
{
"legacy": {
"name": "OverlayScrollbars",
"exports": "auto",
"globals": {
"jquery": "jQuery"
}
"name": "OverlayScrollbars",
"exports": "auto",
"globals": {
"jquery": "jQuery"
}
}
+4 -6
View File
@@ -1,9 +1,7 @@
{
"legacy": {
"name": "OverlayScrollbars",
"exports": "auto",
"globals": {
"jquery": "jQuery"
}
"name": "OverlayScrollbars",
"exports": "auto",
"globals": {
"jquery": "jQuery"
}
}
+73 -50
View File
@@ -10,6 +10,7 @@ import path from 'path';
import resolve from './resolve.config.json';
const projectRootPath = './packages';
const buildConfigNames = ['build.config.js', 'build.config.json'];
const legacyBabelConfig = {
presets: [
@@ -40,40 +41,52 @@ const esmBabelConfig = {
],
};
export default async (config) => {
const getBuildConfig = (projectPath) => {
const buildConfigName = buildConfigNames.find((name) => fs.existsSync(path.resolve(projectPath, name)));
return buildConfigName ? path.resolve(projectPath, buildConfigName) : '';
};
export default async (config, overwriteBuildConfig) => {
const { 'config-project': project } = config;
const defaultInputName = './src/index';
const projectPath = path.resolve(__dirname, projectRootPath, project);
const packageJSONPath = path.resolve(projectPath, 'package.json');
const tsconfigJSONPath = path.resolve(projectPath, 'tsconfig.json');
const buildConfigPath = path.resolve(projectPath, 'build.config.json');
const buildConfigPath = getBuildConfig(projectPath);
const isTypeScriptProject = fs.existsSync(tsconfigJSONPath);
const buildConfigDefaults = {
input: defaultInputName + resolve.extensions.find((ext) => fs.existsSync(path.resolve(projectPath, `${defaultInputName}${ext}`))),
src: './src',
dist: './dist',
types: './types',
tests: './tests',
cache: [],
minVersions: true,
sourcemap: true,
esmBuild: true,
name: project,
exports: 'auto',
};
const buildConfig = {
...buildConfigDefaults,
...(await import(buildConfigPath)),
...(overwriteBuildConfig || {}),
};
const {
input = defaultInputName + resolve.extensions.find((ext) => fs.existsSync(path.resolve(projectPath, `${defaultInputName}${ext}`))),
src = './src',
dist = './dist',
types = './types',
tests = './tests',
cache = [],
minVersions = true,
modules: { sourcemap: modulesSourceMap = true } = {},
legacy: { sourcemap: legacySourceMap = true, exports = 'auto', name = project, globals } = {},
} = await import(buildConfigPath);
const { input, src, dist, types, tests, cache, minVersions, sourcemap, esmBuild, name, exports, globals } = buildConfig;
const { devDependencies = {}, peerDependencies = {} } = await import(packageJSONPath);
const srcPath = path.resolve(projectPath, src);
const distPath = path.resolve(projectPath, dist);
const typesPath = path.resolve(projectPath, types);
const testsPath = path.resolve(projectPath, tests);
const inputPath = path.resolve(projectPath, input);
const srcPath = src ? path.resolve(projectPath, src) : null;
const distPath = dist ? path.resolve(projectPath, dist) : null;
const typesPath = types ? path.resolve(projectPath, types) : null;
const testsPath = tests ? path.resolve(projectPath, tests) : null;
const inputPath = input ? path.resolve(projectPath, input) : null;
const genOutputConfig = (esm) => ({
format: esm ? 'esm' : 'umd',
file: path.resolve(distPath, `${project}${esm ? '.esm' : ''}.js`),
sourcemap: esm ? modulesSourceMap : legacySourceMap,
sourcemap,
...(esm
? {}
: {
@@ -83,12 +96,12 @@ export default async (config) => {
}),
plugins: [
rollupPrettier({
sourcemap: (esm ? modulesSourceMap : legacySourceMap) && 'silent',
sourcemap: sourcemap && 'silent',
}),
],
});
const genConfig = async ({ esm, typeDeclaration }, ...plugins) => {
const genConfig = async ({ esm, typeDeclaration }) => {
const output = genOutputConfig(esm);
return {
input: inputPath,
@@ -111,7 +124,6 @@ export default async (config) => {
),
external: [...Object.keys(devDependencies), ...Object.keys(peerDependencies)],
plugins: [
...plugins,
rollupResolve({
extensions: resolve.extensions,
rootDir: srcPath,
@@ -129,7 +141,7 @@ export default async (config) => {
compilerOptions: {
target: 'ESNext',
sourceMap: true,
declaration: typeDeclaration,
declaration: typeDeclaration && types !== null,
declarationDir: typesPath,
},
exclude: ((await import(tsconfigJSONPath)).exclude || []).concat(testsPath),
@@ -147,33 +159,44 @@ export default async (config) => {
console.log('');
console.log('PROJECT : ', project);
console.log('NODE_ENV: ', process.env.NODE_ENV);
console.log('ENV : ', process.env.NODE_ENV);
console.log('CONFIG : ', buildConfig);
return [
await genConfig(
{ esm: false, typeDeclaration: true },
{
name: 'deleteGeneratedDirs',
options() {
const deletedDirs = del.sync([distPath, typesPath]);
if (deletedDirs.length > 0) {
console.log('Deleted directories:\n', deletedDirs.join('\n'));
}
},
const legacy = await genConfig({ esm: false, typeDeclaration: true });
const esm = esmBuild ? await genConfig({ esm: true, typeDeclaration: false }) : null;
const builds = [legacy, esm]
.filter((build) => build !== null)
.map((build, index, buildsArr) => {
const isFirst = index === 0;
const isLast = index === buildsArr.length - 1;
if (isFirst) {
build.plugins.unshift({
name: 'deleteGeneratedDirs',
options() {
const deletedDirs = del.sync([distPath, typesPath].filter((curr) => curr !== null));
if (deletedDirs.length > 0) {
console.log('Deleted directories:\n', deletedDirs.join('\n'));
}
},
});
}
),
await genConfig(
{ esm: true, typeDeclaration: false },
{
name: 'deleteCacheDirs',
writeBundle() {
const cacheDirs = cache.map((dir) => path.resolve(projectPath, dir));
const deletedDirs = del.sync(cacheDirs);
if (deletedDirs.length > 0) {
console.log('Deleted cache:\n', deletedDirs.join('\n'));
}
},
if (isLast) {
build.plugins.unshift({
name: 'deleteCacheDirs',
writeBundle() {
const cacheDirs = cache.map((dir) => path.resolve(projectPath, dir));
const deletedDirs = del.sync(cacheDirs);
if (deletedDirs.length > 0) {
console.log('Deleted cache:\n', deletedDirs.join('\n'));
}
},
});
}
),
];
return build;
});
return builds;
};