Skip to content

Commit

Permalink
add very basic library type "module"
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Feb 9, 2021
1 parent 89e95d6 commit 35807ca
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 49 deletions.
81 changes: 67 additions & 14 deletions examples/module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ inc();
print(value);
resetCounter();
print(value);

export { inc, print };
```

# methods.js
Expand Down Expand Up @@ -39,11 +41,52 @@ export function reset() {

```javascript
/******/ "use strict";
/******/ // The require scope
/******/ var __webpack_require__ = {};
/******/
```

<details><summary><code>/* webpack runtime code */</code></summary>

``` js
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/************************************************************************/
```

</details>

``` js
var __webpack_exports__ = {};
/*!********************************!*\
!*** ./example.js + 2 modules ***!
\********************************/
/*! namespace exports */
/*! runtime requirements: */
/*! export inc [provided] [used in main] [could be renamed] -> ./counter.js .increment */
/*! export print [provided] [used in main] [could be renamed] -> ./methods.js .print */
/*! runtime requirements: __webpack_exports__, __webpack_require__.d, __webpack_require__.* */

// EXPORTS
__webpack_require__.d(__webpack_exports__, {
"inc": () => (/* reexport */ increment),
"print": () => (/* reexport */ print)
});

;// CONCATENATED MODULE: ./counter.js
let value = 0;
Expand Down Expand Up @@ -72,38 +115,48 @@ increment();
print(value);
counter_reset();
print(value);



var __webpack_exports__inc = __webpack_exports__.inc;
var __webpack_exports__print = __webpack_exports__.print;
export { __webpack_exports__inc as inc, __webpack_exports__print as print };
```

# dist/output.js (production)

```javascript
let o=0;function n(){o++}const c=o=>console.log(o);c(o),n(),n(),n(),c(o),o=0,c(o);
var e={d:(o,r)=>{for(var t in r)e.o(r,t)&&!e.o(o,t)&&Object.defineProperty(o,t,{enumerable:!0,get:r[t]})},o:(e,o)=>Object.prototype.hasOwnProperty.call(e,o)},o={};e.d(o,{a:()=>t,S:()=>a});let r=0;function t(){r++}const a=e=>console.log(e);a(r),t(),t(),t(),a(r),r=0,a(r);var n=o.a,c=o.S;export{n as inc,c as print};
```

# Info

## Unoptimized

```
asset output.js 580 bytes [emitted] [javascript module] (name: main)
chunk (runtime: main) output.js (main) 429 bytes [entry] [rendered]
asset output.js 2.05 KiB [emitted] [javascript module] (name: main)
chunk (runtime: main) output.js (main) 453 bytes (javascript) 396 bytes (runtime) [entry] [rendered]
> ./example.js main
./example.js + 2 modules 429 bytes [built] [code generated]
[no exports]
[no exports used]
runtime modules 396 bytes 2 modules
./example.js + 2 modules 453 bytes [built] [code generated]
[exports: inc, print]
[all exports used]
entry ./example.js main
webpack 5.11.1 compiled successfully
used as library export
webpack 5.20.2 compiled successfully
```

## Production mode

```
asset output.js 82 bytes [emitted] [javascript module] [minimized] (name: main)
chunk (runtime: main) output.js (main) 429 bytes [entry] [rendered]
asset output.js 314 bytes [emitted] [javascript module] [minimized] (name: main)
chunk (runtime: main) output.js (main) 453 bytes (javascript) 396 bytes (runtime) [entry] [rendered]
> ./example.js main
./example.js + 2 modules 429 bytes [built] [code generated]
[no exports]
[no exports used]
runtime modules 396 bytes 2 modules
./example.js + 2 modules 453 bytes [built] [code generated]
[exports: inc, print]
[all exports used]
entry ./example.js main
webpack 5.11.1 compiled successfully
used as library export
webpack 5.20.2 compiled successfully
```
2 changes: 2 additions & 0 deletions examples/module/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ inc();
print(value);
resetCounter();
print(value);

export { inc, print };
5 changes: 4 additions & 1 deletion examples/module/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = {
output: {
module: true
module: true,
library: {
type: "module"
}
},
optimization: {
usedExports: true,
Expand Down
1 change: 1 addition & 0 deletions lib/ExportsInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ class ExportsInfo {
setAllKnownExportsUsed(runtime) {
let changed = false;
for (const exportInfo of this._exports.values()) {
if (!exportInfo.provided) continue;
if (exportInfo.setUsed(UsageState.Used, runtime)) {
changed = true;
}
Expand Down
109 changes: 77 additions & 32 deletions lib/library/AbstractLibraryPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin")
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
/** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
/** @typedef {import("../util/Hash")} Hash */

const COMMON_LIBRARY_NAME_MESSAGE =
Expand Down Expand Up @@ -52,28 +53,36 @@ class AbstractLibraryPlugin {
apply(compiler) {
const { _pluginName } = this;
compiler.hooks.thisCompilation.tap(_pluginName, compilation => {
compilation.hooks.finishModules.tap(_pluginName, () => {
for (const [
name,
{
dependencies: deps,
options: { library }
}
] of compilation.entries) {
const options = this._parseOptionsCached(
library !== undefined ? library : compilation.outputOptions.library
);
if (options !== false) {
const dep = deps[deps.length - 1];
if (dep) {
const module = compilation.moduleGraph.getModule(dep);
if (module) {
this.finishEntryModule(module, name, { options, compilation });
compilation.hooks.finishModules.tap(
{ name: _pluginName, stage: 10 },
() => {
for (const [
name,
{
dependencies: deps,
options: { library }
}
] of compilation.entries) {
const options = this._parseOptionsCached(
library !== undefined
? library
: compilation.outputOptions.library
);
if (options !== false) {
const dep = deps[deps.length - 1];
if (dep) {
const module = compilation.moduleGraph.getModule(dep);
if (module) {
this.finishEntryModule(module, name, {
options,
compilation
});
}
}
}
}
}
});
);

const getOptionsForChunk = chunk => {
if (compilation.chunkGraph.getNumberOfEntryModules(chunk) === 0)
Expand All @@ -85,23 +94,47 @@ class AbstractLibraryPlugin {
);
};

compilation.hooks.additionalChunkRuntimeRequirements.tap(
_pluginName,
(chunk, set) => {
const options = getOptionsForChunk(chunk);
if (options !== false) {
this.runtimeRequirements(chunk, set, { options, compilation });
if (
this.render !== AbstractLibraryPlugin.prototype.render ||
this.runtimeRequirements !==
AbstractLibraryPlugin.prototype.runtimeRequirements
) {
compilation.hooks.additionalChunkRuntimeRequirements.tap(
_pluginName,
(chunk, set) => {
const options = getOptionsForChunk(chunk);
if (options !== false) {
this.runtimeRequirements(chunk, set, { options, compilation });
}
}
}
);
);
}

const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);

hooks.render.tap(_pluginName, (source, renderContext) => {
const options = getOptionsForChunk(renderContext.chunk);
if (options === false) return source;
return this.render(source, renderContext, { options, compilation });
});
if (this.render !== AbstractLibraryPlugin.prototype.render) {
hooks.render.tap(_pluginName, (source, renderContext) => {
const options = getOptionsForChunk(renderContext.chunk);
if (options === false) return source;
return this.render(source, renderContext, { options, compilation });
});
}

if (
this.renderStartup !== AbstractLibraryPlugin.prototype.renderStartup
) {
hooks.renderStartup.tap(
_pluginName,
(source, module, renderContext) => {
const options = getOptionsForChunk(renderContext.chunk);
if (options === false) return source;
return this.renderStartup(source, module, renderContext, {
options,
compilation
});
}
);
}

hooks.chunkHash.tap(_pluginName, (chunk, hash, context) => {
const options = getOptionsForChunk(chunk);
Expand Down Expand Up @@ -151,7 +184,8 @@ class AbstractLibraryPlugin {
* @returns {void}
*/
runtimeRequirements(chunk, set, libraryContext) {
set.add(RuntimeGlobals.returnExportsFromRuntime);
if (this.render !== AbstractLibraryPlugin.prototype.render)
set.add(RuntimeGlobals.returnExportsFromRuntime);
}

/**
Expand All @@ -164,6 +198,17 @@ class AbstractLibraryPlugin {
return source;
}

/**
* @param {Source} source source
* @param {Module} module module
* @param {StartupRenderContext} renderContext render context
* @param {LibraryContext<T>} libraryContext context
* @returns {Source} source with library export
*/
renderStartup(source, module, renderContext, libraryContext) {
return source;
}

/**
* @param {Chunk} chunk the chunk
* @param {Hash} hash hash
Expand Down
8 changes: 6 additions & 2 deletions lib/library/EnableLibraryPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,13 @@ class EnableLibraryPlugin {
}).apply(compiler);
break;
}
case "module":
// TODO implement module library
case "module": {
const ModuleLibraryPlugin = require("./ModuleLibraryPlugin");
new ModuleLibraryPlugin({
type
}).apply(compiler);
break;
}
default:
throw new Error(`Unsupported library type ${type}.
Plugins which provide custom library types must call EnableLibraryPlugin.setEnabled(compiler, type) to disable this error.`);
Expand Down
10 changes: 10 additions & 0 deletions lib/library/ExportPropertyLibraryPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
/** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
Expand Down Expand Up @@ -84,6 +85,15 @@ class ExportPropertyLibraryPlugin extends AbstractLibraryPlugin {
}
moduleGraph.addExtraReason(module, "used as library export");
}

/**
* @param {Chunk} chunk the chunk
* @param {Set<string>} set runtime requirements
* @param {LibraryContext<T>} libraryContext context
* @returns {void}
*/
runtimeRequirements(chunk, set, libraryContext) {}

/**
* @param {Source} source source
* @param {RenderContext} renderContext render context
Expand Down
Loading

0 comments on commit 35807ca

Please sign in to comment.