Coder Social home page Coder Social logo

morganney / magic-comments-loader Goto Github PK

View Code? Open in Web Editor NEW
11.0 11.0 0.0 546 KB

Add webpack magic comments to your dynamic imports at build time.

License: MIT License

JavaScript 100.00%
dynamic-import magic-comments webpack webpack-loader

magic-comments-loader's Introduction

magic-comments-loader's People

Contributors

dependabot[bot] avatar morganney avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

magic-comments-loader's Issues

multiple import() statements on single line fails

Hi,
thanks for great tool!

I have a small issue with multiple import:s in the same file. The comments are only added to the first import when code is like:

reg( [
                { module: import('@pkg/button'), elem: 'Button' },
                { module: import('@pkg/collapse'), elem: 'Collapse' },
                { module: import('@pkg/icon'), elem: 'Icon' },
] );

The webpackChunkName func is only called once!

A work around is to move imports outside the object init :

const b = import('@pkg/button');
const c = import('@pkg/collapse');
const i = import('@pkg/icon');
reg( [
                { module: b, elem: 'Button' },
                { module: c, elem: 'Collapse' },
                { module: i, elem: 'Icon' },
] );

Now comments are added to all!

Add content hash to chunk name

Thanks for making this excellent tool.

I have a feature request. Would you consider an option that adds a content hash to the chunk name inserted into the webpackChunkName magic comment?

I want to do this because I have this in my webpack config:

output: {
      filename: 'js/[name]_bundle.js',
}
...
loader: "magic-comments-loader",
    options: {
       verbose: true,
       webpackChunkName: {
          config: {
             basename: true,
             //contentHash: true,
          }
       }
    }

And this in my web application
import("../../tt/about_box.html").then(module => { ...

magic-comments-loader automatically inserts
/* webpackChunkName: "about_box" */
for me, but I would like to introduce cache busting.

The easiest way I could think of was to add a content-hash to the chunk name. That is, I would like the loader to read the about_box.html file, generate a truncated MD5 has and add it to the end of the chunk name.

I know I could just add "[contenthash]" to output.filename, but this would change the naming scheme for all assets. I only want to change it for dynamic imports.

I looked at the webpackChunkName options in the loader config but didn't find any option that would allow me to provide a function to generate a custom chunk name during build time.

In the end I modified webpackChunkName.js myself to make a proof of concept.

if (config.contentHash) {
    const fileToImport=dirname(filepath)+"/"+importPath;
    chunkName=chunkName+"_"+sync(fileToImport).substr(0, 8);;
  };

Here sync comes from the md5-file module.

Now it works, and I see this log output at build time:

LOG from ../rohan/magic-comments-loader/src/index.js MCL ../rohan/magic-comments-loader/src/index.js??ruleSet[1].rules[1].use[0]!./node_modules/babel-loader/lib/index.js??ruleSet[1].rules[1].use[1]!./javascripts/app/config_pagesc.js
<i> [MCL] javascripts/app/config_pagesc.js : import(/* webpackChunkName: "about_box_5a4d7295" */ "../../tt/about_box.html")

If you like the idea I can send a PR. For prior art see babel-plugin-add-contenthash-to-imports.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.