asset-pipe-js-writer

2019-06-20 admin

asset-pipe-js-writer是什么

什么是asset-pipe-js-writer,Javascript asset feed writer

asset-pipe-js-writer使用教程帮助文档

asset-pipe-js-writer

Greenkeeper badge

This is an internal module intended for use by other modules in the asset-pipe project.

This module reads a CommonJS module javascript file entry point and resolves all the dependencies into an asset feed. It returns a Browserify instance.

The output of this module can be passed into the asset-pipe-js-reader module to build executable javascript bundles for the browser.

Data format

The data format we use is called an asset feed and is also the internal data format used in Browserify.

When Browserify resolves CommonJS modules each dependency will be read and transformed into an object which looks something like this:

{
    "id": "c645cf572a8f5acf8716e4846b408d3b1ca45c58",
    "source":
        "\"use strict\";module.exports.world=function(){return\"world\"};",
    "deps": {},
    "file": "./assets/js/bar.js"
}

When you call .bundle() you get back a stream on which each such object is emitted. One for each dependency.

Installation

$ npm install @asset-pipe/js-writer

Usage

Read a CommonJS module entry point and pipe the feed as JSON to a file on disk:

const JSONStream = require('JSONStream');
const Writer = require('asset-pipe-js-writer');
const fs = require('fs');

const writer = new Writer('./js/browser.main.js');
writer
    .bundle()
    .pipe(JSONStream.stringify())
    .pipe(fs.createWriteStream('./feed/a.json'));

API

This module has the following API:

constructor(files, opts, bundle)

Supported arguments are:

  • files - Same as files in the Browserify constructor (required)
  • opts - Same as opts in the Browserify constructor with dedupe option set to false (this cannot be overriden). (optional). default: {}
  • bundle - boolean - If true this module will output an executable javascript bundle instead of feed of dependencies. (optional). default: false.

Returns a Browserify instance.

Examples

const writer = new Writer('./js/browser.main.js');
const writer = new Writer(['./js/browser.main.js']);
const writer = new Writer('./js/browser.main.js', { debug: true });
const bundle = true;
const writer = new Writer('./js/browser.main.js', {}, bundle);

.bundle()

Returns a stream of data. If the writer was created with bundle: true this will be a javascript text bundle. Otherwise the stream will be in object mode and each dependency will be emitted from the stream as an object.

Returns a readable stream.

Example

writer.bundle().pipe(...)

License

The MIT License (MIT)

Copyright © 2017 - Trygve Lie - post@trygve-lie.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-67738.html

文章标题:asset-pipe-js-writer

回到顶部