Skip to content
New issue

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

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

Already on GitHub? # to your account

How to use "web_accessible_resources" option in manifest.json to inject CSS and JS? #58

Open
JakubKoralewski opened this issue Jan 20, 2019 · 1 comment

Comments

@JakubKoralewski
Copy link

JakubKoralewski commented Jan 20, 2019

I've done this sucessfully in my chrome extension project before changing it as per the boilerplate.

I have a CSS file and a JS file declared in the "web_accessible_resources" option.
This is how I inject it inside of a content script:

// js/content-script.js

//js
const s = document.createElement('script');
s.src = chrome.runtime.getURL('js/script.js');
document.body.appendChild(s);
s.onload = function() {
    this.remove();
};

//css
var style = document.createElement('link');
style.rel = 'stylesheet';
style.type = 'text/css';
style.href = chrome.runtime.getURL('css/style.css');
(document.head || document.documentElement).appendChild(style);

Is this possible with the current state the project is in? I don't know Webpack at all, so maybe all I have to do is transfer the files I need to the /build folder somehow?

EDIT: Previously used File Manager Webpack plugin but this can be easily done with the currently used Copy Webpack Plugin.

new CopyWebpackPlugin([{
        from: 'src/manifest.json',
        transform: function (content, path) {
            // generates the manifest file using the package.json informations
            return Buffer.from(JSON.stringify({
                description: process.env.npm_package_description,
                version: process.env.npm_package_version,
                ...JSON.parse(content.toString())
            }));
        }
    },
    {
        from: path.join(__dirname, 'src', 'injected', 'script.js'),
    }
]),

However it would be great if this was automatically generated from the "web_accessible_resources" option in manifest.json. Additionally, I don't know how you'd apply Babel etc. to these files? Or something like SASS.

Moreover, hot reload on these files doesn't work.

@JakubKoralewski
Copy link
Author

JakubKoralewski commented Jan 21, 2019

Here's how to make SASS (.scss) files work with "web_accesible_resources" (with working hot reload).

  1. Declare the compiled .css in "web_accessible_resources" option:
//manifest.json

"web_accessible_resources": ["globalStyles.css"],
  1. Install and Require MiniCssExtractPlugin.
\\webpack.config.js

var MiniCssExtractPlugin = require('mini-css-extract-plugin');
options.plugins: [
    new MiniCssExtractPlugin({
        // Options similar to the same options in webpackOptions.output
        // both options are optional
        filename: '[name].css',
        chunkFilename: '[id].css'
    }),
]
  1. Make popup.scss and options.scss compile as normal, but make the web_accessible_file be compiled by MiniCssExtractPlugin to a separate file.
\\webpack.config.js

options.module.rules: [
    {
        test: /\.(s*)css$/,
        use: ['style-loader', 'css-loader', 'sass-loader'],
        exclude: /node_modules/
    },
    {
        // where globalStyles.css is the file you have declared in web_accessible_resources!
        test: /globalStyles\.scss$/,
        use: [{
                loader: MiniCssExtractPlugin.loader,
                options: {
                    // you can specify a publicPath here
                    // by default it use publicPath in webpackOptions.output
                    publicPath: 'dist/'
                }
            },
            'css-loader', 'sass-loader'
        ]
    },
]

EDIT: Problem is there is an unnecessary 'globalStyles.bundle.js' file generated.

I had to again turn to the File Manager Webpack Plugin:

new FileManagerWebpackPlugin({
    onEnd: {
        delete: ['build/globalStyles.bundle.js']
    }
}),

JakubKoralewski added a commit to JakubKoralewski/google-calendar-box-select that referenced this issue Jan 22, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant