Skip to content

Commit

Permalink
fix(ui5-tooling-modules): chunks are stored in module name folder (#922)
Browse files Browse the repository at this point in the history
To simplify the handling of chunks of npm packages, they are now stored
in a folder with the name of the module. With this option a single
`resourceroots` mapping is sufficient to delegate all requests to the
npm package and its chunks to the proper location. This is especially
useful for CDN scenarios when the npm package is not included.

Example: CDN usage + `jspdf`

```html
<html>
  <head>
    [...]
    <script ...
      data-sap-ui-resourceroots='{
        ...
        "jspdf": "resources/jspdf"
      }'
```

Using this `resourceroots` mapping, all requests to `jspdf` and its
chunks are now resolved to `resources/jspdf*`.

* `jspdf` => `resources/jspdf.js`
* `jspdf/0c1H2u3N4k5` => `resources/jspdf/0c1H2u3N4k5.js`

Fixes #915
  • Loading branch information
petermuessig authored Dec 10, 2023
1 parent 367a366 commit c2ad65d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable no-unused-vars */
module.exports = function ({ log } = {}) {
const isBuiltInModule = function (module) {
try {
if (!require("path").isAbsolute(module) && require.resolve(module) === module) {
return true;
}
} catch (ex) {
/* */
}
return false;
};
return {
name: "polyfill-node-ignore",
resolveId: function (source) {
if (isBuiltInModule(source)) {
return { id: `${source}?polyfill-node-ignore`, moduleSideEffects: false };
}
return null;
},
load: function (source) {
if (source.endsWith("?polyfill-node-ignore")) {
return "";
}
return null;
},
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable no-unused-vars */
const nodePolyfills = require("rollup-plugin-polyfill-node");
module.exports = function ({ log } = {}) {
const { resolveId } = nodePolyfills();
return {
name: "polyfill-node-override",
resolveId: function (importee, importer, options) {
if (importee === "http2") {
return { id: "http2?node-polyfill-override", moduleSideEffects: false };
}
if (importee === "async_hooks") {
return { id: "async_hooks?node-polyfill-override", moduleSideEffects: false };
}
if (importee.startsWith("node:")) {
return resolveId.call(this, importee.substr("node:".length), importer, options);
}
return null;
},
load: function (source) {
if (source === "http2?node-polyfill-override") {
return `export const constants = {
HTTP2_HEADER_AUTHORITY: "authority",
HTTP2_HEADER_METHOD: "method",
HTTP2_HEADER_PATH: "path",
HTTP2_HEADER_SCHEME: "scheme",
HTTP2_HEADER_CONTENT_LENGTH: "content-length",
HTTP2_HEADER_EXPECT: "expect",
HTTP2_HEADER_STATUS: "status"
};`;
}
if (source === "async_hooks?node-polyfill-override") {
return `export class AsyncResource {};`;
}
return null;
},
};
};
13 changes: 6 additions & 7 deletions packages/ui5-tooling-modules/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const { nodeResolve } = require("@rollup/plugin-node-resolve");
const commonjs = require("@rollup/plugin-commonjs");
const json = require("@rollup/plugin-json");
const nodePolyfills = require("rollup-plugin-polyfill-node");
const nodePolyfillsOverride = require("./rollup-plugin-polyfill-node-override");
const nodePolyfillsIgnore = require("./rollup-plugin-polyfill-node-ignore");
const amdCustom = require("./rollup-plugin-amd-custom");
const skipAssets = require("./rollup-plugin-skip-assets");
const injectESModule = require("./rollup-plugin-inject-esmodule");
Expand Down Expand Up @@ -184,27 +186,24 @@ module.exports = function (log) {
"process.env.NODE_ENV": JSON.stringify("development"),
},
}),
nodePolyfillsOverride(),
nodePolyfills(),
nodePolyfillsIgnore(),
injectESModule(),
skipAssets({
log,
extensions: ["css"],
modules: ["crypto"],
}),
commonjs({
defaultIsModuleExports: true,
}),
amdCustom(),
nodePolyfills(),
json(),
nodeResolve({
mainFields,
preferBuiltins: false,
}),
pnpmResolve({
options: {
cwd,
mainFields,
},
resolveModule: function (moduleName) {
return that.resolveModule(moduleName, { cwd, depPaths, mainFields });
},
Expand Down Expand Up @@ -235,7 +234,7 @@ module.exports = function (log) {
define: "sap.ui.define",
},
entryFileNames: `${moduleName}.js`,
chunkFileNames: `${moduleName}-[hash].js`,
chunkFileNames: `${moduleName}/[hash].js`,
sourcemap: false, // isMiddleware ? "inline" : true
});

Expand Down
2 changes: 1 addition & 1 deletion packages/ui5-tooling-modules/test/__snap__/jspdf.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sap.ui.define(['exports', './jspdf-ZSsH8-2i'], (function (exports, jspdf_es_min) { 'use strict';
sap.ui.define(['exports', './jspdf/Ho2Fpw1t'], (function (exports, jspdf_es_min) { 'use strict';



Expand Down

Large diffs are not rendered by default.

0 comments on commit c2ad65d

Please sign in to comment.