Comments (5)
@purell It's currently not possible with webpack to import a JavaScript file into an HTML file (via <script src
).
This is because webpack has been designed as JavaScript bundler. Other file types, like CSS and HTML, need to be translated to JavaScript modules first. Unfortunately, this kind of import (JS into HTML) is not easy to achieve with the current architecture. However, we're looking for ways to treat CSS and HTML as first-class citizens, but for now I just can say that this is impossible 😞.
from extract-loader.
Its not impossible, just hard, you can create a loader that uses emitFile to emit the JS and remove it from the HTML. Its doable using the posthtml, its similar to what I did in the other post about removing the CSS, its the same problem. Perhaps html-loader could do this for us, someone could ping them.
Here is what I am doing.
function extract_js (content, loaderContext) {
try {
const loaderUtils = require('loader-utils');
const posthtml = require('posthtml');
return posthtml()
.use(tree => {
if (tree.length === 0) return tree;
return tree.match([
{ tag: 'script' }
], node => {
const content = node.content;
const url = loaderUtils.interpolateName(this, '[hash:26]c0ffee.js', {
content,
});
loaderContext.emitFile(url, content)
node.tag = false;
return node;
});
})
.process(content, { sync: true })
.html;
} catch (error) {
loaderContext.emitError(error);
return content;
}
}
function filter_extracted (tag, attribute, attributes) {
return !(attributes[attribute] && attributes[attribute].indexOf('c0ffee') !== -1);
}
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [{
loader: 'babel-loader',
options: CONFIG.babel,
}]
},
{
test: /\.template$/,
use: [{
loader: 'raw-loader',
}, {
loader: 'extract-loader',
}, {
loader: 'html-loader',
options: {
minimize: true,
attributes: {
root: resolve(CONFIG.assetsDir),
list: [
{
tag: 'script',
attribute: 'type',
type: 'src',
filter: filter_extracted_js,
}
],
},
preprocessor: extract_js,
}
}]
}
]
},
}
from extract-loader.
I forked, made the change, and all tests are still passing. Pull request to follow.
from extract-loader.
I'm sorry, the extract-loader has really been designed to cope with the css-loader and the html-loader. If you're doing more sophisticated stuff, you're better off with the extract-text-webpack-plugin.
Or did the proposed change (from your PR) help your use-case?
from extract-loader.
@heptahedron : I'm stuck since many days trying to extract JS (script) files from HTML. Here is my attemp to get help on Stackoverflow.
@jhnns : Do you have any suggestion on how to use extract-text-pluging to achieve what we need ? I saw some PR related to this issue like #7, #20. Are they fixing the problem ? I applied those patch on my extractLoader.js
from the node_modules folder but I'm now getting the error ReferenceError: require is not defined
while parsing the javascript file.
Here is the only line that my javascript contains :
var $ = require("jquery");
from extract-loader.
Related Issues (20)
- SyntaxError: unknown: Unexpected token (import) HOT 12
- Is this loader maintained? HOT 4
- webpack5 use extract-loader to extract html file,but extract-loader always report error. HOT 3
- No dependencies to successor loader
- Reporting a vulnerability
- Archive this package
- loaderContext.loadModule API throw error when using with css-loader
- Missing Babel dependencies in 5.0
- Shouldn't sourceMaps be ignored if the devtool option is set to false?
- Import error with ES6 js script embedded in html body HOT 1
- working incorrect with [email protected] HOT 1
- Doesn't work with aliases properly HOT 3
- undefined image src if image used twice in html HOT 1
- v5.2.0 breaks parts of code loaded with ref-loader HOT 1
- Update dependencies to babel7.
- style="background-image: url('file');" not parsed
- could there be boolean option to ignore .js files?
- Webpack 5.1.3 and PublicPath auto HOT 3
- Generating "[object Object]" for placeholders... HOT 4
- Extract loader doesn't work with css-loader 5.x HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from extract-loader.