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",
"name": "OverlayScrollbars", "exports": "auto",
"exports": "auto", "globals": {
"globals": { "jquery": "jQuery"
"jquery": "jQuery"
}
} }
} }
+4 -6
View File
@@ -1,9 +1,7 @@
{ {
"legacy": { "name": "OverlayScrollbars",
"name": "OverlayScrollbars", "exports": "auto",
"exports": "auto", "globals": {
"globals": { "jquery": "jQuery"
"jquery": "jQuery"
}
} }
} }
+73 -50
View File
@@ -10,6 +10,7 @@ import path from 'path';
import resolve from './resolve.config.json'; import resolve from './resolve.config.json';
const projectRootPath = './packages'; const projectRootPath = './packages';
const buildConfigNames = ['build.config.js', 'build.config.json'];
const legacyBabelConfig = { const legacyBabelConfig = {
presets: [ 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 { 'config-project': project } = config;
const defaultInputName = './src/index'; const defaultInputName = './src/index';
const projectPath = path.resolve(__dirname, projectRootPath, project); const projectPath = path.resolve(__dirname, projectRootPath, project);
const packageJSONPath = path.resolve(projectPath, 'package.json'); const packageJSONPath = path.resolve(projectPath, 'package.json');
const tsconfigJSONPath = path.resolve(projectPath, 'tsconfig.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 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 { const { input, src, dist, types, tests, cache, minVersions, sourcemap, esmBuild, name, exports, globals } = buildConfig;
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 { devDependencies = {}, peerDependencies = {} } = await import(packageJSONPath); const { devDependencies = {}, peerDependencies = {} } = await import(packageJSONPath);
const srcPath = path.resolve(projectPath, src); const srcPath = src ? path.resolve(projectPath, src) : null;
const distPath = path.resolve(projectPath, dist); const distPath = dist ? path.resolve(projectPath, dist) : null;
const typesPath = path.resolve(projectPath, types); const typesPath = types ? path.resolve(projectPath, types) : null;
const testsPath = path.resolve(projectPath, tests); const testsPath = tests ? path.resolve(projectPath, tests) : null;
const inputPath = path.resolve(projectPath, input); const inputPath = input ? path.resolve(projectPath, input) : null;
const genOutputConfig = (esm) => ({ const genOutputConfig = (esm) => ({
format: esm ? 'esm' : 'umd', format: esm ? 'esm' : 'umd',
file: path.resolve(distPath, `${project}${esm ? '.esm' : ''}.js`), file: path.resolve(distPath, `${project}${esm ? '.esm' : ''}.js`),
sourcemap: esm ? modulesSourceMap : legacySourceMap, sourcemap,
...(esm ...(esm
? {} ? {}
: { : {
@@ -83,12 +96,12 @@ export default async (config) => {
}), }),
plugins: [ plugins: [
rollupPrettier({ rollupPrettier({
sourcemap: (esm ? modulesSourceMap : legacySourceMap) && 'silent', sourcemap: sourcemap && 'silent',
}), }),
], ],
}); });
const genConfig = async ({ esm, typeDeclaration }, ...plugins) => { const genConfig = async ({ esm, typeDeclaration }) => {
const output = genOutputConfig(esm); const output = genOutputConfig(esm);
return { return {
input: inputPath, input: inputPath,
@@ -111,7 +124,6 @@ export default async (config) => {
), ),
external: [...Object.keys(devDependencies), ...Object.keys(peerDependencies)], external: [...Object.keys(devDependencies), ...Object.keys(peerDependencies)],
plugins: [ plugins: [
...plugins,
rollupResolve({ rollupResolve({
extensions: resolve.extensions, extensions: resolve.extensions,
rootDir: srcPath, rootDir: srcPath,
@@ -129,7 +141,7 @@ export default async (config) => {
compilerOptions: { compilerOptions: {
target: 'ESNext', target: 'ESNext',
sourceMap: true, sourceMap: true,
declaration: typeDeclaration, declaration: typeDeclaration && types !== null,
declarationDir: typesPath, declarationDir: typesPath,
}, },
exclude: ((await import(tsconfigJSONPath)).exclude || []).concat(testsPath), exclude: ((await import(tsconfigJSONPath)).exclude || []).concat(testsPath),
@@ -147,33 +159,44 @@ export default async (config) => {
console.log(''); console.log('');
console.log('PROJECT : ', project); console.log('PROJECT : ', project);
console.log('NODE_ENV: ', process.env.NODE_ENV); console.log('ENV : ', process.env.NODE_ENV);
console.log('CONFIG : ', buildConfig);
return [ const legacy = await genConfig({ esm: false, typeDeclaration: true });
await genConfig( const esm = esmBuild ? await genConfig({ esm: true, typeDeclaration: false }) : null;
{ esm: false, typeDeclaration: true },
{ const builds = [legacy, esm]
name: 'deleteGeneratedDirs', .filter((build) => build !== null)
options() { .map((build, index, buildsArr) => {
const deletedDirs = del.sync([distPath, typesPath]); const isFirst = index === 0;
if (deletedDirs.length > 0) { const isLast = index === buildsArr.length - 1;
console.log('Deleted directories:\n', deletedDirs.join('\n'));
} 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'));
}
},
});
} }
), if (isLast) {
await genConfig( build.plugins.unshift({
{ esm: true, typeDeclaration: false }, name: 'deleteCacheDirs',
{ writeBundle() {
name: 'deleteCacheDirs', const cacheDirs = cache.map((dir) => path.resolve(projectPath, dir));
writeBundle() { const deletedDirs = del.sync(cacheDirs);
const cacheDirs = cache.map((dir) => path.resolve(projectPath, dir)); if (deletedDirs.length > 0) {
const deletedDirs = del.sync(cacheDirs); console.log('Deleted cache:\n', deletedDirs.join('\n'));
if (deletedDirs.length > 0) { }
console.log('Deleted cache:\n', deletedDirs.join('\n')); },
} });
},
} }
),
]; return build;
});
return builds;
}; };