Skip to content

Commit

Permalink
Build esm with rollup (#369)
Browse files Browse the repository at this point in the history
* Implement rollup build

* Fix unit tests

* Fix rollup source maps
  • Loading branch information
dk981234 authored Feb 25, 2025
1 parent 90fccfb commit 77b8307
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 89 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}]
Expand Down
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -58,4 +64,4 @@
"pre-push": "npm run pre-push-check"
}
}
}
}
62 changes: 62 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -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,
},
],
};
};
13 changes: 13 additions & 0 deletions rollup.fonts.config.js
Original file line number Diff line number Diff line change
@@ -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;
};
2 changes: 1 addition & 1 deletion src/doc_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
5 changes: 3 additions & 2 deletions src/jspdf_plugins/acroform.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* jsPDF AcroForm Plugin
* @module AcroForm
*/
var jspdf = require('jspdf');
import { jsPDF } from "jspdf";

(function (jsPDF, globalObj) {
'use strict';

Expand Down Expand Up @@ -2922,4 +2923,4 @@ var jspdf = require('jspdf');
PasswordField: AcroFormPasswordField,
Appearance: AcroFormAppearance
};
})(jspdf.jsPDF, (typeof window !== 'undefined' && window || typeof global !== 'undefined' && global));
})(jsPDF, (typeof window !== 'undefined' && window || typeof global !== 'undefined' && global));
File renamed without changes.
4 changes: 2 additions & 2 deletions src/jspdf_plugins/from_html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
1 change: 1 addition & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"noImplicitAny": true,
"importHelpers": false,
"baseUrl": ".",
"allowJs": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
Expand Down
40 changes: 0 additions & 40 deletions webpack.fesm.js

This file was deleted.

39 changes: 0 additions & 39 deletions webpack.fonts.fesm.js

This file was deleted.

0 comments on commit 77b8307

Please sign in to comment.