Skip to content

Latest commit

 

History

History
164 lines (121 loc) · 2.25 KB

README-zh.md

File metadata and controls

164 lines (121 loc) · 2.25 KB

inline-script-loader

这是一个webpack loader,可以在html中嵌入javascript

📘 English

安装

npm

$ npm install inline-script-loader --save-dev

yarn

$ yarn add inline-script-loader -D

如何使用

创建一个 javascript 文件: common/lib/global.js

var global = {
    name:'inline-script-loader'
};

并在src/index.html模板文件中引入

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Document</title>
    <%= require('inline-script-loader!common/lib/global.js') %>
</head>
<body>
</body>
</html>

最后 webpack 输出 dist/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Document</title>
    <script type="text/javascript">
        var global = {
            name:'inline-script-loader'
        };
    </script>
</head>
<body>
</body>
</html>

Options

id

定义内联脚本<script>标签的id

  • example
require('inline-script-loader?id=inline-script!common/lib/global.js')
  • output
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Document</title>
    <script type="text/javascript" id="inline-script">
        var global = {
            name:'inline-script-loader'
        };
    </script>
</head>
<body>
</body>
</html>

type

定义内联脚本<script>标签的type属性

  • example
require('inline-script-loader?type=text/ecmascript!common/lib/global.js')
  • output
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Document</title>
    <script type="text/ecmascript">
        var global = {
            name:'inline-script-loader'
        };
    </script>
</head>
<body>
</body>
</html>

isUglify

是否压缩脚本

  • example
require('inline-script-loader?isUglify=true!common/lib/global.js')
  • output
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Document</title>
    <script type="text/ecmascript">
        var global={name:'inline-script-loader'};
    </script>
</head>
<body>
</body>
</html>

License