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

Feature: npm init #9

Open
holtwick opened this issue Sep 21, 2020 · 5 comments
Open

Feature: npm init #9

holtwick opened this issue Sep 21, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@holtwick
Copy link

holtwick commented Sep 21, 2020

I created a little something to get started with a new estrella project: https://github.com/holtwick/create-estrella

You can now enter:

npm init estrella myproj1

To get started.

If you like the idea, I can also pass the project to you.

Other interesting templates might be:

It might also be nice to have it right inside estrella command line tool itself, like:

estrella -init myproj1
@rsms
Copy link
Owner

rsms commented Sep 30, 2020

Nice! Thanks for making this. I'm happy to let you maintain it.

Some feedback

Generating two build files seems unnecessary (i.e. build.config.js and build.js). In my opinion there's a "bad" culture in the JS world of creating & requiring a large number of configuration files for every single little tool (e.g. cssnext, prettier, npm, webpack, etc.) With Estrella I want to avoid enforcing that upon people and thus instead of using a configuration-file approach, Estrella instead lets you put together your build script with whatever configuration and other tools you might need. I think it would be better to generate a really minimal single build.js file.


I see you've added dependencies listed in package.json to esbuild's "external" list. This is going to be a problem for most people!

i.e.

  external: [
    ...Object.keys(pkg.dependencies ?? {}),
    ...Object.keys(pkg.devDependencies ?? {}),
    ...Object.keys(pkg.peerDependencies ?? {}),
  ],

A key part of the esbuild project is to be a really good bundler and this may prevent a lot of people from bundling libraries, which is pretty common when you "bundle" javascript. I propose that you remove this and instead add a sentence of documentation to the postscript of the "init" action (shown when npm init estrella is run.) For example: "If you don't want to bundle your npm packages, list external modules in 'external: ["modulename", ...]' in build.js."


Mentioned above already but worth clarifying: You're making a lot of assumptions about what people want to make. For example jsxFactory: "h" is not common, inline sourcemaps are bad for people who make web sites and target:'es2015' is likely not what you expect out of the box (plus, esbuild does not currently have transformations for a lot of things to fit into es2015.)

Again I recommend that you generate a super simple and minimal build.js, like this:

#!/usr/bin/env node
const { build } = require("estrella")
const pkg = require("./package.json")
build({
  entry: "src/index.ts",
  outfile: pkg.main,
})

:-)

@holtwick
Copy link
Author

Thank you very much for the feedback. You are right, the lightness of the setup is the biggest plus. I will adjust that in create-estrella.

Obviously I took the settings from an existing project ;) I separated the config there, because I reused it for a Jest transformer. My build.jest.js file looked like this:

const esbuild = require('esbuild')
const common = require('./build.config.js')

module.exports = {

  // https://jestjs.io/docs/en/troubleshooting#caching-issues
  getCacheKey() {
    return Math.random().toString()
  },

  process(content, filename) {
    let result = esbuild.buildSync({
      ...common,
      sourcemap: 'inline',
      write: false,
      entryPoints: [filename],
    })

    return new TextDecoder('utf-8').decode(result.outputFiles[0].contents)
  },
}

And the package.json contains:

"jest": {
    "transform": {
      "^.+\\.jsx?$": "./build.jest.js"
    },
    "testEnvironment": "node",
    "testPathIgnorePatterns": [
      "node_modules/",
      "dist/"
    ]
},

Do you have an idea how to nicely combine this with Estrella?


I might add the special cases as further options for the init script:

  • Basic (default, minimal config, 'Hello World')
  • Jest
  • Node.js server
  • NPM module

I'll post an update here once I implemented it.

Thanks again for the great work on Estrella and for clarifying the philosophy behind it.

@rsms
Copy link
Owner

rsms commented Sep 30, 2020

Regarding the "jest" file:

I'm not familiar with Jest enough to know what to do here.
What I'd try would be to consolidate the estrella build and jest transformer into one file; something like this:

build.js

const { build, file } = require('estrella')

if (module.id != ".") {
  // imported by jest as a transformer
  return module.exports = {
    getCacheKey() { /* ... */ },

    async process(content, filename) {
      await build({
        entry: filename,
        outfile: tempfile
      })
      return file.read(tempfile, "utf8")
    }
  }
}

// running directly -- build product
build({
  entry: "main.ts",
  outfile: "out/app.js"
})

Alternatively just keep the jest transformer separate, maybe in a "misc" directory.

@rsms rsms changed the title npm init Feature: npm init Sep 30, 2020
holtwick added a commit to holtwick/create-estrella that referenced this issue Oct 1, 2020
@holtwick
Copy link
Author

holtwick commented Oct 1, 2020

I fixed create-estrella to use the simple sample you suggested.

More templates will follow and are tracked here https://github.com/holtwick/create-estrella/issues

The module.id trick is neat. Sadly the jest transformer does not support async:

Holtwick-Code-2020-kbK3ekIn@2x

In order to get it work, a synchronous build would be required. It would also be nice to be able to build to a string directly and the handle the result oneself. But I understand if this is out of scope of estrella, even though it would be nice to have ;)

@holtwick
Copy link
Author

holtwick commented Oct 1, 2020

Here you go, more templates for create-estrella. @rsms, I would love to hear your opinion on the module template: https://github.com/holtwick/create-estrella/blob/master/template-js-module/build.js

@rsms rsms added the enhancement New feature or request label Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants