使用webpack版本2.2.0.
我有一个带有单个entry配置的单页应用程序:entry: { app: ['./js/main.js'] }.这是一个动态加载视图import('./js/views/1')以进行代码分割的应用程序.
我遇到的问题是,所产生的views/1,views/2等..这得到创建的文件有一吨他们的内部复制模块.文件及其内容如下所示:
./js/main.js
./js/views/1.js./js/modules/a.js./js/modules/b.js./js/views/2.js./js/modules/b.js./js/modules/c.js请注意这两个views/1.js和views/2.js具有共享的完整副本modules/b.js模块.在我的情况下,我有几十个视图和更多的共享模块,所以重复是我的一个巨大的问题.这是`webpack-bundle-analyzer'的样子:
我已经尝试添加CommonChunkPlugin帮助来解决这个问题,但它似乎根本不会影响动态导入.
我正在寻找的是一种让webpack自动将所有共享模块移动到单独文件中的方法.理想情况下,我希望webpack输出以下块:
./js/main.js
./js/views/1.js./js/views/2.js./js/modules/a.js./js/modules/b.js./js/modules/c.js所以每个模块实际上都是一个单独的块.这将是使用HTTP2加载它们的最佳方式.
以下是示例项目的代码:https://github.com/EvNaverniouk/webpack-code-splitting
我怎样才能做到这一点?
我相信这与此问题有关:https://github.com/webpack/webpack/issues/3981
我无法弄清楚如何在两个文件中共享类型定义.考虑:
// /file_a.js
export const randomVariable: MySharedType = {thing: true};
// /file_b.js
export const anotherRandomVariable: MySharedType = {thing: false};
Run Code Online (Sandbox Code Playgroud)
两个文件都使用该MySharedType类型,因此我想将该类型声明移动到某些共享类型定义.
所以我为它创建了一个新目录,并在[libs]我的部分下指向它flowconfig.结果就是我有:
// in /flow/typeExtensions/SharedTypes.js
// @flow
export type MySharedType = {thing: boolean}
Run Code Online (Sandbox Code Playgroud)
但是,流程不想玩得好,仍然给我错误:
2: export const randomVariable: MySharedType = {thing: true}
^^^^^^^^^^^^ identifier `MySharedType`. Could not resolve name
Run Code Online (Sandbox Code Playgroud)
我找不到任何有关如何创建全局共享流类型定义的文档或示例.
这是一个小型回购,展示了这个问题:https://github.com/EvNaverniouk/shared-flow-types
请帮忙.
任何人都可以向我解释为什么"b"返回undefined以及如何解决这个问题?当我通过引用调用原型函数时,为什么"this"范围会丢失?
MyClass = function(test) {
this.test = test;
}
MyClass.prototype.myfunc = function() {
return this.test;
}
var a = new MyClass('asd').myfunc();
var b = new MyClass('asd').myfunc;
// Returns "asd" correctly
console.log(a)
// Returns undefined??
console.log(b())
Run Code Online (Sandbox Code Playgroud)
===编辑/解决方案===
正如plalx所写,在我的情况下,正确的解决方案是使用.bind().所以结果如下:
MyClass = function(test) {
this.test = test;
}
MyClass.prototype.myfunc = function() {
return this.test;
}
var a = new MyClass('asd').myfunc();
var b = new MyClass('asd'),
bfunc = b.myfunc.bind(b)
// Returns "asd" correctly
console.log(a)
// Also returns "asd" correctly!
console.log(bfunc())
Run Code Online (Sandbox Code Playgroud) 我的扩展程序已将CSS文件保存到用户的配置文件目录中.现在,我想将这个CSS文件加载到一个窗口中.
sheetheet/utils似乎有一个loadSheet(window,uri,type)方法(https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/stylesheet_utils)但我不能弄清楚如何将我的CSS文件路径转换为预期的URI对象.
我的代码是这样的:
const ssutils = require("sdk/stylesheet/utils"),
windows = require("sdk/windows");
var path_to_file = "c:\users\myname\appdata\local\temp\tmppr9imy.mozrunner\myextension\mycssfile.css"
for (let wind of windows.browserWindows) {
// What is the magic function I need to use?
ssutils.loadSheet(wind, someMagicFunctionHere(path_to_file), "user");
}
Run Code Online (Sandbox Code Playgroud)