This is a webpack loader
that can embed javascript
in html
.
📘 中文文档
$ npm install inline-script-loader --save-dev
$ yarn add inline-script-loader -D
Create a javascript
file: common/lib/global.js
var global = {
name:'inline-script-loader'
};
And introduced in the src/index.html
template file
<!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>
Finally webpack
output 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>
Define the id of the inline script <script>
tag
- 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>
Define the type attribute of the inline script <script>
tag
- 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>
Whether to compress the script
- 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>