npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

script-custom-module

v2.2.0

Published

A plugin written by Typescript to let developer compile ts,react,vue content in client side with native es module. No bundler, just with one CDN and 4 lines configuration, and you are ready to use your favorite ES javascript to prototype your new idea.

Downloads

50

Readme

Script Custom Module

A plugin written by Typescript to let developer compile ts,react,vue content in client side with native es module. No bundler, just with one CDN and 4 lines configuration, and you are ready to use your favorite ES javascript to prototype your new idea.

Install

Template Setup

  • Install this package as a global or local npm dependency
  • create and serve a testing project with default setting
  • open browser with url http://localhost:1234
# as global
$ npm install -g script-custom-module
$ create-scm
$ serve-scm
# or as local
$ npm install script-custom-module
$ npx create-scm
$ npx serve-scm

Manual Setup

Since this plugin is mainly used in client browser, simply install with CDN.

<script src="https://cdn.jsdelivr.net/npm/script-custom-module/dist/custom-script.global.js"></script>

Quick Usage

Setup Custom Scripts

use the setup method to initialize from specific entrypoint file, then all dependency will got auto registered to the importmap, so that we can easily import our dependency by just calling native import method

<!-- index.html -->
<head>
  <script>
  // initialize custom modules and create importmap
  CustomScript.setup({
    entry: 'src/index.jsx',
    mode: 'all',
  });
  </script>
</head>

Notice: setup will parse dependency from specific entrypoint, and loop into its dependencies.

// entrypoint: index.js
import { createApp } from 'vue';
import App from 'src/App.vue';
import 'src/index.css';
import 'src/no.js';

createApp(App).mount('#app');

Notice: since the path starts with /, ./ will be considered as native esm, please import dependency always from root with relative path liked src/xxxx.js or xxxx.css with file extension so that CustomScript can parse your file with correct process.

Run up a local server to serve your static content

you can install serve or any other http service to run up a dev server for your folder.

Commands

create-scm [-t|--template=TEMPLATE] [PROJECT_DIR]

create a new scm project with template

  • TEMPLATE: react, vue, default is react
  • PROJECT_DIR: project directory, default is scm-project

serve-scm {[-p|--port=PORT] [-h|--host=HOST]} [PROJECT_DIR]

serve a folder with native Nodejs server by host and port

  • PORT: port number, default is 1234
  • HOST: hostname, default is localhost
  • PROJECT_DIR: project directory, default is scm-project

APIs

In window.CustomScript, we can use following methods & states.

setup(options)

the core method to initialize this plugin.

CustomScript.setup({
  // required, or provide <script type="root-module">
  entry: 'src/index.jsx',
  // default: ts
  // available: ts, react(react18), react17, vue(vue3), all(ts + react + vue)
  // each mode will auto inject necessary imports & scopes in import map
  mode: 'ts',
  // optional, the path will be added before all your dependency import except vue compiler path, default: ''
  publicPath: '',
  // optional, if you want to serve your own vue compiler path
  vueCompilerPath: 'https://cdn.jsdelivr.net/npm/script-custom-module/dist/vue-parser.mjs',
  // optional, put anything you want to manually use in your project
  // this importmap will got merged with those dependency parsed by your entry file
  importmap: {
    imports: {
      // optional, if you need to use some library as native es module
      // eg. styled-components
      'styled-components':
        'https://unpkg.com/@esm-bundle/styled-components/esm/styled-components.browser.min.js',
    },
    scopes: {}
  },
  // optional, if you want to give the dependency module as object without fetching
  // reference to below part "Sourcemap Mode"
  sourceMap: {},
});

Compile feature

Typescript/React Compile

By default, all the dependency end with extension js, ts, jsx, tsx will got compiled by Babel

Scss/Css Compile

dependency end with extension css, scss will got compiled by Sass, and auto injected to <head>

Vue Compile

dependency end with extension vue will got compiled by @vue/compiler-sfc plugin by esm, and auto generated js, css into <head> to load content.

Sourcemap Mode

with sourcemap mode, all dependency are injected by a user provided map object, and CustomScript will only process those rawCode you provided in map to generate compiled content to serve on browser.

<!-- index.html -->
<script>
window.CustomScript.setup({
  // entry should also exists inside your sourceMap
  entry: 'src/index.js',
  sourceMap: {
    'src/index.js': 'import sum from "src/sum.js";\nconsole.log(sum(1, 2));',
    'src/sum.js': 'export default (a, b) => a + b;',
  },
});
</script>

Not support html file in sourcemap

Root Module Entry

if the entry in options is not provided, CustomScript will search for <script type="root-module"> as following

<head>
  <!-- root-module -->
  <script type="root-module" src="src/index.js"></script>
</head>
<body>
  <div id="app"></div>

  <!-- or -->
  <script type="root-module">
    import { createApp } from 'vue';
    import App from 'src/App.vue';

    createApp(App).mount('#app');
  </script>
</body>

Use Demos

For more use demos, please refer here

Reference

This plugin is mainly inspired by inline-module, just modified about the loader system.