@digigov/cli-build 2.0.0-e7d30530 → 2.0.0-eaf330f5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,119 @@
1
+ // Mostly shared from
2
+ // https://github.com/mui-org/material-ui/blob/master/babel.config.js
3
+ const lib = require("@digigov/cli/lib");
4
+
5
+ function makeBabelConfig(dir, opts = { docs: false, proptypes: false }) {
6
+ const project = lib.resolveProject(dir);
7
+ const aliases = !project.externalLockFile ? lib.aliases(true) : {};
8
+
9
+ const BABEL_ENV = process.env.BABEL_ENV || "esm";
10
+ const BABEL_PUBLISH = process.env.BABEL_PUBLISH || false;
11
+ const NODE_ENV = process.env.NODE_ENV;
12
+ const IS_COMMONJS = BABEL_ENV === "cjs" || NODE_ENV === "test";
13
+
14
+ const PRESETS = [
15
+ [
16
+ require.resolve("@babel/preset-env"),
17
+ {
18
+ modules: IS_COMMONJS ? "commonjs" : false,
19
+ },
20
+ ],
21
+ require.resolve("@babel/preset-react"),
22
+ ];
23
+
24
+ if (project.isTs) {
25
+ PRESETS.push(require.resolve("@babel/preset-typescript"));
26
+ }
27
+
28
+ const PLUGINS_COMMON = [
29
+ require.resolve("babel-plugin-optimize-clsx"),
30
+ [
31
+ require.resolve("@babel/plugin-proposal-class-properties"),
32
+ { loose: true },
33
+ ],
34
+ [
35
+ require.resolve("@babel/plugin-proposal-object-rest-spread"),
36
+ { loose: true },
37
+ ],
38
+ // any package needs to declare 7.4.4 as a runtime dependency. default is ^7.0.0
39
+ [require.resolve("@babel/plugin-transform-runtime"), { version: "^7.4.4" }],
40
+ // for IE 11 support
41
+ require.resolve("@babel/plugin-transform-object-assign"),
42
+ require.resolve("babel-plugin-transform-react-constant-elements"),
43
+ ];
44
+
45
+ const PLUGINS_PUBLISH = [
46
+ require.resolve("babel-plugin-transform-dev-warning"),
47
+ [
48
+ require.resolve("babel-plugin-react-remove-properties"),
49
+ { properties: ["data-testid"] },
50
+ ],
51
+ [
52
+ require.resolve("babel-plugin-transform-react-remove-prop-types"),
53
+ {
54
+ mode: "unsafe-wrap",
55
+ },
56
+ ],
57
+ ];
58
+
59
+ const PLUGINS = PLUGINS_COMMON;
60
+
61
+ // Apps images are handled using `next-images` plugin. For libraries there is no
62
+ // explicit way to provide assets to the App using the library. While not
63
+ // considered a very good practice, one way to provide images via libraries is
64
+ // to embed the image data into the library code.
65
+ if (project.isLib || project.isApp) {
66
+ PLUGINS.push(require.resolve("babel-plugin-inline-import-data-uri"));
67
+ }
68
+
69
+ if (BABEL_PUBLISH) {
70
+ PLUGINS.push(...PLUGINS_PUBLISH);
71
+ }
72
+
73
+ if (!opts.docs) {
74
+ let resolverAlias = {};
75
+ if (NODE_ENV === "test") resolverAlias = aliases;
76
+ if (BABEL_ENV === "cjs") {
77
+ resolverAlias = Object.keys(aliases).reduce((acc, key) => {
78
+ if (key !== project.name) {
79
+ acc[`^${key}/(.+)`] = `${key}/cjs/\\1`;
80
+ }
81
+ return acc;
82
+ }, {});
83
+ }
84
+
85
+ const RESOLVER = [
86
+ require.resolve("babel-plugin-module-resolver"),
87
+ {
88
+ alias: resolverAlias,
89
+ extensions: [".js", ".jsx", ".ts", ".tsx", ".json"],
90
+ loglevel: "silent",
91
+ },
92
+ ];
93
+ PLUGINS.push(RESOLVER);
94
+ }
95
+
96
+ if (project.isApp) {
97
+ PRESETS.push(require.resolve("next/babel"));
98
+ }
99
+
100
+ const CONFIG = {
101
+ presets: PRESETS,
102
+ plugins: PLUGINS,
103
+ ignore: [/@babel[\\|/]runtime/],
104
+ env: {
105
+ coverage: {
106
+ plugins: [require.resolve("babel-plugin-istanbul")],
107
+ },
108
+ test: {
109
+ sourceMaps: "both",
110
+ plugins: [],
111
+ },
112
+ },
113
+ };
114
+ return CONFIG;
115
+ }
116
+ module.exports = {
117
+ makeBabelConfig,
118
+ config: makeBabelConfig(),
119
+ };
@@ -0,0 +1 @@
1
+ module.exports = require("./babel.common.cjs").config;
package/build.js CHANGED
@@ -1,9 +1,9 @@
1
- import { logger } from '@digigov/cli/lib';
1
+ import { DigigovCommand, logger } from "@digigov/cli/lib";
2
2
 
3
- import assert from 'assert';
4
- import path from 'path';
5
- import fs from 'fs-extra';
6
- import baseEsbuild from 'esbuild';
3
+ import assert from "assert";
4
+ import path from "path";
5
+ import fs from "fs-extra";
6
+ import baseEsbuild from "esbuild";
7
7
 
8
8
  /**
9
9
  * Generate TypeScript declaration files
@@ -13,24 +13,24 @@ import baseEsbuild from 'esbuild';
13
13
  * @param {string} project.src - The project source directory
14
14
  * @param {string} project.distDir - The project build directory
15
15
  * @param {string} tsconfig - The tsconfig path
16
- * @param {import("@digigov/cli/lib").DigigovCommand} ctx - The command context
16
+ * @param {DigigovCommand} ctx - The command context
17
17
  */
18
18
  export async function generateTypeDeclarationFiles(project, tsconfig, ctx) {
19
- logger.debug('Building types...');
19
+ logger.debug("Building types...");
20
20
 
21
21
  const distDir = path.resolve(project.root, project.distDir);
22
22
  const projectBasename = path.basename(project.root);
23
23
 
24
- await ctx.exec('tsc', [
25
- '--emitDeclarationOnly',
26
- '--outDir',
27
- 'dist',
28
- '--project',
24
+ await ctx.exec("tsc", [
25
+ "--emitDeclarationOnly",
26
+ "--outDir",
27
+ "dist",
28
+ "--project",
29
29
  tsconfig,
30
30
  ]);
31
31
 
32
32
  const projectBasePath = path.join(distDir, projectBasename);
33
- logger.debug('Project base path', projectBasePath);
33
+ logger.debug("Project base path", projectBasePath);
34
34
  if (await fs.exists(projectBasePath)) {
35
35
  const typesIncluded = await fs.readdir(path.join(distDir));
36
36
  const srcPath = path.join(distDir, projectBasename, project.src);
@@ -39,19 +39,19 @@ export async function generateTypeDeclarationFiles(project, tsconfig, ctx) {
39
39
  await Promise.all([
40
40
  // Move src files to dist
41
41
  ...paths.map((p) => {
42
- logger.debug('Moving types file', p);
42
+ logger.debug("Moving types file", p);
43
43
  fs.move(path.join(srcPath, p), path.join(distDir, p));
44
44
  }),
45
45
  // Remove dirs
46
46
  ...typesIncluded.map((typesDir) => {
47
- logger.debug('Removing types directory', typesDir);
47
+ logger.debug("Removing types directory", typesDir);
48
48
  fs.rm(path.join(distDir, typesDir), { recursive: true });
49
49
  }),
50
50
  ]).catch((err) => {
51
- logger.error('Error while building types', err);
51
+ logger.error("Error while building types", err);
52
52
  });
53
53
  }
54
- logger.debug('Types built.');
54
+ logger.debug("Types built.");
55
55
  }
56
56
 
57
57
  /**
@@ -62,19 +62,11 @@ export async function generateTypeDeclarationFiles(project, tsconfig, ctx) {
62
62
  * @param {string | undefined} options.tsconfig - The tsconfig path
63
63
  * @param {"esm" | "cjs"} options.format - The module format
64
64
  * @param {string} options.outdir - The output directory
65
- * @param {boolean | undefined} [options.noLogs] - Whether to log debug information
66
65
  */
67
- export function buildFormat({
68
- files: entryPoints,
69
- tsconfig,
70
- format,
71
- outdir,
72
- noLogs,
73
- }) {
74
- assert(format === 'esm' || format === 'cjs', 'Invalid format');
66
+ export function buildFormat({ files: entryPoints, tsconfig, format, outdir }) {
67
+ assert(format === "esm" || format === "cjs", "Invalid format");
75
68
 
76
- if (!noLogs)
77
- logger.log(`Running: esbuild for ${format.toUpperCase()} format`);
69
+ logger.log(`Running: esbuild for ${format.toUpperCase()} format`);
78
70
  return baseEsbuild.build({
79
71
  ...BASE_OPTIONS,
80
72
  entryPoints,
@@ -86,8 +78,8 @@ export function buildFormat({
86
78
 
87
79
  /** @type {baseEsbuild.BuildOptions} */
88
80
  export const BASE_OPTIONS = {
89
- logLevel: 'error',
90
- platform: 'node',
81
+ logLevel: "error",
82
+ platform: "node",
91
83
  sourcemap: true,
92
- target: ['esnext'],
84
+ target: ["esnext"],
93
85
  };
package/common.js CHANGED
@@ -1,7 +1,7 @@
1
- import fs from 'fs-extra';
2
- import path from 'path';
1
+ import fs from "fs-extra";
2
+ import path from "path";
3
3
 
4
- const POSSIBLE_TS_CONFIGS = ['tsconfig.production.json', 'tsconfig.json'];
4
+ const POSSIBLE_TS_CONFIGS = ["tsconfig.production.json", "tsconfig.json"];
5
5
 
6
6
  /**
7
7
  * Get the tsconfig path for the given project
package/copy-files.js CHANGED
@@ -1,7 +1,7 @@
1
- import { logger, resolveProject } from '@digigov/cli/lib';
2
- import fs from 'fs-extra';
3
- import path from 'path';
4
- import glob from 'globby';
1
+ import { logger, resolveProject } from "@digigov/cli/lib";
2
+ import fs from "fs-extra";
3
+ import path from "path";
4
+ import glob from "globby";
5
5
 
6
6
  const packagePath = process.cwd();
7
7
  const project = resolveProject();
@@ -20,14 +20,14 @@ function includeFileInBuild(file) {
20
20
  }
21
21
 
22
22
  function copyRegistryFilesToSrc() {
23
- const registryPath = path.resolve(buildPath, 'registry/index.js');
24
- const lazyPath = path.resolve(buildPath, 'lazy/index.js');
23
+ const registryPath = path.resolve(buildPath, "registry/index.js");
24
+ const lazyPath = path.resolve(buildPath, "lazy/index.js");
25
25
  if (!fs.existsSync(registryPath) || !fs.existsSync(lazyPath)) return;
26
26
 
27
- const srcPath = path.resolve(buildPath, 'src');
27
+ const srcPath = path.resolve(buildPath, "src");
28
28
  logger.debug(`Copying registry and lazy files to ${srcPath}`);
29
- fs.copySync(registryPath, path.resolve(srcPath, 'registry.js'));
30
- fs.copySync(lazyPath, path.resolve(srcPath, 'lazy.js'));
29
+ fs.copySync(registryPath, path.resolve(srcPath, "registry.js"));
30
+ fs.copySync(lazyPath, path.resolve(srcPath, "lazy.js"));
31
31
  }
32
32
 
33
33
  /**
@@ -35,23 +35,21 @@ function copyRegistryFilesToSrc() {
35
35
  */
36
36
  function createRootPackageFile() {
37
37
  const packageData = fs.readFileSync(
38
- path.resolve(packagePath, './package.json'),
39
- 'utf8'
38
+ path.resolve(packagePath, "./package.json"),
39
+ "utf8",
40
40
  );
41
-
42
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
43
41
  const { nyc, scripts, devDependencies, workspaces, ...packageDataOther } =
44
42
  JSON.parse(packageData);
45
43
  const newPackageData = {
46
44
  ...packageDataOther,
47
45
  private: false,
48
- main: './cjs/index.js',
49
- module: './index.js',
50
- typings: './index.d.ts',
46
+ main: "./cjs/index.js",
47
+ module: "./index.js",
48
+ typings: "./index.d.ts",
51
49
  };
52
- const targetPath = path.resolve(buildPath, './package.json');
50
+ const targetPath = path.resolve(buildPath, "./package.json");
53
51
 
54
- fs.writeFileSync(targetPath, JSON.stringify(newPackageData, null, 2), 'utf8');
52
+ fs.writeFileSync(targetPath, JSON.stringify(newPackageData, null, 2), "utf8");
55
53
  logger.debug(`Created package.json in build directory`);
56
54
 
57
55
  return newPackageData;
@@ -62,24 +60,24 @@ function createRootPackageFile() {
62
60
  *
63
61
  */
64
62
  function createNestedPackageFiles() {
65
- const indexPaths = glob.sync(path.join(buildPath, '**/index.js'), {
66
- ignore: [path.join(buildPath, 'cjs/**')],
63
+ const indexPaths = glob.sync(path.join(buildPath, "**/index.js"), {
64
+ ignore: [path.join(buildPath, "cjs/**")],
67
65
  });
68
66
 
69
67
  indexPaths.forEach((indexPath) => {
70
- if (indexPath === path.join(buildPath, 'index.js')) return;
68
+ if (indexPath === path.join(buildPath, "index.js")) return;
71
69
  const packageData = {
72
70
  sideEffects: false,
73
- module: './index.js',
74
- types: './index.d.ts',
71
+ module: "./index.js",
72
+ types: "./index.d.ts",
75
73
  main: path.relative(
76
74
  path.dirname(indexPath),
77
- indexPath.replace(buildPath, path.join(buildPath, '/cjs'))
75
+ indexPath.replace(buildPath, path.join(buildPath, "/cjs")),
78
76
  ),
79
77
  };
80
78
  fs.writeFileSync(
81
- path.join(path.dirname(indexPath), 'package.json'),
82
- JSON.stringify(packageData, null, 2)
79
+ path.join(path.dirname(indexPath), "package.json"),
80
+ JSON.stringify(packageData, null, 2),
83
81
  );
84
82
  });
85
83
  }
@@ -91,8 +89,8 @@ function createNestedPackageFiles() {
91
89
  * @param {string} string - The string to prepend
92
90
  */
93
91
  function prepend(file, string) {
94
- const data = fs.readFileSync(file, 'utf8');
95
- fs.writeFileSync(file, string + data, 'utf8');
92
+ const data = fs.readFileSync(file, "utf8");
93
+ fs.writeFileSync(file, string + data, "utf8");
96
94
  logger.debug(`Prepended license to ${file}`);
97
95
  }
98
96
 
@@ -102,21 +100,21 @@ function prepend(file, string) {
102
100
  * @param {object} packageData - The package data
103
101
  */
104
102
  function addLicense(packageData) {
105
- const license = `/** @license Digigov v${packageData['version']}
103
+ const license = `/** @license Digigov v${packageData["version"]}
106
104
  *
107
105
  * This source code is licensed under the BSD-2-Clause license found in the
108
106
  * LICENSE file in the root directory of this source tree.
109
107
  */
110
108
  `;
111
- ['./index.js', './index.mjs'].map(async (file) => {
109
+ ["./index.js", "./index.mjs"].map(async (file) => {
112
110
  try {
113
111
  prepend(path.resolve(buildPath, file), license);
114
112
  } catch (err) {
115
113
  if (
116
- typeof err === 'object' &&
114
+ typeof err === "object" &&
117
115
  err &&
118
- 'code' in err &&
119
- err.code === 'ENOENT'
116
+ "code" in err &&
117
+ err.code === "ENOENT"
120
118
  ) {
121
119
  logger.debug(`Skipped license for ${file}`);
122
120
  } else {
@@ -130,13 +128,13 @@ function addLicense(packageData) {
130
128
  * Create separate index modules for each directory
131
129
  */
132
130
  function createSeparateIndexModules() {
133
- const files = glob.sync(path.join(buildPath, '**/*.js'), {
134
- ignore: [path.join(buildPath, '**/index.js')],
131
+ const files = glob.sync(path.join(buildPath, "**/*.js"), {
132
+ ignore: [path.join(buildPath, "**/index.js")],
135
133
  });
136
134
 
137
135
  files.forEach((file) => {
138
- fs.mkdirSync(file.replace(/\.js$/, ''));
139
- fs.renameSync(file, file.replace(/\.js$/, '/index.js'));
136
+ fs.mkdirSync(file.replace(/\.js$/, ""));
137
+ fs.renameSync(file, file.replace(/\.js$/, "/index.js"));
140
138
  });
141
139
  }
142
140
 
@@ -152,10 +150,10 @@ export default function run() {
152
150
  [
153
151
  // use enhanced readme from workspace root for `@digigov/ui`
154
152
  // packageData.name === '@digigov/ui' ? '../../README.md' : './README.md',
155
- './src',
156
- './README.md',
157
- './CHANGELOG.md',
158
- '../../LICENSE',
159
- ].map((file) => includeFileInBuild(file));
160
- addLicense(packageData);
153
+ "./src",
154
+ "./README.md",
155
+ "./CHANGELOG.md",
156
+ "../../LICENSE",
157
+ ].map((file) => includeFileInBuild(file)),
158
+ addLicense(packageData);
161
159
  }