Skip to content

Commit 0b9de5e

Browse files
authored
Fix built assets to work when imported/required (#470)
* Fix externalsType for UMD to support non-browser * Fix ambiguous imports to support module build * Remove ES and CJS builds, keeping just UMD
1 parent 993d9d7 commit 0b9de5e

File tree

4 files changed

+12
-31
lines changed

4 files changed

+12
-31
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
"name": "html2pdf.js",
33
"version": "0.10.0",
44
"description": "Client-side HTML-to-PDF rendering using pure JS",
5-
"main": "dist/require/html2pdf.cjs.js",
6-
"module": "dist/include/html2pdf.es.js",
7-
"browser": "dist/html2pdf.js",
5+
"main": "dist/html2pdf.js",
86
"files": [
97
"/src",
108
"/dist"

src/plugin/jspdf-plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Import dependencies.
2-
import jsPDF from 'jspdf';
2+
import { jsPDF } from 'jspdf';
33

44
// Get dimensions of a PDF page, as determined by jsPDF.
55
jsPDF.getPageSize = function(orientation, unit, format) {

src/worker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import jsPDF from 'jspdf';
2-
import html2canvas from 'html2canvas';
1+
import { jsPDF } from 'jspdf';
2+
import * as html2canvas from 'html2canvas';
33
import { objType, createElement, cloneNode, toPx } from './utils.js';
44
import es6promise from 'es6-promise';
55
var Promise = es6promise.Promise;

webpack.config.js

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const webpack = require('webpack');
33
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
44
const pkg = require('./package.json');
55

6+
const externals = [ 'jspdf', 'html2canvas' ];
67
const banner = `${pkg.name} v${pkg.version}
78
Copyright (c) ${(new Date).getFullYear()} Erik Koopmans
89
Released under the ${pkg.license} License.`;
@@ -13,7 +14,7 @@ module.exports = env => {
1314
const watch = isDev;
1415
const useAnalyzer = env.analyzer;
1516

16-
const makeBrowserConfig = (filename, { bundle, min } = {}) => ({
17+
const makeUMDConfig = (filename, { bundle, min } = {}) => ({
1718
output: {
1819
filename,
1920
library: {
@@ -24,8 +25,8 @@ module.exports = env => {
2425
}
2526
},
2627
target: 'browserslist',
27-
externals: bundle ? [] : ['jspdf', 'html2canvas'],
28-
externalsType: 'global',
28+
externals: bundle ? [] : externals,
29+
externalsType: 'umd',
2930
optimization: { minimize: min },
3031
devtool: min ? 'source-map' : false,
3132
bundleAnalyzer: {
@@ -35,30 +36,12 @@ module.exports = env => {
3536
},
3637
});
3738

38-
const makeNodeConfig = (filename, { libraryTarget, target, externalsType, ...config }) => ({
39-
output: {
40-
filename,
41-
libraryTarget,
42-
},
43-
target,
44-
externals: ['jspdf', 'html2canvas'],
45-
externalsType,
46-
babelOptions: {
47-
presets: ['@babel/preset-env'],
48-
targets: { node: "current" },
49-
},
50-
...config,
51-
});
52-
53-
5439
const builds = {
55-
browser: makeBrowserConfig('html2pdf.js'),
56-
browserBundle: makeBrowserConfig('html2pdf.bundle.js', { bundle: true }),
57-
node: makeNodeConfig('require/html2pdf.cjs.js', { libraryTarget: 'commonjs2', target: 'node', externalsType: 'commonjs' }),
58-
es: makeNodeConfig('include/html2pdf.es.js', { libraryTarget: 'module', target: 'es6', externalsType: 'module', experiments: { outputModule: true } }),
40+
umd: makeUMDConfig('html2pdf.js'),
41+
umdBundle: makeUMDConfig('html2pdf.bundle.js', { bundle: true }),
5942
...(isDev ? {} : {
60-
browserMin: makeBrowserConfig('html2pdf.min.js', { min: true }),
61-
browserBundleMin: makeBrowserConfig('html2pdf.bundle.min.js', { bundle: true, min: true }),
43+
umdMin: makeUMDConfig('html2pdf.min.js', { min: true }),
44+
umdBundleMin: makeUMDConfig('html2pdf.bundle.min.js', { bundle: true, min: true }),
6245
}),
6346
};
6447

0 commit comments

Comments
 (0)