From 77b8307ebe06696b646c618656be85288fa4696f Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 25 Feb 2025 10:41:06 +0300 Subject: [PATCH] Build esm with rollup (#369) * Implement rollup build * Fix unit tests * Fix rollup source maps --- jest.config.js | 2 +- package.json | 14 +++-- rollup.config.js | 62 +++++++++++++++++++ rollup.fonts.config.js | 13 ++++ src/doc_controller.ts | 2 +- src/jspdf_plugins/acroform.js | 5 +- .../{acroform.ts => acroform_radio.ts} | 0 src/jspdf_plugins/from_html.js | 4 +- tsconfig.test.json | 1 + webpack.fesm.js | 40 ------------ webpack.fonts.fesm.js | 39 ------------ 11 files changed, 93 insertions(+), 89 deletions(-) create mode 100644 rollup.config.js create mode 100644 rollup.fonts.config.js rename src/jspdf_plugins/{acroform.ts => acroform_radio.ts} (100%) delete mode 100644 webpack.fesm.js delete mode 100644 webpack.fonts.fesm.js diff --git a/jest.config.js b/jest.config.js index 694c6005..615042b7 100644 --- a/jest.config.js +++ b/jest.config.js @@ -16,7 +16,7 @@ module.exports = { coverageReporters: ["json", "lcov", "text", "html", "text-summary", "cobertura"], roots: ["tests"], transform: { - "^.+\\.(ts|tsx)?$": ["ts-jest", { + "^.+\\.(js|ts|tsx)?$": ["ts-jest", { diagnostics: false, tsconfig: "tsconfig.test.json" }] diff --git a/package.json b/package.json index 4510d177..401ea4da 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,9 @@ "release": "standard-version --message \"Release: %s [azurepipelines skip]\" ", "doc_gen": "node doc_generator/lib_docgenerator.js src/entries/pdf.ts", "doc_update": "chmod +x ./docupdate_npm.sh && ./docupdate_npm.sh", - "build": "webpack --env buildType=dev --env emitDeclarations --env emitNonSourceFiles && webpack --env buildType=prod && webpack --config ./webpack.fesm.js --env buildType=prod", - "build:fonts": "webpack --config ./webpack.fonts.js --env buildType=dev && webpack --config ./webpack.fonts.js --env buildType=prod && webpack --config ./webpack.fonts.fesm.js --env buildType=prod", - "watch:dev": "concurrently \"webpack --env buildType=dev --env emitDeclarations --watch\" \"webpack --config ./webpack.fesm.js --env buildType=prod --watch\" ", + "build": "webpack --env buildType=dev --env emitDeclarations --env emitNonSourceFiles && webpack --env buildType=prod && rollup -c", + "build:fonts": "webpack --config ./webpack.fonts.js --env buildType=dev && webpack --config ./webpack.fonts.js --env buildType=prod && rollup -c rollup.fonts.config.js", + "watch:dev": "concurrently \"webpack --env buildType=dev --env emitDeclarations --watch\" \"rollup -c -w\" ", "lint": "eslint ./src --quiet", "pre-push-check": "npm run lint && npm run build && npm test" }, @@ -27,6 +27,10 @@ "survey-core": "latest" }, "devDependencies": { + "@rollup/plugin-node-resolve": "^16.0.0", + "@rollup/plugin-commonjs": "^28.0.2", + "@rollup/plugin-replace": "^6.0.2", + "@rollup/plugin-typescript": "^12.1.2", "jest-expect-message": "^1.1.3", "@types/jest": "^23.3.3", "@types/lodash": "4.14.121", @@ -43,6 +47,8 @@ "jest-junit": "^16.0.0", "http-server": "^14.1.1", "rimraf": "2.5.4", + "rollup": "^4.34.8", + "rollup-plugin-license": "^3.6.0", "standard-version": "^9.5.0", "surveyjs-doc-generator": "git+https://github.com/surveyjs/surveyjs-doc-generator.git", "tslib": "^2.8.1", @@ -58,4 +64,4 @@ "pre-push": "npm run pre-push-check" } } -} +} \ No newline at end of file diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 00000000..4c570fe7 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,62 @@ +const typescript = require("@rollup/plugin-typescript"); +const nodeResolve = require("@rollup/plugin-node-resolve"); +const commonjs = require("@rollup/plugin-commonjs"); +const replace = require("@rollup/plugin-replace"); +const bannerPlugin = require("rollup-plugin-license"); + +const path = require("path"); +const VERSION = require("./package.json").version; + +const banner = [ + "surveyjs - SurveyJS PDF library v" + VERSION, + "Copyright (c) 2015-" + new Date().getFullYear() + " Devsoft Baltic OÜ - http://surveyjs.io/", + "License: MIT (http://www.opensource.org/licenses/mit-license.php)" + ].join("\n"); +const input = { "survey.pdf": path.resolve(__dirname, "./src/entries/pdf.ts") }; +module.exports = (options) => { + options = options ?? {}; + if(!options.tsconfig) { + options.tsconfig = path.resolve(__dirname, "./tsconfig.fesm.json"); + } + if(!options.dir) { + options.dir = path.resolve(__dirname, "./build/fesm"); + } + return { + input, + context: "this", + + plugins: [ + nodeResolve(), + commonjs(), + typescript({ inlineSources: true, sourceMap: true, tsconfig: options.tsconfig, compilerOptions: { + declaration: false, + declarationDir: null + } }), + replace({ + preventAssignment: false, + values: { + "process.env.RELEASE_DATE": JSON.stringify(new Date().toISOString().slice(0, 10)), + "process.env.VERSION": JSON.stringify(VERSION), + } + }), + bannerPlugin({ + banner: { + content: banner, + commentStyle: "ignored", + } + }) + ], + external: [ + "jspdf", + "survey-core", + ], + output: [ + { + dir: options.dir, + format: "esm", + exports: "named", + sourcemap: true, + }, + ], + }; +}; \ No newline at end of file diff --git a/rollup.fonts.config.js b/rollup.fonts.config.js new file mode 100644 index 00000000..f5cc3406 --- /dev/null +++ b/rollup.fonts.config.js @@ -0,0 +1,13 @@ +const defaultConfig = require("./rollup.config"); +const path = require("path"); +const fs = require("fs"); +const input = { + "survey.pdf.fonts": path.resolve(__dirname, "./src/fonts.ts"), +}; + +module.exports = () => { + const config = defaultConfig(); + config.input = input; + config.external = ["survey-pdf"]; + return config; +}; \ No newline at end of file diff --git a/src/doc_controller.ts b/src/doc_controller.ts index 05ec8396..4bd091fd 100644 --- a/src/doc_controller.ts +++ b/src/doc_controller.ts @@ -3,7 +3,7 @@ import { IHTMLRenderType } from './flat_layout/flat_html'; import { SurveyHelper } from './helper_survey'; import { LocalizableString } from 'survey-core'; // import Fonts from './fonts'; -import setRadioAppearance from './jspdf_plugins/acroform'; +import setRadioAppearance from './jspdf_plugins/acroform_radio'; import './jspdf_plugins/acroform.js'; import './jspdf_plugins/from_html.js'; diff --git a/src/jspdf_plugins/acroform.js b/src/jspdf_plugins/acroform.js index aee34c49..d1cf0240 100644 --- a/src/jspdf_plugins/acroform.js +++ b/src/jspdf_plugins/acroform.js @@ -11,7 +11,8 @@ * jsPDF AcroForm Plugin * @module AcroForm */ -var jspdf = require('jspdf'); +import { jsPDF } from "jspdf"; + (function (jsPDF, globalObj) { 'use strict'; @@ -2922,4 +2923,4 @@ var jspdf = require('jspdf'); PasswordField: AcroFormPasswordField, Appearance: AcroFormAppearance }; -})(jspdf.jsPDF, (typeof window !== 'undefined' && window || typeof global !== 'undefined' && global)); \ No newline at end of file +})(jsPDF, (typeof window !== 'undefined' && window || typeof global !== 'undefined' && global)); \ No newline at end of file diff --git a/src/jspdf_plugins/acroform.ts b/src/jspdf_plugins/acroform_radio.ts similarity index 100% rename from src/jspdf_plugins/acroform.ts rename to src/jspdf_plugins/acroform_radio.ts diff --git a/src/jspdf_plugins/from_html.js b/src/jspdf_plugins/from_html.js index 51f69781..18dde06a 100644 --- a/src/jspdf_plugins/from_html.js +++ b/src/jspdf_plugins/from_html.js @@ -11,7 +11,7 @@ * * ==================================================================== */ - var jspdf = require("jspdf"); + import { jsPDF } from "jspdf"; (function (jsPDFAPI) { var clone, _DrillForContent, FontNameDB, FontStyleMap, TextAlignMap, FontWeightMap, FloatMap, ClearMap, GetCSS, PurgeWhiteSpace, Renderer, ResolveFont, ResolveUnitedNumber, UnitedNumberMap, elementHandledElsewhere, images, loadImgs, checkForFooter, process, tableToJson; @@ -1227,4 +1227,4 @@ if (!settings.elementHandlers) settings.elementHandlers = {}; return process(this, HTML, isNaN(x) ? 4 : x, isNaN(y) ? 4 : y, settings, callback); }; - })(jspdf.jsPDF.API); + })(jsPDF.API); diff --git a/tsconfig.test.json b/tsconfig.test.json index 5267f18f..6dac106d 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -11,6 +11,7 @@ "noImplicitAny": true, "importHelpers": false, "baseUrl": ".", + "allowJs": true, "experimentalDecorators": true, "moduleResolution": "node", "allowSyntheticDefaultImports": true diff --git a/webpack.fesm.js b/webpack.fesm.js deleted file mode 100644 index a90d3acf..00000000 --- a/webpack.fesm.js +++ /dev/null @@ -1,40 +0,0 @@ -"use strict"; -const webpackCommonConfigCreator = require("./webpack.config"); -const { merge } = require("webpack-merge"); - -function getConfig(options) { - const buildPath = __dirname + "/build/fesm/"; - const config = { - mode: "production", - devtool: "source-map", - output: { - filename: "[name]" + ".js", - path: buildPath, - library: { - type: "module" - } - }, - experiments: { - outputModule: true, - }, - optimization: { - minimize: false - }, - externalsType: "module", - externals: { - "survey-core": "survey-core", - "jspdf": "jspdf" - } - }; - - return config; -} - -module.exports = function (options) { - options.tsConfigFile = "tsconfig.fesm.json"; - const config = webpackCommonConfigCreator(options); - config.output = {}; - config.externals = {}; - delete config.mode; - return merge(config, getConfig(options)); -}; \ No newline at end of file diff --git a/webpack.fonts.fesm.js b/webpack.fonts.fesm.js deleted file mode 100644 index 7017fa1e..00000000 --- a/webpack.fonts.fesm.js +++ /dev/null @@ -1,39 +0,0 @@ -"use strict"; -const webpackCommonConfigCreator = require("./webpack.fonts"); -const { merge } = require("webpack-merge"); - -function getConfig(options) { - const buildPath = __dirname + "/build/fesm/"; - const config = { - mode: "production", - devtool: "source-map", - output: { - filename: "[name]" + ".js", - path: buildPath, - library: { - type: "module" - } - }, - experiments: { - outputModule: true, - }, - optimization: { - minimize: false - }, - externalsType: "module", - externals: { - "survey-pdf": "survey-pdf", - } - }; - - return config; -} - -module.exports = function (options) { - options.tsConfigFile = "tsconfig.fesm.json"; - const config = webpackCommonConfigCreator(options); - config.output = {}; - config.externals = {}; - delete config.mode; - return merge(config, getConfig(options)); -}; \ No newline at end of file