GithubHelp home page GithubHelp logo

dependents / node-precinct Goto Github PK

View Code? Open in Web Editor NEW
206.0 3.0 36.0 1.33 MB

Unleash the detectives

License: MIT License

JavaScript 100.00%
detective javascript sass stylus less ast module dependencies commonjs amd

node-precinct's Introduction

precinct

CI npm version npm downloads

Unleash the detectives

npm install precinct

Uses the appropriate detective to find the dependencies of a file or its AST.

Supports:

  • JavaScript modules: AMD, CommonJS, and ES6
  • TypeScript
  • CSS Preprocessors: Sass, Scss, Stylus, and Less
  • CSS (PostCSS)

Usage

const fs = require('fs');
const precinct = require('precinct');

const content = fs.readFileSync('myFile.js', 'utf8');

// Pass in a file's content or an AST
const deps = precinct(content);

You may pass options (to individual detectives) based on the module type via an optional second object argument detective(content, options), for example:

Example call:

precinct(content, {
  amd: {
    skipLazyLoaded: true
  },
  type: 'amd'
});
  • The supported module type prefixes are: amd, commonjs, css, es6, less, sass, scss, stylus, ts, tsx, vue.

Current options:

  • amd.skipLazyLoaded: tells the AMD detective to omit lazy-loaded dependencies (i.e., inner requires).
  • es6.mixedImports: allows for all dependencies to be fetched from a file that contains both CJS and ES6 imports.
    • Note: This will work for any file format that contains an ES6 import.
  • css.url: tells the CSS detective to include url() references to images, fonts, etc.

Finding non-JavaScript (ex: Sass and Stylus) dependencies:

const fs = require('fs');
const content = fs.readFileSync('styles.scss', 'utf8');

const sassDeps = precinct(content, { type: 'sass' });
const stylusDeps = precinct(content, { type: 'stylus' });

Or, if you just want to pass in a filepath and get the dependencies:

const { paperwork } = require('precinct');

const deps = paperwork('myFile.js');
const deps2 = paperwork('styles.scss');

precinct.paperwork(filename, options)

Supported options:

  • includeCore: (default: true) set to false to exclude core Node.js dependencies from the list of dependencies.
  • fileSystem: (default: undefined) set to an alternative fs implementation that will be used to read the file path.
  • You may also pass detective-specific configuration like you would to precinct(content, options).

CLI

Assumes a global install precinct with npm install -g precinct.

precinct [options] path/to/file

Run precinct --help to see all options.

License

MIT

node-precinct's People

Contributors

bpscott avatar coderaiser avatar davidfirst avatar dazinator avatar dependabot[bot] avatar desnoo avatar flaki avatar fregante avatar havunen avatar jimthedev avatar jkemp-spotify avatar joscha avatar kaelwd avatar mrjoelkemp avatar netlify-team-account-1 avatar pahen avatar realityking avatar skn0tt avatar xhmikosr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-precinct's Issues

bug

background url address can not be parse,eg:
background: url(../../../../img/dataManagement/labelDataSet/add_table_icon_disabled.svg)

Support customizing options for walker

var walker = new Walker();

In my case, I would like to use a walker with custom options

var walker = new Walker( {
  plugins: [
    'flow',
    'classPrivateProperties',
    'nullishCoalescingOperator',
  ]
} )

how about adding walkerOptions in second parameter, e.g.

precinct( content, { walkerOptions: {} } )

and also thanks for your great work!

Keep a changelog (at least for breaking changes)

It's fairly hard to know what one should look out for when upgrading from5.x to 6.x when there are no notes about it anywhere โ€“ no changelog file, no tag notes, no release ๐Ÿ™‚

Some other arguments in favor of a changelog: https://keepachangelog.com/

If you feel it's too much work to keep a changelog for each and every version, then at least log the breaking changes so that it becomes easy to upgrade and tag along with new major versions. That way you will also help keeping as many users as possible on the newest version, which helps with possible contributions from others ๐Ÿ™‚

Type Definitions missing

I'm trying to use precinct from typescript, but it complains that:

Could not find a declaration file for module 'precinct'.

I think it'd be well worth adding some jsdoc style type definitions and distributing them. Since you already have jsdoc style type definitions you should extract them and distribute them. The package.json can add a types field to point typescript at the right location.

Webpack's `require.context` not being employed

Related/original report: pahen/madge#117

It appears Precinct does not work with webpack's dynamic loading with require.context (basically glob) so e.g.

var context = require.context(".", true, /\.js$/)
context.keys().forEach(context)

This ought to load all the modules with /.js$/ from the current directory (".") and subdirectories (that's what the true specifies).

It does not appear to do so, at least via Madge/node-dependency-tree, and I did not see any mention of it in the Precinct code.

Just a heads up.

๐Ÿป

svelte-style html files

Hey,

I'm trying to extract dependencies from a svelte-style .html file.
It seems like precinct is trying to parse it like JSX:

โฏ env DEBUG=precinct npx precinct svelte/HelloWorld.html
  precinct options given:  { es6: {} } +0ms
  precinct could not parse content: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>? (7:0) +8ms
[]

This is the file content:

<div class="container">
	<h1>Hello {name}!</h1>

	<EditingMessage></EditingMessage>
</div>

<style>
	.container {
		text-align: center;
		margin-top: 2rem;

		font-family: sans-serif;
	}
</style>

<script>
	import EditingMessage from './EditingMessage.html';

	export default {
		components: { EditingMessage }
	}
</script>

I think vue single file components look quite similarly.

I may have some time later this week to look into this myself, just wanted to document this somewhere.

Missing `mixedImports` for TypeScript files

Was trying to use mixedImports for my company's TypeScript project but I found it's not supported. It should be as simple as copying the following but for TypeScript:

node-precinct/index.js

Lines 120 to 122 in 6eb6c27

function detectiveEs6Cjs(ast, detectiveOptions) {
return detectiveEs6(ast, detectiveOptions).concat(detectiveCjs(ast, detectiveOptions));
}

detective-typescript uses an unsupported version of typescript

When I run madge, I get this TS warning

WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-eslint-parser.

You may find that it works just fine, or you may not.

SUPPORTED TYPESCRIPT VERSIONS: ~3.0.1

YOUR TYPESCRIPT VERSION: 3.1.3 

I opened an issue on typescript-eslint-parser, and we've found that the reason is that detective-typescript declares a TS version that is slightly higher than what typescript-eslint-parser supports.

We should either downgrade the version here, or work with typescript-eslint-parser to get it to raise its supported version. There's not much different between the two versions, they may just need to verify that this version works.

require.main.require not being parsed

Library works for cases like require("whatever") but unfortunately it doesn't detect require.main.require("whatever") which is also legal and used to reference modules relative to the main JS file. Any chance to support this?

Export regular text instead of array in cli

CLI tools don't expect to receive input in the JavaScript array format, I don't know what to do with this, I'd have to parse it as JS:

$ precinct file.js
[ 'a', 'b' ]

bur rather it should just be a plain list:

$ precinct file.js
a
b

Add option to use parse_dammit

Hi again @mrjoelkemp. Would you consider adding a flag or option so that acorn_loose and more specifically the parse_dammit method can be used instead of acorn.parse? I have a fork that changes node-precinct to use acorn_loose but I'd imagine there are cases where either loose or strict could be useful. It seems like the consumer of the library would want to pick given their specific use case.

In my specific use case I am using your node-list-dependencies package but it crashes if one of the jspm modules has serious problems in a file with a .js extension. This results in only half of the list of dependencies being generated. In the case of generating dependencies it may be worthwhile in some cases to not quit processing dependencies but rather to keep going.

Update detective-typescript's parser

While running madge I'm getting big nasty warning:

=============

WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-estree.

You may find that it works just fine, or you may not.

SUPPORTED TYPESCRIPT VERSIONS: ~3.2.1

YOUR TYPESCRIPT VERSION: 3.3.3333

Please only submit bug reports when using the officially supported version.

=============

Probably could be as easy as swapping typescript-estree to @typescript-eslint/typescript-estree in package.json.

Dynamic Imports in ES6 module without export are not detected

I am using something like this to bootstrap a single-page application:

moduleA.js:

(async () => {
  const {hello} = await import('./moduleB');
  hello();
})();

module:B.js

export const hello = () => {
  console.log('Module B: hello');
};

precinct doesn't detect the dependency to moduleB.js

const {paperwork} = require('precinct');
const deps = paperwork('./moduleA.js'));
console.log(`Deps A: ${deps}`);

If I add an export to moduleA, the dependecy will be detected:

(async () => {
  const {hello} = await import('./moduleB');
  hello();
})();

export default undefined;

Blowing up when trying to parse a Gruntfile

It's technically in CommonJS, but not sure why it's failing:

    at Object.fs.openSync (fs.js:432:18)
    at Object.fs.readFileSync (fs.js:289:15)
    at module.exports (/Users/jokemp/Documents/node-dependents/node_modules/precinct/index.js:12:20)
    at processDependents (/Users/jokemp/Documents/node-dependents/index.js:130:22)
    at /Users/jokemp/Documents/node-dependents/index.js:73:7
    at Array.forEach (native)
    at Object.processFiles (/Users/jokemp/Documents/node-dependents/index.js:71:11)
    at Object.module.exports.for (/Users/jokemp/Documents/node-dependents/index.js:39:16)
    at process.<anonymous> (/Users/jokemp/Documents/node-dependentError: ENAMETOOLONG, name too long

TypeScript file with a cast fails to find any dependencies

example.ts

import React from 'react';

export default class Example extends React.Component<any, any> {
  render() { 
    console.log(this as any);
    return null;
  }
}
$ precinct ./example.ts 
[]

If you remove the as any cast then:

$ precinct ./example.ts 
[ 'react' ]

This means that precinct misses imports in any file using casts in a function call.

Does not get correct dependencies when mixing es6 and cjs import syntax

I'm working on a rather large code base that is slowly transitioning from es5 to es6. There are a lot of files that have mixed imports: i.e. both require() statements and import statements. node-precinct does not correctly extract all the dependencies.

For example, given the file:

require('./foo');
import './bar';

only the foo dependency will be recognized.

If you flip it around, and have a file like:

import './bar';
require('./foo');

only the bar dependency will be recognized.

So in otherwords, precinct use looks at the first syntax it encounters and uses that for the rest of the file, even though the file may have a mix of cjs and es6.

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.