Repository navigation
Expand file tree
/
Copy pathgulpfile.js
More file actions
207 lines (180 loc) · 6.19 KB
/
Copy pathgulpfile.js
File metadata and controls
207 lines (180 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const webpack2 = require("webpack");
const webpack_stream = require("webpack-stream");
let replace = require("gulp-replace");
let gulp = require("gulp");
const path = require("path");
const fs = require("fs");
const { execSync } = require("child_process");
// const __dirname = import.meta.dirname;
let src = gulp.src,
dest = gulp.dest;
const licenseHeaderLibre = fs
.readFileSync("./src/license_header_libre.js")
.toString();
let alias = {};
const webpack_config = {
mode: "none",
experiments: undefined,
// entry: {
// "generic-legacy/build/pdf": path.resolve(__dirname, "src/", "pdf.js"),
// "generic-legacy/build/pdf.worker": path.resolve(__dirname, "src/", "pdf.worker.js"),
// // 'dflip-pro/js/dflip': path.resolve(__dirname, 'src/js/', 'dflip-pro.js'),
// // 'dflip-lite-js/js/dflip': path.resolve(__dirname, 'src/js/', 'dflip-lite-js.js'),
// },
output: {
filename: "[name].js",
libraryTarget: "umd",
umdNamedDefine: true,
globalObject: "globalThis",
},
performance: { hints: false },
plugins: [
new webpack2.BannerPlugin({ banner: licenseHeaderLibre, raw: true }),
],
resolve: {
// extensions: [".js"],
alias: {},
},
// optimization: {
// // We do not want to minimize our code.
// minimize: false,
// },
// target: ["web", "es5"],
// devtool: 'source-map',
module: {
rules: [
{
// loader: "babel-loader",
exclude: /(node_modules[\\\/]core-js)/,
// options: {
// presets: [
// [
// "@babel/preset-env",
// {
// corejs: "3.32.2",
// shippedProposals: true,
// useBuiltIns: "usage",
// },
// ],
// ],
// plugins: ["@babel/plugin-transform-modules-commonjs"],
// targets: "last 2 versions, Chrome >= 92, Firefox ESR, Safari >= 15.4, Node >= 18, > 1%, not IE > 0, not dead",
// },
loader: "swc-loader",
options: {
env: {
targets: "last 2 versions, Chrome >= 92, Firefox ESR, Safari >= 15.4, Node >= 18, > 1%, not IE > 0, not dead",
// debug: true
}
}
},
{
loader: path.join(__dirname, "external/webpack/pdfjsdev-loader.mjs"),
options: {
rootPath: __dirname,
saveComments: false,
defines: {
SKIP_BABEL: false,
TESTING: false,
GENERIC: true,
MOZCENTRAL: false,
GECKOVIEW: false,
CHROME: false,
MINIFIED: false,
COMPONENTS: false,
LIB: false,
IMAGE_DECODERS: false,
BUNDLE_VERSION: "5.3.31",
BUNDLE_BUILD: "47ad820d9",
DEFAULT_PREFERENCES: {},
},
},
},
],
noParse: /jquery|lodash/,
},
node: false,
};
alias["display-cmap_reader_factory"] =
"src/display/cmap_reader_factory.js";
alias["display-standard_fontdata_factory"] =
"src/display/standard_fontdata_factory.js";
alias["display-wasm_factory"] = "src/display/wasm_factory.js";
alias["display-fetch_stream"] = "src/display/fetch_stream.js";
alias["display-network"] = "src/display/network.js";
alias["display-node_stream"] = "src/display/node_stream.js";
alias["display-node_utils"] = "src/display/node_utils.js";
alias["display-l10n_utils"] = "web/l10n_utils.js";
alias["display-svg"] = "src/display/svg.js";
for (const key in alias) {
alias[key] = path.resolve(__dirname, alias[key]);
}
webpack_config.resolve.alias = alias;
function addGlobalExports(amdName, jsName) {
const replacer = [
`module\\.exports = factory\\(\\);`,
`define\\("${amdName}", \\[\\], factory\\);`,
`exports\\["${amdName}"\\] = factory\\(\\);`,
`root\\["${amdName}"\\] = factory\\(\\);`,
];
const regex = new RegExp(`(${replacer.join("|")})`, "gm");
return replace(regex, match => {
switch (match) {
case `module.exports = factory();`:
return `module.exports = root.${jsName} = factory();`;
case `define("${amdName}", [], factory);`:
return `define("${amdName}", [], () => { return (root.${jsName} = factory()); });`;
case `exports["${amdName}"] = factory();`:
return `exports["${amdName}"] = root.${jsName} = factory();`;
case `root["${amdName}"] = factory();`:
return `root["${amdName}"] = root.${jsName} = factory();`;
}
return match;
});
}
function replaceNonWebpackImport() {
return replace("__non_webpack_import__", "import");
}
function replaceWebpackRequire() {
// Produced bundles can be rebundled again, avoid collisions (e.g. in api.js)
// by renaming __webpack_require__ to something else.
return replace("__webpack_require__", "__w_pdfjs_require__");
}
// madge --image graph.svg src/pdf.js
// madge --image graph-worker.svg src/pdf.worker.js
gulp.task("webpack-prod", gulp.series(
function MainBundle() {
const mainAMDName = "pdfjs-dist/build/pdf";
const mainOutputName = "pdf.js";
webpack_config.output.filename = mainOutputName;
webpack_config.output.library = mainAMDName;
// console.log(webpack_config);
return gulp
.src("src/pdf.js")
.pipe(webpack_stream(webpack_config))
.pipe(replaceWebpackRequire())
.pipe(replaceNonWebpackImport())
// .pipe(replace("DV_VERSION", packageJson.version))
.pipe(addGlobalExports(mainAMDName, "pdfjsLib"))
.pipe(gulp.dest("build/generic-legacy/build"));
},
function WorkerBundle() {
const mainAMDName = "pdfjs-dist/build/pdf-worker";
const mainOutputName = "pdf.worker.js";
webpack_config.output.filename = mainOutputName;
webpack_config.output.library = mainAMDName;
// console.log(webpack_config);
return gulp
.src("src/pdf.worker.js")
.pipe(webpack_stream(webpack_config))
.pipe(replaceWebpackRequire())
.pipe(replaceNonWebpackImport())
// .pipe(replace("DV_VERSION", packageJson.version))
.pipe(addGlobalExports(mainAMDName, "pdfjsWorker"))
.pipe(gulp.dest("build/generic-legacy/build"));
},
));
gulp.task('watch-dev', gulp.series('webpack-prod', function watch_dev() {
gulp.watch('src/**/*', gulp.series('webpack-prod'));
}));
gulp.task('default', gulp.series('webpack-prod'));