Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to inline scripts and styles #33

Open
wmertens opened this issue Oct 26, 2015 · 3 comments
Open

Allow to inline scripts and styles #33

wmertens opened this issue Oct 26, 2015 · 3 comments

Comments

@wmertens
Copy link

would be nice if the loader converted <script src... and <script>...</script> to requires.

Not sure how to achieve the multiple javascript output from a single module, though.

@resistdesign
Copy link

+1

1 similar comment
@8427003
Copy link

8427003 commented Jun 9, 2016

+1

@hemanth hemanth mentioned this issue Sep 13, 2016
4 tasks
@michael-ciniawsky michael-ciniawsky added this to Feature in Dashboard Apr 9, 2017
@michael-ciniawsky michael-ciniawsky self-assigned this Apr 9, 2017
@michael-ciniawsky michael-ciniawsky changed the title Handle javascript? Transpile <script> to HTML__SCRIPT___${idx} Feb 6, 2018
@michael-ciniawsky michael-ciniawsky changed the title Transpile <script> to HTML__SCRIPT___${idx} [v1.0.0] Transpile <script> to HTML__SCRIPT___${idx} Feb 6, 2018
@michael-ciniawsky michael-ciniawsky added this to the 1.0.0 milestone Feb 6, 2018
@michael-ciniawsky michael-ciniawsky changed the title [v1.0.0] Transpile <script> to HTML__SCRIPT___${idx} [v1.0.0-alpha] Transpile <script> to HTML__SCRIPT___${idx} Feb 6, 2018
@alexander-akait alexander-akait changed the title [v1.0.0-alpha] Transpile <script> to HTML__SCRIPT___${idx} Allow to inline scripts and styles Mar 18, 2020
@alexander-akait alexander-akait modified the milestones: 1.0.0, 2.0.0 Mar 18, 2020
@Luiz-Monad
Copy link

You do it like this, for example, for CSS I'm doing.

function extract_css (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: 'style' }
                ], node => {
                    if (!node.content) {
                        node.tag = false;
                        return node;
                    }
                    const content = node.content;
                    const url = loaderUtils.interpolateName(this, '[hash:24]deadc0de.css', {
                        content,
                    });
                    loaderContext.emitFile(url, content);  //here, just emit the file
                    node.tag = 'link';
                    node.attrs = {
                        rel: 'stylesheet',
                        href: url,
                    };
                    node.content = [];
                    return node;
                });
            })
            .process(content, { sync: true })
            .html;
    } catch (error) {
        loaderContext.emitError(error);
        return content;
    }
}
function filter_extracted_css (tag, attribute, attributes) {
    return !(attributes[attribute] && attributes[attribute].indexOf('deadc0de') !== -1);
}

And then

 ///webpack.config.js
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [{
                    loader: 'file-loader',
                }, {
                    loader: 'extract-loader',
                }, {
                    loader: 'css-loader',
                }]
            },
            {
                test: /\.val\.js$/,
                use: [{
                    loader: 'raw-loader',
                }, {
                    loader: 'extract-loader',
                }, {
                    loader: 'html-loader',
                    options: {
                        minimize: true,
                        attributes: {
                            root: resolve(CONFIG.assetsDir),
                            list: [
                                {
                                    tag: 'img',
                                    attribute: 'src',
                                    type: 'src',
                                    filter: util.filter_extracted_file,
                                },
                                {
                                    tag: 'link',
                                    attribute: 'href',
                                    type: 'src',
                                    filter: util.filter_extracted_css,
                                }
                            ],
                        },
                        preprocessor: util.extract_css,
                    }
                }, {
                    loader: 'val-loader',
                }]
            }
        ]
    },

Caveat, all my resources are public rooted paths like "/something.jpg"

@alexander-akait alexander-akait modified the milestones: 2.0.0, 3.0.0 Feb 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Dashboard
Feature
Development

No branches or pull requests

7 participants