Skip to content

Commit

Permalink
feat($newApi): add low-level flushFiles function + docs
Browse files Browse the repository at this point in the history
  • Loading branch information
faceyspacey committed Apr 29, 2017
1 parent 19e378b commit dc98ca4
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 24 deletions.
42 changes: 40 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,59 @@ module.exports = {
'import/ignore': ['node_modules', 'flow-typed', '\\.(css|styl|svg|json)$'],
rules: {
'no-shadow': 0,
'comma-dangle': 0,
'no-use-before-define': 0,
'no-param-reassign': 0,
'react/prop-types': 0,
'react/no-render-return-value': 0,
'no-confusing-arrow': 0,
'no-underscore-dangle': 0,
'no-plusplus': 0,
camelcase: 1,
'prefer-template': 1,
'react/no-array-index-key': 1,
'global-require': 1,
'react/jsx-indent': 1,
'dot-notation': 1,
'import/no-named-default': 1,
'no-unused-vars': 1,
'import/no-unresolved': 1,
'no-nested-ternary': 1,
semi: [2, 'never'],
'flowtype/semi': [2, 'never'],
'jsx-quotes': [2, 'prefer-single'],
'react/jsx-filename-extension': [2, { extensions: ['.jsx', '.js'] }],
'spaced-comment': [2, 'always', { markers: ['?'] }],
'arrow-parens': [2, 'as-needed', { requireForBlockBody: false }]
'arrow-parens': [2, 'as-needed', { requireForBlockBody: false }],
'brace-style': [2, 'stroustrup'],
'import/no-extraneous-dependencies': [
'error',
{
devDependencies: true,
optionalDependencies: true,
peerDependencies: true
}
],
'comma-dangle': [
2,
{
arrays: 'never',
objects: 'never',
imports: 'never',
exports: 'never',
functions: 'never'
}
],
'max-len': [
'error',
{
code: 80,
tabWidth: 2,
ignoreUrls: true,
ignoreComments: true,
ignoreRegExpLiterals: true,
ignoreStrings: true,
ignoreTemplateLiterals: true
}
]
}
}
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ language: node_js
cache: yarn
notifications:
email: false
webhooks:
urls:
- https://webhooks.gitter.im/e/146fee0a5fa417b93059
on_success: always # options: [always|never|change] default: always
on_failure: always # options: [always|never|change] default: always
on_start: never # options: [always|never|change] default: always
node_js:
- stable
script:
Expand All @@ -10,4 +16,5 @@ after_success:
- yarn run semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
- /^v\d+\.\d+\.\d+$/

18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -626,5 +626,23 @@ you'll send over the wire in initial requests. You be the judge of that. We look
opinion.
## Low-level API: `flushFiles`
For advanced users that want all files flushed (`.js`, `.css` or whatever else might be in there) and without named entry chunks (such as `bootstrap`, `vendor`, and `main`), here you go:
```js
import ReactLoadable from 'react-loadable'
import { flushFiles } from 'webpack-flush-chunks'

const moduleIds = ReactLoadable.flushRequires()
const files = flushFiles(moduleIds, stats, /* rootDir if babel */)

const scripts = files.filter(file => /\.js/.test(file))
const styles = files.filter(file => /\.css/.test(file))
```
> i.e. this will get you all files corresponding to flushed "dynamic" chunks, no more
If what you want is full-on compilation `chunk` objects (and any information it contains, which for 99% of most projects is unnecessary), create an issue and we'll add it. But until there is an actual need, we would like to keep the API simple.
## Contributing
We use [commitizen](https://github.com/commitizen/cz-cli), so run `npm run commit` to make commits. A command-line form will appear, requiring you answer a few questions to automatically produce a nicely formatted commit. Releases, semantic version numbers, tags and changelogs will automatically be generated based on these commits thanks to [semantic-release](https://github.com/semantic-release/semantic-release). Be good.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"commit": "git-cz",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"prepublish": "yarn build",
"format": "prettier --single-quote --parser=flow --semi=false --write '{src,__tests__}/**/*.js'",
"format": "prettier --single-quote --parser=flow --semi=false --write '{src,__tests__}/**/*.js' && npm run lint",
"test": "jest",
"lint": "eslint --fix src __tests__ __fixtures__",
"build": "babel src -d dist"
Expand Down
25 changes: 18 additions & 7 deletions src/flushChunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const defaults = {
after: ['main']
}

/** FLUSH CHUNKS */
/** PUBLIC API */

export default (pathsOrIds: Files, stats: Stats, opts?: Options): Api =>
flushChunks(pathsOrIds, stats, IS_WEBPACK, opts)
Expand All @@ -55,26 +55,35 @@ const flushChunks = (
opts?: Options = {}
) => {
const beforeEntries = opts.before || defaults.before

const files = !isWebpack
? flushBabel(pathsOrIds, stats, opts.rootDir)
: flushWebpack(pathsOrIds, stats)

const files = flush(pathsOrIds, stats, opts.rootDir, isWebpack)
const afterEntries = opts.after || defaults.after

return createApiWithCss(
[
...resolveEntryFiles(beforeEntries, stats.assetsByChunkName),
...files.filter(isUnique),
...files,
...resolveEntryFiles(afterEntries, stats.assetsByChunkName)
],
stats.publicPath,
opts.outputPath
)
}

const flushFiles = (pathsOrIds: Files, stats: Stats, rootDir: ?string) =>
flush(pathsOrIds, stats, rootDir, IS_WEBPACK)

/** BABEL VS. WEBPACK FLUSHING */

const flush = (
pathsOrIds: Files,
stats: Stats,
rootDir: ?string,
isWebpack: boolean
) =>
(!isWebpack
? flushBabel(pathsOrIds, stats, rootDir).filter(isUnique)
: flushWebpack(pathsOrIds, stats).filter(isUnique))

const flushBabel = (paths: Files, stats: Stats, rootDir: ?string): Files => {
if (!rootDir) {
throw new Error(
Expand Down Expand Up @@ -162,6 +171,8 @@ const resolveEntryFiles = (

export {
flushChunks,
flushFiles,
flush,
flushBabel,
flushWebpack,
createFilesByPath,
Expand Down
26 changes: 13 additions & 13 deletions wallaby.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
module.exports = wallaby => {
process.env.NODE_ENV = "test";
process.env.NODE_ENV = 'test'

return {
files: [
{ pattern: "src/**/*.js", load: false },
{ pattern: "package.json", load: false },
{ pattern: "__tests__/**/*.snap", load: false },
{ pattern: "__fixtures__/**/*.js", load: false }
{ pattern: 'src/**/*.js', load: false },
{ pattern: 'package.json', load: false },
{ pattern: '__tests__/**/*.snap', load: false },
{ pattern: '__fixtures__/**/*.js', load: false }
],

filesWithNoCoverageCalculated: ["__fixtures__/**/*.js"],
filesWithNoCoverageCalculated: ['__fixtures__/**/*.js'],

tests: ["__tests__/**/*.js"],
tests: ['__tests__/**/*.js'],

env: {
type: "node",
runner: "node"
type: 'node',
runner: 'node'
},

testFramework: "jest",
testFramework: 'jest',
compilers: {
"**/*.js": wallaby.compilers.babel({ babelrc: true })
'**/*.js': wallaby.compilers.babel({ babelrc: true })
},
debug: false
};
};
}
}

0 comments on commit dc98ca4

Please sign in to comment.