mirror of https://github.com/webpack/webpack.git
Compare commits
4 Commits
cc530bbe6b
...
d23a6f238a
Author | SHA1 | Date |
---|---|---|
|
d23a6f238a | |
|
c7dd066327 | |
|
85bacbdc6e | |
|
fad1bc1f32 |
|
@ -254,10 +254,12 @@ class ContextModule extends Module {
|
|||
} else if (this.options.namespaceObject) {
|
||||
identifier += "|namespace object";
|
||||
}
|
||||
if (this.options.attributes) {
|
||||
identifier += `|importAttributes: ${JSON.stringify(this.options.attributes)}`;
|
||||
}
|
||||
if (this.layer) {
|
||||
identifier += `|layer: ${this.layer}`;
|
||||
}
|
||||
|
||||
return identifier;
|
||||
}
|
||||
|
||||
|
|
|
@ -53,6 +53,7 @@ class AssetSourceGenerator extends Generator {
|
|||
|
||||
const encodedSource = originalSource.buffer().toString("base64");
|
||||
|
||||
runtimeRequirements.add(RuntimeGlobals.requireScope);
|
||||
runtimeRequirements.add(RuntimeGlobals.toBinary);
|
||||
|
||||
let sourceContent;
|
||||
|
|
|
@ -68,8 +68,8 @@ class ContextElementDependency extends ModuleDependency {
|
|||
*/
|
||||
getResourceIdentifier() {
|
||||
let str = super.getResourceIdentifier();
|
||||
if (this.attributes !== undefined) {
|
||||
str += JSON.stringify(this.attributes);
|
||||
if (this.attributes) {
|
||||
str += `|importAttributes${JSON.stringify(this.attributes)}`;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
|
|
@ -79,8 +79,11 @@ class HarmonyImportDependency extends ModuleDependency {
|
|||
*/
|
||||
getResourceIdentifier() {
|
||||
let str = super.getResourceIdentifier();
|
||||
if (this.attributes !== undefined) {
|
||||
str += JSON.stringify(this.attributes);
|
||||
if (this.defer) {
|
||||
str += "|defer";
|
||||
}
|
||||
if (this.attributes) {
|
||||
str += `|importAttributes${JSON.stringify(this.attributes)}`;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
|
|
@ -35,6 +35,19 @@ class ImportContextDependency extends ContextDependency {
|
|||
return "esm";
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {string | null} an identifier to merge equal requests
|
||||
*/
|
||||
getResourceIdentifier() {
|
||||
let str = super.getResourceIdentifier();
|
||||
|
||||
if (this.options.attributes) {
|
||||
str += `|importAttributes${JSON.stringify(this.options.attributes)}`;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ObjectSerializerContext} context context
|
||||
*/
|
||||
|
|
|
@ -50,8 +50,8 @@ class ImportDependency extends ModuleDependency {
|
|||
*/
|
||||
getResourceIdentifier() {
|
||||
let str = super.getResourceIdentifier();
|
||||
if (this.attributes !== undefined) {
|
||||
str += JSON.stringify(this.attributes);
|
||||
if (this.attributes) {
|
||||
str += `|importAttributes${JSON.stringify(this.attributes)}`;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
|
|
@ -376,28 +376,40 @@ class LazyCompilationPlugin {
|
|||
apply(compiler) {
|
||||
/** @type {BackendApi} */
|
||||
let backend;
|
||||
compiler.hooks.beforeCompile.tapAsync(PLUGIN_NAME, (params, callback) => {
|
||||
if (backend !== undefined) return callback();
|
||||
const promise = this.backend(compiler, (err, result) => {
|
||||
if (err) return callback(err);
|
||||
backend = /** @type {BackendApi} */ (result);
|
||||
callback();
|
||||
});
|
||||
if (promise && promise.then) {
|
||||
promise.then((b) => {
|
||||
backend = b;
|
||||
compiler.hooks.beforeCompile.tapAsync(
|
||||
PLUGIN_NAME,
|
||||
(/** @type {any} */ params, /** @type {(err?: Error | null) => void} */ callback) => {
|
||||
if (backend !== undefined) return callback();
|
||||
const promise = this.backend(compiler, (err, result) => {
|
||||
if (err) return callback(err);
|
||||
backend = /** @type {BackendApi} */ (result);
|
||||
callback();
|
||||
}, callback);
|
||||
});
|
||||
if (promise && promise.then) {
|
||||
promise.then((b) => {
|
||||
backend = b;
|
||||
callback();
|
||||
}, callback);
|
||||
}
|
||||
}
|
||||
});
|
||||
);
|
||||
compiler.hooks.thisCompilation.tap(
|
||||
PLUGIN_NAME,
|
||||
/**
|
||||
* @param {import("../Compilation")} compilation
|
||||
* @param {{ normalModuleFactory: import("../NormalModuleFactory") }} param1
|
||||
*/
|
||||
(compilation, { normalModuleFactory }) => {
|
||||
normalModuleFactory.hooks.module.tap(
|
||||
PLUGIN_NAME,
|
||||
/**
|
||||
* @param {Module} module
|
||||
* @param {*} createData
|
||||
* @param {*} resolveData
|
||||
*/
|
||||
(module, createData, resolveData) => {
|
||||
if (
|
||||
resolveData.dependencies.every((dep) =>
|
||||
resolveData.dependencies.every((dep: any) =>
|
||||
HMR_DEPENDENCY_TYPES.has(dep.type)
|
||||
)
|
||||
) {
|
||||
|
@ -457,7 +469,7 @@ class LazyCompilationPlugin {
|
|||
);
|
||||
}
|
||||
);
|
||||
compiler.hooks.shutdown.tapAsync(PLUGIN_NAME, (callback) => {
|
||||
compiler.hooks.shutdown.tapAsync(PLUGIN_NAME, (callback: (...args: any[]) => void) => {
|
||||
backend.dispose(callback);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -134,11 +134,21 @@ class SystemLibraryPlugin extends AbstractLibraryPlugin {
|
|||
);
|
||||
if (used) {
|
||||
if (otherUnused || used !== exportInfo.name) {
|
||||
instructions.push(
|
||||
`${external}${propertyAccess([
|
||||
used
|
||||
])} = module${propertyAccess([exportInfo.name])};`
|
||||
);
|
||||
if (exportInfo.name === "default") {
|
||||
// Ideally we should use `module && module.__esModule ? module['default'] : module`
|
||||
// But we need to keep compatibility with SystemJS format libraries (they are using `default`) and bundled SystemJS libraries from commonjs format
|
||||
instructions.push(
|
||||
`${external}${propertyAccess([
|
||||
used
|
||||
])} = module["default"] || module;`
|
||||
);
|
||||
} else {
|
||||
instructions.push(
|
||||
`${external}${propertyAccess([
|
||||
used
|
||||
])} = module${propertyAccess([exportInfo.name])};`
|
||||
);
|
||||
}
|
||||
handledNames.push(exportInfo.name);
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -1,12 +1,17 @@
|
|||
import * as style from "./style.css";
|
||||
import file from "./file.text" with { type: "bytes" };
|
||||
|
||||
it("should work", () => {
|
||||
it("should work", async () => {
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
const text = decoder.decode(file);
|
||||
|
||||
expect(text).toBe("a Ā 𐀀 文 🦄 Text");
|
||||
|
||||
const dyn = (await import("./file.text?other", { with: { type: "bytes" } })).default;
|
||||
const dynText = decoder.decode(dyn);
|
||||
|
||||
expect(dynText).toBe("a Ā 𐀀 文 🦄 Text");
|
||||
|
||||
if (typeof getComputedStyle === "function") {
|
||||
const style = getComputedStyle(document.body);
|
||||
expect(style.getPropertyValue("--my-url")).toBe(" url(data:application/octet-stream;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2MDAgNjAwIj48dGl0bGU+aWNvbi1zcXVhcmUtc21hbGw8L3RpdGxlPjxwYXRoIGZpbGw9IiNGRkYiIGQ9Ik0zMDAgLjFMNTY1IDE1MHYyOTkuOUwzMDAgNTk5LjggMzUgNDQ5LjlWMTUweiIvPjxwYXRoIGZpbGw9IiM4RUQ2RkIiIGQ9Ik01MTcuNyA0MzkuNUwzMDguOCA1NTcuOHYtOTJMNDM5IDM5NC4xbDc4LjcgNDUuNHptMTQuMy0xMi45VjE3OS40bC03Ni40IDQ0LjF2MTU5bDc2LjQgNDQuMXpNODEuNSA0MzkuNWwyMDguOSAxMTguMnYtOTJsLTEzMC4yLTcxLjYtNzguNyA0NS40em0tMTQuMy0xMi45VjE3OS40bDc2LjQgNDQuMXYxNTlsLTc2LjQgNDQuMXptOC45LTI2My4yTDI5MC40IDQyLjJ2ODlsLTEzNy4zIDc1LjUtMS4xLjYtNzUuOS00My45em00NDYuOSAwTDMwOC44IDQyLjJ2ODlMNDQ2IDIwNi44bDEuMS42IDc1LjktNDR6Ii8+PHBhdGggZmlsbD0iIzFDNzhDMCIgZD0iTTI5MC40IDQ0NC44TDE2MiAzNzQuMVYyMzQuMmwxMjguNCA3NC4xdjEzNi41em0xOC40IDBsMTI4LjQtNzAuNnYtMTQwbC0xMjguNCA3NC4xdjEzNi41ek0yOTkuNiAzMDN6bS0xMjktODVsMTI5LTcwLjlMNDI4LjUgMjE4bC0xMjguOSA3NC40LTEyOS03NC40eiIvPjwvc3ZnPg==)");
|
||||
|
|
|
@ -14,5 +14,15 @@ module.exports = {
|
|||
scope.window.document.head.appendChild(link);
|
||||
|
||||
run++;
|
||||
},
|
||||
findBundle(i) {
|
||||
if (i === 2) {
|
||||
return ["bundle2.mjs"];
|
||||
}
|
||||
|
||||
return [
|
||||
`file_text_other.bundle${i}.${i === 2 ? "mjs" : "js"}`,
|
||||
`bundle${i}.${i === 2 ? "mjs" : "js"}`
|
||||
];
|
||||
}
|
||||
};
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"foo": "bar"
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
it("show override request", async () => {
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
const mod = "file.ext";
|
||||
const loadedMod = (await import(`./files/${mod}`, { with: { type: "bytes" } })).default;
|
||||
const text = decoder.decode(loadedMod);
|
||||
|
||||
expect(JSON.parse(text)).toEqual({ foo: "bar" });
|
||||
|
||||
const otherLoadedMod = (await import(`./files/${mod}`, { with: { type: "json" } })).default;
|
||||
|
||||
expect(otherLoadedMod.foo).toBe("bar");
|
||||
});
|
|
@ -0,0 +1,5 @@
|
|||
"use strict";
|
||||
|
||||
const supportsTextDecoder = require("../../../helpers/supportsTextDecoder");
|
||||
|
||||
module.exports = () => supportsTextDecoder();
|
|
@ -0,0 +1,4 @@
|
|||
"use strict";
|
||||
|
||||
/** @type {import("../../../../").Configuration} */
|
||||
module.exports = {};
|
|
@ -0,0 +1,12 @@
|
|||
const a = 10;
|
||||
const b = 20;
|
||||
|
||||
class MyClass {
|
||||
getValue() {
|
||||
return "my-class";
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MyClass;
|
||||
module.exports.a = a;
|
||||
module.exports.b = b;
|
|
@ -0,0 +1,11 @@
|
|||
const a = 10;
|
||||
const b = 20;
|
||||
|
||||
class MyClass {
|
||||
getValue() {
|
||||
return "my-class";
|
||||
}
|
||||
}
|
||||
|
||||
export default MyClass;
|
||||
export { a, b };
|
|
@ -914,5 +914,25 @@ module.exports = (env, { testPath }) => [
|
|||
experiments: {
|
||||
outputModule: true
|
||||
}
|
||||
},
|
||||
{
|
||||
entry: "./esm.js",
|
||||
output: {
|
||||
uniqueName: "system-esm",
|
||||
filename: "system-esm.js",
|
||||
library: {
|
||||
type: "system"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
entry: "./commonjs.js",
|
||||
output: {
|
||||
uniqueName: "system-commonjs",
|
||||
filename: "system-commonjs.js",
|
||||
library: {
|
||||
type: "system"
|
||||
}
|
||||
}
|
||||
}
|
||||
];
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
import MyClass, {a, b} from "library-commonjs";
|
||||
|
||||
it("should get exports from systemjs library (" + NAME + ")", function() {
|
||||
expect(new MyClass().getValue()).toBe("my-class")
|
||||
expect(a).toBe(10);
|
||||
expect(b).toBe(20);
|
||||
});
|
|
@ -0,0 +1,17 @@
|
|||
"use strict";
|
||||
|
||||
const System = require("../../../helpers/fakeSystem");
|
||||
|
||||
module.exports = {
|
||||
beforeExecute: () => {
|
||||
System.init();
|
||||
},
|
||||
moduleScope(scope) {
|
||||
scope.System = System;
|
||||
scope.System.setRequire(scope.require);
|
||||
},
|
||||
afterExecute() {
|
||||
delete global.webpackChunk;
|
||||
System.execute("(anonym)");
|
||||
}
|
||||
};
|
|
@ -0,0 +1,27 @@
|
|||
"use strict";
|
||||
|
||||
const path = require("path");
|
||||
const webpack = require("../../../../");
|
||||
|
||||
/** @type {(env: Env, options: TestOptions) => import("../../../../").Configuration[]} */
|
||||
module.exports = (env, { testPath }) => [
|
||||
{
|
||||
entry: "./system-external-commonjs.js",
|
||||
output: {
|
||||
library: {
|
||||
type: "system"
|
||||
}
|
||||
},
|
||||
externals: {
|
||||
"library-commonjs": path.resolve(
|
||||
testPath,
|
||||
"../0-create-library/system-commonjs.js"
|
||||
)
|
||||
},
|
||||
plugins: [
|
||||
new webpack.DefinePlugin({
|
||||
NAME: JSON.stringify("systemjs with external from commonjs format")
|
||||
})
|
||||
]
|
||||
}
|
||||
];
|
|
@ -0,0 +1,7 @@
|
|||
import MyClass, {a, b} from "library-esm";
|
||||
|
||||
it("should get exports from systemjs library (" + NAME + ")", function() {
|
||||
expect(new MyClass().getValue()).toBe("my-class")
|
||||
expect(a).toBe(10);
|
||||
expect(b).toBe(20);
|
||||
});
|
|
@ -0,0 +1,17 @@
|
|||
"use strict";
|
||||
|
||||
const System = require("../../../helpers/fakeSystem");
|
||||
|
||||
module.exports = {
|
||||
beforeExecute: () => {
|
||||
System.init();
|
||||
},
|
||||
moduleScope(scope) {
|
||||
scope.System = System;
|
||||
scope.System.setRequire(scope.require);
|
||||
},
|
||||
afterExecute() {
|
||||
delete global.webpackChunk;
|
||||
System.execute("(anonym)");
|
||||
}
|
||||
};
|
|
@ -0,0 +1,24 @@
|
|||
"use strict";
|
||||
|
||||
const path = require("path");
|
||||
const webpack = require("../../../../");
|
||||
|
||||
/** @type {(env: Env, options: TestOptions) => import("../../../../").Configuration[]} */
|
||||
module.exports = (env, { testPath }) => [
|
||||
{
|
||||
entry: "./system-external-esm.js",
|
||||
output: {
|
||||
library: {
|
||||
type: "system"
|
||||
}
|
||||
},
|
||||
externals: {
|
||||
"library-esm": path.resolve(testPath, "../0-create-library/system-esm.js")
|
||||
},
|
||||
plugins: [
|
||||
new webpack.DefinePlugin({
|
||||
NAME: JSON.stringify("systemjs with external from ES module format")
|
||||
})
|
||||
]
|
||||
}
|
||||
];
|
Loading…
Reference in New Issue