Skip to content

Commit

Permalink
Fix built assets to work when imported/required (#470)
Browse files Browse the repository at this point in the history
* Fix externalsType for UMD to support non-browser

* Fix ambiguous imports to support module build

* Remove ES and CJS builds, keeping just UMD
  • Loading branch information
eKoopmans committed Sep 2, 2021
1 parent 993d9d7 commit 0b9de5e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 31 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"name": "html2pdf.js",
"version": "0.10.0",
"description": "Client-side HTML-to-PDF rendering using pure JS",
"main": "dist/require/html2pdf.cjs.js",
"module": "dist/include/html2pdf.es.js",
"browser": "dist/html2pdf.js",
"main": "dist/html2pdf.js",
"files": [
"/src",
"/dist"
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/jspdf-plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import dependencies.
import jsPDF from 'jspdf';
import { jsPDF } from 'jspdf';

// Get dimensions of a PDF page, as determined by jsPDF.
jsPDF.getPageSize = function(orientation, unit, format) {
Expand Down
4 changes: 2 additions & 2 deletions src/worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jsPDF from 'jspdf';
import html2canvas from 'html2canvas';
import { jsPDF } from 'jspdf';
import * as html2canvas from 'html2canvas';
import { objType, createElement, cloneNode, toPx } from './utils.js';
import es6promise from 'es6-promise';
var Promise = es6promise.Promise;
Expand Down
33 changes: 8 additions & 25 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const pkg = require('./package.json');

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

const makeBrowserConfig = (filename, { bundle, min } = {}) => ({
const makeUMDConfig = (filename, { bundle, min } = {}) => ({
output: {
filename,
library: {
Expand All @@ -24,8 +25,8 @@ module.exports = env => {
}
},
target: 'browserslist',
externals: bundle ? [] : ['jspdf', 'html2canvas'],
externalsType: 'global',
externals: bundle ? [] : externals,
externalsType: 'umd',
optimization: { minimize: min },
devtool: min ? 'source-map' : false,
bundleAnalyzer: {
Expand All @@ -35,30 +36,12 @@ module.exports = env => {
},
});

const makeNodeConfig = (filename, { libraryTarget, target, externalsType, ...config }) => ({
output: {
filename,
libraryTarget,
},
target,
externals: ['jspdf', 'html2canvas'],
externalsType,
babelOptions: {
presets: ['@babel/preset-env'],
targets: { node: "current" },
},
...config,
});


const builds = {
browser: makeBrowserConfig('html2pdf.js'),
browserBundle: makeBrowserConfig('html2pdf.bundle.js', { bundle: true }),
node: makeNodeConfig('require/html2pdf.cjs.js', { libraryTarget: 'commonjs2', target: 'node', externalsType: 'commonjs' }),
es: makeNodeConfig('include/html2pdf.es.js', { libraryTarget: 'module', target: 'es6', externalsType: 'module', experiments: { outputModule: true } }),
umd: makeUMDConfig('html2pdf.js'),
umdBundle: makeUMDConfig('html2pdf.bundle.js', { bundle: true }),
...(isDev ? {} : {
browserMin: makeBrowserConfig('html2pdf.min.js', { min: true }),
browserBundleMin: makeBrowserConfig('html2pdf.bundle.min.js', { bundle: true, min: true }),
umdMin: makeUMDConfig('html2pdf.min.js', { min: true }),
umdBundleMin: makeUMDConfig('html2pdf.bundle.min.js', { bundle: true, min: true }),
}),
};

Expand Down

0 comments on commit 0b9de5e

Please sign in to comment.