- webpack概述
- 入口起点(Entry Points)
- 输出(Output)
- 模块(Mode)
- 加载器(Loaders)
- 插件(Plugins)
- 配置(Configuration)
- 模块(Modules)
- 模块解析(Module Resolution)
- 依赖图表(Dependency Graph)
- 文件清单(Manifest)
- 构建目标(Targets)
- 模块热替换(Hot Module Replacement)
- 第二部分:配置
- 使用不同语言进行配置(Configuration Languages)
- 多种配置类型
- 入口和上下文(Entry and Context)
- 输出(Output)
- 模块(Module)
- 解析(Resolve)
- 插件(Plugins)
- 开发中 Server(DevServer)
- 开发工具(Devtool)
- 构建目标(Targets)
- Watch 和 WatchOptions
- 外部扩展(Externals)
- 性能(Performance)
- Node
- 统计(Stats)
- 其它选项(Other Options)
- 第三部分:API
- 命令行接口(CLI)
- 包含统计数据的文件(stats data)
- Node.js API
- 模块热替换(Hot Module Replacement)
- 加载器 API
- 模块方法(module methods)
- 模块变量(module variables)
- Plugin API
- compiler 钩子
- compilation 钩子
- resolver
- parser
- 第四部分:指南
- 安装
- 起步
- 管理资源
- 管理输出
- 开发
- 模块热替换
- Tree shaking
- 生产环境构建
- 代码拆分(Code Splitting)
- 懒加载(Lazy Loading)
- 缓存(Caching)
- 创建库 (Library)
- Shimming
- 渐进式网络应用程序
- TypeScript
- 迁移到新版本
- 使用环境变量
- 构建性能
- 内容安全策略
- 开发 - Vagrant
- 管理依赖
- Public Path(公共路径)
- 集成(Integrations)
- 第五部分:加载器
- babel-loader
- yaml-frontmatter-loader
- cache-loader
- coffee-loader
- coffee-redux-loader
- coverjs-loader
- css-loader
- exports-loader
- expose-loader
- extract-loader
- file-loader
- gzip-loader
- html-loader
- i18n-loader
- imports-loader
- istanbul-instrumenter-loader
- jshint-loader
- json-loader
- json5-loader
- less-loader
- bundle-loader
- multi-loader
- node-loader
- null-loader
- polymer-webpack-loader
- postcss-loader
- raw-loader
- react-proxy-loader
- restyle-loader
- sass-loader
- script-loader
- source-map-loader
- style-loader
- svg-inline-loader
- thread-loader
- transform-loader
- url-loader
- val-loader
- worker-loader
- mocha-loader
- 第六部分:插件
- AggressiveSplittingPlugin
- ZopfliWebpackPlugin
- BannerPlugin
- ClosureWebpackPlugin
- CommonsChunkPlugin
- ComponentWebpackPlugin
- CompressionWebpackPlugin
- ContextReplacementPlugin
- CopyWebpackPlugin
- DefinePlugin
- DllPlugin
- EnvironmentPlugin
- EvalSourceMapDevToolPlugin
- ExtractTextWebpackPlugin
- HashedModuleIdsPlugin
- HotModuleReplacementPlugin
- HtmlWebpackPlugin
- BabelMinifyWebpackPlugin
- IgnorePlugin
- LoaderOptionsPlugin
- MinChunkSizePlugin
- ModuleConcatenationPlugin
- NamedModulesPlugin
- NormalModuleReplacementPlugin
- NpmInstallWebpackPlugin
- PrefetchPlugin
- ProfilingPlugin
- ProvidePlugin
- SourceMapDevToolPlugin
- SplitChunksPlugin
- UglifyjsWebpackPlugin
- WatchIgnorePlugin
- I18nWebpackPlugin
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
val-loader
Executes the given module to generate source code on build time
安装
npm i -D val-loader
用法
此 loader 所加载的模块必须符合以下接口
加载的模块必须使用以下函数接口,将 default
export 导出为一个函数。
module.exports = function () {...};
Modules transpiled by Babel are also supported
export default function () {...};
Function Interface
The function will be called with the loader options
and must either return
{Object}
Following the Object Interface
{Promise}
Resolving to an {Object}
following the Object Interface
Object Interface
Name | Type | Default | Description | |
---|---|---|---|---|
code | `{String\ | Buffer}` | undefined | (Required) The code that is passed to the next loader or to webpack |
sourceMap | {Object} | undefined | (Optional) Will be passed to the next loader or to webpack | |
ast | {Array<{Object}>} | undefined | (Optional) An Abstract Syntax Tree that will be passed to the next loader. Useful to speed up the build time if the next loader uses the same AST | |
dependencies | {Array<{String}>} | [] | An array of absolute, native paths to file dependencies that need to be watched for changes | |
contextDependencies | {Array<{String}>} | [] | An array of absolute, native paths to directory dependencies that need to be watched for changes | |
cacheable | {Boolean} | false | Flag whether the code can be re-used in watch mode if none of the dependencies have changed |
Options
val-loader
itself has no options. The options are passed as they are (without cloning them) to the exported function
Examples
If you have a module like this
answer.js
function answer () {
return {
code: 'module.exports = 42;'
}
};
module.exports = answer;
you can use the val-loader to generate source code on build time
webpack.config.js
module.exports = {
...
module: {
rules: [
{
test: require.resolve('path/to/answer.js'),
use: [
{
loader: 'val-loader'
}
]
}
]
}
};
Complete
A complete example of all available features looks like this
answer.js
const ask = require('./ask.js');
const generate = require('./generate.js');
function answer(options) {
return ask(options.question)
.then(generate)
.then(result => ({
ast: result.abstractSyntaxTree,
code: result.code,
// Mark dependencies of answer().
// The function will be re-executed if one of these
// dependencies has changed in watch mode.
dependencies: [
// Array of absolute native paths!
require.resolve('./ask.js'),
require.resolve('./generate.js')
],
// Flag the generated code as cacheable.
// If none of the dependencies have changed,
// the function won't be executed again.
cacheable: true
sourceMap: result.sourceMap,
})
);
}
module.exports = answer;
webpack.config.js
module.exports = {
...
module: {
rules: [
{
test: require.resolve('path/to/answer.js'),
use: [
{
loader: 'val-loader',
options: {
question: 'What is the meaning of life?'
}
}
]
}
]
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论