From 764455df8990c91122926d4a9557bf75aa870595 Mon Sep 17 00:00:00 2001 From: David Arayan Date: Mon, 4 Dec 2023 16:54:53 +1100 Subject: [PATCH] - Removed generator import from runtime, its now exported as separate package - Added multi-target exports --- sdk-core/package.json | 17 +++++ sdk-core/src/generator/generator.ts | 2 + sdk-core/src/generator/generators/project.ts | 73 +++++++++++++++++--- sdk-core/src/generator/index.ts | 2 + sdk-core/src/index.ts | 5 +- 5 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 sdk-core/src/generator/index.ts diff --git a/sdk-core/package.json b/sdk-core/package.json index 8582972..a3b50fa 100644 --- a/sdk-core/package.json +++ b/sdk-core/package.json @@ -5,6 +5,23 @@ "main": "dist/index.js", "module": "dist/index.js", "types": "dist/index.d.ts", + "exports": { + ".": { + "require": "./dist/index.js", + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "./runtime": { + "require": "./dist/index.js", + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, + "./generator": { + "require": "./dist/generator/index.js", + "import": "./dist/generator/index.js", + "types": "./dist/generator/index.d.ts" + } + }, "scripts": { "clean": "rm -rf dist node_modules package-lock.json generated && npm cache clean --force", "build": "npm install && npm run build-ts", diff --git a/sdk-core/src/generator/generator.ts b/sdk-core/src/generator/generator.ts index 72baab1..4e065a1 100644 --- a/sdk-core/src/generator/generator.ts +++ b/sdk-core/src/generator/generator.ts @@ -33,6 +33,8 @@ export class Generator { await fs.promises.writeFile(`${outputDir}/${project.packageJson.fname}`, project.packageJson.data); // write the .tsconfig file await fs.promises.writeFile(`${outputDir}/${project.tsConfig.fname}`, project.tsConfig.data); + // write the .webpack.config.js file + await fs.promises.writeFile(`${outputDir}/${project.webpack.fname}`, project.webpack.data); // write the README.md file await fs.promises.writeFile(`${outputDir}/${project.readme.fname}`, project.readme.data); diff --git a/sdk-core/src/generator/generators/project.ts b/sdk-core/src/generator/generators/project.ts index 3f8b951..9e3776f 100644 --- a/sdk-core/src/generator/generators/project.ts +++ b/sdk-core/src/generator/generators/project.ts @@ -1,4 +1,4 @@ -import version from "../../version"; +import version from '../../version'; export interface PackageJsonVars { readonly name: string; @@ -18,6 +18,10 @@ export interface GeneratedProject { readonly fname: string; readonly data: string; }; + readonly webpack: { + readonly fname: string; + readonly data: string; + }; readonly npmRc: { readonly fname: string; readonly data: string; @@ -43,13 +47,17 @@ export class Project { fname: 'tsconfig.json', data: JSON.stringify(Project.generateTsConfig(), null, 2) }, + webpack: { + fname: 'webpack.config.js', + data: Project.generateWebpackConfig(vars) + }, npmIgnore: { fname: '.npmignore', data: Project.generateNpmIgnore() }, npmRc: { fname: '.npmrc', - data: "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" + data: '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' }, readme: { fname: 'README.md', @@ -58,20 +66,66 @@ export class Project { } } + /** + * Generates the package.json file for the new project + */ + public static generateWebpackConfig(vars: PackageJsonVars): any { + const webpack = `{ + name: '${vars.name}', + mode: 'production', + devtool: 'source-map', + entry: { + main: ['./dist/index.js'] + }, + output: { + path: path.resolve(__dirname, './build'), + filename: "bundle.min.js" + }, + module: {}, + plugins: [ + new CleanWebpackPlugin() + ], + optimization: { + minimize: true, + minimizer: [ + new TerserPlugin({ + terserOptions: { + format: { + comments: false, + }, + keep_classnames: true, + keep_fnames: false, + sourceMap: true + }, + extractComments: false + }), + ], + } + }`; + + let output = `const { CleanWebpackPlugin } = require('clean-webpack-plugin');\n`; + output += `const TerserPlugin = require("terser-webpack-plugin");\n`; + output += `const path = require("path");\n\n`; + + return `${output}\nmodule.exports=${webpack}\n`; + } + /** * Generates the package.json file for the new project */ public static generatePackageJson(vars: PackageJsonVars): any { return { - name: '@plattar/' + vars.name, + name: `@plattar/${vars.name}`, version: vars.version, - description: 'Generated using @plattar/sdk-core and used for interfacing with ' + vars.name + ' backend service', + description: `Generated using @plattar/sdk-core and used for interfacing with ${vars.name} backend service`, main: 'dist/index.js', module: 'dist/index.js', types: 'dist/index.d.ts', + sideEffects: false, scripts: { - clean: 'rm -rf dist node_modules package-lock.json && npm cache clean --force', - build: 'npm install && npm run build-ts', + 'webpack:build': 'webpack', + clean: 'rm -rf dist build node_modules package-lock.json && npm cache clean --force', + build: 'npm install && npm run build-ts && npm run webpack:build', 'build-ts': 'tsc --noEmitOnError', 'clean:build': 'npm run clean && npm run build', }, @@ -89,10 +143,13 @@ export class Project { }, homepage: 'https://www.plattar.com', dependencies: { - '@plattar/sdk-core': '^' + version, + '@plattar/sdk-core': `^${version}`, }, devDependencies: { - typescript: '^5.2.2' + typescript: '^5.3.2', + webpack: '^5.89.0', + 'webpack-cli': '^5.1.4', + 'clean-webpack-plugin': '^4.0.0', }, publishConfig: { access: 'public', diff --git a/sdk-core/src/generator/index.ts b/sdk-core/src/generator/index.ts new file mode 100644 index 0000000..e12d038 --- /dev/null +++ b/sdk-core/src/generator/index.ts @@ -0,0 +1,2 @@ +// export generator - this only works in NodeJS +export * from "./generator"; \ No newline at end of file diff --git a/sdk-core/src/index.ts b/sdk-core/src/index.ts index 11644b4..fa51226 100644 --- a/sdk-core/src/index.ts +++ b/sdk-core/src/index.ts @@ -4,7 +4,4 @@ export * from "./core/core-object"; export * from "./core/global-object-pool"; export * from "./core/query/core-file-query"; export * from "./core/query/core-query"; -export * from "./core/query/errors/core-error"; - -// export generator - this only works in NodeJS -export * from "./generator/generator"; \ No newline at end of file +export * from "./core/query/errors/core-error"; \ No newline at end of file