Skip to content

Commit

Permalink
- Removed generator import from runtime, its now exported as separate…
Browse files Browse the repository at this point in the history
… package

- Added multi-target exports
  • Loading branch information
DavidArayan committed Dec 4, 2023
1 parent ec6a2f8 commit 764455d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
17 changes: 17 additions & 0 deletions sdk-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions sdk-core/src/generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
73 changes: 65 additions & 8 deletions sdk-core/src/generator/generators/project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import version from "../../version";
import version from '../../version';

export interface PackageJsonVars {
readonly name: string;
Expand All @@ -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;
Expand All @@ -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',
Expand All @@ -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',
},
Expand All @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions sdk-core/src/generator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// export generator - this only works in NodeJS
export * from "./generator";
5 changes: 1 addition & 4 deletions sdk-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
export * from "./core/query/errors/core-error";

0 comments on commit 764455d

Please sign in to comment.