import-html-entry
import html and get the exports of entry
Last updated 5 years ago by kuitos .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install import-html-entry 
SYNC missed versions from official npm registry.

import-html-entry

Treats the index html as manifest and loads the assets(css,js), get the exports from entry script.

<!-- subApp/index.html -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
</head>
<body>

<!-- mark the entry script with entry attribute -->
<script src="https://unpkg.com/mobx@5.0.3/lib/mobx.umd.js" entry></script>
<script src="https://unpkg.com/react@16.4.2/umd/react.production.min.js"></script>
</body>
</html>
import importHTML from 'import-html-entry';

importHTML('./subApp/index.html')
    .then(res => {
        console.log(res.template);

        res.execScripts().then(exports => {
            const mobx = exports;
            const { observable } = mobx;
            observable({
                name: 'kuitos'
            })
        })
});

API

importHTML(url, opts?)

Parameters
  • url - string - required, URL of the index HTML.
  • opts - ImportEntryOpts - optional, Load configuration.
Return
  • Promise<IImportResult>
Type
  • ImportEntryOpts

    • fetch - typeof window.fetch | { fn?: typeof window.fetch, autoDecodeResponse?: boolean } - optional, Custom fetch method.
      • autoDecodeResponse - optional, Auto decode when the charset is not utf-8(like gbk or gb2312), default is false.
    • getPublicPath - (entry: Entry) => string - optional, Customize the assets public path.
    • getTemplate - (tpl: string) => string - optional, Customize the HTML template before proceeding.
  • IImportResult

    • template - string - Processed HTML template.
    • assetPublicPath - string - Public path for assets.
    • getExternalScripts - Promise<string[]> - Scripts URL from template.
    • getExternalStyleSheets - Promise<string[]> - StyleSheets URL from template.
    • execScripts - (sandbox?: object, strictGlobal?: boolean, execScriptsHooks?: ExecScriptsHooks): Promise<unknown> - the return value is the last property on window or proxy window which set by the entry script.
      • sandbox - optional, Window or proxy window.
      • strictGlobal - optional, Strictly enforce the sandbox.
  • ExecScriptsHooks

    • beforeExec - (code: string, script: string) => string | void - optional, call it before executing each script, if return value is a string, replace code with return value.
      • code - The inline script as a string.
      • script - The URL of external script.
    • afterExec - (code: string, script: string) => void - optional, call it after executing each script, and the call will stop if the execution error occurs.
      • code - The inline script as a string.
      • script - The URL of external script.
Usage

Treats the index html as manifest and loads the assets(css,js), get the exports from entry script.

Sample
import importHTML from 'import-html-entry';

const opts = {
    fetch: {
        fn: (...args) => window.fetch(...args),
        autoDecodeResponse: true,
    },
    getPublicPath: (entry) => `${entry}/newPublicPath/`,
    getTemplate: (tpl) => tpl.replace(/SOME_RULES/, '\n//Replaced\n'),
}

importHTML('./subApp/index.html')
    .then(res => {
        res.execScripts().then(exports => {
            console.log(exports);
        })
});

importEntry(entry, opts?)

Parameters
  • entry - Entry - required, URL of the index HTML or assets.
  • opts - ImportEntryOpts - optional, Load configuration.
Return
  • Promise<IImportResult>
Type
  • Entry - string | { styles?: string[], scripts?: string[], html?: string } - When type as string, importEntry will run as importHTML, otherwise will load scripts and add styleSheets in your HTML string which you're provided or not.
    • styles - The URL for styles.
    • scripts - The URL for scripts.
    • html - The HTML template as a string, default is empty string.

Other type as same as importHTML.

Usage

Loads the assets(css,js) and embed into HTML template, get the exports from entry script.

Sample
import { importEntry } from 'import-html-entry';

const opts = {
    fetch: {
        fn: (...args) => window.fetch(...args),
        autoDecodeResponse: true,
    },
    getPublicPath: (entry) => `${entry}/newPublicPath/`,
    getTemplate: (tpl) => tpl.replace(/SOME_RULES/, '\n//Replaced\n'),
}

const entryOpts = {
    styles: [
        'https://unpkg.com/antd@3.13.6/dist/antd.min.css',
    ],
    scripts: [
        'https://unpkg.com/react@16.4.2/umd/react.production.min.js'
    ],
    html: `<!DOCTYPE html>
            <head>
                <meta charset="UTF-8">
            </head>
            <body>
                <div id="root"></div>
            </body>
        </html>`
}

importEntry('./subApp/index.html')
    .then(res => {
        res.execScripts().then(exports => {
            console.log(exports);
        })
});

execScripts(entry, scripts, proxy, opts?)

Parameters
  • entry - string - required, The URL of entry assets (will use last of scripts when entry is null).
  • scripts - string[] - required, The URL for scripts (should always include entry when entry is valid URL).
  • proxy - Window - required, Window or proxy window.
  • opts - ExecScriptsOpts - optional, Exec configuration.
Return
  • Promise<T> - The returned value is the last property on window or proxy window which set by the entry script.
Type
  • ExecScriptsOpts
    • fetch - typeof window.fetch - optional, Custom fetch method.
    • strictGlobal - boolean - optional, Strictly enforce the sandbox.
    • success - (exports: unknown) => void - optional, Use callback to get the result when successfully.
      • exports - Same as the return value.
    • error - CallableFunction - optional, Use callback to get the result when error.
    • ExecScriptsHooks.
Usage

Loads the scripts by URL on the custom sandbox, get the exports from entry script.

Sample
import { execScripts } from 'import-html-entry';

const scripts = [
    'https://demo.com/entry.js',
    'https://unpkg.com/react@16.4.2/umd/react.production.min.js'
]

execScripts(
    'https://demo.com/entry.js',
    scripts,
    windows, // or custom sandbox
    {
        fetch: (...args) => window.fetch(...args),,
        strictGlobal: true,
        success: () => {},
        error: () => {},
    }
);

FAQ

Why is the resolved value of execScripts different from my expectation (e.g. {} / null / other values)?

The execScripts will return the last property on window or proxy window which is set by the entry script. If the html entry has more than one script that is deferred, the resolved value of execScripts will be the value set by the last script, which may not be as expected.

To solve this problem, make sure the entry script is the last in the html entry. For example, if you are using html-webpack-plugin, you can set scriptLoading: 'blocking' in the plugin options.

Current Tags

  • 1.17.0                                ...           latest (5 months ago)
  • 1.6.0-2                                ...           next (5 years ago)

93 Versions

  • 1.17.0                                ...           5 months ago
  • 1.16.0                                ...           5 months ago
  • 1.15.2                                ...           a year ago
  • 1.15.1                                ...           a year ago
  • 1.15.0                                ...           a year ago
  • 1.14.6                                ...           2 years ago
  • 1.14.5                                ...           2 years ago
  • 1.14.4                                ...           2 years ago
  • 1.14.3                                ...           2 years ago
  • 1.14.2                                ...           2 years ago
  • 1.14.1                                ...           2 years ago
  • 1.14.0                                ...           2 years ago
  • 1.13.0                                ...           2 years ago
  • 1.12.0                                ...           3 years ago
  • 1.11.1                                ...           4 years ago
  • 1.11.0                                ...           4 years ago
  • 1.10.0                                ...           4 years ago
  • 1.9.3                                ...           4 years ago
  • 1.9.2                                ...           4 years ago
  • 1.9.1                                ...           4 years ago
  • 1.9.0                                ...           4 years ago
  • 1.8.1                                ...           4 years ago
  • 1.8.0                                ...           4 years ago
  • 1.7.5                                ...           4 years ago
  • 1.7.4                                ...           4 years ago
  • 1.7.3                                ...           5 years ago
  • 1.7.2                                ...           5 years ago
  • 1.7.1                                ...           5 years ago
  • 1.7.0                                ...           5 years ago
  • 1.6.0                                ...           5 years ago
  • 1.6.0-2                                ...           5 years ago
  • 1.6.0-1                                ...           5 years ago
  • 1.6.0-0                                ...           5 years ago
  • 1.5.1                                ...           5 years ago
  • 1.5.0                                ...           5 years ago
  • 1.4.1                                ...           5 years ago
  • 1.4.0                                ...           5 years ago
  • 1.3.10                                ...           5 years ago
  • 1.3.9                                ...           5 years ago
  • 1.3.8                                ...           5 years ago
  • 1.3.7                                ...           5 years ago
  • 1.3.6                                ...           5 years ago
  • 1.3.5                                ...           5 years ago
  • 1.3.4                                ...           5 years ago
  • 1.3.3                                ...           5 years ago
  • 1.3.2                                ...           5 years ago
  • 1.3.1                                ...           5 years ago
  • 1.3.0                                ...           5 years ago
  • 1.2.6                                ...           5 years ago
  • 1.2.5                                ...           5 years ago
  • 1.2.4                                ...           5 years ago
  • 1.2.3                                ...           5 years ago
  • 1.2.2                                ...           5 years ago
  • 1.2.1                                ...           5 years ago
  • 1.2.0                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.4.12                                ...           5 years ago
  • 0.4.11                                ...           6 years ago
  • 0.4.10                                ...           6 years ago
  • 0.4.9                                ...           6 years ago
  • 0.4.8                                ...           6 years ago
  • 0.4.7                                ...           6 years ago
  • 0.4.6                                ...           6 years ago
  • 0.4.5                                ...           6 years ago
  • 0.4.4                                ...           6 years ago
  • 0.4.3                                ...           6 years ago
  • 0.4.2                                ...           6 years ago
  • 0.4.1                                ...           6 years ago
  • 0.4.0                                ...           6 years ago
  • 0.3.1                                ...           6 years ago
  • 0.2.5                                ...           6 years ago
  • 0.2.4                                ...           6 years ago
  • 0.2.3                                ...           6 years ago
  • 0.2.2                                ...           6 years ago
  • 0.2.1                                ...           6 years ago
  • 0.2.0                                ...           6 years ago
  • 0.1.13                                ...           6 years ago
  • 0.1.12                                ...           6 years ago
  • 0.1.11                                ...           6 years ago
  • 0.1.10                                ...           6 years ago
  • 0.1.9                                ...           6 years ago
  • 0.1.8                                ...           6 years ago
  • 0.1.6                                ...           6 years ago
  • 0.1.5                                ...           6 years ago
  • 0.1.4                                ...           6 years ago
  • 0.1.3                                ...           6 years ago
  • 0.1.2                                ...           6 years ago
  • 0.1.1                                ...           6 years ago
  • 0.1.0                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (11)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE