From f00616a5eb4a032e74bd51685d3bb365d9220f55 Mon Sep 17 00:00:00 2001 From: Rene Haas Date: Thu, 27 Oct 2022 10:03:00 +0200 Subject: [PATCH] improve build and repo setup --- .eslintrc.js | 1 + local/rollup/src/bundle/script.default.js | 4 +-- local/rollup/src/bundle/script.esbuild.js | 4 +-- local/rollup/src/bundle/types.js | 5 ++- local/rollup/src/createRollupConfig.js | 12 ++++--- local/rollup/src/defaultOptions.js | 3 +- .../createPlaywrightRollupConfig.js | 36 +++---------------- tsconfig.json | 3 ++ 8 files changed, 25 insertions(+), 43 deletions(-) create mode 100644 tsconfig.json diff --git a/.eslintrc.js b/.eslintrc.js index 9e049b2..e125ba1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -90,6 +90,7 @@ module.exports = { './examples/**/tsconfig.json', './website/**/tsconfig.json', './local/**/tsconfig.json', + './tsconfig.json', ], }, env: { diff --git a/local/rollup/src/bundle/script.default.js b/local/rollup/src/bundle/script.default.js index b4a1687..c476d21 100644 --- a/local/rollup/src/bundle/script.default.js +++ b/local/rollup/src/bundle/script.default.js @@ -37,13 +37,13 @@ module.exports = (resolve, options) => { const { rollup, paths, versions, alias, extractStyles, banner } = options; const { output: rollupOutput, input, plugins = [], ...rollupOptions } = rollup; const { name, file, globals, exports, sourcemap: rawSourcemap, ...outputConfig } = rollupOutput; - const { dist: distPath } = paths; + const { js: jsPath } = paths; const sourcemap = rawSourcemap; return versions .map(({ format, generatedCode, file: filePathOverride, extension, minifiedVersion }) => { const needsGlobals = format === 'umd' || format === 'iife'; - const filePath = path.resolve(distPath, `${file}${extension || '.js'}`); + const filePath = path.resolve(jsPath, `${file}${extension || '.js'}`); const baseOutput = { ...outputConfig, diff --git a/local/rollup/src/bundle/script.esbuild.js b/local/rollup/src/bundle/script.esbuild.js index acc8d03..cffa2e3 100644 --- a/local/rollup/src/bundle/script.esbuild.js +++ b/local/rollup/src/bundle/script.esbuild.js @@ -12,7 +12,7 @@ module.exports = (resolve, options) => { const { rollup, paths, alias, extractStyles, banner } = options; const { output: rollupOutput, input, plugins = [], ...rollupOptions } = rollup; const { file, sourcemap: rawSourcemap, ...outputConfig } = rollupOutput; - const { dist: distPath } = paths; + const { js: jsPath } = paths; const sourcemap = rawSourcemap; const output = { @@ -20,7 +20,7 @@ module.exports = (resolve, options) => { sourcemap, format: 'esm', generatedCode: 'es2015', - file: path.resolve(distPath, `${file}.js`), + file: path.resolve(jsPath, `${file}.js`), }; return { diff --git a/local/rollup/src/bundle/types.js b/local/rollup/src/bundle/types.js index 78c98ac..8106469 100644 --- a/local/rollup/src/bundle/types.js +++ b/local/rollup/src/bundle/types.js @@ -1,5 +1,4 @@ const fs = require('fs'); -const { basename } = require('path'); const path = require('path'); const rollupDts = require('rollup-plugin-dts'); @@ -27,7 +26,7 @@ module.exports = (resolve, options) => { plugins: [rollupTs(input, true)], }, { - input: path.join(typesPath, `${basename(input).replace('.ts', '.d.ts')}`), + input: path.join(typesPath, `${path.basename(input).replace('.ts', '.d.ts')}`), output: { file: dtsOutput, }, @@ -48,7 +47,7 @@ module.exports = (resolve, options) => { writeBundle() { const filesAndDirs = fs.readdirSync(typesPath); filesAndDirs.forEach((fileOrDir) => { - if (basename(fileOrDir) !== basename(dtsOutput)) { + if (path.basename(fileOrDir) !== path.basename(dtsOutput)) { fs.rmSync(path.join(typesPath, fileOrDir), { recursive: true }); } }); diff --git a/local/rollup/src/createRollupConfig.js b/local/rollup/src/createRollupConfig.js index 002ac51..2e383c2 100644 --- a/local/rollup/src/createRollupConfig.js +++ b/local/rollup/src/createRollupConfig.js @@ -30,6 +30,7 @@ const resolvePath = (basePath, pathToResolve, appendExt) => { const mergeAndResolveOptions = (userOptions) => { const { + outDir: defaultOutDir, paths: defaultPaths, versions: defaultVersions, alias: defaultAlias, @@ -42,6 +43,7 @@ const mergeAndResolveOptions = (userOptions) => { } = defaultOptions; const { project, + outDir: rawOutDir, paths: rawPaths = {}, alias: rawAlias = {}, rollup: rawRollup = {}, @@ -64,6 +66,7 @@ const mergeAndResolveOptions = (userOptions) => { banner: rawBanner ?? defaultBanner, versions: rawVersions ?? defaultVersions, useEsbuild: rawUseEsbuild ?? defaultUseEsbuild, + outDir: rawOutDir ?? defaultOutDir, paths: { ...defaultPaths, ...rawPaths, @@ -83,13 +86,14 @@ const mergeAndResolveOptions = (userOptions) => { }, }, }; - const { dist, types, styles } = mergedOptions.paths; + const { outDir, paths } = mergedOptions; + const { js, types, styles } = paths; const pluginFromFn = (plugin) => typeof plugin === 'function' ? plugin(mergedOptions, workspaceRoot, workspaces) : plugin; - mergedOptions.paths.dist = resolvePath(projectPath, dist); - mergedOptions.paths.types = resolvePath(projectPath, types); - mergedOptions.paths.styles = resolvePath(projectPath, styles); + paths.js = resolvePath(projectPath, path.join(outDir, js)); + paths.types = resolvePath(projectPath, path.join(outDir, types)); + paths.styles = resolvePath(projectPath, path.join(outDir, styles)); mergedOptions.rollup.input = resolvePath(projectPath, mergedOptions.rollup.input, true); mergedOptions.rollup.output = { diff --git a/local/rollup/src/defaultOptions.js b/local/rollup/src/defaultOptions.js index dc60659..610428c 100644 --- a/local/rollup/src/defaultOptions.js +++ b/local/rollup/src/defaultOptions.js @@ -3,8 +3,9 @@ module.exports = { verbose: false, banner: null, useEsbuild: false, + outDir: './dist', paths: { - dist: './dist', + js: '.', types: './types', styles: './styles', }, diff --git a/local/rollup/src/playwright/createPlaywrightRollupConfig.js b/local/rollup/src/playwright/createPlaywrightRollupConfig.js index e595224..a8cc09b 100644 --- a/local/rollup/src/playwright/createPlaywrightRollupConfig.js +++ b/local/rollup/src/playwright/createPlaywrightRollupConfig.js @@ -18,7 +18,7 @@ const portRange = { }; const paths = { - dist: './.build', + outDir: './.build', input: './index.browser', html: './index.html', }; @@ -30,7 +30,7 @@ module.exports = (testDir, useEsbuild, dev) => { }, {}); const { min, max } = portRange; - const { dist, input, html: htmlPath } = testPaths; + const { outDir, input, html: htmlPath } = testPaths; const name = path.basename(testDir); const htmlName = `${name}.html`; const port = Math.floor(Math.random() * (max - min + 1) + min); @@ -43,9 +43,7 @@ module.exports = (testDir, useEsbuild, dev) => { banner: testDir, extractStyle: false, extractTypes: false, - paths: { - dist, - }, + outDir, versions: [ { format: 'iife', @@ -53,30 +51,6 @@ module.exports = (testDir, useEsbuild, dev) => { minifiedVersion: false, }, ], - // if the import would be 'overlayscrollbars' and the package name is also 'overlayscrollbars' esbuild needs an alias to resolve it correctly - alias: (workspaceRoot, workspaces, resolvePath) => - workspaces.reduce((obj, resolvedPath) => { - const absolutePath = path.resolve(workspaceRoot, resolvedPath); - try { - // eslint-disable-next-line import/no-dynamic-require, global-require - const projTsConfig = require(`${path.resolve( - workspaceRoot, - resolvedPath - )}/tsconfig.json`); - // eslint-disable-next-line import/no-dynamic-require, global-require - const projPackageJson = require(`${path.resolve( - workspaceRoot, - resolvedPath - )}/package.json`); - - const { name: projectName } = projPackageJson; - const { compilerOptions } = projTsConfig; - const { baseUrl } = compilerOptions; - - obj[projectName] = resolvePath(absolutePath, path.join(baseUrl, projectName), true); - } catch {} - return obj; - }, {}), rollup: { input, context: 'this', @@ -91,7 +65,7 @@ module.exports = (testDir, useEsbuild, dev) => { ), rollupPluginServe({ port, - contentBase: dist, + contentBase: outDir, historyApiFallback: `/${htmlName}`, host: '127.0.0.1', verbose: isDev, @@ -102,7 +76,7 @@ module.exports = (testDir, useEsbuild, dev) => { isDev && rollupAdditionalWatchFiles([htmlPath]), isDev && rollupPluginLivereload({ - watch: dist, + watch: outDir, port: port - 1, verbose: false, }), diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..3e4df8c --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "@~local/tsconfig" +}